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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
<?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/CWebTest.php';
require_once dirname(__FILE__).'/traits/TableTrait.php';
require_once dirname(__FILE__).'/behaviors/CMessageBehavior.php';
/**
* @backup items
*/
class testPageLowLevelDiscovery extends CWebTest {
use TableTrait;
const HOST_ID = 90001;
private $selector = 'xpath://form[@name="discovery"]/table[@class="list-table"]';
/**
* Attach MessageBehavior to the test.
*
* @return array
*/
public function getBehaviors() {
return [CMessageBehavior::class];
}
public function testPageLowLevelDiscovery_CheckLayout() {
$this->page->login()->open('host_discovery.php?filter_set=1&filter_hostids%5B0%5D='.self::HOST_ID.'&context=host');
$form = $this->query('name:zbx_filter')->one()->asForm();
// Check all field names.
$fields = ['Host groups', 'Hosts', 'Name', 'Key', 'Type', 'Update interval',
'Keep lost resources period', 'SNMP OID', 'State', 'Status'];
$this->assertEquals($fields, $form->getLabels()->asText());
// Check filter collapse/expand.
foreach (['true', 'false'] as $status) {
$filter_tab = $this->query('xpath://a[contains(@class, "filter-trigger")]')->one();
$filter_tab->parents('xpath:/li[@aria-expanded="'.$status.'"]')->one()->click();
}
// Check all dropdowns.
$dropdowns = [
'Type' => ['Zabbix agent', 'Zabbix agent (active)', 'Simple check',
'SNMP agent', 'Zabbix internal','Zabbix trapper', 'External check',
'Database monitor', 'HTTP agent', 'IPMI agent', 'SSH agent',
'TELNET agent', 'JMX agent', 'Dependent item', 'all'],
'State' => ['Normal', 'Not supported', 'all'],
'Status' => ['all', 'Enabled', 'Disabled']
];
foreach ($dropdowns as $name => $values) {
foreach ($values as $value) {
$form->fill([$name => $value]);
$this->assertEquals($form->getField($name)->getValue(), $value);
switch ($value) {
case 'SNMP agent':
$this->assertTrue($form->getField('SNMP OID')->isVisible());
break;
case 'Zabbix trapper':
$this->assertFalse($form->getField('Update interval')->isVisible());
break;
case 'Normal':
case 'Not supported':
$this->assertFalse($form->getField('Status')->isEnabled());
$this->assertEquals('all', $form->getField('Status')->getText());
break;
}
}
}
// Check filter buttons.
foreach (['Apply', 'Reset'] as $button) {
$this->assertTrue($form->query('button', $button)->one()->isPresent());
}
// Checking Title, Header and Column names.
$headers = ['', 'Host', 'Name', 'Items', 'Triggers', 'Graphs', 'Hosts',
'Key', 'Interval', 'Type', 'Status', 'Info'];
$this->page->assertTitle('Configuration of discovery rules');
$this->page->assertHeader('Discovery rules');
$table = $this->query($this->selector)->asTable()->one();
$this->assertSame($headers, $table->getHeadersText());
// Check table buttons.
foreach (['Enable', 'Disable', 'Execute now', 'Delete'] as $button) {
$this->assertTrue($this->query('button', $button)->one()->isPresent());
}
}
public function testPageLowLevelDiscovery_ResetButton() {
$this->page->login()->open('host_discovery.php?filter_set=1&filter_hostids%5B0%5D='.self::HOST_ID.'&context=host');
$table = $this->query($this->selector)->asTable()->one();
$form = $this->query('name:zbx_filter')->one()->asForm();
// Check table contents before filtering.
$start_rows_count = $table->getRows()->count();
$this->assertTableStats($start_rows_count);
$start_contents = $this->getTableData();
// Filling fields with needed discovery rule info.
$form->fill(['Name' => 'Discovery rule 3']);
$form->submit();
// Check that filtered count matches expected.
$this->assertEquals(1, $table->getRows()->count());
$this->assertTableStats(1);
// Checking that filtered discovery rule matches expected.
$this->assertEquals(['Discovery rule 3'], $this->getTableData());
// After pressing reset button, check that previous discovery rules are displayed again.
$form->query('button:Reset')->one()->click();
$this->assertEquals($start_rows_count, $table->getRows()->count());
$this->assertTableStats($table->getRows()->count());
$this->assertEquals($start_contents, $this->getTableData());
}
/**
* @backup items
*/
public function testPageLowLevelDiscovery_EnableDisableSingle() {
$this->page->login()->open('host_discovery.php?filter_set=1&filter_hostids%5B0%5D='.self::HOST_ID.'&context=host');
$table = $this->query($this->selector)->asTable()->one();
$row = $table->findRow('Name', 'Discovery rule 2');
// Clicking Enabled/Disabled link
$discovery_status = ['Enabled' => 1, 'Disabled' => 0];
foreach ($discovery_status as $action => $expected_status) {
$row->query('link', $action)->one()->click();
$status = CDBHelper::getValue('SELECT status FROM items WHERE name='.zbx_dbstr('Discovery rule 2').' and hostid='
.self::HOST_ID);
$this->assertEquals($expected_status, $status);
$message_action = ($action === 'Enabled') ? 'disabled' : 'enabled';
$this->assertEquals('Discovery rule '.$message_action, CMessageElement::find()->one()->getTitle());
$link_color = ($action === 'Enabled') ? 'red' : 'green';
$this->assertTrue($row->query('xpath://td/a[@class="link-action '.$link_color.'"]')->one()->isPresent());
}
}
public function testPageLowLevelDiscovery_EnableDisableAll() {
$lld_names = ['Discovery rule 1', 'Discovery rule 2', 'Discovery rule 3'];
$this->page->login()->open('host_discovery.php?filter_set=1&filter_hostids%5B0%5D='.self::HOST_ID.'&context=host');
// Press Enable or Disable buttons and check the result.
foreach (['Disable', 'Enable'] as $action) {
$this->massChangeStatus($action);
$expected_status = ($action === 'Disable') ? 1 : 0;
foreach ($lld_names as $name) {
$status = CDBHelper::getValue('SELECT status FROM items WHERE name='.zbx_dbstr($name).
' and hostid='.self::HOST_ID);
$this->assertEquals($expected_status, $status);
}
}
}
public static function getCheckNowData() {
return [
[
[
'expected' => TEST_GOOD,
'names' => [
'Discovery rule 1',
'Discovery rule 2',
'Discovery rule 3'
],
'message' => 'Request sent successfully',
'hostid' => self::HOST_ID
]
],
[
[
'expected' => TEST_GOOD,
'names' => [
'Discovery rule 2'
],
'message' => 'Request sent successfully',
'hostid' => self::HOST_ID
]
],
[
[
'expected' => TEST_BAD,
'names' => [
'Discovery rule 2'
],
'disabled' => true,
'message' => 'Cannot send request',
'details' => 'Cannot send request: discovery rule "Discovery rule 2" on host "Host for host prototype tests" is not monitored.',
'hostid' => self::HOST_ID
]
],
[
[
'expected' => TEST_BAD,
'names' => [
'Discovery rule for triggers filtering'
],
'message' => 'Cannot send request',
'details' => 'Cannot send request: wrong discovery rule type.',
'hostid' => 99062
]
],
[
[
'expected' => TEST_BAD,
'names' => [
'Temp Status Discovery'
],
'template' => true,
'hostid' => 10250
]
]
];
}
/**
* @backup items
*
* @dataProvider getCheckNowData
*/
public function testPageLowLevelDiscovery_CheckNow($data) {
$context = array_key_exists('template', $data) ? '&context=template' : '&context=host';
$this->page->login()->open('host_discovery.php?filter_set=1&filter_hostids%5B0%5D='.$data['hostid'].$context);
// Enable all LLDs, so Check now can be send successfully.
$this->massChangeStatus('Enable');
$this->selectTableRows($data['names'], 'Name', $this->selector);
if (CTestArrayHelper::get($data, 'disabled')) {
$this->query('button:Disable')->one()->click();
$this->page->acceptAlert();
$this->selectTableRows($data['names'], 'Name', $this->selector);
}
if (CTestArrayHelper::get($data, 'template', false)) {
$this->assertFalse($this->query('button:Execute now')->one(false)->isValid());
}
else {
$this->query('button:Execute now')->one()->click();
$this->assertMessage($data['expected'], $data['message'], CTestArrayHelper::get($data, 'details'));
}
$this->page->logout();
}
/**
* Return table data by Name
*
* @return array
*/
private function getTableData() {
$result = [];
foreach ($this->query($this->selector)->asTable()->one()->getRows() as $row) {
$result[] = $row->getColumn('Name')->getText();
}
return $result;
}
public static function getFilterData() {
return [
[
[
'filter' => [
'Host groups' => 'Templates/Databases'
],
'context' => 'template',
'rows' => 84
]
],
[
[
'filter' => [
'Type' => 'Zabbix agent',
'Name' => 'Containers discovery'
],
'context' => 'template',
'expected' => [
'Containers discovery'
]
]
],
[
[
'filter' => [
'Hosts' => [
'Simple form test host'
]
],
'expected' => [
'testFormDiscoveryRule',
'testFormDiscoveryRule1',
'testFormDiscoveryRule2',
'testFormDiscoveryRule3',
'testFormDiscoveryRule4'
]
]
],
[
[
'filter' => [
'Name' => 'testFormDiscoveryRule2'
],
'expected' => [
'testFormDiscoveryRule2'
]
]
],
[
[
'filter' => [
'Update interval' => '0'
],
'expected' => [
'Test discovery rule'
]
]
],
[
[
'filter' => [
'Hosts' => [
'Visible host for template linkage',
'Test item host'
]
],
'expected' => [
'delete Discovery Rule',
'Test discovery rule'
]
]
],
[
[
'filter' => [
'Hosts' => [
'Visible host for template linkage',
'Test item host'
],
'Key' => 'key'
],
'expected' => [
'delete Discovery Rule'
]
]
],
[
[
'filter' => [
'Host groups' => [
'Inheritance test'
],
'Templates' => [
'Inheritance test template with host prototype'
]
],
'context' => 'template',
'expected' => [
'Discovery rule for host prototype test'
]
]
],
[
[
'filter' => [
'Host groups' => [
'Zabbix servers'
],
'Name' => 'DiscoveryRule ZBX6663'
],
'expected' => [
'DiscoveryRule ZBX6663',
'Template ZBX6663 Second: DiscoveryRule ZBX6663 Second'
]
]
],
[
[
'filter' => [
'Key' => 'array.cache.discovery',
'Type' => 'HTTP agent'
],
'context' => 'template',
'expected' => [
'Array controller cache discovery',
'Array controller cache discovery',
'Array controller cache discovery',
'Array controller cache discovery'
]
]
],
[
[
'filter' => [
'State' => 'Normal'
],
'expected' => [
'Linux block devices by Zabbix agent: Block devices discovery',
'Linux filesystems by Zabbix agent: Mounted filesystem discovery',
'Linux network interfaces by Zabbix agent: Network interface discovery'
]
]
],
[
[
'filter' => [
'Name' => 'Block',
'State' => 'Normal'
],
'expected' => [
'Linux block devices by Zabbix agent: Block devices discovery'
]
]
],
[
[
'filter' => [
'Host groups' => 'Templates/Operating systems',
'Type' => 'Dependent item'
],
'context' => 'template',
'rows' => 6
]
],
[
[
'filter' => [
'Type' => 'Database monitor',
'Update interval' => '1h',
'Name'=> 'Non-local database'
],
'context' => 'template',
'expected' => [
'Non-local database discovery'
]
]
],
[
[
'filter' => [
'Status' => 'Disabled'
],
'context' => 'template',
'expected' => [
'Discovery-rule-layout-test-001'
]
]
],
[
[
'filter' => [
'Status' => 'Enabled',
'Name' => 'Discovery-rule'
],
'expected' => [
'Discovery-rule-layout-test-002'
]
]
],
[
[
'filter' => [
'Keep lost resources period' => '50d'
],
'context' => 'template',
'expected' => [
'Discovery-rule-layout-test-001'
]
]
],
[
[
'filter' => [
'Name' => 'empty try'
],
'expected' => []
]
],
[
[
'filter' => [
'Host groups' => 'Zabbix servers',
'Hosts' => 'Test item host',
'Name' => 'Test discovery rule',
'Key' => 'test',
'Type' => 'Zabbix agent',
'Update interval' => '0',
'Keep lost resources period' => '30d',
'State' => 'all',
'Status' => 'Enabled'
],
'expected' => [
'Test discovery rule'
]
]
]
];
}
/**
* @dataProvider getFilterData
*/
public function testPageLowLevelDiscovery_Filter($data) {
$context = CTestArrayHelper::get($data, 'context', 'host');
$this->page->login()->open('host_discovery.php?filter_name=&filter_key='.
'&filter_type=-1&filter_delay=&filter_lifetime=&filter_snmp_oid='.
'&filter_state=-1&filter_status=-1&filter_set=1&context='.$context);
$form = $this->query('name:zbx_filter')->one()->asForm();
$form->fill($data['filter']);
$form->submit();
$this->page->waitUntilReady();
$table = $this->query($this->selector)->asTable()->one();
if (array_key_exists('expected', $data)) {
$this->assertTableDataColumn($data['expected'], 'Name', $this->selector);
}
if (array_key_exists('rows', $data)) {
$this->assertEquals($data['rows'], $table->getRows()->count());
}
}
private function massChangeStatus($action) {
$table = $this->query($this->selector)->asTable()->one();
$this->query('id:all_items')->asCheckbox()->one()->check();
$this->query('button', $action)->one()->click();
$this->page->acceptAlert();
$string = ($table->getRows()->count() == 1) ? 'Discovery rule ' : 'Discovery rules ';
$this->assertEquals($string.lcfirst($action).'d', CMessageElement::find()->one()->getTitle());
}
public static function getDeleteAllButtonData() {
return [
[
[
'expected' => TEST_GOOD,
'hostid' => self::HOST_ID,
'filter' => [
'Hosts' => 'Host for host prototype tests',
'Keep lost resources period' => ''
],
'keys' => [
'key1',
'key2',
'key3'
],
'message' => 'Discovery rules deleted',
'db_count' => 0
]
],
[
[
'expected' => TEST_BAD,
'hostid' => 50001,
'filter' => [
'Hosts' => 'Host ZBX6663',
'Key' => 'drule-ZBX6663-second'
],
'keys' => [
'drule-ZBX6663-second'
],
'message' => 'Cannot delete discovery rules',
'details' => 'Cannot delete templated items.',
'db_count' => 1
]
]
];
}
/**
* @dataProvider getDeleteAllButtonData
*/
public function testPageLowLevelDiscovery_DeleteAllButton($data) {
$this->page->login()->open('host_discovery.php?filter_set=1&filter_hostids%5B0%5D='.$data['hostid'].'&context=host');
// Delete all discovery rules.
$form = $this->query('name:zbx_filter')->one()->asForm();
$form->fill($data['filter']);
$form->submit();
$this->selectTableRows($data['keys'], 'Key', $this->selector);
$this->query('button:Delete')->one()->click();
$this->page->acceptAlert();
$this->assertMessage($data['expected'], $data['message'], CTestArrayHelper::get($data, 'details'));
foreach ($data['keys'] as $key) {
$count = CDBHelper::getCount('SELECT status FROM items WHERE key_='
.zbx_dbstr($key).' and hostid='.zbx_dbstr($data['hostid']));
$this->assertEquals($data['db_count'], $count);
}
}
}
|