From bbd96fd3758d378f76d6afa5b73a4b3e7ab24b1f Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 8 Mar 2016 21:24:42 +0100 Subject: [PATCH] IcingaHostTest: more tests --- .../Director/Objects/IcingaHostTest.php | 131 ++++++++++++++++-- .../Director/Objects/rendered/host2.out | 17 +++ .../Director/Objects/rendered/host3.out | 14 ++ 3 files changed, 147 insertions(+), 15 deletions(-) create mode 100644 test/php/library/Director/Objects/rendered/host2.out create mode 100644 test/php/library/Director/Objects/rendered/host3.out diff --git a/test/php/library/Director/Objects/IcingaHostTest.php b/test/php/library/Director/Objects/IcingaHostTest.php index 095ed996..f3347075 100644 --- a/test/php/library/Director/Objects/IcingaHostTest.php +++ b/test/php/library/Director/Objects/IcingaHostTest.php @@ -3,13 +3,14 @@ namespace Tests\Icinga\Module\Director\Objects; use Icinga\Module\Director\Objects\IcingaHost; +use Icinga\Module\Director\Objects\IcingaZone; use Icinga\Module\Director\Test\BaseTestCase; class IcingaHostTest extends BaseTestCase { protected $testHostName = '___TEST___host'; - public function testWhetherHostPropertiesCanBeSet() + public function testPropertiesCanBeSet() { $host = $this->host(); $host->display_name = 'Something else'; @@ -19,7 +20,7 @@ class IcingaHostTest extends BaseTestCase ); } - public function testWhetherHostsCanBeReplaced() + public function testCanBeReplaced() { $host = $this->host(); $newHost = IcingaHost::create( @@ -51,7 +52,7 @@ class IcingaHostTest extends BaseTestCase ); } - public function testWhetherHostsCanBeMerged() + public function testCanBeMerged() { $host = $this->host(); $newHost = IcingaHost::create( @@ -82,7 +83,7 @@ class IcingaHostTest extends BaseTestCase ); } - public function testWhetherDistinctCustomVarsCanBeSetWithoutSideEffects() + public function testDistinctCustomVarsCanBeSetWithoutSideEffects() { $host = $this->host(); $host->set('vars.test2', 18); @@ -100,7 +101,7 @@ class IcingaHostTest extends BaseTestCase ); } - public function testWhetherHostVarsArePersisted() + public function testVarsArePersisted() { if ($this->skipForMissingDb()) { return; @@ -133,7 +134,7 @@ class IcingaHostTest extends BaseTestCase ); } - public function testWhetherAHostRendersCorrectly() + public function testRendersCorrectly() { $this->assertEquals( (string) $this->host(), @@ -143,13 +144,7 @@ class IcingaHostTest extends BaseTestCase public function testGivesPlainObjectWithInvalidUnresolvedDependencies() { - $props = array( - 'zone' => 'invalid', - 'check_command' => 'unknown', - 'event_command' => 'What event?', - 'check_period' => 'Not time is a good time @ nite', - 'command_endpoint' => 'nirvana', - ); + $props = $this->getDummyRelatedProperties(); $host = $this->host(); foreach ($props as $k => $v) { @@ -162,6 +157,91 @@ class IcingaHostTest extends BaseTestCase } } + public function testCorrectlyStoresLazyRelations() + { + if ($this->skipForMissingDb()) { + return; + } + $db = $this->getDb(); + $host = $this->host(); + $host->zone = '___TEST___zone'; + $this->assertEquals( + '___TEST___zone', + $host->zone + ); + + $zone = $this->newObject('zone', '___TEST___zone'); + $zone->store($db); + + $host->store($db); + $zone->delete(); + $host->delete(); + } + + /** + * @expectedException \Icinga\Exception\NotFoundError + */ + public function testFailsToStoreWithMissingLazyRelations() + { + if ($this->skipForMissingDb()) { + return; + } + $db = $this->getDb(); + $host = $this->host(); + $host->zone = '___TEST___zone'; + $host->store($db); + } + + public function testHandlesUnmodifiedProperties() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + $host = $this->host(); + $host->store($db); + + $parent = $this->newObject('host', '___TEST___parent'); + $parent->store($db); + $host->imports = '___TEST___parent'; + + $host->store($db); + + $plain = $host->getPlainUnmodifiedObject(); + $this->assertEquals( + 'string', + $plain->vars->test1 + ); + $host->vars()->set('test1', 'nada'); + + $host->store(); + + $plain = $host->getPlainUnmodifiedObject(); + $this->assertEquals( + 'nada', + $plain->vars->test1 + ); + + $host->vars()->set('test1', 'string'); + $plain = $host->getPlainUnmodifiedObject(); + $this->assertEquals( + 'nada', + $plain->vars->test1 + ); + + $plain = $host->getPlainUnmodifiedObject(); + $test = IcingaHost::create((array) $plain); + + $this->assertEquals( + $this->loadRendered('host3'), + (string) $test + ); + + $host->delete(); + $parent->delete(); + } + public function testRendersWithInvalidUnresolvedDependencies() { $newHost = $this->host(); @@ -191,6 +271,17 @@ class IcingaHostTest extends BaseTestCase $host->store($this->getDb()); } + protected function getDummyRelatedProperties() + { + return array( + 'zone' => 'invalid', + 'check_command' => 'unknown', + 'event_command' => 'What event?', + 'check_period' => 'Not time is a good time @ nite', + 'command_endpoint' => 'nirvana', + ); + } + protected function host() { return IcingaHost::create(array( @@ -222,8 +313,18 @@ class IcingaHostTest extends BaseTestCase { if ($this->hasDb()) { $db = $this->getDb(); - if (IcingaHost::exists($this->testHostName, $db)) { - IcingaHost::load($this->testHostName, $db)->delete(); + $kill = array($this->testHostName, '___TEST___parent'); + foreach ($kill as $name) { + if (IcingaHost::exists($name, $db)) { + IcingaHost::load($name, $db)->delete(); + } + } + + $kill = array('___TEST___zone'); + foreach ($kill as $name) { + if (IcingaZone::exists($name, $db)) { + IcingaZone::load($name, $db)->delete(); + } } } } diff --git a/test/php/library/Director/Objects/rendered/host2.out b/test/php/library/Director/Objects/rendered/host2.out new file mode 100644 index 00000000..06022bd7 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/host2.out @@ -0,0 +1,17 @@ +object Host "___TEST___host" { + display_name = "Whatever" + address = "127.0.0.127" + check_command = "unknown" + check_period = "Not time is a good time @ nite" + event_command = "What event?" + zone = "invalid" + command_endpoint = "nirvana" + vars.test1 = "string" + vars.test2 = 17 + vars.test3 = false + vars.test4 = { + "a" = [ "dict", "ionary" ] + "this" = "is" + } +} + diff --git a/test/php/library/Director/Objects/rendered/host3.out b/test/php/library/Director/Objects/rendered/host3.out new file mode 100644 index 00000000..215c2b0c --- /dev/null +++ b/test/php/library/Director/Objects/rendered/host3.out @@ -0,0 +1,14 @@ +object Host "___TEST___host" { + import "___TEST___parent" + + display_name = "Whatever" + address = "127.0.0.127" + vars.test1 = "nada" + vars.test2 = 17 + vars.test3 = false + vars.test4 = { + "a" = [ "dict", "ionary" ] + "this" = "is" + } +} +