icingaweb2-module-director/test/php/library/Director/Objects/HostApplyMatchesTest.php
Eric Lippmann b6af283732 License source files as GPL-3.0-or-later
Add SPDX license headers and mark source files as GPL-3.0-or-later to
preserve the option to relicense under later GPL versions.
2026-03-24 11:30:06 +01:00

96 lines
2.7 KiB
PHP

<?php
// SPDX-FileCopyrightText: 2018 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Tests\Icinga\Module\Director\Objects;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Objects\HostApplyMatches;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Test\BaseTestCase;
class HostApplyMatchesTest extends BaseTestCase
{
public function testExactMatches()
{
$matcher = HostApplyMatches::prepare($this->sampleHost());
$this->assertTrue(
$matcher->matchesFilter(
Filter::fromQueryString('host.name=%22aha%22')
)
);
$this->assertFalse(
$matcher->matchesFilter(
Filter::fromQueryString('host.name=%22ahaa%22')
)
);
}
public function testWildcardMatches()
{
$matcher = HostApplyMatches::prepare($this->sampleHost());
$this->assertTrue(
$matcher->matchesFilter(
Filter::fromQueryString('host.name=%22ah*%22')
)
);
$this->assertTrue(
$matcher->matchesFilter(
Filter::fromQueryString('host.name=%22*h*%22')
)
);
$this->assertFalse(
$matcher->matchesFilter(
Filter::fromQueryString('host.name=%22*g*%22')
)
);
}
public function testStringVariableMatches()
{
$matcher = HostApplyMatches::prepare($this->sampleHost());
$this->assertTrue(
$matcher->matchesFilter(
Filter::fromQueryString('host.vars.location=%22*urem*%22')
)
);
$this->assertTrue(
$matcher->matchesFilter(
Filter::fromQueryString('host.vars.location=%22Nuremberg%22')
)
);
$this->assertFalse(
$matcher->matchesFilter(
Filter::fromQueryString('host.vars.location=%22Nurembergg%22')
)
);
}
public function testArrayVariableMatches()
{
$matcher = HostApplyMatches::prepare($this->sampleHost());
$this->assertTrue(
$matcher->matchesFilter(
Filter::fromQueryString('%22Amazing%22=host.vars.tags')
)
);
$this->assertFalse(
$matcher->matchesFilter(
Filter::fromQueryString('%22Amazingg%22=host.vars.tags')
)
);
}
protected function sampleHost()
{
return IcingaHost::create(array(
'object_type' => 'object',
'object_name' => 'aha',
'vars' => array(
'location' => 'Nuremberg',
'tags' => array('Special', 'Amazing'),
)
), $this->getDb());
}
}