Test Failed
Push — master ( cea608...9ea46a )
by Beraldo
04:17 queued 01:53
created

build.tests.models.test_link_protection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 458
Duplicated Lines 66.16 %

Importance

Changes 0
Metric Value
eloc 375
dl 303
loc 458
rs 10
c 0
b 0
f 0
wmc 13

13 Methods

Rating   Name   Duplication   Size   Complexity  
A TestLinkProtection.test_handle_link_up_case_2() 40 40 1
B TestLinkProtection.test_handle_link_down_case_3() 43 43 1
B TestLinkProtection.test_handle_link_down_case_4() 0 44 1
A TestLinkProtection.test_is_using_primary_path() 20 20 1
A TestLinkProtection.test_handle_link_up_case_1() 38 38 1
A TestLinkProtection.test_handle_link_up_case_4() 0 40 1
A TestLinkProtection.test_deploy_to_case_3() 16 16 1
B TestLinkProtection.test_handle_link_down_case_2() 42 42 1
A TestLinkProtection.test_deploy_to_case_2() 20 20 1
A TestLinkProtection.test_handle_link_up_case_3() 0 40 1
B TestLinkProtection.test_handle_link_down_case_1() 42 42 1
A TestLinkProtection.test_is_using_backup_path() 20 20 1
A TestLinkProtection.test_deploy_to_case_1() 22 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
"""Module to test the LinkProtection class."""
2
import sys
3
from unittest import TestCase
4
from unittest.mock import patch
5
6
from kytos.core.common import EntityStatus
7
8
# pylint: disable=wrong-import-position
9
sys.path.insert(0, '/var/lib/kytos/napps/..')
10
# pylint: enable=wrong-import-position
11
12
from napps.kytos.mef_eline.models import EVC, Path  # NOQA
13
from napps.kytos.mef_eline.tests.helpers import get_link_mocked, get_uni_mocked  # NOQA
14
15
16
class TestLinkProtection(TestCase):  # pylint: disable=too-many-public-methods
17
    """Tests to validate LinkProtection class."""
18
19 View Code Duplication
    def test_is_using_backup_path(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
        """Test test is using backup path."""
21
        backup_path = [
22
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
23
                                metadata={"s_vlan": 5}),
24
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
25
                                metadata={"s_vlan": 6})
26
        ]
27
28
        attributes = {
29
            "name": "circuit_name",
30
            "uni_a": get_uni_mocked(is_valid=True),
31
            "uni_z": get_uni_mocked(is_valid=True),
32
            "backup_path": backup_path
33
        }
34
35
        evc = EVC(**attributes)
36
        self.assertFalse(evc.is_using_backup_path())
37
        evc.current_path = evc.backup_path
38
        self.assertTrue(evc.is_using_backup_path())
39
40 View Code Duplication
    def test_is_using_primary_path(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
41
        """Test test is using primary path."""
42
        primary_path = [
43
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
44
                                metadata={"s_vlan": 5}),
45
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
46
                                metadata={"s_vlan": 6})
47
        ]
48
49
        attributes = {
50
            "name": "circuit_name",
51
            "uni_a": get_uni_mocked(is_valid=True),
52
            "uni_z": get_uni_mocked(is_valid=True),
53
            "primary_path": primary_path
54
        }
55
56
        evc = EVC(**attributes)
57
        self.assertFalse(evc.is_using_primary_path())
58
        evc.current_path = evc.primary_path
59
        self.assertTrue(evc.is_using_primary_path())
60
61 View Code Duplication
    @patch('napps.kytos.mef_eline.models.log')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
62
    def test_deploy_to_case_1(self, log_mocked):
63
        """Test if the path is equal to current_path."""
64
        primary_path = [
65
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
66
                                metadata={"s_vlan": 5}),
67
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
68
                                metadata={"s_vlan": 6})
69
        ]
70
        attributes = {
71
            "name": "circuit_name",
72
            "uni_a": get_uni_mocked(is_valid=True),
73
            "uni_z": get_uni_mocked(is_valid=True),
74
            "primary_path": primary_path
75
        }
76
        evc = EVC(**attributes)
77
        evc.current_path = evc.primary_path
78
79
        expected_deployed = evc.deploy_to('primary_path', evc.primary_path)
80
        expected_msg = 'primary_path is equal to current_path.'
81
        log_mocked.debug.assert_called_with(expected_msg)
82
        self.assertTrue(expected_deployed)
83
84 View Code Duplication
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
85
    def test_deploy_to_case_2(self, deploy_mocked):
86
        """Test deploy with all links up."""
87
        deploy_mocked.return_value = True
88
89
        primary_path = [
90
                 get_link_mocked(status=EntityStatus.UP),
91
                 get_link_mocked(status=EntityStatus.UP)
92
        ]
93
        attributes = {
94
            "name": "circuit_name",
95
            "uni_a": get_uni_mocked(is_valid=True),
96
            "uni_z": get_uni_mocked(is_valid=True),
97
            "primary_path": primary_path,
98
            "enabled": True
99
        }
100
        evc = EVC(**attributes)
101
        deployed = evc.deploy_to('primary_path', evc.primary_path)
102
        deploy_mocked.assert_called_with(evc.primary_path)
103
        self.assertTrue(deployed)
104
105 View Code Duplication
    def test_deploy_to_case_3(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
106
        """Test deploy with one link down."""
107
        primary_path = [
108
                 get_link_mocked(status=EntityStatus.DOWN),
109
                 get_link_mocked(status=EntityStatus.UP)
110
        ]
111
        attributes = {
112
            "name": "circuit_name",
113
            "uni_a": get_uni_mocked(is_valid=True),
114
            "uni_z": get_uni_mocked(is_valid=True),
115
            "primary_path": primary_path,
116
            "enabled": True
117
        }
118
        evc = EVC(**attributes)
119
        deployed = evc.deploy_to('primary_path', evc.primary_path)
120
        self.assertFalse(deployed)
121
122 View Code Duplication
    @patch('napps.kytos.mef_eline.models.log')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
123
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
124
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
125
    def test_handle_link_down_case_1(self, deploy_to_mocked, deploy_mocked,
126
                                     log_mocked):
127
        """Test if deploy_to backup path is called."""
128
        deploy_mocked.return_value = True
129
        deploy_to_mocked.return_value = True
130
        primary_path = [
131
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
132
                                metadata={"s_vlan": 5},
133
                                status=EntityStatus.DOWN),
134
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
135
                                metadata={"s_vlan": 6},
136
                                status=EntityStatus.UP),
137
        ]
138
        backup_path = [
139
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
140
                                metadata={"s_vlan": 5},
141
                                status=EntityStatus.UP),
142
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
143
                                metadata={"s_vlan": 6},
144
                                status=EntityStatus.UP),
145
        ]
146
        attributes = {
147
            "name": "circuit_name",
148
            "uni_a": get_uni_mocked(is_valid=True),
149
            "uni_z": get_uni_mocked(is_valid=True),
150
            "primary_path": primary_path,
151
            "backup_path": backup_path,
152
            "enabled": True
153
        }
154
        evc = EVC(**attributes)
155
156
        evc.current_path = evc.primary_path
157
        current_handle_link_down = evc.handle_link_down()
158
        self.assertEqual(deploy_mocked.call_count, 0)
159
        deploy_to_mocked.assert_called_once_with('backup_path',
160
                                                 evc.backup_path)
161
        self.assertTrue(current_handle_link_down)
162
        msg = f"{evc} deployed after link down."
163
        log_mocked.debug.assert_called_once_with(msg)
164
165 View Code Duplication
    @patch('napps.kytos.mef_eline.models.log')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
166
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
167
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
168
    def test_handle_link_down_case_2(self, deploy_to_mocked, deploy_mocked,
169
                                     log_mocked):
170
        """Test if deploy_to backup path is called."""
171
        deploy_mocked.return_value = True
172
        deploy_to_mocked.return_value = True
173
        primary_path = [
174
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
175
                                metadata={"s_vlan": 5},
176
                                status=EntityStatus.UP),
177
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
178
                                metadata={"s_vlan": 6},
179
                                status=EntityStatus.UP),
180
        ]
181
        backup_path = [
182
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
183
                                metadata={"s_vlan": 5},
184
                                status=EntityStatus.DOWN),
185
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
186
                                metadata={"s_vlan": 6},
187
                                status=EntityStatus.UP),
188
        ]
189
        attributes = {
190
            "name": "circuit_name",
191
            "uni_a": get_uni_mocked(is_valid=True),
192
            "uni_z": get_uni_mocked(is_valid=True),
193
            "primary_path": primary_path,
194
            "backup_path": backup_path,
195
            "enabled": True
196
        }
197
198
        evc = EVC(**attributes)
199
        evc.current_path = evc.backup_path
200
        current_handle_link_down = evc.handle_link_down()
201
        self.assertEqual(deploy_mocked.call_count, 0)
202
        deploy_to_mocked.assert_called_once_with('primary_path',
203
                                                 evc.primary_path)
204
        self.assertTrue(current_handle_link_down)
205
        msg = f"{evc} deployed after link down."
206
        log_mocked.debug.assert_called_once_with(msg)
207
208 View Code Duplication
    @patch('napps.kytos.mef_eline.models.log')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
209
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
210
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
211
    def test_handle_link_down_case_3(self, deploy_to_mocked, deploy_mocked,
212
                                     log_mocked):
213
        """Test if circuit without dynamic path is return failed."""
214
        deploy_mocked.return_value = False
215
        deploy_to_mocked.return_value = False
216
        primary_path = [
217
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
218
                                metadata={"s_vlan": 5},
219
                                status=EntityStatus.DOWN),
220
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
221
                                metadata={"s_vlan": 6},
222
                                status=EntityStatus.UP),
223
        ]
224
        backup_path = [
225
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
226
                                metadata={"s_vlan": 5},
227
                                status=EntityStatus.DOWN),
228
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
229
                                metadata={"s_vlan": 6},
230
                                status=EntityStatus.UP),
231
        ]
232
        attributes = {
233
            "name": "circuit_name",
234
            "uni_a": get_uni_mocked(is_valid=True),
235
            "uni_z": get_uni_mocked(is_valid=True),
236
            "primary_path": primary_path,
237
            "backup_path": backup_path,
238
            "enabled": True
239
        }
240
241
        evc = EVC(**attributes)
242
        evc.current_path = evc.backup_path
243
        current_handle_link_down = evc.handle_link_down()
244
        self.assertEqual(deploy_mocked.call_count, 0)
245
        self.assertEqual(deploy_to_mocked.call_count, 1)
246
        deploy_to_mocked.assert_called_once_with('primary_path',
247
                                                 evc.primary_path)
248
        self.assertFalse(current_handle_link_down)
249
        msg = f'Failed to re-deploy {evc} after link down.'
250
        log_mocked.debug.assert_called_once_with(msg)
251
252
    @patch('napps.kytos.mef_eline.models.log')
253
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
254
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
255
    def test_handle_link_down_case_4(self, deploy_to_mocked, deploy_mocked,
256
                                     log_mocked):
257
        """Test if circuit without dynamic path is return failed."""
258
        deploy_mocked.return_value = True
259
        deploy_to_mocked.return_value = False
260
        primary_path = [
261
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
262
                                metadata={"s_vlan": 5},
263
                                status=EntityStatus.DOWN),
264
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
265
                                metadata={"s_vlan": 6},
266
                                status=EntityStatus.UP),
267
        ]
268
        backup_path = [
269
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
270
                                metadata={"s_vlan": 5},
271
                                status=EntityStatus.DOWN),
272
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
273
                                metadata={"s_vlan": 6},
274
                                status=EntityStatus.UP),
275
        ]
276
        attributes = {
277
            "name": "circuit_name",
278
            "uni_a": get_uni_mocked(is_valid=True),
279
            "uni_z": get_uni_mocked(is_valid=True),
280
            "primary_path": primary_path,
281
            "backup_path": backup_path,
282
            "enabled": True,
283
            "dynamic_backup_path": True
284
        }
285
286
        evc = EVC(**attributes)
287
        evc.current_path = evc.backup_path
288
        current_handle_link_down = evc.handle_link_down()
289
        self.assertEqual(deploy_mocked.call_count, 1)
290
        self.assertEqual(deploy_to_mocked.call_count, 1)
291
        deploy_to_mocked.assert_called_once_with('primary_path',
292
                                                 evc.primary_path)
293
        self.assertTrue(current_handle_link_down)
294
        msg = f"{evc} deployed after link down."
295
        log_mocked.debug.assert_called_once_with(msg)
296
297 View Code Duplication
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
298
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
299
    def test_handle_link_up_case_1(self, deploy_to_mocked, deploy_mocked):
300
        """Test if handle link up do nothing when is using primary path."""
301
        deploy_mocked.return_value = True
302
        deploy_to_mocked.return_value = True
303
        primary_path = [
304
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
305
                                metadata={"s_vlan": 5},
306
                                status=EntityStatus.UP),
307
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
308
                                metadata={"s_vlan": 6},
309
                                status=EntityStatus.UP),
310
        ]
311
        backup_path = [
312
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
313
                                metadata={"s_vlan": 5},
314
                                status=EntityStatus.UP),
315
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
316
                                metadata={"s_vlan": 6},
317
                                status=EntityStatus.UP),
318
        ]
319
        attributes = {
320
            "name": "circuit_name",
321
            "uni_a": get_uni_mocked(is_valid=True),
322
            "uni_z": get_uni_mocked(is_valid=True),
323
            "primary_path": primary_path,
324
            "backup_path": backup_path,
325
            "enabled": True,
326
            "dynamic_backup_path": True
327
        }
328
329
        evc = EVC(**attributes)
330
        evc.current_path = evc.primary_path
331
        current_handle_link_up = evc.handle_link_up(backup_path[0])
332
        self.assertEqual(deploy_mocked.call_count, 0)
333
        self.assertEqual(deploy_to_mocked.call_count, 0)
334
        self.assertTrue(current_handle_link_up)
335
336 View Code Duplication
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
337
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
338
    def test_handle_link_up_case_2(self, deploy_to_mocked, deploy_mocked):
339
        """Test if it is changing from backup_path to primary_path."""
340
        deploy_mocked.return_value = True
341
        deploy_to_mocked.return_value = True
342
        primary_path = [
343
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
344
                                metadata={"s_vlan": 5},
345
                                status=EntityStatus.UP),
346
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
347
                                metadata={"s_vlan": 6},
348
                                status=EntityStatus.UP),
349
        ]
350
        backup_path = [
351
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
352
                                metadata={"s_vlan": 5},
353
                                status=EntityStatus.UP),
354
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
355
                                metadata={"s_vlan": 6},
356
                                status=EntityStatus.UP),
357
        ]
358
        attributes = {
359
            "name": "circuit_name",
360
            "uni_a": get_uni_mocked(is_valid=True),
361
            "uni_z": get_uni_mocked(is_valid=True),
362
            "primary_path": primary_path,
363
            "backup_path": backup_path,
364
            "enabled": True,
365
            "dynamic_backup_path": True
366
        }
367
368
        evc = EVC(**attributes)
369
        evc.current_path = evc.backup_path
370
        current_handle_link_up = evc.handle_link_up(primary_path[0])
371
        self.assertEqual(deploy_mocked.call_count, 0)
372
        self.assertEqual(deploy_to_mocked.call_count, 1)
373
        deploy_to_mocked.assert_called_once_with('primary_path',
374
                                                 evc.primary_path)
375
        self.assertTrue(current_handle_link_up)
376
377
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
378
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
379
    def test_handle_link_up_case_3(self, deploy_to_mocked, deploy_mocked):
380
        """Test if it is deployed after the backup is up."""
381
        deploy_mocked.return_value = True
382
        deploy_to_mocked.return_value = True
383
        primary_path = [
384
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
385
                                metadata={"s_vlan": 5},
386
                                status=EntityStatus.DOWN),
387
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
388
                                metadata={"s_vlan": 6},
389
                                status=EntityStatus.UP),
390
        ]
391
        backup_path = [
392
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
393
                                metadata={"s_vlan": 5},
394
                                status=EntityStatus.DOWN),
395
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
396
                                metadata={"s_vlan": 6},
397
                                status=EntityStatus.UP),
398
        ]
399
        attributes = {
400
            "name": "circuit_name",
401
            "uni_a": get_uni_mocked(is_valid=True),
402
            "uni_z": get_uni_mocked(is_valid=True),
403
            "primary_path": primary_path,
404
            "backup_path": backup_path,
405
            "enabled": True,
406
            "dynamic_backup_path": True
407
        }
408
409
        evc = EVC(**attributes)
410
        evc.current_path = Path([])
411
        current_handle_link_up = evc.handle_link_up(backup_path[0])
412
        self.assertEqual(deploy_mocked.call_count, 0)
413
        self.assertEqual(deploy_to_mocked.call_count, 1)
414
        deploy_to_mocked.assert_called_once_with('backup_path',
415
                                                 evc.backup_path)
416
        self.assertTrue(current_handle_link_up)
417
418
    @patch('napps.kytos.mef_eline.models.EVCDeploy.deploy')
419
    @patch('napps.kytos.mef_eline.models.LinkProtection.deploy_to')
420
    def test_handle_link_up_case_4(self, deploy_to_mocked, deploy_mocked):
421
        """Test if it is deployed after the dynamic_backup_path deploy."""
422
        deploy_mocked.return_value = True
423
        deploy_to_mocked.return_value = False
424
        primary_path = [
425
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
426
                                metadata={"s_vlan": 5},
427
                                status=EntityStatus.DOWN),
428
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
429
                                metadata={"s_vlan": 6},
430
                                status=EntityStatus.UP),
431
        ]
432
        backup_path = [
433
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
434
                                metadata={"s_vlan": 5},
435
                                status=EntityStatus.DOWN),
436
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
437
                                metadata={"s_vlan": 6},
438
                                status=EntityStatus.UP),
439
        ]
440
        attributes = {
441
            "name": "circuit_name",
442
            "uni_a": get_uni_mocked(is_valid=True),
443
            "uni_z": get_uni_mocked(is_valid=True),
444
            "primary_path": primary_path,
445
            "backup_path": backup_path,
446
            "enabled": True,
447
            "dynamic_backup_path": True
448
        }
449
450
        evc = EVC(**attributes)
451
        evc.current_path = Path([])
452
        current_handle_link_up = evc.handle_link_up(backup_path[0])
453
        self.assertEqual(deploy_mocked.call_count, 1)
454
        self.assertEqual(deploy_to_mocked.call_count, 1)
455
        deploy_to_mocked.assert_called_once_with('backup_path',
456
                                                 evc.backup_path)
457
        self.assertTrue(current_handle_link_up)
458