diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /test/php/library/Director/Objects/IcingaServiceTest.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | test/php/library/Director/Objects/IcingaServiceTest.php | 293 |
1 files changed, 293 insertions, 0 deletions
diff --git a/test/php/library/Director/Objects/IcingaServiceTest.php b/test/php/library/Director/Objects/IcingaServiceTest.php new file mode 100644 index 0000000..468db67 --- /dev/null +++ b/test/php/library/Director/Objects/IcingaServiceTest.php @@ -0,0 +1,293 @@ +<?php + +namespace Tests\Icinga\Module\Director\Objects; + +use Icinga\Module\Director\IcingaConfig\IcingaConfig; +use Icinga\Module\Director\Objects\IcingaHost; +use Icinga\Module\Director\Objects\IcingaService; +use Icinga\Module\Director\Test\BaseTestCase; + +class IcingaServiceTest extends BaseTestCase +{ + protected $testHostName = '___TEST___host'; + + protected $testServiceName = '___TEST___service'; + + protected $createdServices = array(); + + public function testUnstoredHostCanBeLazySet() + { + $service = $this->service(); + $service->display_name = 'Something else'; + $service->host = 'not yet'; + $this->assertEquals( + 'not yet', + $service->host + ); + } + + /** + * @expectedException \RuntimeException + */ + 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); + $service->delete(); + } + + public function testAcceptsAssignRules() + { + $service = $this->service(); + $service->object_type = 'apply'; + $service->assign_filter = 'host.address="127.*"'; + } + + /** + * @expectedException \LogicException + */ + public function testRefusesAssignRulesWhenNotBeingAnApply() + { + $service = $this->service(); + $service->assign_filter = 'host.address=127.*'; + } + + public function testAcceptsAndRendersFlatAssignRules() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $service = $this->service(); + + // Service apply rule rendering requires access to settings: + $service->setConnection($db); + $service->object_type = 'apply'; + $service->assign_filter = 'host.address="127.*"|host.vars.env="test"'; + + $this->assertEquals( + $this->loadRendered('service1'), + (string) $service + ); + + $this->assertEquals( + 'host.address="127.*"|host.vars.env="test"', + $service->assign_filter + ); + } + + public function testAcceptsAndRendersStructuredAssignRules() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $service = $this->service(); + // Service apply rule rendering requires access to settings: + $service->setConnection($db); + $service->object_type = 'apply'; + $service->assign_filter = 'host.address="127.*"|host.vars.env="test"'; + + $this->assertEquals( + $this->loadRendered('service1'), + (string) $service + ); + + $this->assertEquals( + 'host.address="127.*"|host.vars.env="test"', + $service->assign_filter = 'host.address="127.*"|host.vars.env="test"' + ); + } + + public function testPersistsAssignRules() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $service = $this->service(); + $service->object_type = 'apply'; + $service->assign_filter = '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.*"|host.vars.env="test"', + $service->assign_filter + ); + + $service->delete(); + } + + public function testRendersToTheCorrectZone() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + $service = $this->service()->setConnection($db); + + $config = new IcingaConfig($db); + $service->renderToConfig($config); + $masterzone = $db->getMasterZoneName(); + $this->assertEquals( + array('zones.d/' . $masterzone . '/services.conf'), + $config->getFileNames() + ); + } + + public function testVariablesInPropertiesAndCustomVariables() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $service = $this->service('___TEST___service_$not_replaced$'); + $service->setConnection($db); + $service->object_type = 'apply'; + $service->display_name = 'Service: $host.vars.replaced$'; + $service->assign_filter = 'host.address="127.*"'; + $service->{'vars.custom_var'} = '$host.vars.replaced$'; + + $this->assertEquals( + $this->loadRendered('service3'), + (string) $service + ); + } + + public function testVariablesAreNotReplacedForNonApplyObjects() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $host = $this->host(); + $host->store($db); + + $service = $this->service('___TEST___service_$not_replaced$'); + $service->object_type = 'object'; + $service->host_id = $host->get('id'); + $service->display_name = 'Service: $host.vars.not_replaced$'; + $service->{'vars.custom_var'} = '$host.vars.not_replaced$'; + $service->store($db); + + $service = IcingaService::loadWithAutoIncId($service->id, $db); + $this->assertEquals( + $this->loadRendered('service4'), + (string) $service + ); + } + + public function testApplyForRendersInVariousModes() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $service = $this->service()->setConnection($db); + $service->object_type = 'apply'; + $service->apply_for = 'host.vars.test1'; + $service->assign_filter = 'host.vars.env="test"'; + $this->assertEquals( + $this->loadRendered('service5'), + (string) $service + ); + + $service->object_name = '___TEST$config$___service $host.var.bla$'; + $this->assertEquals( + $this->loadRendered('service6'), + (string) $service + ); + + $service->object_name = ''; + $this->assertEquals( + $this->loadRendered('service7'), + (string) $service + ); + } + + protected function host() + { + return IcingaHost::create(array( + 'object_name' => $this->testHostName, + 'object_type' => 'object', + 'address' => '127.0.0.1', + )); + } + + protected function service($objectName = null) + { + if ($objectName === null) { + $objectName = $this->testServiceName; + } + $this->createdServices[] = $objectName; + return IcingaService::create(array( + 'object_name' => $objectName, + '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 = $this->createdServices; + foreach ($kill as $name) { + if (IcingaService::exists(array($name), $db)) { + IcingaService::load($name, $db)->delete(); + } + } + } + } +} |