Passed
Pull Request — master (#78)
by
unknown
02:38
created

build.tests.unit.managers.test_deployer   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 384
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 281
dl 0
loc 384
ccs 166
cts 166
cp 1
rs 10
c 0
b 0
f 0
wmc 20

4 Methods

Rating   Name   Duplication   Size   Complexity  
B TestDeployer.setUp() 0 116 1
B TestDeployer.test_mw_case_2() 0 87 5
B TestDeployer.test_mw_case_3() 0 67 7
C TestDeployer.test_mw_case_1() 0 90 7
1
"""Tests for the deployer module."""
2
3 1
from unittest import TestCase
4 1
from unittest.mock import MagicMock
5
6 1
from collections import Counter
7
8 1
from datetime import datetime, timedelta
9 1
import pytz
10 1
from kytos.lib.helpers import get_controller_mock
11 1
from napps.kytos.maintenance.models import MaintenanceWindow as MW
12
13 1
from napps.kytos.maintenance.managers.deployer import (
14
    MaintenanceDeployer,
15
)
16
17 1
class TestDeployer(TestCase):
18
    """Test of the MaintenanceDeployer class."""
19 1
    def setUp(self):
20 1
        self.controller = get_controller_mock()
21 1
        self.start = datetime.now(pytz.utc)
22 1
        self.start += timedelta(days=1)
23 1
        self.end = self.start + timedelta(hours=6)
24 1
        self.switches = [
25
            "01:23:45:67:89:ab:cd:ef"
26
        ]
27 1
        self.maintenance = MW(
28
            start=self.start,
29
            end=self.end,
30
            switches=self.switches
31
        )
32
33 1
        self.deployer = MaintenanceDeployer(self.controller, Counter(), Counter(), Counter())
34
        # Initialize Switches
35 1
        self.controller.switches = {
36
            '01:23:45:67:89:ab:cd:ef': MagicMock(
37
                id='01:23:45:67:89:ab:cd:ef',
38
                interfaces = {},
39
            ),
40
            '01:23:45:67:65:ab:cd:ef': MagicMock(
41
                id='01:23:45:67:65:ab:cd:ef',
42
                interfaces = {},
43
            ),
44
            '01:23:45:67:66:ab:cd:ef': MagicMock(
45
                id='01:23:45:67:66:ab:cd:ef',
46
                interfaces = {},
47
            ),
48
        }
49
        # Initialize Interfaces
50 1
        self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces = {
51
            0: MagicMock(
52
                id = '01:23:45:67:89:ab:cd:ef:0',
53
                switch = self.controller.switches['01:23:45:67:89:ab:cd:ef'],
54
                link = None,
55
            ),
56
            1: MagicMock(
57
                id = '01:23:45:67:89:ab:cd:ef:1',
58
                switch = self.controller.switches['01:23:45:67:89:ab:cd:ef'],
59
                link = None,
60
            ),
61
            2: MagicMock(
62
                id = '01:23:45:67:89:ab:cd:ef:2',
63
                switch = self.controller.switches['01:23:45:67:89:ab:cd:ef'],
64
                link = None,
65
            ),
66
        }
67
68 1
        self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces = {
69
            0: MagicMock(
70
                id = '01:23:45:67:65:ab:cd:ef:0',
71
                switch = self.controller.switches['01:23:45:67:65:ab:cd:ef'],
72
                link = None,
73
            ),
74
            1: MagicMock(
75
                id = '01:23:45:67:65:ab:cd:ef:1',
76
                switch = self.controller.switches['01:23:45:67:65:ab:cd:ef'],
77
                link = None,
78
            ),
79
            2: MagicMock(
80
                id = '01:23:45:67:65:ab:cd:ef:2',
81
                switch = self.controller.switches['01:23:45:67:65:ab:cd:ef'],
82
                link = None,
83
            ),
84
        }
85
86 1
        self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces = {
87
            0: MagicMock(
88
                id = '01:23:45:67:66:ab:cd:ef:0',
89
                switch = self.controller.switches['01:23:45:67:66:ab:cd:ef'],
90
                link = None,
91
            ),
92
            1: MagicMock(
93
                id = '01:23:45:67:66:ab:cd:ef:1',
94
                switch = self.controller.switches['01:23:45:67:66:ab:cd:ef'],
95
                link = None,
96
            ),
97
            2: MagicMock(
98
                id = '01:23:45:67:66:ab:cd:ef:2',
99
                switch = self.controller.switches['01:23:45:67:66:ab:cd:ef'],
100
                link = None,
101
            ),
102
        }
103
104
        # Initialize Links
105 1
        self.link_1 = MagicMock(
106
            id = 'link_1',
107
            endpoint_a = self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces[0],
108
            endpoint_b = self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces[0],
109
        )
110 1
        self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces[0].link = self.link_1
111 1
        self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces[0].link = self.link_1
112
113 1
        self.link_2 = MagicMock(
114
            id = 'link_2',
115
            endpoint_a = self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces[1],
116
            endpoint_b = self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces[0],
117
        )
118 1
        self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces[1].link = self.link_2
119 1
        self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces[0].link = self.link_2
120
121 1
        self.link_3 = MagicMock(
122
            id = 'link_3',
123
            endpoint_a = self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces[1],
124
            endpoint_b = self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces[2],
125
        )
126 1
        self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces[1].link = self.link_3
127 1
        self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces[2].link = self.link_3
128
129
130 1
        self.controller.napps[('kytos', 'topology')] = MagicMock(
131
            links = {
132
                'link_1': self.link_1,
133
                'link_2': self.link_2,
134
                'link_3': self.link_3,
135
            }
136
        )
137
138 1
    def test_mw_case_1(self):
139
        """Test deploying a maintenance window to switches."""
140 1
        buffer_put_mock = MagicMock()
141 1
        self.controller.buffers.app.put = buffer_put_mock
142 1
        maintenance = self.maintenance.copy(
143
            update = {
144
                'switches': [
145
                    '01:23:45:67:89:ab:cd:ef',
146
                    '01:23:45:67:65:ab:cd:ef'
147
                ],
148
                'interfaces': [],
149
                'links': [],
150
            }
151
        )
152 1
        self.deployer.start_mw(maintenance)
153 1
        args, kwargs = buffer_put_mock.call_args
154 1
        event = args[0]
155 1
        assert event.name == 'topology.interruption.start'
156 1
        assert event.content['type'] == 'maintenance'
157 1
        assert sorted(event.content['switches']) == [
158
            '01:23:45:67:65:ab:cd:ef',
159
            '01:23:45:67:89:ab:cd:ef',
160
        ]
161 1
        assert sorted(event.content['interfaces']) == [
162
            '01:23:45:67:65:ab:cd:ef:0',
163
            '01:23:45:67:65:ab:cd:ef:1',
164
            '01:23:45:67:65:ab:cd:ef:2',
165
            '01:23:45:67:89:ab:cd:ef:0',
166
            '01:23:45:67:89:ab:cd:ef:1',
167
            '01:23:45:67:89:ab:cd:ef:2',
168
        ]
169 1
        assert sorted(event.content['links']) == [
170
            'link_1',
171
            'link_2',
172
        ]
173
174
        # Check whats in maintenance
175
        # Switches
176 1
        assert not self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'])
177 1
        assert not self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'])
178 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:66:ab:cd:ef'])
179
        # Interfaces
180 1
        for interface in self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces.values():
181 1
            assert not self.deployer.interface_not_in_maintenance(interface)
182 1
        for interface in self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces.values():
183 1
            assert not self.deployer.interface_not_in_maintenance(interface)
184 1
        for interface in self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces.values():
185 1
            assert     self.deployer.interface_not_in_maintenance(interface)
186
        # Links
187 1
        assert not self.deployer.link_not_in_maintenance(self.link_1)
188 1
        assert not self.deployer.link_not_in_maintenance(self.link_2)
189 1
        assert     self.deployer.link_not_in_maintenance(self.link_3)
190
191 1
        self.deployer.end_mw(maintenance)
192 1
        args, kwargs = buffer_put_mock.call_args
193 1
        event = args[0]
194 1
        assert event.name == 'topology.interruption.end'
195 1
        assert event.content['type'] == 'maintenance'
196 1
        assert sorted(event.content['switches']) == [
197
            '01:23:45:67:65:ab:cd:ef',
198
            '01:23:45:67:89:ab:cd:ef',
199
        ]
200 1
        assert sorted(event.content['interfaces']) == [
201
            '01:23:45:67:65:ab:cd:ef:0',
202
            '01:23:45:67:65:ab:cd:ef:1',
203
            '01:23:45:67:65:ab:cd:ef:2',
204
            '01:23:45:67:89:ab:cd:ef:0',
205
            '01:23:45:67:89:ab:cd:ef:1',
206
            '01:23:45:67:89:ab:cd:ef:2',
207
        ]
208 1
        assert sorted(event.content['links']) == [
209
            'link_1',
210
            'link_2',
211
        ]
212
        # Check whats in maintenance
213
        # Switches
214 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'])
215 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'])
216 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:66:ab:cd:ef'])
217
        # Interfaces
218 1
        for interface in self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces.values():
219 1
            assert     self.deployer.interface_not_in_maintenance(interface)
220 1
        for interface in self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces.values():
221 1
            assert     self.deployer.interface_not_in_maintenance(interface)
222 1
        for interface in self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces.values():
223 1
            assert     self.deployer.interface_not_in_maintenance(interface)
224
        # Links
225 1
        assert     self.deployer.link_not_in_maintenance(self.link_1)
226 1
        assert     self.deployer.link_not_in_maintenance(self.link_2)
227 1
        assert     self.deployer.link_not_in_maintenance(self.link_3)
228
229 1
    def test_mw_case_2(self):
230
        """Test deploying a maintenance window to interfaces."""
231 1
        buffer_put_mock = MagicMock()
232 1
        self.controller.buffers.app.put = buffer_put_mock
233
        
234 1
        maintenance = self.maintenance.copy(
235
            update = {
236
                'switches': [],
237
                'interfaces': [
238
                    '01:23:45:67:65:ab:cd:ef:0',
239
                    '01:23:45:67:89:ab:cd:ef:0',
240
                    '01:23:45:67:89:ab:cd:ef:1',
241
                ],
242
                'links': [],
243
            }
244
        )
245 1
        self.deployer.start_mw(maintenance)
246 1
        args, kwargs = buffer_put_mock.call_args
247 1
        event = args[0]
248 1
        assert event.name == 'topology.interruption.start'
249 1
        assert event.content['type'] == 'maintenance'
250 1
        assert sorted(event.content['switches']) == []
251 1
        assert sorted(event.content['interfaces']) == [
252
            '01:23:45:67:65:ab:cd:ef:0',
253
            '01:23:45:67:89:ab:cd:ef:0',
254
            '01:23:45:67:89:ab:cd:ef:1',
255
        ]
256 1
        assert sorted(event.content['links']) == [
257
            'link_1',
258
            'link_2',
259
        ]
260
261
        # Check whats in maintenance
262
        # Switches
263 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'])
264 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'])
265 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:66:ab:cd:ef'])
266
        # Interfaces
267
268 1
        assert not self.deployer.interface_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces[0])
269 1
        assert not self.deployer.interface_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces[1])
270 1
        assert     self.deployer.interface_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces[2])
271
272 1
        assert not self.deployer.interface_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces[0])
273 1
        assert     self.deployer.interface_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces[1])
274 1
        assert     self.deployer.interface_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces[2])
275
276 1
        for interface in self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces.values():
277 1
            assert     self.deployer.interface_not_in_maintenance(interface)
278
279
        # Links
280 1
        assert not self.deployer.link_not_in_maintenance(self.link_1)
281 1
        assert not self.deployer.link_not_in_maintenance(self.link_2)
282 1
        assert     self.deployer.link_not_in_maintenance(self.link_3)
283
284 1
        self.deployer.end_mw(maintenance)
285 1
        args, kwargs = buffer_put_mock.call_args
286 1
        event = args[0]
287 1
        assert event.name == 'topology.interruption.end'
288 1
        assert event.content['type'] == 'maintenance'
289 1
        assert sorted(event.content['switches']) == []
290 1
        assert sorted(event.content['switches']) == []
291 1
        assert sorted(event.content['interfaces']) == [
292
            '01:23:45:67:65:ab:cd:ef:0',
293
            '01:23:45:67:89:ab:cd:ef:0',
294
            '01:23:45:67:89:ab:cd:ef:1',
295
        ]
296 1
        assert sorted(event.content['links']) == [
297
            'link_1',
298
            'link_2',
299
        ]
300
        # Check whats in maintenance
301
        # Switches
302 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'])
303 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'])
304 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:66:ab:cd:ef'])
305
        # Interfaces
306 1
        for interface in self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces.values():
307 1
            assert     self.deployer.interface_not_in_maintenance(interface)
308 1
        for interface in self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces.values():
309 1
            assert     self.deployer.interface_not_in_maintenance(interface)
310 1
        for interface in self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces.values():
311 1
            assert     self.deployer.interface_not_in_maintenance(interface)
312
        # Links
313 1
        assert     self.deployer.link_not_in_maintenance(self.link_1)
314 1
        assert     self.deployer.link_not_in_maintenance(self.link_2)
315 1
        assert     self.deployer.link_not_in_maintenance(self.link_3)
316
317 1
    def test_mw_case_3(self):
318
        """Test deploying a maintenance window to links."""
319 1
        buffer_put_mock = MagicMock()
320 1
        self.controller.buffers.app.put = buffer_put_mock
321 1
        maintenance = self.maintenance.copy(
322
            update = {
323
                'switches': [],
324
                'interfaces': [],
325
                'links': ['link_1', 'link_2'],
326
            }
327
        )
328 1
        self.deployer.start_mw(maintenance)
329 1
        args, kwargs = buffer_put_mock.call_args
330 1
        event = args[0]
331 1
        assert event.name == 'topology.interruption.start'
332 1
        assert event.content['type'] == 'maintenance'
333 1
        assert sorted(event.content['switches']) == []
334 1
        assert sorted(event.content['interfaces']) == []
335 1
        assert sorted(event.content['links']) == [
336
            'link_1',
337
            'link_2',
338
        ]
339
340
        # Check whats in maintenance
341
        # Switches
342 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'])
343 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'])
344 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:66:ab:cd:ef'])
345
        # Interfaces
346 1
        for interface in self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces.values():
347 1
            assert     self.deployer.interface_not_in_maintenance(interface)
348 1
        for interface in self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces.values():
349 1
            assert     self.deployer.interface_not_in_maintenance(interface)
350 1
        for interface in self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces.values():
351 1
            assert     self.deployer.interface_not_in_maintenance(interface)
352
        # Links
353 1
        assert not self.deployer.link_not_in_maintenance(self.link_1)
354 1
        assert not self.deployer.link_not_in_maintenance(self.link_2)
355 1
        assert     self.deployer.link_not_in_maintenance(self.link_3)
356
357 1
        self.deployer.end_mw(maintenance)
358 1
        args, kwargs = buffer_put_mock.call_args
359 1
        event = args[0]
360 1
        assert event.name == 'topology.interruption.end'
361 1
        assert event.content['type'] == 'maintenance'
362 1
        assert sorted(event.content['switches']) == []
363 1
        assert sorted(event.content['interfaces']) == []
364 1
        assert sorted(event.content['links']) == [
365
            'link_1',
366
            'link_2',
367
        ]
368
        # Check whats in maintenance
369
        # Switches
370 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:89:ab:cd:ef'])
371 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:65:ab:cd:ef'])
372 1
        assert     self.deployer.switch_not_in_maintenance(self.controller.switches['01:23:45:67:66:ab:cd:ef'])
373
        # Interfaces
374 1
        for interface in self.controller.switches['01:23:45:67:89:ab:cd:ef'].interfaces.values():
375 1
            assert     self.deployer.interface_not_in_maintenance(interface)
376 1
        for interface in self.controller.switches['01:23:45:67:65:ab:cd:ef'].interfaces.values():
377 1
            assert     self.deployer.interface_not_in_maintenance(interface)
378 1
        for interface in self.controller.switches['01:23:45:67:66:ab:cd:ef'].interfaces.values():
379 1
            assert     self.deployer.interface_not_in_maintenance(interface)
380
        # Links
381 1
        assert     self.deployer.link_not_in_maintenance(self.link_1)
382 1
        assert     self.deployer.link_not_in_maintenance(self.link_2)
383
        assert     self.deployer.link_not_in_maintenance(self.link_3)
384