Skip to content

Commit

Permalink
Macros: Properly support CompatHost and CompatService
Browse files Browse the repository at this point in the history
fixes #900
  • Loading branch information
nilmerg committed Oct 13, 2023
1 parent 0f380c8 commit 4ff4443
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion library/Icingadb/Common/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function expandMacros(string $input, $object): string
*/
public function resolveMacro(string $macro, $object): string
{
if ($object instanceof Host) {
if ($object instanceof Host || (property_exists($object, 'type') && $object->type === 'host')) {
$objectType = 'host';
} else {
$objectType = 'service';
Expand Down
97 changes: 77 additions & 20 deletions test/php/library/Icingadb/Common/MacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Tests\Icinga\Modules\Icingadb\Common;

use Icinga\Module\Icingadb\Common\Macros;
use Icinga\Module\Icingadb\Compat\CompatHost;
use Icinga\Module\Icingadb\Compat\CompatService;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use ipl\Orm\Query;
Expand Down Expand Up @@ -34,20 +36,44 @@ public function testHostMacros()

$host->hostgroup = new Query();

$this->assertEquals($host->name, $this->expandMacros('$host.name$', $host));
$this->assertEquals($host->name, $this->expandMacros('$name$', $host));
$this->assertEquals($host->address, $this->expandMacros('$host.address$', $host));
$this->assertEquals($host->address6, $this->expandMacros('$host.address6$', $host));
$this->performHostMacroTests($host, $host);
}

public function testHostMacrosOnCompatObject()
{
if (! class_exists('Icinga\Module\Monitoring\Object\Host')) {
$this->markTestSkipped('This test requires the monitoring module');
}

$host = new Host();
$host->name = 'test';
$host->address = '1.1.1.1';
$host->address6 = '::1';
$host->vars = self::VARS;

$host->hostgroup = new Query();

$compatHost = new CompatHost($host);

$this->performHostMacroTests($compatHost, $host);
}

protected function performHostMacroTests($host, $source)
{
$this->assertEquals($source->name, $this->expandMacros('$host.name$', $host));
$this->assertEquals($source->name, $this->expandMacros('$name$', $host));
$this->assertEquals($source->address, $this->expandMacros('$host.address$', $host));
$this->assertEquals($source->address6, $this->expandMacros('$host.address6$', $host));

// A Host can have more than one hostgroups
$this->assertEquals('$host.hostgroup$', $this->expandMacros('$host.hostgroup$', $host));
$this->assertEquals('$host.hostgroup.name$', $this->expandMacros('$host.hostgroup.name$', $host));

// Host custom vars
$this->assertEquals($host->vars['os'], $this->expandMacros('$host.vars.os$', $host));
$this->assertEquals($host->vars['os'], $this->expandMacros('$vars.os$', $host));
$this->assertEquals($host->vars['days[2]'], $this->expandMacros('$vars.days[2]$', $host));
$this->assertEquals($host->vars['days[4]'], $this->expandMacros('$host.vars.days[4]$', $host));
$this->assertEquals($source->vars['os'], $this->expandMacros('$host.vars.os$', $host));
$this->assertEquals($source->vars['os'], $this->expandMacros('$vars.os$', $host));
$this->assertEquals($source->vars['days[2]'], $this->expandMacros('$vars.days[2]$', $host));
$this->assertEquals($source->vars['days[4]'], $this->expandMacros('$host.vars.days[4]$', $host));

// Host to service relation
$this->assertEquals('$service.name$', $this->expandMacros('$service.name$', $host));
Expand Down Expand Up @@ -76,9 +102,40 @@ public function testServiceMacros()

$service->host = $host;

$this->assertEquals($service->name, $this->expandMacros('$service.name$', $service));
$this->assertEquals($service->name, $this->expandMacros('$name$', $service));
$this->assertEquals($service->description, $this->expandMacros('$service.description$', $service));
$this->performServiceMacroTests($service, $service);
}

public function testServiceMacrosOnCompatObject()
{
if (! class_exists('Icinga\Module\Monitoring\Object\Service')) {
$this->markTestSkipped('This test requires the monitoring module');
}

$service = new Service();
$service->name = 'test-service';
$service->description = 'A test service';
$service->vars = self::VARS;

$service->servicegroup = new Query();

$host = new Host();
$host->name = 'test';
$host->address = '1.1.1.1';
$host->hostgroup = new ResultSet(new \ArrayIterator());
$host->vars = self::VARS;

$service->host = $host;

$compatService = new CompatService($service);

$this->performServiceMacroTests($compatService, $service);
}

protected function performServiceMacroTests($service, $source)
{
$this->assertEquals($source->name, $this->expandMacros('$service.name$', $service));
$this->assertEquals($source->name, $this->expandMacros('$name$', $service));
$this->assertEquals($source->description, $this->expandMacros('$service.description$', $service));

// A Service can have more than one hostgroups
$this->assertEquals(
Expand All @@ -91,18 +148,18 @@ public function testServiceMacros()
);

// Service custom vars
$this->assertEquals($service->vars['os'], $this->expandMacros('$service.vars.os$', $service));
$this->assertEquals($service->vars['os'], $this->expandMacros('$vars.os$', $service));
$this->assertEquals($service->vars['days[2]'], $this->expandMacros('$vars.days[2]$', $service));
$this->assertEquals($service->vars['days[4]'], $this->expandMacros('$service.vars.days[4]$', $service));
$this->assertEquals($source->vars['os'], $this->expandMacros('$service.vars.os$', $service));
$this->assertEquals($source->vars['os'], $this->expandMacros('$vars.os$', $service));
$this->assertEquals($source->vars['days[2]'], $this->expandMacros('$vars.days[2]$', $service));
$this->assertEquals($source->vars['days[4]'], $this->expandMacros('$service.vars.days[4]$', $service));

$this->assertEquals($host->name, $this->expandMacros('$host.name$', $service));
$this->assertEquals($host->address, $this->expandMacros('$host.address$', $service));
$this->assertEquals($source->host->name, $this->expandMacros('$host.name$', $service));
$this->assertEquals($source->host->address, $this->expandMacros('$host.address$', $service));

// Host custom vars
$this->assertEquals($host->vars['os'], $this->expandMacros('$host.vars.os$', $service));
$this->assertEquals($host->vars['days[0]'], $this->expandMacros('$host.vars.days[0]$', $service));
$this->assertEquals($host->vars['days[3]'], $this->expandMacros('$host.vars.days[3]$', $service));
$this->assertEquals($source->host->vars['os'], $this->expandMacros('$host.vars.os$', $service));
$this->assertEquals($source->host->vars['days[0]'], $this->expandMacros('$host.vars.days[0]$', $service));
$this->assertEquals($source->host->vars['days[3]'], $this->expandMacros('$host.vars.days[3]$', $service));

// A Host can have more than one hostgroups
$this->assertEquals(
Expand Down

0 comments on commit 4ff4443

Please sign in to comment.