From 1034ff3867fcbd4daa39c794df4aff069ac74335 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 24 Mar 2016 06:44:02 +0100 Subject: [PATCH] IcingaServiceTest: new assignment-related tests --- .../Director/Objects/IcingaServiceTest.php | 248 ++++++++++++++++++ .../Director/Objects/rendered/service1.out | 14 + .../Director/Objects/rendered/service2.out | 14 + 3 files changed, 276 insertions(+) create mode 100644 test/php/library/Director/Objects/IcingaServiceTest.php create mode 100644 test/php/library/Director/Objects/rendered/service1.out create mode 100644 test/php/library/Director/Objects/rendered/service2.out diff --git a/test/php/library/Director/Objects/IcingaServiceTest.php b/test/php/library/Director/Objects/IcingaServiceTest.php new file mode 100644 index 00000000..b15fb838 --- /dev/null +++ b/test/php/library/Director/Objects/IcingaServiceTest.php @@ -0,0 +1,248 @@ +service(); + $service->display_name = 'Something else'; + $service->host = 'not yet'; + $this->assertEquals( + 'not yet', + $service->host + ); + } + + /** + * @expectedException \Icinga\Exception\NotFoundError + */ + public function testFailsToStoreWithMissingLazyRelations() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + $service = $this->service(); + $service->display_name = 'Something else'; + $service->host = 'not yet'; + $service->store($db); + } + + public function testAcceptsAssignRules() + { + $service = $this->service(); + $service->object_type = 'apply'; + $service->assignments = array( + 'host.address="127.*"' + ); + } + + /** + * @expectedException Icinga\Exception\ProgrammingError + */ + public function testRefusesAssignRulesWhenNotBeingAnApply() + { + $service = $this->service(); + $service->assignments = array( + 'host.address=127.*' + ); + } + + public function testAcceptsAndRendersFlatAssignRules() + { + $service = $this->service(); + $service->object_type = 'apply'; + $service->assignments = array( + 'host.address="127.*"', + 'host.vars.env="test"' + ); + + $this->assertEquals( + $this->loadRendered('service1'), + (string) $service + ); + + $this->assertEquals( + 'host.address="127.*"', + $service->toPlainObject()->assignments['assign'][0] + ); + } + + public function testAcceptsAndRendersStructuredAssignRules() + { + $service = $this->service(); + $service->object_type = 'apply'; + $service->assignments = array( + 'host.address="127.*"', + 'host.vars.env="test"' + ); + + $this->assertEquals( + $this->loadRendered('service1'), + (string) $service + ); + + $this->assertEquals( + 'host.address="127.*"', + $service->toPlainObject()->assignments['assign'][0] + ); + } + + public function testPersistsAssignRules() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $service = $this->service(); + $service->object_type = 'apply'; + $service->assignments = array( + 'host.address="127.*"', + 'host.vars.env="test"' + ); + $service->store($db); + + $service = IcingaService::loadWithAutoIncId($service->id, $db); + $this->assertEquals( + $this->loadRendered('service1'), + (string) $service + ); + + $this->assertEquals( + 'host.address="127.*"', + $service->toPlainObject()->assignments['assign'][0] + ); + } + + public function testStaysUnmodifiedWhenSameFiltersAreSetInDifferentWays() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $service = $this->service(); + $service->object_type = 'apply'; + $service->assignments = 'host.address="127.*"'; + $service->store($db); + $this->assertFalse($service->hasBeenModified()); + + $service->assignments = array( + 'host.address="127.*"', + ); + $this->assertFalse($service->hasBeenModified()); + + $service->assignments = 'host.address="128.*"'; + $this->assertTrue($service->hasBeenModified()); + + $service->store(); + $this->assertFalse($service->hasBeenModified()); + + $service->assignments = array('assign' => 'host.address="128.*"'); + $this->assertFalse($service->hasBeenModified()); + + $service->assignments = array( + 'assign' => array( + 'host.address="128.*"' + ) + ); + + $this->assertFalse($service->hasBeenModified()); + + $service->assignments = array( + 'assign' => array( + 'host.address="128.*"' + ), + 'ignore' => 'host.name="localhost"' + ); + + $this->assertTrue($service->hasBeenModified()); + + $service->store(); + $service = IcingaService::loadWithAutoIncId($service->id, $db); + + $this->assertEquals( + 'host.address="128.*"', + $service->toPlainObject()->assignments['assign'][0] + ); + + $this->assertEquals( + 'host.name="localhost"', + $service->toPlainObject()->assignments['ignore'][0] + ); + + $this->assertEquals( + $this->loadRendered('service2'), + (string) $service + ); + } + + protected function host() + { + return IcingaHost::create(array( + 'object_name' => $this->testHostName, + 'object_type' => 'object', + 'address' => '127.0.0.1', + )); + } + + protected function service() + { + return IcingaService::create(array( + 'object_name' => $this->testServiceName, + 'object_type' => 'object', + 'display_name' => 'Whatever service', + 'vars' => array( + 'test1' => 'string', + 'test2' => 17, + 'test3' => false, + 'test4' => (object) array( + 'this' => 'is', + 'a' => array( + 'dict', + 'ionary' + ) + ) + ) + )); + } + + protected function loadRendered($name) + { + return file_get_contents(__DIR__ . '/rendered/' . $name . '.out'); + } + + public function tearDown() + { + if ($this->hasDb()) { + $db = $this->getDb(); + $kill = array($this->testHostName); + foreach ($kill as $name) { + if (IcingaHost::exists($name, $db)) { + IcingaHost::load($name, $db)->delete(); + } + } + + $kill = array($this->testServiceName); + foreach ($kill as $name) { + if (IcingaService::exists(array($name), $db)) { + IcingaService::load($name, $db)->delete(); + } + } + } + } +} diff --git a/test/php/library/Director/Objects/rendered/service1.out b/test/php/library/Director/Objects/rendered/service1.out new file mode 100644 index 00000000..94dca947 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/service1.out @@ -0,0 +1,14 @@ +apply Service "___TEST___service" { + display_name = "Whatever service" + vars.test1 = "string" + vars.test2 = 17 + vars.test3 = false + vars.test4 = { + "a" = [ "dict", "ionary" ] + "this" = "is" + } + + assign where match("127.*", host.address) + assign where host.vars.env == "test" +} + diff --git a/test/php/library/Director/Objects/rendered/service2.out b/test/php/library/Director/Objects/rendered/service2.out new file mode 100644 index 00000000..48e5aab1 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/service2.out @@ -0,0 +1,14 @@ +apply Service "___TEST___service" { + display_name = "Whatever service" + vars.test1 = "string" + vars.test2 = 17 + vars.test3 = false + vars.test4 = { + "a" = [ "dict", "ionary" ] + "this" = "is" + } + + assign where match("128.*", host.address) + ignore where host.name == "localhost" +} +