summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/Import/HostSyncTest.php
blob: eeee7a4c548edd35525dd2c6e35bcbd7cc0ca93a (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php

namespace Tests\Icinga\Module\Director\IcingaConfig;

use Icinga\Module\Director\Objects\IcingaHostGroup;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Test\SyncTest;

class HostSyncTest extends SyncTest
{
    protected $objectType = 'host';

    protected $keyColumn = 'host';

    public function testSimpleSync()
    {
        $this->runImport(array(
            array(
                'host'    => 'SYNCTEST_simple',
                'address' => '127.0.0.1',
                'os'      => 'Linux'
            )
        ));

        $this->setUpProperty(array(
            'source_expression' => '${host}',
            'destination_field' => 'object_name',
            'priority'          => 10,
        ));
        $this->setUpProperty(array(
            'source_expression' => '${address}',
            'destination_field' => 'address',
            'priority'          => 11,
        ));
        $this->setUpProperty(array(
            'source_expression' => '${os}',
            'destination_field' => 'vars.os',
            'priority'          => 12,
        ));

        $this->assertTrue($this->sync->hasModifications(), 'Should have modifications pending');
        $this->assertGreaterThan(0, $this->sync->apply(), 'Should successfully apply at least 1 update');
        $this->assertFalse($this->sync->hasModifications(), 'Should not have modifications pending after sync');
    }

    public function testSyncWithoutData()
    {
        $this->runImport(array());

        $this->setUpProperty(array(
            'source_expression' => '${host}',
            'destination_field' => 'object_name',
            'priority'          => 10,
        ));

        $this->assertFalse($this->sync->hasModifications(), 'Should not have modifications pending');
    }

    public function testSyncMultipleGroups()
    {
        $this->requireGroups(['SYNCTEST_groupa', 'SYNCTEST_groupb']);
        $this->runImport([[
        'host' => 'SYNCTEST_groups1',
            'g1'   => 'SYNCTEST_groupa',
            'g2'   => 'SYNCTEST_groupb'
        ], [
            'host' => 'SYNCTEST_groups2',
            'g1'   => null,
            'g2'   => 'SYNCTEST_groupb'
        ]]);

        $this->setUpProperty(array(
            'source_expression' => '${host}',
            'destination_field' => 'object_name',
            'priority'          => 10,
        ));
        $this->setUpProperty(array(
            'source_expression' => '${g1}',
            'destination_field' => 'groups',
            'priority'          => 12,
        ));
        $this->setUpProperty(array(
            'source_expression' => '${g2}',
            'destination_field' => 'groups',
            'priority'          => 11,
        ));

        $this->assertTrue($this->sync->hasModifications(), 'Should have modifications pending');

        $modifications = array();
        /** @var IcingaObject $mod */
        foreach ($this->sync->getExpectedModifications() as $mod) {
            $name = $mod->object_name;
            $modifications[$name] = $mod;

            switch ($name) {
                case 'SYNCTEST_groups1':
                    $this->assertEquals([
                        'SYNCTEST_groupa',
                        'SYNCTEST_groupb',
                    ], $mod->get('groups'));
                    break;
                case 'SYNCTEST_groups2':
                    $this->assertEquals([
                        'SYNCTEST_groupb',
                    ], $mod->get('groups'));
                    break;
            }
        }

        $this->assertGreaterThan(0, $this->sync->apply(), 'Should successfully apply at least 1 update');
        $this->assertFalse($this->sync->hasModifications(), 'Should not have modifications pending after sync');
    }

    public function testSyncFilteredData()
    {
        $this->runImport(array(
            array(
                'host'    => 'SYNCTEST_filtered_in',
                'address' => '127.0.0.1',
                'os'      => 'Linux',
                'sync'    => 'yes'
            ),
            array(
                'host'    => 'SYNCTEST_filtered_out',
                'address' => '127.0.0.1',
                'os'      => null,
                'sync'    => 'no'
            ),
            array(
                'host'      => 'SYNCTEST_filtered_in_unusedfield',
                'address'   => '127.0.0.1',
                'os'        => null,
                'sync'      => 'no',
                'othersync' => '1'
            ),
            array(
                'host'      => 'SYNCTEST_filtered_in_unusedfield_propfilter',
                'address'   => '127.0.0.1',
                'os'        => null,
                'magic'     => '2',
                'sync'      => 'no',
                'othersync' => '1'
            )
        ));

        $this->rule->set('filter_expression', 'sync=yes|othersync=1');
        $this->rule->store();

        $this->setUpProperty(array(
            'source_expression' => '${host}',
            'destination_field' => 'object_name',
            'priority'          => 10,
        ));
        $this->setUpProperty(array(
            'source_expression' => '${address}',
            'destination_field' => 'address',
            'priority'          => 11,
        ));
        $this->setUpProperty(array(
            'source_expression' => 'test',
            'destination_field' => 'vars.magic',
            'filter_expression' => 'magic!=',
            'priority'          => 12,
        ));

        $modifications = array();
        /** @var IcingaObject $mod */
        foreach ($this->sync->getExpectedModifications() as $mod) {
            $name = $mod->object_name;
            $modifications[$name] = $mod;

            $this->assertEquals(
                '127.0.0.1',
                $mod->get('address'),
                $name . ': address should not be synced'
            );
            $this->assertNull($mod->get('vars.os'), $name . ': vars.os should not be synced');

            if ($name === 'SYNCTEST_filtered_in_unusedfield_propfilter') {
                $this->assertEquals(
                    'test',
                    $mod->get('vars.magic'),
                    $name . ': vars.magic should not be synced'
                );
            } else {
                $this->assertNull($mod->get('vars.magic'), $name . ': vars.magic should not be synced');
            }
        }

        $this->assertArrayHasKey(
            'SYNCTEST_filtered_in',
            $modifications,
            'SYNCTEST_filtered_in should be modified'
        );
        $this->assertArrayNotHasKey(
            'SYNCTEST_filtered_out',
            $modifications,
            'SYNCTEST_filtered_out should be synced'
        );
        $this->assertArrayHasKey(
            'SYNCTEST_filtered_in_unusedfield',
            $modifications,
            'SYNCTEST_filtered_in_unusedfield should be modified'
        );
        $this->assertArrayHasKey(
            'SYNCTEST_filtered_in_unusedfield_propfilter',
            $modifications,
            'SYNCTEST_filtered_in_unusedfield_propfilter should be modified'
        );
    }

    /**
     * @param $names
     * @throws \Icinga\Exception\ConfigurationError
     * @throws \Icinga\Exception\IcingaException
     */
    protected function requireGroups($names)
    {
        foreach ($names as $name) {
            if (! IcingaHostGroup::exists($name, $this->getDb())) {
                IcingaHostGroup::create([
                    'object_name' => $name,
                    'object_type' => 'object',
                ], $this->getDb())->store();
            }
        }
    }

    /**
     * @param $names
     * @throws \Icinga\Exception\ConfigurationError
     * @throws \Icinga\Exception\IcingaException
     * @throws \Icinga\Exception\NotFoundError
     */
    protected function removeGroups($names)
    {
        foreach ($names as $name) {
            if (IcingaHostGroup::exists($name, $this->getDb())) {
                IcingaHostGroup::load($name, $this->getDb())->delete();
            }
        }
    }

    public function tearDown()
    {
        $this->removeGroups(['SYNCTEST_groupa', 'SYNCTEST_groupb']);
        parent::tearDown();
    }
}