summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/IcingaConfig/AssignRendererTest.php
blob: b9f574efadd4f22b0c9d80d57db05b72fc51daa7 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

namespace Tests\Icinga\Module\Director\IcingaConfig;

use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
use Icinga\Module\Director\Test\BaseTestCase;

class AssignRendererTest extends BaseTestCase
{
    public function testEqualMatchIsCorrectlyRendered()
    {
        $string = 'host.name="localhost"';
        $expected = 'assign where host.name == "localhost"';
        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testNegationIsRenderedCorrectlyOnRootLevel()
    {
        $string = '!(host.name="one"&host.name="two")';
        $expected = 'assign where !(host.name == "one" && host.name == "two")';
        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testNegationIsRenderedCorrectlyOnDeeperLevel()
    {
        $string = 'host.address="127.*"&!host.name="localhost"';
        $expected = 'assign where match("127.*", host.address) && !(host.name == "localhost")';
        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testWildcardsRenderAMatchMethod()
    {
        $string = 'host.address="127.0.0.*"';
        $expected = 'assign where match("127.0.0.*", host.address)';
        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testACombinedFilterRendersCorrectly()
    {
        $string = 'host.name="*internal"|(service.vars.priority<2'
            . '&host.vars.is_clustered=true)';

        $expected = 'assign where match("*internal", host.name) ||'
            . ' (service.vars.priority < 2 && host.vars.is_clustered)';

        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testSlashesAreNotEscaped()
    {
        $string = 'host.name=' . json_encode('a/b');

        $expected = 'assign where host.name == "a/b"';

        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testFakeContainsOperatorRendersCorrectly()
    {
        $string = json_encode('member') . '=host.groups';

        $expected = 'assign where "member" in host.groups';

        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );

        $string = json_encode('member') . '=host.vars.some_array';

        $expected = 'assign where "member" in host.vars.some_array';

        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testInArrayIsRenderedCorrectly()
    {
        $string = 'host.name=' . json_encode(array('a' ,'b'));

        $expected = 'assign where host.name in [ "a", "b" ]';

        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    public function testWhetherSlashesAreNotEscaped()
    {
        $string = 'host.name=' . json_encode('a/b');

        $expected = 'assign where host.name == "a/b"';

        $this->assertEquals(
            $expected,
            $this->renderer($string)->renderAssign()
        );
    }

    protected function renderer($string)
    {
        return AssignRenderer::forFilter(Filter::fromQueryString($string));
    }
}