From 18db984057b83ca4962c89b6b79bdce6a660b58f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:42:35 +0200 Subject: Adding upstream version 2.4.0. Signed-off-by: Daniel Baumann --- test/bootstrap.php | 16 ++ test/config/authentication.ini | 0 test/config/config.ini | 0 .../processes/broken_wrong-operator.conf | 1 + .../businessprocess/processes/combined.conf | 1 + .../processes/simple_with-header.conf | 13 ++ .../processes/simple_without-header.conf | 6 + test/php/library/Businessprocess/BpNodeTest.php | 39 ++++ test/php/library/Businessprocess/HostNodeTest.php | 63 ++++++ test/php/library/Businessprocess/MetadataTest.php | 32 +++ .../Businessprocess/Operators/AndOperatorTest.php | 214 +++++++++++++++++++++ .../Operators/DegradedOperatorTest.php | 159 +++++++++++++++ .../Businessprocess/Operators/MinOperatorTest.php | 174 +++++++++++++++++ .../Businessprocess/Operators/NotOperatorTest.php | 151 +++++++++++++++ .../Businessprocess/Operators/OrOperatorTest.php | 116 +++++++++++ .../library/Businessprocess/ServiceNodeTest.php | 56 ++++++ .../php/library/Businessprocess/SimulationTest.php | 47 +++++ .../Businessprocess/Storage/LegacyStorageTest.php | 138 +++++++++++++ .../Businessprocess/Web/Component/TabsTest.php | 17 ++ test/phpunit-compat.php | 10 + test/setup_vendor.sh | 82 ++++++++ 21 files changed, 1335 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/modules/businessprocess/processes/broken_wrong-operator.conf create mode 100644 test/config/modules/businessprocess/processes/combined.conf create mode 100644 test/config/modules/businessprocess/processes/simple_with-header.conf create mode 100644 test/config/modules/businessprocess/processes/simple_without-header.conf create mode 100644 test/php/library/Businessprocess/BpNodeTest.php create mode 100644 test/php/library/Businessprocess/HostNodeTest.php create mode 100644 test/php/library/Businessprocess/MetadataTest.php create mode 100644 test/php/library/Businessprocess/Operators/AndOperatorTest.php create mode 100644 test/php/library/Businessprocess/Operators/DegradedOperatorTest.php create mode 100644 test/php/library/Businessprocess/Operators/MinOperatorTest.php create mode 100644 test/php/library/Businessprocess/Operators/NotOperatorTest.php create mode 100644 test/php/library/Businessprocess/Operators/OrOperatorTest.php create mode 100644 test/php/library/Businessprocess/ServiceNodeTest.php create mode 100644 test/php/library/Businessprocess/SimulationTest.php create mode 100644 test/php/library/Businessprocess/Storage/LegacyStorageTest.php create mode 100644 test/php/library/Businessprocess/Web/Component/TabsTest.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..e12df22 --- /dev/null +++ b/test/bootstrap.php @@ -0,0 +1,16 @@ +expectException(\Icinga\Module\Businessprocess\Exception\NestingError::class); + + /** @var BpNode $bpNode */ + $bpNode = $this->makeLoop()->getNode('d'); + $bpNode->checkForLoops(); + } + + public function testNestingErrorReportsFullLoop() + { + $this->expectException(\Icinga\Module\Businessprocess\Exception\NestingError::class); + $this->expectExceptionMessage('d -> a -> b -> c -> a'); + + /** @var BpNode $bpNode */ + $bpNode = $this->makeLoop()->getNode('d'); + $bpNode->checkForLoops(); + } + + public function testStateForALoopGivesUnknown() + { + $loop = $this->makeLoop(); + /** @var BpNode $bpNode */ + $bpNode = $loop->getNode('d'); + $this->assertEquals( + 'UNKNOWN', + $bpNode->getStateName() + ); + } +} diff --git a/test/php/library/Businessprocess/HostNodeTest.php b/test/php/library/Businessprocess/HostNodeTest.php new file mode 100644 index 0000000..ef4155d --- /dev/null +++ b/test/php/library/Businessprocess/HostNodeTest.php @@ -0,0 +1,63 @@ +assertEquals( + 'localhost', + $this->localhost()->getHostname() + ); + } + + public function testReturnsCorrectIdentifierWhenCastedToString() + { + $this->assertEquals( + 'localhost;Hoststatus', + $this->localhost()->getName() + ); + } + + public function testReturnsCorrectAlias() + { + $this->assertEquals( + 'localhost', + $this->localhost()->getAlias() + ); + } + + public function testRendersCorrectLink() + { + $this->assertEquals( + '' + . 'localhost', + $this->localhost()->getLink()->render() + ); + } + + public function testSettingAnInvalidStateFails() + { + $this->expectException(\Icinga\Exception\ProgrammingError::class); + $bp = new BpConfig(); + $host = $bp->createHost('localhost')->setState(98); + $bp->createBp('p')->addChild($host)->getState(); + } + + /** + * @return HostNode + */ + protected function localhost() + { + $bp = new BpConfig(); + return (new HostNode((object) array( + 'hostname' => 'localhost', + 'state' => 0, + )))->setBpConfig($bp)->setAlias('localhost'); + } +} diff --git a/test/php/library/Businessprocess/MetadataTest.php b/test/php/library/Businessprocess/MetadataTest.php new file mode 100644 index 0000000..765caf8 --- /dev/null +++ b/test/php/library/Businessprocess/MetadataTest.php @@ -0,0 +1,32 @@ +assertFalse( + $meta->nameIsPrefixedWithOneOf(array()) + ); + $this->assertFalse( + $meta->nameIsPrefixedWithOneOf(array('matchr', 'atchme')) + ); + $this->assertTrue( + $meta->nameIsPrefixedWithOneOf(array('not', 'mat', 'yes')) + ); + $this->assertTrue( + $meta->nameIsPrefixedWithOneOf(array('m')) + ); + $this->assertTrue( + $meta->nameIsPrefixedWithOneOf(array('matchme')) + ); + $this->assertFalse( + $meta->nameIsPrefixedWithOneOf(array('matchmenot')) + ); + } +} diff --git a/test/php/library/Businessprocess/Operators/AndOperatorTest.php b/test/php/library/Businessprocess/Operators/AndOperatorTest.php new file mode 100644 index 0000000..9e87cf1 --- /dev/null +++ b/test/php/library/Businessprocess/Operators/AndOperatorTest.php @@ -0,0 +1,214 @@ +emptyConfigSection()); + $expressions = array( + 'a = b;c', + 'a = b;c & c;d & d;e', + ); + + foreach ($expressions as $expression) { + $this->assertInstanceOf( + 'Icinga\\Module\\Businessprocess\\BpConfig', + $storage->loadFromString('dummy', $expression) + ); + } + } + + public function testThreeTimesCriticalIsCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 2); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoTimesCriticalAndOkIsCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 0); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testCriticalAndWarningAndOkIsCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testUnknownAndWarningAndOkIsUnknown() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 3); + + $this->assertEquals( + 'UNKNOWN', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoTimesWarningAndOkIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testThreeTimesOkIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 0); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testSimpleAndOperationWorksCorrectly() + { + $bp = new BpConfig(); + $bp->throwErrors(); + $host = $bp->createHost('localhost')->setState(1); + $service = $bp->createService('localhost', 'ping')->setState(1); + $p = $bp->createBp('p'); + $p->addChild($host); + $p->addChild($service); + + $this->assertEquals( + 'DOWN', + $host->getStateName() + ); + + $this->assertEquals( + 'WARNING', + $service->getStateName() + ); + + $this->assertEquals( + 'CRITICAL', + $p->getStateName() + ); + } + + public function testSimpleOrOperationWorksCorrectly() + { + $bp = new BpConfig(); + $bp->throwErrors(); + $host = $bp->createHost('localhost')->setState(1); + $service = $bp->createService('localhost', 'ping')->setState(1); + $p = $bp->createBp('p', '|'); + $p->addChild($host); + $p->addChild($service); + + $this->assertEquals('DOWN', $host->getStateName()); + $this->assertEquals('WARNING', $service->getStateName()); + $this->assertEquals('WARNING', $p->getStateName()); + } + + public function testPendingIsAccepted() + { + $bp = new BpConfig(); + $host = $bp->createHost('localhost')->setState(99); + $service = $bp->createService('localhost', 'ping')->setState(99); + $p = $bp->createBp('p') + ->addChild($host) + ->addChild($service); + + $this->assertEquals( + 'PENDING', + $p->getStateName() + ); + } + + public function testWhetherWarningIsWorseThanPending() + { + $bp = new BpConfig(); + $host = $bp->createHost('localhost')->setState(99); + $service = $bp->createService('localhost', 'ping')->setState(1); + $p = $bp->createBp('p') + ->addChild($host) + ->addChild($service); + + $this->assertEquals( + 'WARNING', + $p->getStateName() + ); + } + + public function testPendingIsWorseThanUpOrOk() + { + $bp = new BpConfig(); + $host = $bp->createHost('localhost')->setState(99); + $service = $bp->createService('localhost', 'ping')->setState(0); + $p = $bp->createBp('p') + ->addChild($host) + ->addChild($service); + + $this->assertEquals( + 'PENDING', + $p->getStateName() + ); + + $p->clearState(); + $host->setState(0); + $service->setState(99); + + $this->assertEquals( + 'PENDING', + $p->getStateName() + ); + } + + /** + * @return BpConfig + */ + protected function getBp() + { + $storage = new LegacyStorage($this->emptyConfigSection()); + $expression = 'a = b;c & c;d & d;e'; + $bp = $storage->loadFromString('dummy', $expression); + $bp->createBp('b'); + $bp->createBp('c'); + $bp->createBp('d'); + + return $bp; + } +} diff --git a/test/php/library/Businessprocess/Operators/DegradedOperatorTest.php b/test/php/library/Businessprocess/Operators/DegradedOperatorTest.php new file mode 100644 index 0000000..72ed5e5 --- /dev/null +++ b/test/php/library/Businessprocess/Operators/DegradedOperatorTest.php @@ -0,0 +1,159 @@ +emptyConfigSection()); + $expressions = [ + 'a = b;c', + 'a = b;c % c;d % d;e', + ]; + + foreach ($expressions as $expression) { + $this->assertInstanceOf( + 'Icinga\\Module\\Businessprocess\\BpConfig', + $storage->loadFromString('dummy', $expression) + ); + } + } + + public function testThreeTimesCriticalIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 2); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoTimesCriticalAndOkIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 0); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testCriticalAndWarningAndOkIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testUnknownAndWarningAndOkIsUnknown() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 3); + + $this->assertEquals( + 'UNKNOWN', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoTimesWarningAndOkIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testUnknownAndWarningAndCriticalIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 3); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testThreeTimesOkIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 0); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testSimpleDegOperationWorksCorrectly() + { + $bp = new BpConfig(); + $bp->throwErrors(); + $host = $bp->createHost('localhost')->setState(0); + $service = $bp->createService('localhost', 'ping')->setState(2); + $p = $bp->createBp('p'); + $p->setOperator('%'); + $p->addChild($host); + $p->addChild($service); + + $this->assertEquals( + 'UP', + $host->getStateName() + ); + + $this->assertEquals( + 'CRITICAL', + $service->getStateName() + ); + + $this->assertEquals( + 'WARNING', + $p->getStateName() + ); + } + + /** + * @return BpConfig + */ + protected function getBp() + { + $storage = new LegacyStorage($this->emptyConfigSection()); + $expression = 'a = b;c % c;d % d;e'; + $bp = $storage->loadFromString('dummy', $expression); + $bp->createBp('b'); + $bp->createBp('c'); + $bp->createBp('d'); + + return $bp; + } +} diff --git a/test/php/library/Businessprocess/Operators/MinOperatorTest.php b/test/php/library/Businessprocess/Operators/MinOperatorTest.php new file mode 100644 index 0000000..986589a --- /dev/null +++ b/test/php/library/Businessprocess/Operators/MinOperatorTest.php @@ -0,0 +1,174 @@ +emptyConfigSection()); + $expressions = array( + 'a = 1 of: b;c', + 'a = 2 of: b;c + c;d + d;e', + ); + $this->getName(); + foreach ($expressions as $expression) { + $this->assertInstanceOf( + 'Icinga\\Module\\Businessprocess\\BpConfig', + $storage->loadFromString('dummy', $expression) + ); + } + } + public function testTwoOfThreeTimesCriticalAreAtLeastCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 2); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoOfTwoTimesCriticalAndUnknownAreAtLeastCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 3); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoOfCriticalAndWarningAndOkAreAtLeastCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoOfUnknownAndWarningAndCriticalAreAtLeastCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 3); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoOfTwoTimesWarningAndUnknownAreAtLeastUnknown() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 3); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'UNKNOWN', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoOfThreeTimesOkAreAtLeastOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 0); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testTenWithAllOk() + { + $bp = $this->getBp(10, 9, 0); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testTenWithOnlyTwoCritical() + { + $bp = $this->getBp(10, 8, 0); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 2); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testTenWithThreeCritical() + { + $bp = $this->getBp(10, 8, 0); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 2); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testTenWithThreeWarning() + { + $bp = $this->getBp(10, 8, 0); + $bp->setNodeState('b;c', 1); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + /** + * @return BpConfig + */ + protected function getBp($count = 3, $min = 2, $defaultState = null) + { + $names = array(); + $a = 97; + for ($i = 1; $i <= $count; $i++) { + $names[] = chr($a + $i) . ';' . chr($a + $i + 1); + } + + $storage = new LegacyStorage($this->emptyConfigSection()); + $expression = sprintf('a = %d of: %s', $min, join(' + ', $names)); + $bp = $storage->loadFromString('dummy', $expression); + foreach ($names as $n) { + if ($defaultState !== null) { + $bp->setNodeState($n, $defaultState); + } + } + + return $bp; + } +} diff --git a/test/php/library/Businessprocess/Operators/NotOperatorTest.php b/test/php/library/Businessprocess/Operators/NotOperatorTest.php new file mode 100644 index 0000000..fb62545 --- /dev/null +++ b/test/php/library/Businessprocess/Operators/NotOperatorTest.php @@ -0,0 +1,151 @@ +emptyConfigSection()); + $expressions = array( + 'a = !b;c', + 'a = ! b;c', + 'a = b;c ! c;d ! d;e', + 'a = ! b;c ! c;d ! d;e !', + ); + + foreach ($expressions as $expression) { + $this->assertInstanceOf( + 'Icinga\\Module\\Businessprocess\\BpConfig', + $storage->loadFromString('dummy', $expression) + ); + } + } + + public function testASimpleNegationGivesTheCorrectResult() + { + $storage = new LegacyStorage($this->emptyConfigSection()); + $expression = 'a = !b;c'; + $bp = $storage->loadFromString('dummy', $expression); + $a = $bp->getNode('a'); + $b = $bp->getNode('b;c')->setState(3); + $this->assertEquals( + 'OK', + $a->getStateName() + ); + + $a->clearState(); + $b->setState(0); + $this->assertEquals( + 'CRITICAL', + $a->getStateName() + ); + } + + public function testThreeTimesCriticalIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 2); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testThreeTimesUnknownIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 3); + $bp->setNodeState('c;d', 3); + $bp->setNodeState('d;e', 3); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testThreeTimesWarningIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 1); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testThreeTimesOkIsCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 0); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testNotOkAndWarningAndCriticalIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testNotWarningAndUnknownAndCriticalIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 3); + $bp->setNodeState('c;d', 2); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testNotTwoTimesWarningAndOkIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + /** + * @return BpConfig + */ + protected function getBp() + { + $storage = new LegacyStorage($this->emptyConfigSection()); + $expression = 'a = ! b;c ! c;d ! d;e'; + $bp = $storage->loadFromString('dummy', $expression); + + return $bp; + } +} diff --git a/test/php/library/Businessprocess/Operators/OrOperatorTest.php b/test/php/library/Businessprocess/Operators/OrOperatorTest.php new file mode 100644 index 0000000..02043d0 --- /dev/null +++ b/test/php/library/Businessprocess/Operators/OrOperatorTest.php @@ -0,0 +1,116 @@ +emptyConfigSection()); + $expressions = array( + 'a = b;c', + 'a = b;c | c;d | d;e', + ); + + foreach ($expressions as $expression) { + $this->assertInstanceOf( + 'Icinga\\Module\\Businessprocess\\BpConfig', + $storage->loadFromString('dummy', $expression) + ); + } + } + + public function testThreeTimesCriticalIsCritical() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 2); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'CRITICAL', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoTimesCriticalOrUnknownIsUnknown() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 3); + $bp->setNodeState('d;e', 2); + + $this->assertEquals( + 'UNKNOWN', + $bp->getNode('a')->getStateName() + ); + } + + public function testCriticalOrWarningOrOkIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 0); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testUnknownOrWarningOrCriticalIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 2); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 3); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + public function testTwoTimesWarningAndOkIsOk() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 0); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'OK', + $bp->getNode('a')->getStateName() + ); + } + + public function testThreeTimesWarningIsWarning() + { + $bp = $this->getBp(); + $bp->setNodeState('b;c', 1); + $bp->setNodeState('c;d', 1); + $bp->setNodeState('d;e', 1); + + $this->assertEquals( + 'WARNING', + $bp->getNode('a')->getStateName() + ); + } + + /** + * @return BpConfig + */ + protected function getBp() + { + $storage = new LegacyStorage($this->emptyConfigSection()); + $expression = 'a = b;c | c;d | d;e'; + $bp = $storage->loadFromString('dummy', $expression); + + return $bp; + } +} diff --git a/test/php/library/Businessprocess/ServiceNodeTest.php b/test/php/library/Businessprocess/ServiceNodeTest.php new file mode 100644 index 0000000..d56529d --- /dev/null +++ b/test/php/library/Businessprocess/ServiceNodeTest.php @@ -0,0 +1,56 @@ +assertEquals( + 'localhost', + $this->pingOnLocalhost()->getHostname() + ); + } + + public function testReturnsCorrectServiceDescription() + { + $this->assertEquals( + 'ping <> pong', + $this->pingOnLocalhost()->getServiceDescription() + ); + } + + public function testReturnsCorrectAlias() + { + $this->assertEquals( + 'localhost: ping <> pong', + $this->pingOnLocalhost()->getAlias() + ); + } + + public function testRendersCorrectLink() + { + $this->assertEquals( + '' + . 'localhost: ping <> pong', + $this->pingOnLocalhost()->getLink()->render() + ); + } + + /** + * @return ServiceNode + */ + protected function pingOnLocalhost() + { + $bp = new BpConfig(); + return (new ServiceNode((object) array( + 'hostname' => 'localhost', + 'service' => 'ping <> pong', + 'state' => 0, + )))->setBpConfig($bp)->setHostAlias('localhost')->setAlias('ping <> pong'); + } +} diff --git a/test/php/library/Businessprocess/SimulationTest.php b/test/php/library/Businessprocess/SimulationTest.php new file mode 100644 index 0000000..aefeb91 --- /dev/null +++ b/test/php/library/Businessprocess/SimulationTest.php @@ -0,0 +1,47 @@ +assertInstanceOf( + $class, + Simulation::create() + ); + } + + public function testAppliedSimulation() + { + $data = (object) array( + 'state' => 0, + 'acknowledged' => false, + 'in_downtime' => false + ); + $config = $this->makeInstance()->loadProcess('simple_with-header'); + $simulation = Simulation::create(array( + 'host1;Hoststatus' => $data + )); + $parent = $config->getBpNode('singleHost'); + + $config->applySimulation($simulation); + $this->assertEquals( + 'OK', + $parent->getStateName() + ); + + $parent->clearState(); + $data->state = 1; + $simulation->set('host1;Hoststatus', $data); + $config->applySimulation($simulation); + $this->assertEquals( + 'CRITICAL', + $parent->getStateName() + ); + } +} diff --git a/test/php/library/Businessprocess/Storage/LegacyStorageTest.php b/test/php/library/Businessprocess/Storage/LegacyStorageTest.php new file mode 100644 index 0000000..b3be257 --- /dev/null +++ b/test/php/library/Businessprocess/Storage/LegacyStorageTest.php @@ -0,0 +1,138 @@ +assertInstanceOf( + $baseClass, + new LegacyStorage($this->emptyConfigSection()) + ); + } + + public function testDefaultConfigDirIsDiscoveredCorrectly() + { + $this->assertEquals( + $this->getTestsBaseDir('config/modules/businessprocess/processes'), + $this->makeInstance()->getConfigDir() + ); + } + + public function testAllAvailableProcessesAreListed() + { + $keys = array_keys($this->makeInstance()->listProcesses()); + $this->assertEquals( + array( + 'broken_wrong-operator', + 'combined', + 'simple_with-header', + 'simple_without-header', + ), + $keys + ); + } + + public function testHeaderTitlesAreRespectedInProcessList() + { + $keys = array_values($this->makeInstance()->listProcesses()); + $this->assertEquals( + array( + 'broken_wrong-operator', + 'combined', + 'Simple with header (simple_with-header)', + 'simple_without-header', + ), + $keys + ); + } + + public function testProcessFilenameIsReturned() + { + $this->assertEquals( + $this->getTestsBaseDir('config/modules/businessprocess/processes/simple_with-header.conf'), + $this->makeInstance()->getFilename('simple_with-header') + ); + } + + public function testAnExistingProcessExists() + { + $this->assertTrue( + $this->makeInstance()->hasProcess('simple_with-header') + ); + } + + public function testAMissingProcessIsMissing() + { + $this->assertFalse( + $this->makeInstance()->hasProcess('simple_with-headerx') + ); + } + + public function testAValidProcessCanBeLoaded() + { + $this->assertInstanceOf( + $this->processClass, + $this->makeInstance()->loadProcess('simple_with-header') + ); + } + + public function testProcessConfigCanBeLoadedFromAString() + { + $this->assertInstanceOf( + $this->processClass, + $this->makeInstance()->loadFromString('dummy', 'a = Host1;ping & Host2;ping') + ); + } + + public function testFullProcessSourceCanBeFetched() + { + $this->assertEquals( + file_get_contents( + $this->getTestsBaseDir( + 'config/modules/businessprocess/processes/simple_with-header.conf' + ) + ), + $this->makeInstance()->getSource('simple_with-header') + ); + } + + public function testTitleCanBeReadFromConfig() + { + $this->assertEquals( + 'Simple with header', + $this->makeInstance()->loadProcess('simple_with-header')->getMetadata()->get('Title') + ); + } + + public function testInfoUrlBeReadFromConfig() + { + $this->assertEquals( + 'https://top.example.com/', + $this->makeInstance()->loadProcess('simple_with-header')->getBpNode('top')->getInfoUrl() + ); + } + + public function testAConfiguredLoopCanBeParsed() + { + $this->assertInstanceOf( + $this->processClass, + $this->makeLoop() + ); + } + + public function testImportedNodesCanBeParsed() + { + $this->assertInstanceOf( + $this->processClass, + $this->makeInstance()->loadProcess('combined') + ); + } +} diff --git a/test/php/library/Businessprocess/Web/Component/TabsTest.php b/test/php/library/Businessprocess/Web/Component/TabsTest.php new file mode 100644 index 0000000..f1181d2 --- /dev/null +++ b/test/php/library/Businessprocess/Web/Component/TabsTest.php @@ -0,0 +1,17 @@ +assertInstanceOf( + 'Icinga\Module\Businessprocess\Web\Component\Tabs', + new Tabs() + ); + } +} 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 @@ +