1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
require_once dirname(__FILE__).'/../include/CAPITest.php';
/**
* @backup hosts
*/
class testHost extends CAPITest {
public static function host_delete() {
return [
[
'hostids' => [
'61001'
],
'expected_error' => 'Cannot delete host because maintenance "maintenance_has_only_host" must contain at least one host or host group.'
],
[
'hostids' => [
'61001',
'61003'
],
'expected_error' => 'Cannot delete selected hosts because maintenance "maintenance_has_only_host" must contain at least one host or host group.'
],
[
'hostids' => [
'61003'
],
'expected_error' => null
],
[
'hostids' => [
'61004',
'61005'
],
'expected_error' => 'Cannot delete selected hosts because maintenance "maintenance_two_hosts" must contain at least one host or host group.'
],
[
'hostids' => [
'61004'
],
'expected_error' => null
]
];
}
public static function host_create() {
return [
[
'request' => [
'groups' => ['5'],
'host' => 'new host 1'
],
'expected_error' => "Incorrect value for field \"groups\": the parameter \"groupid\" is missing."
],
[
'request' => [
'groups' => [
'groupid' => 4
],
'host' => 'new host 2'
],
'expected_error' => null
],
[
'request' => [
'groups' => [
'groupid' => 4
],
'host' => 'new host 3',
'interfaces' => []
],
'expected_error' => null
],
[
'request' => [
'groups' => [
'groupid' => 4
],
'host' => 'new host 4',
'interfaces' => ''
],
'expected_error' => 'Incorrect arguments passed to function.'
],
[
'request' => [
'groups' => [
'groupid' => 4
],
'host' => 'new host 5',
'interfaces' => 'string'
],
'expected_error' => 'Incorrect arguments passed to function.'
],
[
'request' => [
'groups' => [
'groupid' => 4
],
'host' => 'new host 6',
'interfaces' => 10
],
'expected_error' => 'Incorrect arguments passed to function.'
]
];
}
/**
* @dataProvider host_create
*/
public function testHost_Create($request, $expected_error) {
$this->call('host.create', $request, $expected_error);
}
/**
* @dataProvider host_delete
*/
public function testHost_Delete($hostids, $expected_error) {
$result = $this->call('host.delete', $hostids, $expected_error);
if ($expected_error === null) {
foreach ($result['result']['hostids'] as $id) {
$this->assertEquals(0, CDBHelper::getCount('select * from hosts where hostid='.zbx_dbstr($id)));
}
}
}
public static function host_select_tags() {
return [
'Get test host tag as extend' => [
'params' => [
'hostids' => [50032],
'selectTags' => 'extend'
],
'expected_result' => [
'tags' => [
['tag' => 'b', 'value' => 'b']
]
]
],
'Get test host tag excluding value' => [
'params' => [
'hostids' => [50032],
'selectTags' => ['tag']
],
'expected_result' => [
'tags' => [
['tag' => 'b']
]
]
],
'Get test host tag excluding name' => [
'params' => [
'hostids' => [50032],
'selectTags' => ['value']
],
'expected_result' => [
'tags' => [
['value' => 'b']
]
]
]
];
}
/**
* @dataProvider host_select_tags
*/
public function testHost_SelectTags($params, $expected_result) {
$result = $this->call('host.get', $params);
foreach ($result['result'] as $host) {
foreach ($expected_result as $field => $expected_value){
$this->assertArrayHasKey($field, $host, 'Field should be present.');
$this->assertEquals($host[$field], $expected_value, 'Returned value should match.');
}
}
}
public static function dataHostFieldPresence() {
return [
'Check if {"output": "extend"} includes "inventory_mode" and excludes write-only properties' => [
'request' => [
'output' => 'extend',
'hostids' => ['99013']
],
'expected_result' => [
'hostid' => '99013',
'inventory_mode' => '-1',
// Write-only properties.
'tls_psk_identity' => null,
'tls_psk' => null
]
],
'Check it is not possible to select write-only fields' => [
'request' => [
'output' => ['host', 'tls_psk', 'tls_psk_identity'],
'hostids' => ['99013']
],
'expected_result' => [
'hostid' => '99013',
// Sample of unspecified property.
'inventory_mode' => null,
// Write-only properties.
'tls_psk_identity' => null,
'tls_psk' => null
]
],
'Check direct request of inventory_mode and other properties' => [
'request' => [
'output' => ['inventory_mode', 'tls_connect', 'name'],
'hostids' => ['99013']
],
'expected_result' => [
'hostid' => '99013',
'inventory_mode' => '-1',
// Samples of other specified properties.
'tls_connect' => '1',
'name' => 'Host OS - Windows'
]
]
];
}
/**
* @dataProvider dataHostFieldPresence
*/
public function testHost_FieldPresenceAndExclusion($request, $expected_result) {
$result = $this->call('host.get', $request, null);
foreach ($result['result'] as $host) {
foreach ($expected_result as $key => $value) {
if ($value !== null) {
$this->assertArrayHasKey($key, $host, 'Key '.$key.' should be present in host output.');
$this->assertEquals($value, $host[$key], 'Value should match.');
}
else {
$this->assertArrayNotHasKey($key, $host, 'Key '.$key.' should NOT be present in host output');
}
}
}
}
}
|