Passed
Push — master ( 9aeaeb...4fab7d )
by Vinicius
04:29 queued 14s
created

TestEVC.test_circuit_representation()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 1
rs 9.95
c 0
b 0
f 0
1
"""Module to test the EVCBase class."""
2 1
import sys
3 1
from unittest.mock import MagicMock, call, patch
4
5 1
import pytest
6
7 1
from kytos.core.exceptions import KytosTagError
8 1
from kytos.core.interface import TAGRange
9 1
from napps.kytos.mef_eline.models import Path
10
11
# pylint: disable=wrong-import-position
12 1
sys.path.insert(0, "/var/lib/kytos/napps/..")
13
# pylint: enable=wrong-import-position, disable=ungrouped-imports
14 1
from napps.kytos.mef_eline.exceptions import DuplicatedNoTagUNI  # NOQA  pycodestyle
15 1
from napps.kytos.mef_eline.models import EVC  # NOQA  pycodestyle
16 1
from napps.kytos.mef_eline.scheduler import \
17
    CircuitSchedule  # NOQA  pycodestyle
18 1
from napps.kytos.mef_eline.tests.helpers import (  # NOQA  pycodestyle
19
    get_controller_mock, get_uni_mocked)
20
21
22 1
class TestEVC():  # pylint: disable=too-many-public-methods, no-member
23
    """Tests to verify EVC class."""
24
25 1
    def test_attributes_empty(self):
26
        """Test if the EVC raises an error with name is required."""
27 1
        attributes = {"controller": get_controller_mock()}
28 1
        error_message = "name is required."
29 1
        with pytest.raises(ValueError) as handle_error:
30 1
            EVC(**attributes)
31 1
        assert error_message in str(handle_error)
32
33 1
    def test_expected_requiring_redeploy_attributes(self) -> None:
34
        """Test expected attributes_requiring_redeploy."""
35 1
        expected = [
36
            "primary_path",
37
            "backup_path",
38
            "dynamic_backup_path",
39
            "queue_id",
40
            "sb_priority",
41
            "primary_constraints",
42
            "secondary_constraints",
43
            "uni_a",
44
            "uni_z",
45
        ]
46 1
        assert EVC.attributes_requiring_redeploy == expected
47
48 1
    def test_expected_updatable_attributes(self) -> None:
49
        """Test expected updatable_attributes."""
50 1
        expected = [
51
            "uni_a",
52
            "uni_z",
53
            "name",
54
            "start_date",
55
            "end_date",
56
            "queue_id",
57
            "bandwidth",
58
            "primary_path",
59
            "backup_path",
60
            "dynamic_backup_path",
61
            "primary_constraints",
62
            "secondary_constraints",
63
            "owner",
64
            "sb_priority",
65
            "service_level",
66
            "circuit_scheduler",
67
            "metadata",
68
            "enabled"
69
        ]
70 1
        assert EVC.updatable_attributes == set(expected)
71
72 1
    def test_without_uni_a(self):
73
        """Test if the EVC raises and error with UNI A is required."""
74 1
        attributes = {
75
            "controller": get_controller_mock(),
76
            "name": "circuit_name",
77
        }
78 1
        error_message = "uni_a is required."
79 1
        with pytest.raises(ValueError) as handle_error:
80 1
            EVC(**attributes)
81 1
        assert error_message in str(handle_error)
82
83 1
    def test_without_uni_z(self):
84
        """Test if the EVC raises and error with UNI Z is required."""
85 1
        attributes = {
86
            "controller": get_controller_mock(),
87
            "name": "circuit_name",
88
            "uni_a": get_uni_mocked(is_valid=True),
89
        }
90 1
        error_message = "uni_z is required."
91 1
        with pytest.raises(ValueError) as handle_error:
92 1
            EVC(**attributes)
93 1
        assert error_message in str(handle_error)
94
95 1
    @pytest.mark.parametrize(
96
        "name,value",
97
        [
98
            ("archived", True),
99
            ("_id", True),
100
            ("active", True),
101
            ("current_path", []),
102
            ("creation_time", "date"),
103
        ]
104
    )
105 1
    def test_update_read_only(self, name, value):
106
        """Test if raises an error when trying to update read only attr."""
107 1
        attributes = {
108
            "controller": get_controller_mock(),
109
            "name": "circuit_name",
110
            "dynamic_backup_path": True,
111
            "uni_a": get_uni_mocked(is_valid=True),
112
            "uni_z": get_uni_mocked(is_valid=True),
113
        }
114
115 1
        update_dict = {name: value}
116 1
        error_message = f"{name} can't be updated."
117 1
        with pytest.raises(ValueError) as handle_error:
118 1
            evc = EVC(**attributes)
119 1
            evc.update(**update_dict)
120 1
        assert error_message in str(handle_error)
121
122 1
    def test_update_invalid(self):
123
        """Test updating with an invalid attr"""
124 1
        attributes = {
125
            "controller": get_controller_mock(),
126
            "name": "circuit_name",
127
            "dynamic_backup_path": True,
128
            "uni_a": get_uni_mocked(is_valid=True),
129
            "uni_z": get_uni_mocked(is_valid=True),
130
        }
131 1
        evc = EVC(**attributes)
132 1
        with pytest.raises(ValueError) as handle_error:
133 1
            evc.update(xyz="abc")
134 1
        assert (
135
            "xyz can't be updated."
136
            in str(handle_error)
137
        )
138
139 1
    @patch("napps.kytos.mef_eline.models.EVC.sync")
140 1
    def test_update_disable(self, _sync_mock):
141
        """Test if evc is disabled."""
142 1
        attributes = {
143
            "controller": get_controller_mock(),
144
            "name": "circuit_name",
145
            "dynamic_backup_path": True,
146
            "enable": True,
147
            "uni_a": get_uni_mocked(is_valid=True),
148
            "uni_z": get_uni_mocked(is_valid=True),
149
        }
150 1
        update_dict = {"enabled": False}
151 1
        evc = EVC(**attributes)
152 1
        evc.update(**update_dict)
153 1
        assert evc.is_enabled() is False
154
155 1
    @patch("napps.kytos.mef_eline.models.EVC.sync")
156 1
    def test_update_empty_primary_path(self, _sync_mock):
157
        """Test if an empty primary path can be set."""
158 1
        initial_primary_path = Path([MagicMock(id=1), MagicMock(id=2)])
159 1
        attributes = {
160
            "controller": get_controller_mock(),
161
            "name": "circuit_name",
162
            "dynamic_backup_path": True,
163
            "primary_path": initial_primary_path,
164
            "enable": True,
165
            "uni_a": get_uni_mocked(is_valid=True),
166
            "uni_z": get_uni_mocked(is_valid=True),
167
        }
168 1
        update_dict = {"primary_path": Path([])}
169 1
        evc = EVC(**attributes)
170 1
        assert evc.primary_path == initial_primary_path
171 1
        evc.update(**update_dict)
172 1
        assert len(evc.primary_path) == 0
173
174 1
    @patch("napps.kytos.mef_eline.models.EVC.sync")
175 1
    def test_update_empty_path_non_dynamic_backup(self, _sync_mock):
176
        """Test if an empty primary path can't be set if dynamic."""
177 1
        initial_primary_path = Path([MagicMock(id=1), MagicMock(id=2)])
178 1
        attributes = {
179
            "controller": get_controller_mock(),
180
            "name": "circuit_name",
181
            "dynamic_backup_path": False,
182
            "primary_path": initial_primary_path,
183
            "enable": True,
184
            "uni_a": get_uni_mocked(is_valid=True),
185
            "uni_z": get_uni_mocked(is_valid=True),
186
        }
187 1
        update_dict = {"primary_path": Path([])}
188 1
        evc = EVC(**attributes)
189 1
        assert evc.primary_path == initial_primary_path
190 1
        with pytest.raises(ValueError) as handle_error:
191 1
            evc.update(**update_dict)
192 1
        assert (
193
            'The EVC must have a primary path or allow dynamic paths.'
194
            in str(handle_error)
195
        )
196
197 1
    @patch("napps.kytos.mef_eline.models.EVC.sync")
198 1
    def test_update_empty_backup_path(self, _sync_mock):
199
        """Test if an empty backup path can be set."""
200 1
        initial_backup_path = Path([MagicMock(id=1), MagicMock(id=2)])
201 1
        attributes = {
202
            "controller": get_controller_mock(),
203
            "name": "circuit_name",
204
            "dynamic_backup_path": True,
205
            "backup_path": initial_backup_path,
206
            "enable": True,
207
            "uni_a": get_uni_mocked(is_valid=True),
208
            "uni_z": get_uni_mocked(is_valid=True),
209
        }
210 1
        update_dict = {"backup_path": Path([])}
211 1
        evc = EVC(**attributes)
212 1
        assert evc.backup_path == initial_backup_path
213 1
        evc.update(**update_dict)
214 1
        assert len(evc.backup_path) == 0
215
216 1
    @patch("napps.kytos.mef_eline.models.EVC.sync")
217 1
    def test_update_empty_backup_path_non_dynamic(self, _sync_mock):
218
        """Test if an empty backup path can be set even if it's non dynamic."""
219 1
        initial_backup_path = Path([MagicMock(id=1), MagicMock(id=2)])
220 1
        primary_path = Path([MagicMock(id=3), MagicMock(id=4)])
221 1
        attributes = {
222
            "controller": get_controller_mock(),
223
            "name": "circuit_name",
224
            "dynamic_backup_path": False,
225
            "primary_path": primary_path,
226
            "backup_path": initial_backup_path,
227
            "enable": True,
228
            "uni_a": get_uni_mocked(is_valid=True),
229
            "uni_z": get_uni_mocked(is_valid=True),
230
        }
231 1
        update_dict = {"backup_path": Path([])}
232 1
        evc = EVC(**attributes)
233 1
        assert evc.primary_path == primary_path
234 1
        assert evc.backup_path == initial_backup_path
235 1
        evc.update(**update_dict)
236 1
        assert evc.primary_path == primary_path
237 1
        assert len(evc.backup_path) == 0
238
239 1 View Code Duplication
    @patch("napps.kytos.mef_eline.models.EVC.sync")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
240 1
    def test_update_queue(self, _sync_mock):
241
        """Test if evc is set to redeploy."""
242 1
        attributes = {
243
            "controller": get_controller_mock(),
244
            "name": "circuit_name",
245
            "enable": True,
246
            "dynamic_backup_path": True,
247
            "uni_a": get_uni_mocked(is_valid=True),
248
            "uni_z": get_uni_mocked(is_valid=True),
249
        }
250 1
        update_dict = {"queue_id": 3}
251 1
        evc = EVC(**attributes)
252 1
        _, redeploy = evc.update(**update_dict)
253 1
        assert redeploy
254
255 1 View Code Duplication
    @patch("napps.kytos.mef_eline.models.EVC.sync")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
256 1
    def test_update_queue_null(self, _sync_mock):
257
        """Test if evc is set to redeploy."""
258 1
        attributes = {
259
            "controller": get_controller_mock(),
260
            "name": "circuit_name",
261
            "enable": True,
262
            "dynamic_backup_path": True,
263
            "uni_a": get_uni_mocked(is_valid=True),
264
            "uni_z": get_uni_mocked(is_valid=True),
265
        }
266 1
        update_dict = {"queue_id": None}
267 1
        evc = EVC(**attributes)
268 1
        _, redeploy = evc.update(**update_dict)
269 1
        assert redeploy
270
271 1
    def test_update_different_tag_lists(self):
272
        """Test update when tag lists are different."""
273 1
        attributes = {
274
            "controller": get_controller_mock(),
275
            "name": "circuit_name",
276
            "enable": True,
277
            "dynamic_backup_path": True,
278
            "uni_a": get_uni_mocked(is_valid=True),
279
            "uni_z": get_uni_mocked(is_valid=True),
280
        }
281 1
        uni = MagicMock(user_tag=TAGRange("vlan", [[1, 10]]))
282 1
        update_dict = {"uni_a": uni}
283 1
        evc = EVC(**attributes)
284 1
        with pytest.raises(ValueError):
285 1
            evc.update(**update_dict)
286
287 1
    def test_circuit_representation(self):
288
        """Test the method __repr__."""
289 1
        attributes = {
290
            "controller": get_controller_mock(),
291
            "name": "circuit_name",
292
            "uni_a": get_uni_mocked(is_valid=True),
293
            "uni_z": get_uni_mocked(is_valid=True),
294
        }
295 1
        evc = EVC(**attributes)
296 1
        expected_value = f"EVC({evc.id}, {evc.name})"
297 1
        assert str(evc) == expected_value
298
299 1
    def test_comparison_method(self):
300
        """Test the method __eq__."""
301 1
        attributes = {
302
            "controller": get_controller_mock(),
303
            "name": "circuit_name",
304
            "uni_a": get_uni_mocked(is_valid=True),
305
            "uni_z": get_uni_mocked(is_valid=True),
306
        }
307 1
        evc1 = EVC(**attributes)
308 1
        evc2 = EVC(**attributes)
309
310 1
        attributes = {
311
            "controller": get_controller_mock(),
312
            "name": "circuit_name_2",
313
            "uni_a": get_uni_mocked(is_valid=True),
314
            "uni_z": get_uni_mocked(is_valid=True),
315
        }
316 1
        evc3 = EVC(**attributes)
317 1
        evc4 = EVC(**attributes)
318
319 1
        assert evc1 == evc2
320 1
        assert evc1 != evc3
321 1
        assert evc2 != evc3
322 1
        assert evc3 == evc4
323
324 1
    def test_as_dict(self):
325
        """Test the method as_dict."""
326 1
        attributes = {
327
            "controller": get_controller_mock(),
328
            "id": "custom_id",
329
            "name": "custom_name",
330
            "uni_a": get_uni_mocked(is_valid=True),
331
            "uni_z": get_uni_mocked(is_valid=True),
332
            "start_date": "2018-08-21T18:44:54",
333
            "end_date": "2018-08-21T18:44:55",
334
            "primary_links": [],
335
            "request_time": "2018-08-21T19:10:41",
336
            "creation_time": "2018-08-21T18:44:54",
337
            "owner": "my_name",
338
            "circuit_scheduler": [
339
                CircuitSchedule.from_dict(
340
                    {
341
                        "id": 234243247,
342
                        "action": "create",
343
                        "frequency": "1 * * * *",
344
                    }
345
                ),
346
                CircuitSchedule.from_dict(
347
                    {
348
                        "id": 234243239,
349
                        "action": "create",
350
                        "interval": {"hours": 2},
351
                    }
352
                ),
353
            ],
354
            "enabled": True,
355
            "sb_priority": 2,
356
            "service_level": 7,
357
        }
358 1
        evc = EVC(**attributes)
359
360 1
        expected_dict = {
361
            "id": "custom_id",
362
            "name": "custom_name",
363
            "uni_a": attributes["uni_a"].as_dict(),
364
            "uni_z": attributes["uni_z"].as_dict(),
365
            "start_date": "2018-08-21T18:44:54",
366
            "end_date": "2018-08-21T18:44:55",
367
            "bandwidth": 0,
368
            "primary_links": [],
369
            "backup_links": [],
370
            "current_path": [],
371
            "primary_path": [],
372
            "backup_path": [],
373
            "dynamic_backup_path": False,
374
            "request_time": "2018-08-21T19:10:41",
375
            "creation_time": "2018-08-21T18:44:54",
376
            "circuit_scheduler": [
377
                {
378
                    "id": 234243247,
379
                    "action": "create",
380
                    "frequency": "1 * * * *",
381
                },
382
                {
383
                    "id": 234243239,
384
                    "action": "create",
385
                    "interval": {"hours": 2},
386
                },
387
            ],
388
            "active": False,
389
            "enabled": True,
390
            "sb_priority": 2,
391
            "service_level": 7,
392
        }
393 1
        actual_dict = evc.as_dict()
394 1
        for name, value in expected_dict.items():
395 1
            actual = actual_dict.get(name)
396 1
            assert value == actual
397
398
        # Selected fields
399 1
        expected_dict = {
400
            "enabled": True,
401
            "uni_z": attributes["uni_z"].as_dict(),
402
            "circuit_scheduler": [
403
                {
404
                    "id": 234243247,
405
                    "action": "create",
406
                    "frequency": "1 * * * *",
407
                },
408
                {
409
                    "id": 234243239,
410
                    "action": "create",
411
                    "interval": {"hours": 2},
412
                },
413
            ],
414
            "sb_priority": 2,
415
        }
416 1
        selected_fields = {
417
            "enabled", "uni_z", "circuit_scheduler", "sb_priority"
418
        }
419 1
        actual_dict = evc.as_dict(selected_fields)
420 1
        for name, value in expected_dict.items():
421 1
            actual = actual_dict.get(name)
422 1
            assert value == actual
423
424 1
    @staticmethod
425 1
    def test_get_id_from_cookie():
426
        """Test get_id_from_cookie."""
427 1
        attributes = {
428
            "controller": get_controller_mock(),
429
            "name": "circuit_name",
430
            "enable": True,
431
            "uni_a": get_uni_mocked(is_valid=True),
432
            "uni_z": get_uni_mocked(is_valid=True)
433
        }
434 1
        evc = EVC(**attributes)
435 1
        evc_id = evc.id
436 1
        assert evc_id
437 1
        assert evc.get_id_from_cookie(evc.get_cookie()) == evc_id
438
439 1
    @staticmethod
440 1
    def test_get_id_from_cookie_with_leading_zeros():
441
        """Test get_id_from_cookie with leading zeros."""
442
443 1
        attributes = {
444
            "controller": get_controller_mock(),
445
            "name": "circuit_name",
446
            "enable": True,
447
            "uni_a": get_uni_mocked(is_valid=True),
448
            "uni_z": get_uni_mocked(is_valid=True)
449
        }
450 1
        evc = EVC(**attributes)
451 1
        evc_id = "0a2d672d99ff41"
452
        # pylint: disable=protected-access
453 1
        evc._id = evc_id
454
        # pylint: enable=protected-access
455 1
        assert EVC.get_id_from_cookie(evc.get_cookie()) == evc_id
456
457 1
    def test_is_intra_switch(self):
458
        """Test is_intra_switch method."""
459 1
        attributes = {
460
            "controller": get_controller_mock(),
461
            "name": "circuit_name",
462
            "enable": True,
463
            "uni_a": get_uni_mocked(is_valid=True),
464
            "uni_z": get_uni_mocked(is_valid=True)
465
        }
466 1
        evc = EVC(**attributes)
467 1
        assert not evc.is_intra_switch()
468
469 1
        evc.uni_a.interface.switch = evc.uni_z.interface.switch
470 1
        assert evc.is_intra_switch()
471
472 1
    def test_default_queue_id(self):
473
        """Test default queue_id"""
474
475 1
        attributes = {
476
            "controller": get_controller_mock(),
477
            "name": "circuit_1",
478
            "uni_a": get_uni_mocked(is_valid=True),
479
            "uni_z": get_uni_mocked(is_valid=True),
480
            "dynamic_backup_path": True,
481
        }
482
483 1
        evc = EVC(**attributes)
484 1
        assert evc.queue_id == -1
485
486 1
    def test_get_unis_use_tags(self):
487
        """Test _get_unis_use_tags"""
488 1
        old_uni_a = get_uni_mocked(
489
            interface_port=2,
490
            is_valid=True
491
        )
492 1
        old_uni_z = get_uni_mocked(
493
            interface_port=3,
494
            is_valid=True
495
        )
496 1
        attributes = {
497
            "controller": get_controller_mock(),
498
            "name": "circuit_name",
499
            "enable": True,
500
            "uni_a": old_uni_a,
501
            "uni_z": old_uni_z
502
        }
503 1
        evc = EVC(**attributes)
504 1
        evc._use_uni_vlan = MagicMock()
505 1
        evc.make_uni_vlan_available = MagicMock()
506 1
        new_uni_a = get_uni_mocked(tag_value=200, is_valid=True)
507 1
        new_uni_z = get_uni_mocked(tag_value=200, is_valid=True)
508 1
        unis = {"uni_a": new_uni_a}
509 1
        evc._get_unis_use_tags(**unis)
510 1
        assert evc._use_uni_vlan.call_count == 1
511 1
        assert evc._use_uni_vlan.call_args[0][0] == new_uni_a
512 1
        assert evc.make_uni_vlan_available.call_count == 1
513 1
        assert evc.make_uni_vlan_available.call_args[0][0] == old_uni_a
514
515
        # Two UNIs
516 1
        evc = EVC(**attributes)
517 1
        evc._use_uni_vlan = MagicMock()
518 1
        evc.make_uni_vlan_available = MagicMock()
519 1
        unis = {"uni_a": new_uni_a, "uni_z": new_uni_z}
520 1
        evc._get_unis_use_tags(**unis)
521
522 1
        expected = [
523
            call(new_uni_a, uni_dif=old_uni_a),
524
            call(new_uni_z, uni_dif=old_uni_z)
525
        ]
526 1
        evc._use_uni_vlan.assert_has_calls(expected)
527 1
        expected = [
528
            call(old_uni_z, uni_dif=new_uni_z),
529
            call(old_uni_a, uni_dif=new_uni_a)
530
        ]
531 1
        evc.make_uni_vlan_available.assert_has_calls(expected)
532
533 1
    def test_get_unis_use_tags_error(self):
534
        """Test _get_unis_use_tags with KytosTagError"""
535 1
        old_uni_a = get_uni_mocked(
536
            interface_port=2,
537
            is_valid=True
538
        )
539 1
        old_uni_z = get_uni_mocked(
540
            interface_port=3,
541
            is_valid=True
542
        )
543 1
        attributes = {
544
            "controller": get_controller_mock(),
545
            "name": "circuit_name",
546
            "enable": True,
547
            "uni_a": old_uni_a,
548
            "uni_z": old_uni_z
549
        }
550 1
        evc = EVC(**attributes)
551 1
        evc._use_uni_vlan = MagicMock()
552
553
        # UNI Z KytosTagError
554 1
        evc._use_uni_vlan.side_effect = [None, KytosTagError("")]
555 1
        evc.make_uni_vlan_available = MagicMock()
556 1
        new_uni_a = get_uni_mocked(tag_value=200, is_valid=True)
557 1
        new_uni_z = get_uni_mocked(tag_value=200, is_valid=True)
558 1
        unis = {"uni_a": new_uni_a, "uni_z": new_uni_z}
559 1
        with pytest.raises(KytosTagError):
560 1
            evc._get_unis_use_tags(**unis)
561 1
        expected = [
562
            call(new_uni_a, uni_dif=old_uni_a),
563
            call(new_uni_z, uni_dif=old_uni_z)
564
        ]
565 1
        evc._use_uni_vlan.assert_has_calls(expected)
566 1
        assert evc.make_uni_vlan_available.call_count == 1
567 1
        assert evc.make_uni_vlan_available.call_args[0][0] == new_uni_a
568
569
        # UNI A KytosTagError
570 1
        evc = EVC(**attributes)
571 1
        evc._use_uni_vlan = MagicMock()
572 1
        evc._use_uni_vlan.side_effect = [KytosTagError(""), None]
573 1
        evc.make_uni_vlan_available = MagicMock()
574 1
        new_uni_a = get_uni_mocked(tag_value=200, is_valid=True)
575 1
        new_uni_z = get_uni_mocked(tag_value=200, is_valid=True)
576 1
        unis = {"uni_a": new_uni_a, "uni_z": new_uni_z}
577 1
        with pytest.raises(KytosTagError):
578 1
            evc._get_unis_use_tags(**unis)
579 1
        assert evc._use_uni_vlan.call_count == 1
580 1
        assert evc._use_uni_vlan.call_args[0][0] == new_uni_a
581 1
        assert evc.make_uni_vlan_available.call_count == 0
582
583 1
    @patch("napps.kytos.mef_eline.models.evc.range_difference")
584 1
    def test_use_uni_vlan(self, mock_difference):
585
        """Test _use_uni_vlan"""
586 1
        attributes = {
587
            "controller": get_controller_mock(),
588
            "name": "circuit_name",
589
            "enable": True,
590
            "uni_a": get_uni_mocked(is_valid=True),
591
            "uni_z": get_uni_mocked(is_valid=True)
592
        }
593 1
        evc = EVC(**attributes)
594 1
        uni = get_uni_mocked(is_valid=True)
595 1
        uni.interface.use_tags = MagicMock()
596 1
        evc._use_uni_vlan(uni)
597 1
        args = uni.interface.use_tags.call_args[0]
598 1
        assert args[1] == uni.user_tag.value
599 1
        assert args[2] == uni.user_tag.tag_type
600 1
        assert uni.interface.use_tags.call_count == 1
601
602 1
        uni.user_tag.value = "any"
603 1
        evc._use_uni_vlan(uni)
604 1
        assert uni.interface.use_tags.call_count == 2
605
606 1
        uni.user_tag.value = [[1, 10]]
607 1
        uni_dif = get_uni_mocked(tag_value=[[1, 2]])
608 1
        mock_difference.return_value = [[3, 10]]
609 1
        evc._use_uni_vlan(uni, uni_dif)
610 1
        assert uni.interface.use_tags.call_count == 3
611
612 1
        mock_difference.return_value = []
613 1
        evc._use_uni_vlan(uni, uni_dif)
614 1
        assert uni.interface.use_tags.call_count == 3
615
616 1
        uni.interface.use_tags.side_effect = KytosTagError("")
617 1
        with pytest.raises(KytosTagError):
618 1
            evc._use_uni_vlan(uni)
619 1
        assert uni.interface.use_tags.call_count == 4
620
621 1
        uni.user_tag = None
622 1
        evc._use_uni_vlan(uni)
623 1
        assert uni.interface.use_tags.call_count == 4
624
625 1
    @patch("napps.kytos.mef_eline.models.evc.log")
626 1
    def test_make_uni_vlan_available(self, mock_log):
627
        """Test make_uni_vlan_available"""
628 1
        attributes = {
629
            "controller": get_controller_mock(),
630
            "name": "circuit_name",
631
            "enable": True,
632
            "uni_a": get_uni_mocked(is_valid=True),
633
            "uni_z": get_uni_mocked(is_valid=True)
634
        }
635 1
        evc = EVC(**attributes)
636 1
        uni = get_uni_mocked(is_valid=True)
637 1
        uni.interface.make_tags_available = MagicMock()
638
639 1
        evc.make_uni_vlan_available(uni)
640 1
        args = uni.interface.make_tags_available.call_args[0]
641 1
        assert args[1] == uni.user_tag.value
642 1
        assert args[2] == uni.user_tag.tag_type
643 1
        assert uni.interface.make_tags_available.call_count == 1
644
645 1
        uni.user_tag.value = [[1, 10]]
646 1
        uni_dif = get_uni_mocked(tag_value=[[1, 2]])
647 1
        evc.make_uni_vlan_available(uni, uni_dif)
648 1
        assert uni.interface.make_tags_available.call_count == 2
649
650 1
        uni.interface.make_tags_available.side_effect = KytosTagError("")
651 1
        evc.make_uni_vlan_available(uni)
652 1
        assert mock_log.error.call_count == 1
653
654 1
        uni.user_tag = None
655 1
        evc.make_uni_vlan_available(uni)
656 1
        assert uni.interface.make_tags_available.call_count == 3
657
658 1
    def test_remove_uni_tags(self):
659
        """Test remove_uni_tags"""
660 1
        attributes = {
661
            "controller": get_controller_mock(),
662
            "name": "circuit_name",
663
            "enable": True,
664
            "uni_a": get_uni_mocked(is_valid=True),
665
            "uni_z": get_uni_mocked(is_valid=True)
666
        }
667 1
        evc = EVC(**attributes)
668 1
        evc.make_uni_vlan_available = MagicMock()
669 1
        evc.remove_uni_tags()
670 1
        assert evc.make_uni_vlan_available.call_count == 2
671
672 1
    def test_tag_lists_equal(self):
673
        """Test _tag_lists_equal"""
674 1
        attributes = {
675
            "controller": get_controller_mock(),
676
            "name": "circuit_name",
677
            "enable": True,
678
            "uni_a": get_uni_mocked(is_valid=True),
679
            "uni_z": get_uni_mocked(is_valid=True)
680
        }
681 1
        evc = EVC(**attributes)
682 1
        uni = MagicMock(user_tag=TAGRange("vlan", [[1, 10]]))
683 1
        update_dict = {"uni_z": uni}
684 1
        assert evc._tag_lists_equal(**update_dict) is False
685
686 1
        update_dict = {"uni_a": uni, "uni_z": uni}
687 1
        assert evc._tag_lists_equal(**update_dict)
688
689 1
    def test_check_no_tag_duplicate(self):
690
        """Test check_no_tag_duplicate"""
691 1
        uni_01_1 = get_uni_mocked(interface_port=1, switch_dpid="01")
692 1
        uni_01_1.user_tag = None
693 1
        uni_a = uni_01_1
694 1
        uni_z = get_uni_mocked(is_valid=True)
695 1
        attributes = {
696
            "controller": get_controller_mock(),
697
            "name": "circuit_name",
698
            "enable": True,
699
            "uni_a": uni_a,
700
            "uni_z": uni_z
701
        }
702 1
        evc = EVC(**attributes)
703 1
        other_uni = uni_01_1
704 1
        with pytest.raises(DuplicatedNoTagUNI):
705 1
            evc.check_no_tag_duplicate(other_uni)
706
707 1
        other_uni = get_uni_mocked(interface_port=1, switch_dpid="02")
708
        assert evc.check_no_tag_duplicate(other_uni) is None
709