summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/Objects/HostApplyMatchesTest.php
blob: b9f22ca04c3bcedbeed49edf5149be38c96f5011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

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());
    }
}