summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:29 +0000
commita9b77c01caef9ae7a2c84e2333d28ceb028cf4d3 (patch)
tree4a77cd3e323c37b0e5b3d7578b9718cdf1a89262 /test
parentInitial commit. (diff)
downloadicingaweb2-module-eventdb-a9b77c01caef9ae7a2c84e2333d28ceb028cf4d3.tar.xz
icingaweb2-module-eventdb-a9b77c01caef9ae7a2c84e2333d28ceb028cf4d3.zip
Adding upstream version 1.3.0.upstream/1.3.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test')
-rw-r--r--test/bootstrap.php16
-rw-r--r--test/config/authentication.ini0
-rw-r--r--test/config/config.ini0
-rw-r--r--test/config/resources.ini0
-rw-r--r--test/php/library/Eventdb/Data/LegacyFilterParserTest.php77
-rw-r--r--test/php/library/Eventdb/ProvidedHook/Monitoring/EventdbActionHookTest.php186
-rw-r--r--test/phpunit-compat.php10
-rwxr-xr-xtest/setup_vendor.sh69
8 files changed, 358 insertions, 0 deletions
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 @@
+<?php
+
+use Icinga\Module\Eventdb\Test\Bootstrap;
+
+call_user_func(function () {
+ $basedir = dirname(__DIR__);
+ if (! class_exists('PHPUnit_Framework_TestCase')) {
+ require_once __DIR__ . '/phpunit-compat.php';
+ }
+
+ $include_path = $basedir . '/vendor' . PATH_SEPARATOR . ini_get('include_path');
+ ini_set('include_path', $include_path);
+
+ require_once $basedir . '/library/Eventdb/Test/Bootstrap.php';
+ Bootstrap::web($basedir);
+});
diff --git a/test/config/authentication.ini b/test/config/authentication.ini
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/config/authentication.ini
diff --git a/test/config/config.ini b/test/config/config.ini
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/config/config.ini
diff --git a/test/config/resources.ini b/test/config/resources.ini
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/config/resources.ini
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;
+ }
+}
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 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @codingStandardsIgnoreStart
+ */
+class PHPUnit_Framework_TestCase extends TestCase
+{
+}
diff --git a/test/setup_vendor.sh b/test/setup_vendor.sh
new file mode 100755
index 0000000..742dfcd
--- /dev/null
+++ b/test/setup_vendor.sh
@@ -0,0 +1,69 @@
+#!/bin/bash
+
+set -ex
+
+MODULE_HOME=${MODULE_HOME:="$(dirname "$(readlink -f $(dirname "$0"))")"}
+PHP_VERSION="$(php -r 'echo phpversion();')"
+
+ICINGAWEB_VERSION=${ICINGAWEB_VERSION:=2.4.1}
+# TODO: Remove for 2.5.0
+if [[ "$ICINGAWEB_VERSION" =~ 2.4.* ]]; then
+ ICINGAWEB_GITREF=${ICINGAWEB_GITREF:="origin/master"}
+fi
+
+PHPCS_VERSION=${PHPCS_VERSION:=2.9.1}
+
+if [ "$PHP_VERSION" '<' 5.6.0 ]; then
+ PHPUNIT_VERSION=${PHPUNIT_VERSION:=4.8}
+else
+ PHPUNIT_VERSION=${PHPUNIT_VERSION:=5.7}
+fi
+
+cd ${MODULE_HOME}
+
+test -d vendor || mkdir vendor
+cd vendor/
+
+# icingaweb2
+if [ -n "$ICINGAWEB_GITREF" ]; then
+ icingaweb_path="icingaweb2"
+ test ! -L "$icingaweb_path" || rm "$icingaweb_path"
+
+ if [ ! -d "$icingaweb_path" ]; then
+ git clone https://github.com/Icinga/icingaweb2.git "$icingaweb_path"
+ fi
+
+ (
+ set -e
+ cd "$icingaweb_path"
+ git fetch -p
+ git checkout -f "$ICINGAWEB_GITREF"
+ )
+else
+ icingaweb_path="icingaweb2-${ICINGAWEB_VERSION}"
+ if [ ! -e "${icingaweb_path}".tar.gz ]; then
+ wget -O "${icingaweb_path}".tar.gz https://github.com/Icinga/icingaweb2/archive/v"${ICINGAWEB_VERSION}".tar.gz
+ fi
+ if [ ! -d "${icingaweb_path}" ]; then
+ tar xf "${icingaweb_path}".tar.gz
+ fi
+
+ ln -svf "${icingaweb_path}" icingaweb2
+fi
+ln -svf "${icingaweb_path}"/library/Icinga
+ln -svf "${icingaweb_path}"/library/vendor/Zend
+
+# phpunit
+phpunit_path="phpunit-${PHPUNIT_VERSION}"
+if [ ! -e "${phpunit_path}".phar ]; then
+ wget -O "${phpunit_path}".phar https://phar.phpunit.de/phpunit-${PHPUNIT_VERSION}.phar
+fi
+ln -svf "${phpunit_path}".phar phpunit.phar
+
+# phpcs
+phpcs_path="phpcs-${PHPCS_VERSION}"
+if [ ! -e "${phpcs_path}".phar ]; then
+ wget -O "${phpcs_path}".phar \
+ https://github.com/squizlabs/PHP_CodeSniffer/releases/download/${PHPCS_VERSION}/phpcs.phar
+fi
+ln -svf "${phpcs_path}".phar phpcs.phar