IcingaHostTest: more tests

This commit is contained in:
Thomas Gelf 2016-03-08 21:24:42 +01:00
parent 8cb84c7735
commit bbd96fd375
3 changed files with 147 additions and 15 deletions

View File

@ -3,13 +3,14 @@
namespace Tests\Icinga\Module\Director\Objects; namespace Tests\Icinga\Module\Director\Objects;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Module\Director\Test\BaseTestCase; use Icinga\Module\Director\Test\BaseTestCase;
class IcingaHostTest extends BaseTestCase class IcingaHostTest extends BaseTestCase
{ {
protected $testHostName = '___TEST___host'; protected $testHostName = '___TEST___host';
public function testWhetherHostPropertiesCanBeSet() public function testPropertiesCanBeSet()
{ {
$host = $this->host(); $host = $this->host();
$host->display_name = 'Something else'; $host->display_name = 'Something else';
@ -19,7 +20,7 @@ class IcingaHostTest extends BaseTestCase
); );
} }
public function testWhetherHostsCanBeReplaced() public function testCanBeReplaced()
{ {
$host = $this->host(); $host = $this->host();
$newHost = IcingaHost::create( $newHost = IcingaHost::create(
@ -51,7 +52,7 @@ class IcingaHostTest extends BaseTestCase
); );
} }
public function testWhetherHostsCanBeMerged() public function testCanBeMerged()
{ {
$host = $this->host(); $host = $this->host();
$newHost = IcingaHost::create( $newHost = IcingaHost::create(
@ -82,7 +83,7 @@ class IcingaHostTest extends BaseTestCase
); );
} }
public function testWhetherDistinctCustomVarsCanBeSetWithoutSideEffects() public function testDistinctCustomVarsCanBeSetWithoutSideEffects()
{ {
$host = $this->host(); $host = $this->host();
$host->set('vars.test2', 18); $host->set('vars.test2', 18);
@ -100,7 +101,7 @@ class IcingaHostTest extends BaseTestCase
); );
} }
public function testWhetherHostVarsArePersisted() public function testVarsArePersisted()
{ {
if ($this->skipForMissingDb()) { if ($this->skipForMissingDb()) {
return; return;
@ -133,7 +134,7 @@ class IcingaHostTest extends BaseTestCase
); );
} }
public function testWhetherAHostRendersCorrectly() public function testRendersCorrectly()
{ {
$this->assertEquals( $this->assertEquals(
(string) $this->host(), (string) $this->host(),
@ -143,13 +144,7 @@ class IcingaHostTest extends BaseTestCase
public function testGivesPlainObjectWithInvalidUnresolvedDependencies() public function testGivesPlainObjectWithInvalidUnresolvedDependencies()
{ {
$props = array( $props = $this->getDummyRelatedProperties();
'zone' => 'invalid',
'check_command' => 'unknown',
'event_command' => 'What event?',
'check_period' => 'Not time is a good time @ nite',
'command_endpoint' => 'nirvana',
);
$host = $this->host(); $host = $this->host();
foreach ($props as $k => $v) { 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() public function testRendersWithInvalidUnresolvedDependencies()
{ {
$newHost = $this->host(); $newHost = $this->host();
@ -191,6 +271,17 @@ class IcingaHostTest extends BaseTestCase
$host->store($this->getDb()); $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() protected function host()
{ {
return IcingaHost::create(array( return IcingaHost::create(array(
@ -222,8 +313,18 @@ class IcingaHostTest extends BaseTestCase
{ {
if ($this->hasDb()) { if ($this->hasDb()) {
$db = $this->getDb(); $db = $this->getDb();
if (IcingaHost::exists($this->testHostName, $db)) { $kill = array($this->testHostName, '___TEST___parent');
IcingaHost::load($this->testHostName, $db)->delete(); 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();
}
} }
} }
} }

View File

@ -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"
}
}

View File

@ -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"
}
}