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
|
<?php
namespace Tests\Icinga\Module\Eventdb\ProvidedHook\Monitoring;
use Icinga\Application\Config;
use Icinga\Module\Eventdb\ProvidedHook\Monitoring\EventdbActionHook;
use Icinga\Module\Eventdb\Test\BaseTestCase;
use Icinga\Module\Eventdb\Test\PseudoHost;
use Icinga\Module\Eventdb\Test\PseudoMonitoringBackend;
use Icinga\Module\Eventdb\Test\PseudoService;
use Icinga\Web\Navigation\NavigationItem;
class EventdbActionHookTest extends BaseTestCase
{
public function setUp()
{
parent::setUp();
EventdbActionHook::wantCache(false);
}
public function testHostWithoutVarsAndNoConfig()
{
$this->setupConfiguration(null, null, null);
$nav = EventdbActionHook::getActions($this->buildHost(null, null));
$items = $nav->getItems();
$this->assertCount(1, $items);
/** @var NavigationItem $navObj */
$navObj = current($items);
$this->assertEquals('host_name=testhost', $navObj->getUrl()->getQueryString());
}
public function testHostWithoutVarsAndNormalConfig()
{
$this->setupConfiguration();
$nav = EventdbActionHook::getActions($this->buildHost(null, null));
$this->assertCount(0, $nav->getItems());
}
public function testHostWithVars()
{
$this->setupConfiguration();
$nav = EventdbActionHook::getActions($this->buildHost());
$items = $nav->getItems();
$this->assertCount(1, $items);
/** @var NavigationItem $navObj */
$navObj = current($items);
$this->assertEquals('host_name=testhost', $navObj->getUrl()->getQueryString());
}
public function testHostWithVarsAlwaysOn()
{
$this->setupConfiguration('edb', '1');
$nav = EventdbActionHook::getActions($this->buildHost(null, 'othervar'));
$this->assertCount(1, $nav->getItems());
}
public function testServiceWithVarsAlwaysOn()
{
$this->setupConfiguration('edb', null, '1');
$nav = EventdbActionHook::getActions($this->buildService(null, 'othervar'));
$this->assertCount(1, $nav->getItems());
}
public function testHostWithLegacyFilter()
{
$this->setupConfiguration();
$nav = EventdbActionHook::getActions($this->buildHost("{ host: 'test2', programInclusion: 'test2' }"));
$items = $nav->getItems();
$this->assertCount(2, $items);
/** @var NavigationItem $navObj */
$navObj = current($items);
$this->assertEquals('host_name=test2&program=test2', $navObj->getUrl()->getQueryString());
}
public function testHostWithFilter()
{
$this->setupConfiguration();
$nav = EventdbActionHook::getActions($this->buildHost("program=test3"));
$items = $nav->getItems();
$this->assertCount(2, $items);
/** @var NavigationItem $navObj */
$navObj = current($items);
$this->assertEquals("program=test3&host_name=testhost", $navObj->getUrl()->getQueryString());
}
public function testHostWithFilterThatFiltersHost()
{
$this->setupConfiguration();
$nav = EventdbActionHook::getActions($this->buildHost("host_name=test3&program=test3"));
$items = $nav->getItems();
$this->assertCount(2, $items);
/** @var NavigationItem $navObj */
$navObj = current($items);
$this->assertEquals("host_name=test3&program=test3", $navObj->getUrl()->getQueryString());
}
protected function configure($settings = array())
{
$config = Config::module('eventdb');
$section = $config->getSection('monitoring');
foreach ($settings as $key => $val) {
$section->$key = $val;
}
$config->setSection('monitoring', $section);
// NOTE: we need to save here, because Config::module always load config from disk
$config->saveIni();
return $this;
}
protected function setupConfiguration($custom_var = 'edb', $always_host = null, $always_service = null)
{
$this->configure(array(
'custom_var' => $custom_var,
'always_on_host' => $always_host,
'always_on_service' => $always_service,
));
}
protected function monitoringBackend()
{
return PseudoMonitoringBackend::dummy();
}
protected function buildHost($plainFilter = null, $custom_var = 'edb')
{
$host = new PseudoHost($this->monitoringBackend(), 'testhost');
$host->host_name = 'testhost';
$vars = array();
if ($custom_var !== null) {
$vars[$custom_var] = '1';
}
if ($plainFilter !== null) {
$vars[$custom_var . '_filter'] = $plainFilter;
}
$host->provideCustomVars($vars);
return $host;
}
protected function buildService($plainFilter = null, $custom_var = 'edb')
{
$service = new PseudoService($this->monitoringBackend(), 'testhost', 'test');
$service->host_name = 'testhost';
$service->service_description = 'testhost';
$vars = array();
if ($custom_var !== null) {
$vars[$custom_var] = '1';
}
if ($plainFilter !== null) {
$vars[$custom_var . '_filter'] = $plainFilter;
}
$service->provideCustomVars($vars);
return $service;
}
}
|