From 9f39660f50004ca7c49ea171e2a6f199487cd667 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:18:42 +0200 Subject: Adding upstream version 1.3.0. Signed-off-by: Daniel Baumann --- test/bootstrap.php | 16 ++ test/config/authentication.ini | 0 test/config/config.ini | 0 test/config/resources.ini | 0 .../Eventdb/Data/LegacyFilterParserTest.php | 77 +++++++++ .../Monitoring/EventdbActionHookTest.php | 186 +++++++++++++++++++++ test/phpunit-compat.php | 10 ++ test/setup_vendor.sh | 69 ++++++++ 8 files changed, 358 insertions(+) create mode 100644 test/bootstrap.php create mode 100644 test/config/authentication.ini create mode 100644 test/config/config.ini create mode 100644 test/config/resources.ini create mode 100644 test/php/library/Eventdb/Data/LegacyFilterParserTest.php create mode 100644 test/php/library/Eventdb/ProvidedHook/Monitoring/EventdbActionHookTest.php create mode 100644 test/phpunit-compat.php create mode 100755 test/setup_vendor.sh (limited to 'test') diff --git a/test/bootstrap.php b/test/bootstrap.php new file mode 100644 index 0000000..0253904 --- /dev/null +++ b/test/bootstrap.php @@ -0,0 +1,16 @@ + '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 @@ +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; + } +} diff --git a/test/phpunit-compat.php b/test/phpunit-compat.php new file mode 100644 index 0000000..2b1be3a --- /dev/null +++ b/test/phpunit-compat.php @@ -0,0 +1,10 @@ +