summaryrefslogtreecommitdiffstats
path: root/test/php/library/Eventdb
diff options
context:
space:
mode:
Diffstat (limited to 'test/php/library/Eventdb')
-rw-r--r--test/php/library/Eventdb/Data/LegacyFilterParserTest.php77
-rw-r--r--test/php/library/Eventdb/ProvidedHook/Monitoring/EventdbActionHookTest.php186
2 files changed, 263 insertions, 0 deletions
diff --git a/test/php/library/Eventdb/Data/LegacyFilterParserTest.php b/test/php/library/Eventdb/Data/LegacyFilterParserTest.php
new file mode 100644
index 0000000..51b34ab
--- /dev/null
+++ b/test/php/library/Eventdb/Data/LegacyFilterParserTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Tests\Icinga\Module\Eventdb\CustomVariable;
+
+use Icinga\Module\Eventdb\Data\LegacyFilterParser;
+use Icinga\Module\Eventdb\Test\BaseTestCase;
+
+class LegacyFilterParserTest extends BaseTestCase
+{
+ /**
+ * Some filter examples that should work and match the result
+ *
+ * Note: This is not always clean JSON, but it should work!
+ *
+ * @var array
+ */
+ protected $validFilters = array(
+ '{}' => 'host_name=testhost', // default filter
+ '{ host: "test" }' => 'host_name=test',
+ "{ host: 'test' }" => 'host_name=test',
+ "{ host: 'otherhostname' }" => 'host_name=otherhostname',
+ "{ host: 'specialhostname', priorityExclusion: [] }" => 'host_name=specialhostname',
+ "{ host: 'specialhostname', priorityExclusion: [6,7,8] }" => 'host_name=specialhostname&priority!=6&priority!=7&priority!=8',
+ '{ "host": "*" }' => '', // doesn't make much sense, but well...
+ '{ "host": "*", "programInclusion": ["cloud-monitoring"] }' => 'program=cloud-monitoring',
+ '{ "host": ".*", "programInclusion": ["cloud-monitoring"] }' => 'program=cloud-monitoring',
+ '{ "host": "myhost.*.example.com" }' => 'host_name=myhost%2A.example.com',
+ "{ programInclusion: ['test1', 'test2'] }" => 'host_name=testhost&(program=test1|program=test2)',
+ "{ programInclusion: ['test'] }" => 'host_name=testhost&program=test',
+ "{ programExclusion: ['test'] }" => 'host_name=testhost&program!=test',
+ "{ programExclusion: ['test1', 'test2'] }" => 'host_name=testhost&program!=test1&program!=test2',
+ );
+
+ public function testFiltersThatContainSomeJson()
+ {
+ $filters = array(
+ ' { host: "test" } ',
+ ' {} ',
+ '{}',
+ "{\n\"multiline\": 1\n}",
+ );
+ foreach ($filters as $filter) {
+ $this->assertTrue(LegacyFilterParser::isJsonFilter($filter));
+ }
+ }
+
+ public function testFiltersThatDoNotContainJson()
+ {
+ $filters = array(
+ ' {xxxx ',
+ 1337,
+ 'sometext',
+ "{\nbrokenjson\n",
+ );
+ foreach ($filters as $filter) {
+ $this->assertFalse(LegacyFilterParser::isJsonFilter($filter), 'Filter: ' . $filter);
+ }
+
+ }
+
+ public function testParsingFilters()
+ {
+ foreach ($this->validFilters as $json => $result) {
+ $this->assertTrue(
+ LegacyFilterParser::isJsonFilter($json),
+ 'Should be recognized as JSON filter by isJsonFilter'
+ );
+
+ $filter = LegacyFilterParser::parse($json, 'testhost');
+ $this->assertEquals(
+ $result,
+ $filter->toQueryString(),
+ 'Resulting URL filter should match for json: ' . $json
+ );
+ }
+ }
+}
diff --git a/test/php/library/Eventdb/ProvidedHook/Monitoring/EventdbActionHookTest.php b/test/php/library/Eventdb/ProvidedHook/Monitoring/EventdbActionHookTest.php
new file mode 100644
index 0000000..d833d73
--- /dev/null
+++ b/test/php/library/Eventdb/ProvidedHook/Monitoring/EventdbActionHookTest.php
@@ -0,0 +1,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;
+ }
+}