Welcome to mirror list, hosted at ThFree Co, Russian Federation.

testItemPrototype.php « api_json « tests « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3315bf649ed8ce2e4d41ac5e2359c1895206d600 (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/*
** Zabbix
** Copyright (C) 2001-2022 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';
require_once dirname(__FILE__).'/../include/helpers/CDataHelper.php';

/**
 * @backup items
 */
class testItemPrototype extends CAPITest {

	public static function getItemPrototypeCreateData() {
		$valid_item_types = [
			ITEM_TYPE_ZABBIX => '50022',
			ITEM_TYPE_TRAPPER => null,
			ITEM_TYPE_SIMPLE => '50022',
			ITEM_TYPE_INTERNAL => null,
			ITEM_TYPE_ZABBIX_ACTIVE => null,
			ITEM_TYPE_EXTERNAL => '50022',
			ITEM_TYPE_DB_MONITOR => null,
			ITEM_TYPE_IPMI => '50031',
			ITEM_TYPE_SSH => '50022',
			ITEM_TYPE_TELNET => '50022',
			ITEM_TYPE_CALCULATED => null,
			ITEM_TYPE_JMX => '50030',
			ITEM_TYPE_DEPENDENT => null,
			ITEM_TYPE_HTTPAGENT => '50022',
			ITEM_TYPE_SNMP => '50029',
			ITEM_TYPE_SCRIPT => null
		];

		$item_type_tests = [];
		foreach ($valid_item_types as $type => $interfaceid) {
			switch ($type) {
				case ITEM_TYPE_ZABBIX:
				case ITEM_TYPE_SIMPLE:
				case ITEM_TYPE_INTERNAL:
				case ITEM_TYPE_ZABBIX_ACTIVE:
				case ITEM_TYPE_EXTERNAL:
					$params = [
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_DB_MONITOR:
					$params = [
						'params' => 'SELECT * FROM table',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_IPMI:
					$params = [
						'ipmi_sensor' => '1.2.3',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_SSH:
					$params = [
						'username' => 'username',
						'authtype' => ITEM_AUTHTYPE_PASSWORD,
						'params' => 'return true;',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_TELNET:
					$params = [
						'username' => 'username',
						'params' => 'return true;',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_CALCULATED:
					$params = [
						'params' => '1+1',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_JMX:
					$params = [
						'username' => 'username',
						'password' => 'password',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_DEPENDENT:
					$params = [
						'master_itemid' => '150151'
					];
					break;

				case ITEM_TYPE_HTTPAGENT:
					$params = [
						'url' => 'http://0.0.0.0',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_SNMP:
					$params = [
						'snmp_oid' => '1.2.3',
						'delay' => '30s'
					];
					break;

				case ITEM_TYPE_SCRIPT:
					$params = [
						'params' => 'script',
						'timeout' => '30s',
						'delay' => '30s'
					];
					break;

				default:
					$params = [];
					break;
			}

			if ($interfaceid) {
				$params['interfaceid'] = $interfaceid;
			}

			$item_type_tests[] = [
				'request_data' => $params + [
					'hostid' => '50009',
					'ruleid' => '400660',
					'name' => 'Test item prototype of type '.$type,
					'key_' => 'test_item_prototype_of_type_'.$type.'[{#LLD_MACRO}]',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => (string) $type
				],
				'expected_error' => null
			];
		}

		$interfaces_tests = [];
		$optional = [ITEM_TYPE_SIMPLE, ITEM_TYPE_EXTERNAL, ITEM_TYPE_SSH, ITEM_TYPE_TELNET, ITEM_TYPE_HTTPAGENT];
		$required = [ITEM_TYPE_SNMP, ITEM_TYPE_SNMPTRAP, ITEM_TYPE_IPMI, ITEM_TYPE_ZABBIX, ITEM_TYPE_JMX];

		foreach ($item_type_tests as $item_type_test) {
			if (in_array($item_type_test['request_data']['type'], $optional)) {
				unset($item_type_test['request_data']['interfaceid']);

				$request_data = [
					'name' => $item_type_test['request_data']['name'].' missing',
					'key_' => substr($item_type_test['request_data']['key_'], 0, -1).', missing]'
				] + $item_type_test['request_data'];

				$interfaces_tests[] = ['request_data' => $request_data] + $item_type_test;

				$request_data = [
					'name' => $item_type_test['request_data']['name'].' zero',
					'key_' => substr($item_type_test['request_data']['key_'], 0, -1).', zero]',
					'interfaceid' => '0'
				] + $item_type_test['request_data'];

				$interfaces_tests[] = ['request_data' => $request_data] + $item_type_test;
			}
			else if (in_array($item_type_test['request_data']['type'], $required)) {
				unset($item_type_test['request_data']['interfaceid']);
				$item_type_test['expected_error'] = 'Invalid parameter "/1": the parameter "interfaceid" is missing.';
				$interfaces_tests[] = $item_type_test;
			}
		}

		return array_merge([
			[
				'request_data' => [
					'hostid' => '50009',
					'ruleid' => '400660',
					'name' => 'Item prototype with invalid item type',
					'key_' => 'item_prototype_with_invalid_item_type',
					'interfaceid' => '50022',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => '100',
					'delay' => '30s'
				],
				'expected_error' => 'Invalid parameter "/1/type": value must be one of '.implode(', ', [
					ITEM_TYPE_ZABBIX, ITEM_TYPE_TRAPPER, ITEM_TYPE_SIMPLE, ITEM_TYPE_INTERNAL, ITEM_TYPE_ZABBIX_ACTIVE,
					ITEM_TYPE_EXTERNAL, ITEM_TYPE_DB_MONITOR, ITEM_TYPE_IPMI, ITEM_TYPE_SSH, ITEM_TYPE_TELNET,
					ITEM_TYPE_CALCULATED, ITEM_TYPE_JMX, ITEM_TYPE_SNMPTRAP, ITEM_TYPE_DEPENDENT, ITEM_TYPE_HTTPAGENT,
					ITEM_TYPE_SNMP, ITEM_TYPE_SCRIPT
				]).'.'
			],
			// Test update interval for mqtt key of the Agent item type.
			[
				'request_data' => [
					'hostid' => '50009',
					'ruleid' => '400660',
					'name' => 'Test mqtt key',
					'key_' => 'mqtt.get[{#0}]',
					'interfaceid' => '50022',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => ITEM_TYPE_ZABBIX,
					'delay' => '30s'
				],
				'expected_error' => null
			],
			[
				'request_data' => [
					'hostid' => '50009',
					'name' => 'Test mqtt key without delay',
					'key_' => 'mqtt.get[{#1}]',
					'interfaceid' => '50022',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => ITEM_TYPE_ZABBIX
				],
				'expected_error' => 'Invalid parameter "/1": the parameter "ruleid" is missing.'
			],
			[
				'request_data' => [
					'hostid' => '50009',
					'ruleid' => '400660',
					'name' => 'Test mqtt key with 0 delay',
					'key_' => 'mqtt.get[{#2}]',
					'interfaceid' => '50022',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => ITEM_TYPE_ZABBIX,
					'delay' => '0'
				],
				'expected_error' => 'Invalid parameter "/1/delay": cannot be equal to zero without custom intervals.'
			],
			// Test update interval for mqtt key of the Active agent type.
			[
				'request_data' => [
					'hostid' => '50009',
					'ruleid' => '400660',
					'name' => 'Test mqtt key for active agent',
					'key_' => 'mqtt.get[{#3}]',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => ITEM_TYPE_ZABBIX_ACTIVE
				],
				'expected_error' => null
			],
			[
				'request_data' => [
					'hostid' => '50009',
					'ruleid' => '400660',
					'name' => 'Test mqtt key with 0 delay for active agent',
					'key_' => 'mqtt.get[{#4}]',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => ITEM_TYPE_ZABBIX_ACTIVE,
					'delay' => '0'
				],
				'expected_error' => null
			],
			[
				'request_data' => [
					'hostid' => '50009',
					'ruleid' => '400660',
					'name' => 'Test mqtt with wrong key and 0 delay',
					'key_' => 'mqt.get[{#5}]',
					'value_type' => ITEM_VALUE_TYPE_UINT64,
					'type' => ITEM_TYPE_ZABBIX_ACTIVE,
					'delay' => '0'
				],
				'expected_error' => 'Invalid parameter "/1/delay": cannot be equal to zero without custom intervals.'
			]
		], $item_type_tests, $interfaces_tests);
	}

	/**
	 * @dataProvider getItemPrototypeCreateData
	 */
	public function testItemPrototype_Create($request_data, $expected_error) {
		$result = $this->call('itemprototype.create', $request_data, $expected_error);

		if ($expected_error === null) {
			if ($request_data['type'] === ITEM_TYPE_ZABBIX_ACTIVE && substr($request_data['key_'], 0, 8) === 'mqtt.get') {
				$request_data['delay'] = CTestArrayHelper::get($request_data, 'delay', '0');
			}

			if (!array_key_exists('delay', $request_data)) {
				$request_data['delay'] = 0;
			}

			foreach ($result['result']['itemids'] as $id) {
				$db_item = CDBHelper::getRow('SELECT hostid, name, key_, type, delay FROM items WHERE itemid='.zbx_dbstr($id));

				foreach (['hostid', 'name', 'key_', 'type', 'delay'] as $field) {
					$this->assertSame($db_item[$field], strval($request_data[$field]));
				}
			}
		}
	}
}