Passed
Pull Request — master (#491)
by Aldo
05:09
created

TestMain.test_cleanup_evcs_old_path()   B

Complexity

Conditions 1

Size

Total Lines 44
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 37
nop 2
dl 0
loc 44
ccs 25
cts 25
cp 1
crap 1
rs 8.9919
c 0
b 0
f 0
1
"""Module to test the main napp file."""
2 1
import asyncio
3 1
from unittest.mock import (AsyncMock, MagicMock, Mock, call,
4
                           create_autospec, patch)
5
6 1
import pytest
7 1
from kytos.lib.helpers import get_controller_mock, get_test_client
8 1
from kytos.core.helpers import now
9 1
from kytos.core.common import EntityStatus
10 1
from kytos.core.events import KytosEvent
11 1
from kytos.core.exceptions import KytosTagError
12 1
from kytos.core.interface import TAGRange, UNI, Interface
13 1
from napps.kytos.mef_eline.exceptions import InvalidPath
14 1
from napps.kytos.mef_eline.models import EVC
15 1
from napps.kytos.mef_eline.tests.helpers import get_uni_mocked
16
17
18 1
async def test_on_table_enabled():
19
    """Test on_table_enabled"""
20
    # pylint: disable=import-outside-toplevel
21 1
    from napps.kytos.mef_eline.main import Main
22 1
    controller = get_controller_mock()
23 1
    controller.buffers.app.aput = AsyncMock()
24 1
    Main.get_eline_controller = MagicMock()
25 1
    napp = Main(controller)
26
27
    # Succesfully setting table groups
28 1
    content = {"mef_eline": {"epl": 2}}
29 1
    event = KytosEvent(name="kytos/of_multi_table.enable_table",
30
                       content=content)
31 1
    await napp.on_table_enabled(event)
32 1
    assert napp.table_group["epl"] == 2
33 1
    assert napp.table_group["evpl"] == 0
34 1
    assert controller.buffers.app.aput.call_count == 1
35
36
    # Failure at setting table groups
37 1
    content = {"mef_eline": {"unknown": 123}}
38 1
    event = KytosEvent(name="kytos/of_multi_table.enable_table",
39
                       content=content)
40 1
    await napp.on_table_enabled(event)
41 1
    assert controller.buffers.app.aput.call_count == 1
42
43
    # Failure with early return
44 1
    content = {}
45 1
    event = KytosEvent(name="kytos/of_multi_table.enable_table",
46
                       content=content)
47 1
    await napp.on_table_enabled(event)
48 1
    assert controller.buffers.app.aput.call_count == 1
49
50
51
# pylint: disable=too-many-public-methods, too-many-lines
52
# pylint: disable=too-many-arguments,too-many-locals
53 1
class TestMain:
54
    """Test the Main class."""
55
56 1
    def setup_method(self):
57
        """Execute steps before each tests.
58
59
        Set the server_name_url_url from kytos/mef_eline
60
        """
61
62
        # The decorator run_on_thread is patched, so methods that listen
63
        # for events do not run on threads while tested.
64
        # Decorators have to be patched before the methods that are
65
        # decorated with them are imported.
66 1
        patch("kytos.core.helpers.run_on_thread", lambda x: x).start()
67
        # pylint: disable=import-outside-toplevel
68 1
        from napps.kytos.mef_eline.main import Main
69 1
        Main.get_eline_controller = MagicMock()
70 1
        controller = get_controller_mock()
71 1
        self.napp = Main(controller)
72 1
        self.api_client = get_test_client(controller, self.napp)
73 1
        self.base_endpoint = "kytos/mef_eline"
74
75 1
    def test_get_event_listeners(self):
76
        """Verify all event listeners registered."""
77 1
        expected_events = [
78
            "kytos/core.shutdown",
79
            "kytos/core.shutdown.kytos/mef_eline",
80
            "kytos/topology.link_up",
81
            "kytos/topology.link_down",
82
        ]
83 1
        actual_events = self.napp.listeners()
84
85 1
        for _event in expected_events:
86 1
            assert _event in actual_events, _event
87
88 1
    @patch('napps.kytos.mef_eline.main.log')
89 1
    @patch('napps.kytos.mef_eline.main.Main.execute_consistency')
90 1
    def test_execute(self, mock_execute_consistency, mock_log):
91
        """Test execute."""
92 1
        self.napp.execution_rounds = 0
93 1
        self.napp.execute()
94 1
        mock_execute_consistency.assert_called()
95 1
        assert mock_log.debug.call_count == 2
96
97
        # Test locked should return
98 1
        mock_execute_consistency.call_count = 0
99 1
        mock_log.info.call_count = 0
100
        # pylint: disable=protected-access
101 1
        self.napp._lock = MagicMock()
102 1
        self.napp._lock.locked.return_value = True
103
        # pylint: enable=protected-access
104 1
        self.napp.execute()
105 1
        mock_execute_consistency.assert_not_called()
106 1
        mock_log.info.assert_not_called()
107
108 1
    @patch('napps.kytos.mef_eline.main.settings')
109 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
110 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.check_list_traces")
111 1
    def test_execute_consistency(self, mock_check_list_traces, *args):
112
        """Test execute_consistency."""
113 1
        (mongo_controller_upsert_mock, mock_settings) = args
114
115 1
        stored_circuits = {'1': {'name': 'circuit_1'},
116
                           '2': {'name': 'circuit_2'},
117
                           '3': {'name': 'circuit_3'}}
118 1
        mongo_controller_upsert_mock.return_value = True
119 1
        self.napp.mongo_controller.get_circuits.return_value = {
120
            "circuits": stored_circuits
121
        }
122
123 1
        mock_settings.WAIT_FOR_OLD_PATH = -1
124 1
        evc1 = MagicMock(id=1, service_level=0, creation_time=1)
125 1
        evc1.is_enabled.return_value = True
126 1
        evc1.is_active.return_value = False
127 1
        evc1.lock.locked.return_value = False
128 1
        evc1.has_recent_removed_flow.return_value = False
129 1
        evc1.is_recent_updated.return_value = False
130 1
        evc1.execution_rounds = 0
131 1
        evc2 = MagicMock(id=2, service_level=7, creation_time=1)
132 1
        evc2.is_enabled.return_value = True
133 1
        evc2.is_active.return_value = False
134 1
        evc2.lock.locked.return_value = False
135 1
        evc2.has_recent_removed_flow.return_value = False
136 1
        evc2.is_recent_updated.return_value = False
137 1
        evc2.execution_rounds = 0
138 1
        self.napp.circuits = {'1': evc1, '2': evc2}
139 1
        assert self.napp.get_evcs_by_svc_level() == [evc2, evc1]
140
141 1
        mock_check_list_traces.return_value = {
142
                                                1: True,
143
                                                2: False
144
                                            }
145
146 1
        self.napp.execute_consistency()
147 1
        assert evc1.activate.call_count == 1
148 1
        assert evc1.sync.call_count == 1
149 1
        assert evc2.deploy.call_count == 1
150
151 1
    @patch('napps.kytos.mef_eline.main.settings')
152 1
    @patch('napps.kytos.mef_eline.main.Main._load_evc')
153 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
154 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.check_list_traces")
155 1
    def test_execute_consistency_wait_for(self, mock_check_list_traces, *args):
156
        """Test execute and wait for setting."""
157 1
        (mongo_controller_upsert_mock, _, mock_settings) = args
158
159 1
        stored_circuits = {'1': {'name': 'circuit_1'}}
160 1
        mongo_controller_upsert_mock.return_value = True
161 1
        self.napp.mongo_controller.get_circuits.return_value = {
162
            "circuits": stored_circuits
163
        }
164
165 1
        mock_settings.WAIT_FOR_OLD_PATH = -1
166 1
        evc1 = MagicMock(id=1, service_level=0, creation_time=1)
167 1
        evc1.is_enabled.return_value = True
168 1
        evc1.is_active.return_value = False
169 1
        evc1.lock.locked.return_value = False
170 1
        evc1.has_recent_removed_flow.return_value = False
171 1
        evc1.is_recent_updated.return_value = False
172 1
        evc1.execution_rounds = 0
173 1
        evc1.deploy.call_count = 0
174 1
        self.napp.circuits = {'1': evc1}
175 1
        assert self.napp.get_evcs_by_svc_level() == [evc1]
176 1
        mock_settings.WAIT_FOR_OLD_PATH = 1
177
178 1
        mock_check_list_traces.return_value = {1: False}
179
180 1
        self.napp.execute_consistency()
181 1
        assert evc1.deploy.call_count == 0
182 1
        self.napp.execute_consistency()
183 1
        assert evc1.deploy.call_count == 1
184
185 1
    @patch('napps.kytos.mef_eline.main.Main._uni_from_dict')
186 1
    @patch('napps.kytos.mef_eline.models.evc.EVCBase._validate')
187 1
    def test_evc_from_dict(self, _validate_mock, uni_from_dict_mock):
188
        """
189
        Test the helper method that create an EVN from dict.
190
191
        Verify object creation with circuit data and schedule data.
192
        """
193 1
        _validate_mock.return_value = True
194 1
        uni_from_dict_mock.side_effect = ["uni_a", "uni_z"]
195 1
        payload = {
196
            "name": "my evc1",
197
            "uni_a": {
198
                "interface_id": "00:00:00:00:00:00:00:01:1",
199
                "tag": {"tag_type": 'vlan', "value": 80},
200
            },
201
            "uni_z": {
202
                "interface_id": "00:00:00:00:00:00:00:02:2",
203
                "tag": {"tag_type": 'vlan', "value": 1},
204
            },
205
            "circuit_scheduler": [
206
                {"frequency": "* * * * *", "action": "create"}
207
            ],
208
            "queue_id": 5,
209
        }
210
        # pylint: disable=protected-access
211 1
        evc_response = self.napp._evc_from_dict(payload)
212 1
        assert evc_response is not None
213 1
        assert evc_response.uni_a is not None
214 1
        assert evc_response.uni_z is not None
215 1
        assert evc_response.circuit_scheduler is not None
216 1
        assert evc_response.name is not None
217 1
        assert evc_response.queue_id is not None
218
219 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
220 1
    @patch("napps.kytos.mef_eline.models.evc.EVCBase._validate")
221 1
    @patch("kytos.core.Controller.get_interface_by_id")
222 1
    def test_evc_from_dict_paths(
223
        self, _get_interface_by_id_mock, _validate_mock, uni_from_dict_mock
224
    ):
225
        """
226
        Test the helper method that create an EVN from dict.
227
228
        Verify object creation with circuit data and schedule data.
229
        """
230
231 1
        _get_interface_by_id_mock.return_value = get_uni_mocked().interface
232 1
        _validate_mock.return_value = True
233 1
        uni_from_dict_mock.side_effect = ["uni_a", "uni_z"]
234 1
        payload = {
235
            "name": "my evc1",
236
            "uni_a": {
237
                "interface_id": "00:00:00:00:00:00:00:01:1",
238
                "tag": {"tag_type": 'vlan', "value": 80},
239
            },
240
            "uni_z": {
241
                "interface_id": "00:00:00:00:00:00:00:02:2",
242
                "tag": {"tag_type": 'vlan', "value": 1},
243
            },
244
            "current_path": [],
245
            "primary_path": [
246
                {
247
                    "endpoint_a": {
248
                        "interface_id": "00:00:00:00:00:00:00:01:1"
249
                    },
250
                    "endpoint_b": {
251
                        "interface_id": "00:00:00:00:00:00:00:02:2"
252
                    },
253
                }
254
            ],
255
            "backup_path": [],
256
        }
257
258
        # pylint: disable=protected-access
259 1
        evc_response = self.napp._evc_from_dict(payload)
260 1
        assert evc_response is not None
261 1
        assert evc_response.uni_a is not None
262 1
        assert evc_response.uni_z is not None
263 1
        assert evc_response.circuit_scheduler is not None
264 1
        assert evc_response.name is not None
265 1
        assert len(evc_response.current_path) == 0
266 1
        assert len(evc_response.backup_path) == 0
267 1
        assert len(evc_response.primary_path) == 1
268
269 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
270 1
    @patch("napps.kytos.mef_eline.models.evc.EVCBase._validate")
271 1
    @patch("kytos.core.Controller.get_interface_by_id")
272 1
    def test_evc_from_dict_links(
273
        self, _get_interface_by_id_mock, _validate_mock, uni_from_dict_mock
274
    ):
275
        """
276
        Test the helper method that create an EVN from dict.
277
278
        Verify object creation with circuit data and schedule data.
279
        """
280 1
        _get_interface_by_id_mock.return_value = get_uni_mocked().interface
281 1
        _validate_mock.return_value = True
282 1
        uni_from_dict_mock.side_effect = ["uni_a", "uni_z"]
283 1
        payload = {
284
            "name": "my evc1",
285
            "uni_a": {
286
                "interface_id": "00:00:00:00:00:00:00:01:1",
287
                "tag": {"tag_type": 'vlan', "value": 80},
288
            },
289
            "uni_z": {
290
                "interface_id": "00:00:00:00:00:00:00:02:2",
291
                "tag": {"tag_type": 'vlan', "value": 1},
292
            },
293
            "primary_links": [
294
                {
295
                    "endpoint_a": {
296
                        "interface_id": "00:00:00:00:00:00:00:01:1"
297
                    },
298
                    "endpoint_b": {
299
                        "interface_id": "00:00:00:00:00:00:00:02:2"
300
                    },
301
                    "metadata": {
302
                        "s_vlan": {
303
                            "tag_type": 'vlan',
304
                            "value": 100
305
                        }
306
                    },
307
                }
308
            ],
309
            "backup_links": [],
310
        }
311
312
        # pylint: disable=protected-access
313 1
        evc_response = self.napp._evc_from_dict(payload)
314 1
        assert evc_response is not None
315 1
        assert evc_response.uni_a is not None
316 1
        assert evc_response.uni_z is not None
317 1
        assert evc_response.circuit_scheduler is not None
318 1
        assert evc_response.name is not None
319 1
        assert len(evc_response.current_links_cache) == 0
320 1
        assert len(evc_response.backup_links) == 0
321 1
        assert len(evc_response.primary_links) == 1
322
323 1
    async def test_list_without_circuits(self):
324
        """Test if list circuits return 'no circuit stored.'."""
325 1
        circuits = {"circuits": {}}
326 1
        self.napp.mongo_controller.get_circuits.return_value = circuits
327 1
        url = f"{self.base_endpoint}/v2/evc/"
328 1
        response = await self.api_client.get(url)
329 1
        assert response.status_code == 200, response.data
330 1
        assert not response.json()
331
332 1
    async def test_list_no_circuits_stored(self):
333
        """Test if list circuits return all circuits stored."""
334 1
        circuits = {"circuits": {}}
335 1
        self.napp.mongo_controller.get_circuits.return_value = circuits
336
337 1
        url = f"{self.base_endpoint}/v2/evc/"
338 1
        response = await self.api_client.get(url)
339 1
        expected_result = circuits["circuits"]
340 1
        assert response.json() == expected_result
341
342 1 View Code Duplication
    async def test_list_with_circuits_stored(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
343
        """Test if list circuits return all circuits stored."""
344 1
        circuits = {
345
            'circuits':
346
            {"1": {"name": "circuit_1"}, "2": {"name": "circuit_2"}}
347
        }
348 1
        get_circuits = self.napp.mongo_controller.get_circuits
349 1
        get_circuits.return_value = circuits
350
351 1
        url = f"{self.base_endpoint}/v2/evc/"
352 1
        response = await self.api_client.get(url)
353 1
        expected_result = circuits["circuits"]
354 1
        get_circuits.assert_called_with(archived="false", metadata={})
355 1
        assert response.json() == expected_result
356
357 1 View Code Duplication
    async def test_list_with_archived_circuits_archived(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
358
        """Test if list circuits only archived circuits."""
359 1
        circuits = {
360
            'circuits':
361
            {
362
                "1": {"name": "circuit_1", "archived": True},
363
            }
364
        }
365 1
        get_circuits = self.napp.mongo_controller.get_circuits
366 1
        get_circuits.return_value = circuits
367
368 1
        url = f"{self.base_endpoint}/v2/evc/?archived=true&metadata.a=1"
369 1
        response = await self.api_client.get(url)
370 1
        get_circuits.assert_called_with(archived="true",
371
                                        metadata={"metadata.a": "1"})
372 1
        expected_result = {"1": circuits["circuits"]["1"]}
373 1
        assert response.json() == expected_result
374
375 1
    async def test_list_with_archived_circuits_all(self):
376
        """Test if list circuits return all circuits."""
377 1
        circuits = {
378
            'circuits': {
379
                "1": {"name": "circuit_1"},
380
                "2": {"name": "circuit_2", "archived": True},
381
            }
382
        }
383 1
        self.napp.mongo_controller.get_circuits.return_value = circuits
384
385 1
        url = f"{self.base_endpoint}/v2/evc/?archived=null"
386 1
        response = await self.api_client.get(url)
387 1
        expected_result = circuits["circuits"]
388 1
        assert response.json() == expected_result
389
390 1
    async def test_circuit_with_valid_id(self):
391
        """Test if get_circuit return the circuit attributes."""
392 1
        circuit = {"name": "circuit_1"}
393 1
        self.napp.mongo_controller.get_circuit.return_value = circuit
394
395 1
        url = f"{self.base_endpoint}/v2/evc/1"
396 1
        response = await self.api_client.get(url)
397 1
        expected_result = circuit
398 1
        assert response.json() == expected_result
399
400 1
    async def test_circuit_with_invalid_id(self):
401
        """Test if get_circuit return invalid circuit_id."""
402 1
        self.napp.mongo_controller.get_circuit.return_value = None
403 1
        url = f"{self.base_endpoint}/v2/evc/3"
404 1
        response = await self.api_client.get(url)
405
        expected_result = "circuit_id 3 not found"
406
        assert response.json()["description"] == expected_result
407
408 1
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
409 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
410 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
411 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
412 1
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
413 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
414 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
415 1
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
416 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
417 1
    async def test_create_a_circuit_case_1(
418
        self,
419
        validate_mock,
420
        evc_as_dict_mock,
421
        mongo_controller_upsert_mock,
422
        uni_from_dict_mock,
423
        sched_add_mock,
424
        evc_deploy_mock,
425
        mock_use_uni_tags,
426
        mock_tags_equal,
427
        mock_check_duplicate
428
    ):
429
        """Test create a new circuit."""
430
        # pylint: disable=too-many-locals
431 1
        self.napp.controller.loop = asyncio.get_running_loop()
432 1
        validate_mock.return_value = True
433 1
        mongo_controller_upsert_mock.return_value = True
434 1
        evc_deploy_mock.return_value = True
435 1
        mock_use_uni_tags.return_value = True
436 1
        mock_tags_equal.return_value = True
437 1
        mock_check_duplicate.return_value = True
438 1
        uni1 = create_autospec(UNI)
439 1
        uni2 = create_autospec(UNI)
440 1
        uni1.interface = create_autospec(Interface)
441 1
        uni2.interface = create_autospec(Interface)
442 1
        uni1.interface.switch = "00:00:00:00:00:00:00:01"
443 1
        uni2.interface.switch = "00:00:00:00:00:00:00:02"
444 1
        uni_from_dict_mock.side_effect = [uni1, uni2]
445 1
        evc_as_dict_mock.return_value = {}
446 1
        sched_add_mock.return_value = True
447 1
        self.napp.mongo_controller.get_circuits.return_value = {}
448
449 1
        url = f"{self.base_endpoint}/v2/evc/"
450 1
        payload = {
451
            "name": "my evc1",
452
            "frequency": "* * * * *",
453
            "uni_a": {
454
                "interface_id": "00:00:00:00:00:00:00:01:1",
455
                "tag": {"tag_type": 'vlan', "value": 80},
456
            },
457
            "uni_z": {
458
                "interface_id": "00:00:00:00:00:00:00:02:2",
459
                "tag": {"tag_type": 'vlan', "value": 1},
460
            },
461
            "dynamic_backup_path": True,
462
            "primary_constraints": {
463
                "spf_max_path_cost": 8,
464
                "mandatory_metrics": {
465
                    "ownership": "red"
466
                }
467
            },
468
            "secondary_constraints": {
469
                "spf_attribute": "priority",
470
                "mandatory_metrics": {
471
                    "ownership": "blue"
472
                }
473
            }
474
        }
475
476 1
        response = await self.api_client.post(url, json=payload)
477 1
        current_data = response.json()
478
479
        # verify expected result from request
480 1
        assert 201 == response.status_code
481 1
        assert "circuit_id" in current_data
482
483
        # verify uni called
484 1
        uni_from_dict_mock.called_twice()
485 1
        uni_from_dict_mock.assert_any_call(payload["uni_z"])
486 1
        uni_from_dict_mock.assert_any_call(payload["uni_a"])
487
488
        # verify validation called
489 1
        validate_mock.assert_called_once()
490 1
        validate_mock.assert_called_with(
491
            table_group={'evpl': 0, 'epl': 0},
492
            frequency="* * * * *",
493
            name="my evc1",
494
            uni_a=uni1,
495
            uni_z=uni2,
496
            dynamic_backup_path=True,
497
            primary_constraints=payload["primary_constraints"],
498
            secondary_constraints=payload["secondary_constraints"],
499
        )
500
        # verify save method is called
501 1
        mongo_controller_upsert_mock.assert_called_once()
502
503
        # verify evc as dict is called to save in the box
504 1
        evc_as_dict_mock.assert_called()
505
        # verify add circuit in sched
506 1
        sched_add_mock.assert_called_once()
507
508 1
    async def test_create_a_circuit_case_2(self):
509
        """Test create a new circuit trying to send request without a json."""
510 1
        self.napp.controller.loop = asyncio.get_running_loop()
511 1
        url = f"{self.base_endpoint}/v2/evc/"
512
513 1
        response = await self.api_client.post(url)
514
        current_data = response.json()
515
        assert 400 == response.status_code
516
        assert "Missing required request body" in current_data["description"]
517
518 1
    async def test_create_a_circuit_case_3(self):
519
        """Test create a new circuit trying to send request with an
520
        invalid json."""
521 1
        self.napp.controller.loop = asyncio.get_running_loop()
522 1
        url = f"{self.base_endpoint}/v2/evc/"
523
524 1
        response = await self.api_client.post(
525
            url,
526
            json="This is an {Invalid:} JSON",
527
        )
528
        current_data = response.json()
529
        assert 400 == response.status_code
530
        assert "This is an {Invalid:" in current_data["description"]
531
532 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
533 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
534 1
    async def test_create_a_circuit_case_4(
535
        self,
536
        mongo_controller_upsert_mock,
537
        uni_from_dict_mock
538
    ):
539
        """Test create a new circuit trying to send request with an
540
        invalid value."""
541 1
        self.napp.controller.loop = asyncio.get_running_loop()
542
        # pylint: disable=too-many-locals
543 1
        uni_from_dict_mock.side_effect = ValueError("Could not instantiate")
544 1
        mongo_controller_upsert_mock.return_value = True
545 1
        url = f"{self.base_endpoint}/v2/evc/"
546
547 1
        payload = {
548
            "name": "my evc1",
549
            "frequency": "* * * * *",
550
            "uni_a": {
551
                "interface_id": "00:00:00:00:00:00:00:01:76",
552
                "tag": {"tag_type": 'vlan', "value": 80},
553
            },
554
            "uni_z": {
555
                "interface_id": "00:00:00:00:00:00:00:02:2",
556
                "tag": {"tag_type": 'vlan', "value": 1},
557
            },
558
        }
559
560 1
        response = await self.api_client.post(url, json=payload)
561
        current_data = response.json()
562
        expected_data = "Error creating UNI: Invalid value"
563
        assert 400 == response.status_code
564
        assert current_data["description"] == expected_data
565
566
        payload["name"] = 1
567
        response = await self.api_client.post(url, json=payload)
568
        assert 400 == response.status_code, response.data
569
570 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
571 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
572 1
    async def test_create_a_circuit_case_5(
573
        self,
574
        validate_mock,
575
        uni_from_dict_mock
576
    ):
577
        """Test create a new intra circuit with a disabled switch"""
578 1
        self.napp.controller.loop = asyncio.get_running_loop()
579 1
        validate_mock.return_value = True
580 1
        uni1 = create_autospec(UNI)
581 1
        uni1.interface = create_autospec(Interface)
582 1
        uni1.interface.switch = MagicMock()
583 1
        uni1.interface.switch.return_value = "00:00:00:00:00:00:00:01"
584 1
        uni1.interface.switch.status = EntityStatus.DISABLED
585 1
        uni_from_dict_mock.side_effect = [uni1, uni1]
586 1
        url = f"{self.base_endpoint}/v2/evc/"
587 1
        payload = {
588
            "name": "my evc1",
589
            "dynamic_backup_path": True,
590
            "uni_a": {
591
                "interface_id": "00:00:00:00:00:00:00:01:1",
592
                "tag": {"tag_type": 'vlan', "value": 80},
593
            },
594
            "uni_z": {
595
                "interface_id": "00:00:00:00:00:00:00:01:2",
596
                "tag": {"tag_type": 'vlan', "value": 1},
597
            },
598
        }
599
600 1
        response = await self.api_client.post(url, json=payload)
601
        assert 409 == response.status_code, response.data
602
603 1
    async def test_create_a_circuit_invalid_queue_id(self):
604
        """Test create a new circuit with invalid queue_id."""
605 1
        self.napp.controller.loop = asyncio.get_running_loop()
606 1
        url = f"{self.base_endpoint}/v2/evc/"
607
608 1
        payload = {
609
            "name": "my evc1",
610
            "queue_id": 8,
611
            "uni_a": {
612
                "interface_id": "00:00:00:00:00:00:00:01:76",
613
                "tag": {"tag_type": 'vlan', "value": 80},
614
            },
615
            "uni_z": {
616
                "interface_id": "00:00:00:00:00:00:00:02:2",
617
                "tag": {"tag_type": 'vlan', "value": 1},
618
            },
619
        }
620 1
        response = await self.api_client.post(url, json=payload)
621
        current_data = response.json()
622
        expected_data = "8 is greater than the maximum of 7"
623
624
        assert response.status_code == 400
625
        assert expected_data in current_data["description"]
626
627 1
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
628 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
629 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
630 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
631 1
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
632 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
633 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
634 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
635 1
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
636 1
    async def test_create_circuit_already_enabled(
637
        self,
638
        evc_as_dict_mock,
639
        validate_mock,
640
        mongo_controller_upsert_mock,
641
        uni_from_dict_mock,
642
        sched_add_mock,
643
        evc_deploy_mock,
644
        mock_use_uni_tags,
645
        mock_tags_equal,
646
        mock_check_duplicate
647
    ):
648
        """Test create an already created circuit."""
649
        # pylint: disable=too-many-locals
650 1
        self.napp.controller.loop = asyncio.get_running_loop()
651 1
        validate_mock.return_value = True
652 1
        mongo_controller_upsert_mock.return_value = True
653 1
        sched_add_mock.return_value = True
654 1
        evc_deploy_mock.return_value = True
655 1
        mock_tags_equal.return_value = True
656 1
        mock_check_duplicate.return_value = True
657 1
        mock_use_uni_tags.side_effect = [
658
            None, KytosTagError("The EVC already exists.")
659
        ]
660 1
        uni1 = create_autospec(UNI)
661 1
        uni2 = create_autospec(UNI)
662 1
        uni1.interface = create_autospec(Interface)
663 1
        uni2.interface = create_autospec(Interface)
664 1
        uni1.interface.switch = "00:00:00:00:00:00:00:01"
665 1
        uni2.interface.switch = "00:00:00:00:00:00:00:02"
666 1
        uni_from_dict_mock.side_effect = [uni1, uni2, uni1, uni2]
667
668 1
        payload = {
669
            "name": "my evc1",
670
            "uni_a": {
671
                "interface_id": "00:00:00:00:00:00:00:01:1",
672
                "tag": {"tag_type": 'vlan', "value": 80},
673
            },
674
            "uni_z": {
675
                "interface_id": "00:00:00:00:00:00:00:02:2",
676
                "tag": {"tag_type": 'vlan', "value": 1},
677
            },
678
            "dynamic_backup_path": True,
679
        }
680
681 1
        evc_as_dict_mock.return_value = payload
682 1
        response = await self.api_client.post(
683
            f"{self.base_endpoint}/v2/evc/",
684
            json=payload
685
        )
686 1
        assert 201 == response.status_code
687
688 1
        response = await self.api_client.post(
689
            f"{self.base_endpoint}/v2/evc/",
690
            json=payload
691
        )
692
        current_data = response.json()
693
        expected_data = "The EVC already exists."
694
        assert current_data["description"] == expected_data
695
        assert 400 == response.status_code
696
697 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
698 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
699 1
    async def test_create_circuit_case_5(
700
        self,
701
        uni_from_dict_mock,
702
        mock_tags_equal
703
    ):
704
        """Test when neither primary path nor dynamic_backup_path is set."""
705 1
        self.napp.controller.loop = asyncio.get_running_loop()
706 1
        mock_tags_equal.return_value = True
707 1
        url = f"{self.base_endpoint}/v2/evc/"
708 1
        uni1 = create_autospec(UNI)
709 1
        uni2 = create_autospec(UNI)
710 1
        uni1.interface = create_autospec(Interface)
711 1
        uni2.interface = create_autospec(Interface)
712 1
        uni1.interface.switch = "00:00:00:00:00:00:00:01"
713 1
        uni2.interface.switch = "00:00:00:00:00:00:00:02"
714 1
        uni_from_dict_mock.side_effect = [uni1, uni2, uni1, uni2]
715
716 1
        payload = {
717
            "name": "my evc1",
718
            "frequency": "* * * * *",
719
            "uni_a": {
720
                "interface_id": "00:00:00:00:00:00:00:01:1",
721
                "tag": {"tag_type": 'vlan', "value": 80},
722
            },
723
            "uni_z": {
724
                "interface_id": "00:00:00:00:00:00:00:02:2",
725
                "tag": {"tag_type": 'vlan', "value": 1},
726
            },
727
        }
728
729 1
        response = await self.api_client.post(url, json=payload)
730
        current_data = response.json()
731
        expected_data = "The EVC must have a primary path "
732
        expected_data += "or allow dynamic paths."
733
        assert 400 == response.status_code, response.data
734
        assert current_data["description"] == expected_data
735
736 1
    @patch("napps.kytos.mef_eline.main.Main._evc_from_dict")
737 1
    async def test_create_circuit_case_6(self, mock_evc):
738
        """Test create_circuit with KytosTagError"""
739 1
        self.napp.controller.loop = asyncio.get_running_loop()
740 1
        url = f"{self.base_endpoint}/v2/evc/"
741 1
        mock_evc.side_effect = KytosTagError("")
742 1
        payload = {
743
            "name": "my evc1",
744
            "uni_a": {
745
                "interface_id": "00:00:00:00:00:00:00:01:1",
746
            },
747
            "uni_z": {
748
                "interface_id": "00:00:00:00:00:00:00:02:2",
749
            },
750
        }
751 1
        response = await self.api_client.post(url, json=payload)
752
        assert response.status_code == 400, response.data
753
754 1
    @patch("napps.kytos.mef_eline.main.check_disabled_component")
755 1
    @patch("napps.kytos.mef_eline.main.Main._evc_from_dict")
756 1
    async def test_create_circuit_case_7(
757
        self,
758
        mock_evc,
759
        mock_check_disabled_component
760
    ):
761
        """Test create_circuit with InvalidPath"""
762 1
        self.napp.controller.loop = asyncio.get_running_loop()
763 1
        mock_check_disabled_component.return_value = True
764 1
        url = f"{self.base_endpoint}/v2/evc/"
765 1
        uni1 = get_uni_mocked()
766 1
        uni2 = get_uni_mocked()
767 1
        evc = MagicMock(uni_a=uni1, uni_z=uni2)
768 1
        evc.primary_path = MagicMock()
769 1
        evc.backup_path = MagicMock()
770
771
        # Backup_path invalid
772 1
        evc.backup_path.is_valid = MagicMock(side_effect=InvalidPath)
773 1
        mock_evc.return_value = evc
774 1
        payload = {
775
            "name": "my evc1",
776
            "uni_a": {
777
                "interface_id": "00:00:00:00:00:00:00:01:1",
778
            },
779
            "uni_z": {
780
                "interface_id": "00:00:00:00:00:00:00:02:2",
781
            },
782
        }
783 1
        response = await self.api_client.post(url, json=payload)
784
        assert response.status_code == 400, response.data
785
786
        # Backup_path invalid
787
        evc.primary_path.is_valid = MagicMock(side_effect=InvalidPath)
788
        mock_evc.return_value = evc
789
790
        response = await self.api_client.post(url, json=payload)
791
        assert response.status_code == 400, response.data
792
793 1
    @patch("napps.kytos.mef_eline.main.check_disabled_component")
794 1
    @patch("napps.kytos.mef_eline.main.Main._evc_from_dict")
795 1
    async def test_create_circuit_case_8(
796
        self,
797
        mock_evc,
798
        mock_check_disabled_component
799
    ):
800
        """Test create_circuit wit no equal tag lists"""
801 1
        self.napp.controller.loop = asyncio.get_running_loop()
802 1
        mock_check_disabled_component.return_value = True
803 1
        url = f"{self.base_endpoint}/v2/evc/"
804 1
        uni1 = get_uni_mocked()
805 1
        uni2 = get_uni_mocked()
806 1
        evc = MagicMock(uni_a=uni1, uni_z=uni2)
807 1
        evc._tag_lists_equal = MagicMock(return_value=False)
808 1
        mock_evc.return_value = evc
809 1
        payload = {
810
            "name": "my evc1",
811
            "uni_a": {
812
                "interface_id": "00:00:00:00:00:00:00:01:1",
813
                "tag": {"tag_type": 'vlan', "value": [[50, 100]]},
814
            },
815
            "uni_z": {
816
                "interface_id": "00:00:00:00:00:00:00:02:2",
817
                "tag": {"tag_type": 'vlan', "value": [[1, 10]]},
818
            },
819
        }
820 1
        response = await self.api_client.post(url, json=payload)
821
        assert response.status_code == 400, response.data
822
823 1
    async def test_redeploy_evc(self):
824
        """Test endpoint to redeploy an EVC."""
825 1
        evc1 = MagicMock()
826 1
        evc1.is_enabled.return_value = True
827 1
        self.napp.circuits = {"1": evc1, "2": MagicMock()}
828 1
        url = f"{self.base_endpoint}/v2/evc/1/redeploy"
829 1
        response = await self.api_client.patch(url)
830 1
        evc1.remove_failover_flows.assert_called()
831 1
        evc1.remove_current_flows.assert_called()
832 1
        assert response.status_code == 202, response.data
833
834 1
    async def test_redeploy_evc_disabled(self):
835
        """Test endpoint to redeploy an EVC."""
836 1
        evc1 = MagicMock()
837 1
        evc1.is_enabled.return_value = False
838 1
        self.napp.circuits = {"1": evc1, "2": MagicMock()}
839 1
        url = f"{self.base_endpoint}/v2/evc/1/redeploy"
840 1
        response = await self.api_client.patch(url)
841 1
        evc1.remove_failover_flows.assert_not_called()
842 1
        evc1.remove_current_flows.assert_not_called()
843 1
        assert response.status_code == 409, response.data
844
845 1
    async def test_redeploy_evc_deleted(self):
846
        """Test endpoint to redeploy an EVC."""
847 1
        evc1 = MagicMock()
848 1
        evc1.is_enabled.return_value = True
849 1
        self.napp.circuits = {"1": evc1, "2": MagicMock()}
850 1
        url = f"{self.base_endpoint}/v2/evc/3/redeploy"
851 1
        response = await self.api_client.patch(url)
852
        assert response.status_code == 404, response.data
853
854 1
    async def test_list_schedules__no_data_stored(self):
855
        """Test if list circuits return all circuits stored."""
856 1
        self.napp.mongo_controller.get_circuits.return_value = {"circuits": {}}
857
858 1
        url = f"{self.base_endpoint}/v2/evc/schedule"
859
860 1
        response = await self.api_client.get(url)
861 1
        assert response.status_code == 200
862 1
        assert not response.json()
863
864 1
    def _add_mongodb_schedule_data(self, data_mock):
865
        """Add schedule data to mongodb mock object."""
866 1
        circuits = {"circuits": {}}
867 1
        payload_1 = {
868
            "id": "aa:aa:aa",
869
            "name": "my evc1",
870
            "uni_a": {
871
                "interface_id": "00:00:00:00:00:00:00:01:1",
872
                "tag": {"tag_type": 'vlan', "value": 80},
873
            },
874
            "uni_z": {
875
                "interface_id": "00:00:00:00:00:00:00:02:2",
876
                "tag": {"tag_type": 'vlan', "value": 1},
877
            },
878
            "circuit_scheduler": [
879
                {"id": "1", "frequency": "* * * * *", "action": "create"},
880
                {"id": "2", "frequency": "1 * * * *", "action": "remove"},
881
            ],
882
        }
883 1
        circuits["circuits"].update({"aa:aa:aa": payload_1})
884 1
        payload_2 = {
885
            "id": "bb:bb:bb",
886
            "name": "my second evc2",
887
            "uni_a": {
888
                "interface_id": "00:00:00:00:00:00:00:01:2",
889
                "tag": {"tag_type": 'vlan', "value": 90},
890
            },
891
            "uni_z": {
892
                "interface_id": "00:00:00:00:00:00:00:03:2",
893
                "tag": {"tag_type": 'vlan', "value": 100},
894
            },
895
            "circuit_scheduler": [
896
                {"id": "3", "frequency": "1 * * * *", "action": "create"},
897
                {"id": "4", "frequency": "2 * * * *", "action": "remove"},
898
            ],
899
        }
900 1
        circuits["circuits"].update({"bb:bb:bb": payload_2})
901 1
        payload_3 = {
902
            "id": "cc:cc:cc",
903
            "name": "my third evc3",
904
            "uni_a": {
905
                "interface_id": "00:00:00:00:00:00:00:03:1",
906
                "tag": {"tag_type": 'vlan', "value": 90},
907
            },
908
            "uni_z": {
909
                "interface_id": "00:00:00:00:00:00:00:04:2",
910
                "tag": {"tag_type": 'vlan', "value": 100},
911
            },
912
        }
913 1
        circuits["circuits"].update({"cc:cc:cc": payload_3})
914
        # Add one circuit to the mongodb.
915 1
        data_mock.return_value = circuits
916
917 1
    async def test_list_schedules_from_mongodb(self):
918
        """Test if list circuits return specific circuits stored."""
919 1
        self._add_mongodb_schedule_data(
920
            self.napp.mongo_controller.get_circuits
921
        )
922
923 1
        url = f"{self.base_endpoint}/v2/evc/schedule"
924
925
        # Call URL
926 1
        response = await self.api_client.get(url)
927
        # Expected JSON data from response
928 1
        expected = [
929
            {
930
                "circuit_id": "aa:aa:aa",
931
                "schedule": {
932
                    "action": "create",
933
                    "frequency": "* * * * *",
934
                    "id": "1",
935
                },
936
                "schedule_id": "1",
937
            },
938
            {
939
                "circuit_id": "aa:aa:aa",
940
                "schedule": {
941
                    "action": "remove",
942
                    "frequency": "1 * * * *",
943
                    "id": "2",
944
                },
945
                "schedule_id": "2",
946
            },
947
            {
948
                "circuit_id": "bb:bb:bb",
949
                "schedule": {
950
                    "action": "create",
951
                    "frequency": "1 * * * *",
952
                    "id": "3",
953
                },
954
                "schedule_id": "3",
955
            },
956
            {
957
                "circuit_id": "bb:bb:bb",
958
                "schedule": {
959
                    "action": "remove",
960
                    "frequency": "2 * * * *",
961
                    "id": "4",
962
                },
963
                "schedule_id": "4",
964
            },
965
        ]
966
967 1
        assert response.status_code == 200
968 1
        assert expected == response.json()
969
970 1
    async def test_get_specific_schedule_from_mongodb(self):
971
        """Test get schedules from a circuit."""
972 1
        self._add_mongodb_schedule_data(
973
            self.napp.mongo_controller.get_circuits
974
        )
975
976 1
        requested_circuit_id = "bb:bb:bb"
977 1
        evc = self.napp.mongo_controller.get_circuits()
978 1
        evc = evc["circuits"][requested_circuit_id]
979 1
        self.napp.mongo_controller.get_circuit.return_value = evc
980 1
        url = f"{self.base_endpoint}/v2/evc/{requested_circuit_id}"
981
982
        # Call URL
983 1
        response = await self.api_client.get(url)
984
985
        # Expected JSON data from response
986 1
        expected = [
987
            {"action": "create", "frequency": "1 * * * *", "id": "3"},
988
            {"action": "remove", "frequency": "2 * * * *", "id": "4"},
989
        ]
990
991 1
        assert response.status_code == 200
992 1
        assert expected == response.json()["circuit_scheduler"]
993
994 1
    async def test_get_specific_schedules_from_mongodb_not_found(self):
995
        """Test get specific schedule ID that does not exist."""
996 1
        requested_id = "blah"
997 1
        self.napp.mongo_controller.get_circuit.return_value = None
998 1
        url = f"{self.base_endpoint}/v2/evc/{requested_id}"
999
1000
        # Call URL
1001 1
        response = await self.api_client.get(url)
1002
1003
        expected = "circuit_id blah not found"
1004
        # Assert response not found
1005
        assert response.status_code == 404
1006
        assert expected == response.json()["description"]
1007
1008 1
    def _uni_from_dict_side_effect(self, uni_dict):
1009 1
        interface_id = uni_dict.get("interface_id")
1010 1
        tag_dict = uni_dict.get("tag")
1011 1
        interface = Interface(interface_id, "0", MagicMock(id="1"))
1012 1
        return UNI(interface, tag_dict)
1013
1014 1
    @patch("apscheduler.schedulers.background.BackgroundScheduler.add_job")
1015 1
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
1016 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
1017 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1018 1
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
1019 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
1020 1
    async def test_create_schedule(
1021
        self,
1022
        validate_mock,
1023
        evc_as_dict_mock,
1024
        mongo_controller_upsert_mock,
1025
        uni_from_dict_mock,
1026
        sched_add_mock,
1027
        scheduler_add_job_mock
1028
    ):
1029
        """Test create a circuit schedule."""
1030 1
        self.napp.controller.loop = asyncio.get_running_loop()
1031 1
        validate_mock.return_value = True
1032 1
        mongo_controller_upsert_mock.return_value = True
1033 1
        uni_from_dict_mock.side_effect = self._uni_from_dict_side_effect
1034 1
        evc_as_dict_mock.return_value = {}
1035 1
        sched_add_mock.return_value = True
1036
1037 1
        self._add_mongodb_schedule_data(
1038
            self.napp.mongo_controller.get_circuits
1039
        )
1040
1041 1
        requested_id = "bb:bb:bb"
1042 1
        url = f"{self.base_endpoint}/v2/evc/schedule/"
1043
1044 1
        payload = {
1045
            "circuit_id": requested_id,
1046
            "schedule": {"frequency": "1 * * * *", "action": "create"},
1047
            "metadata": {"metadata1": "test_data"},
1048
        }
1049
1050
        # Call URL
1051 1
        response = await self.api_client.post(url, json=payload)
1052 1
        response_json = response.json()
1053
1054 1
        assert response.status_code == 201
1055 1
        scheduler_add_job_mock.assert_called_once()
1056 1
        mongo_controller_upsert_mock.assert_called_once()
1057 1
        assert payload["schedule"]["frequency"] == response_json["frequency"]
1058 1
        assert payload["schedule"]["action"] == response_json["action"]
1059 1
        assert response_json["id"] is not None
1060
1061
        # Case 2: there is no schedule
1062 1
        payload = {
1063
              "circuit_id": "cc:cc:cc",
1064
              "schedule": {
1065
                "frequency": "1 * * * *",
1066
                "action": "create"
1067
              }
1068
            }
1069 1
        response = await self.api_client.post(url, json=payload)
1070 1
        assert response.status_code == 201
1071
1072 1
    async def test_create_schedule_invalid_request(self):
1073
        """Test create schedule API with invalid request."""
1074 1
        self.napp.controller.loop = asyncio.get_running_loop()
1075 1
        evc1 = MagicMock()
1076 1
        self.napp.circuits = {'bb:bb:bb': evc1}
1077 1
        url = f'{self.base_endpoint}/v2/evc/schedule/'
1078
1079
        # case 1: empty post
1080 1
        response = await self.api_client.post(url, json={})
1081
        assert response.status_code == 400
1082
1083
        # case 2: not a dictionary
1084
        payload = []
1085
        response = await self.api_client.post(url, json=payload)
1086
        assert response.status_code == 400
1087
1088
        # case 3: missing circuit id
1089
        payload = {
1090
            "schedule": {
1091
                "frequency": "1 * * * *",
1092
                "action": "create"
1093
            }
1094
        }
1095
        response = await self.api_client.post(url, json=payload)
1096
        assert response.status_code == 400
1097
1098
        # case 4: missing schedule
1099
        payload = {
1100
            "circuit_id": "bb:bb:bb"
1101
        }
1102
        response = await self.api_client.post(url, json=payload)
1103
        assert response.status_code == 400
1104
1105
        # case 5: invalid circuit
1106
        payload = {
1107
            "circuit_id": "xx:xx:xx",
1108
            "schedule": {
1109
                "frequency": "1 * * * *",
1110
                "action": "create"
1111
            }
1112
        }
1113
        response = await self.api_client.post(url, json=payload)
1114
        assert response.status_code == 404
1115
1116
        # case 6: invalid json
1117
        response = await self.api_client.post(url, json="test")
1118
        assert response.status_code == 400
1119
1120 1
    @patch('apscheduler.schedulers.background.BackgroundScheduler.remove_job')
1121 1
    @patch('napps.kytos.mef_eline.scheduler.Scheduler.add')
1122 1
    @patch('napps.kytos.mef_eline.main.Main._uni_from_dict')
1123 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1124 1
    @patch('napps.kytos.mef_eline.main.EVC.as_dict')
1125 1
    @patch('napps.kytos.mef_eline.models.evc.EVC._validate')
1126 1
    async def test_update_schedule(
1127
        self,
1128
        validate_mock,
1129
        evc_as_dict_mock,
1130
        mongo_controller_upsert_mock,
1131
        uni_from_dict_mock,
1132
        sched_add_mock,
1133
        scheduler_remove_job_mock
1134
    ):
1135
        """Test create a circuit schedule."""
1136 1
        self.napp.controller.loop = asyncio.get_running_loop()
1137 1
        mongo_payload_1 = {
1138
            "circuits": {
1139
                "aa:aa:aa": {
1140
                    "id": "aa:aa:aa",
1141
                    "name": "my evc1",
1142
                    "uni_a": {
1143
                        "interface_id": "00:00:00:00:00:00:00:01:1",
1144
                        "tag": {"tag_type": 'vlan', "value": 80},
1145
                    },
1146
                    "uni_z": {
1147
                        "interface_id": "00:00:00:00:00:00:00:02:2",
1148
                        "tag": {"tag_type": 'vlan', "value": 1},
1149
                    },
1150
                    "circuit_scheduler": [
1151
                        {
1152
                            "id": "1",
1153
                            "frequency": "* * * * *",
1154
                            "action": "create"
1155
                        }
1156
                    ],
1157
                }
1158
            }
1159
        }
1160
1161 1
        validate_mock.return_value = True
1162 1
        mongo_controller_upsert_mock.return_value = True
1163 1
        sched_add_mock.return_value = True
1164 1
        uni_from_dict_mock.side_effect = ["uni_a", "uni_z"]
1165 1
        evc_as_dict_mock.return_value = {}
1166 1
        self.napp.mongo_controller.get_circuits.return_value = mongo_payload_1
1167 1
        scheduler_remove_job_mock.return_value = True
1168
1169 1
        requested_schedule_id = "1"
1170 1
        url = f"{self.base_endpoint}/v2/evc/schedule/{requested_schedule_id}"
1171
1172 1
        payload = {"frequency": "*/1 * * * *", "action": "create"}
1173
1174
        # Call URL
1175 1
        response = await self.api_client.patch(url, json=payload)
1176 1
        response_json = response.json()
1177
1178 1
        assert response.status_code == 200
1179 1
        scheduler_remove_job_mock.assert_called_once()
1180 1
        mongo_controller_upsert_mock.assert_called_once()
1181 1
        assert payload["frequency"] == response_json["frequency"]
1182 1
        assert payload["action"] == response_json["action"]
1183 1
        assert response_json["id"] is not None
1184
1185 1
    @patch('napps.kytos.mef_eline.main.Main._find_evc_by_schedule_id')
1186 1
    async def test_update_no_schedule(
1187
        self, find_evc_by_schedule_id_mock
1188
    ):
1189
        """Test update a circuit schedule."""
1190 1
        self.napp.controller.loop = asyncio.get_running_loop()
1191 1
        url = f"{self.base_endpoint}/v2/evc/schedule/1"
1192 1
        payload = {"frequency": "*/1 * * * *", "action": "create"}
1193
1194 1
        find_evc_by_schedule_id_mock.return_value = None, None
1195
1196 1
        response = await self.api_client.patch(url, json=payload)
1197
        assert response.status_code == 404
1198
1199 1
    @patch("apscheduler.schedulers.background.BackgroundScheduler.remove_job")
1200 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
1201 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1202 1
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
1203 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
1204 1
    async def test_delete_schedule(self, *args):
1205
        """Test create a circuit schedule."""
1206 1
        (
1207
            validate_mock,
1208
            evc_as_dict_mock,
1209
            mongo_controller_upsert_mock,
1210
            uni_from_dict_mock,
1211
            scheduler_remove_job_mock,
1212
        ) = args
1213
1214 1
        mongo_payload_1 = {
1215
            "circuits": {
1216
                "2": {
1217
                    "id": "2",
1218
                    "name": "my evc1",
1219
                    "uni_a": {
1220
                        "interface_id": "00:00:00:00:00:00:00:01:1",
1221
                        "tag": {"tag_type": 'vlan', "value": 80},
1222
                    },
1223
                    "uni_z": {
1224
                        "interface_id": "00:00:00:00:00:00:00:02:2",
1225
                        "tag": {"tag_type": 'vlan', "value": 1},
1226
                    },
1227
                    "circuit_scheduler": [
1228
                        {
1229
                            "id": "1",
1230
                            "frequency": "* * * * *",
1231
                            "action": "create"
1232
                        }
1233
                    ],
1234
                }
1235
            }
1236
        }
1237 1
        validate_mock.return_value = True
1238 1
        mongo_controller_upsert_mock.return_value = True
1239 1
        uni_from_dict_mock.side_effect = ["uni_a", "uni_z"]
1240 1
        evc_as_dict_mock.return_value = {}
1241 1
        self.napp.mongo_controller.get_circuits.return_value = mongo_payload_1
1242 1
        scheduler_remove_job_mock.return_value = True
1243
1244 1
        requested_schedule_id = "1"
1245 1
        url = f"{self.base_endpoint}/v2/evc/schedule/{requested_schedule_id}"
1246
1247
        # Call URL
1248 1
        response = await self.api_client.delete(url)
1249
1250 1
        assert response.status_code == 200
1251 1
        scheduler_remove_job_mock.assert_called_once()
1252 1
        mongo_controller_upsert_mock.assert_called_once()
1253 1
        assert "Schedule removed" in f"{response.json()}"
1254
1255 1
    @patch('napps.kytos.mef_eline.main.Main._find_evc_by_schedule_id')
1256 1
    async def test_delete_schedule_not_found(self, mock_find_evc_by_sched):
1257
        """Test delete a circuit schedule - unexisting."""
1258 1
        mock_find_evc_by_sched.return_value = (None, False)
1259 1
        url = f'{self.base_endpoint}/v2/evc/schedule/1'
1260 1
        response = await self.api_client.delete(url)
1261
        assert response.status_code == 404
1262
1263 1
    def test_get_evcs_by_svc_level(self) -> None:
1264
        """Test get_evcs_by_svc_level."""
1265 1
        levels = [1, 2, 4, 2, 7]
1266 1
        evcs = {i: MagicMock(service_level=v, creation_time=1)
1267
                for i, v in enumerate(levels)}
1268 1
        self.napp.circuits = evcs
1269 1
        expected_levels = sorted(levels, reverse=True)
1270 1
        evcs_by_level = self.napp.get_evcs_by_svc_level()
1271 1
        assert evcs_by_level
1272
1273 1
        for evc, exp_level in zip(evcs_by_level, expected_levels):
1274 1
            assert evc.service_level == exp_level
1275
1276 1
        evcs = {i: MagicMock(service_level=1, creation_time=i)
1277
                for i in reversed(range(2))}
1278 1
        self.napp.circuits = evcs
1279 1
        evcs_by_level = self.napp.get_evcs_by_svc_level()
1280 1
        for i in range(2):
1281 1
            assert evcs_by_level[i].creation_time == i
1282
1283 1
        self.napp.circuits[1].is_enabled = lambda: False
1284 1
        evcs_by_level = self.napp.get_evcs_by_svc_level()
1285 1
        assert len(evcs_by_level) == 1
1286
1287 1
        self.napp.circuits[1].is_enabled = lambda: False
1288 1
        evcs_by_level = self.napp.get_evcs_by_svc_level(enable_filter=False)
1289 1
        assert len(evcs_by_level) == 2
1290
1291 1
    async def test_get_circuit_not_found(self):
1292
        """Test /v2/evc/<circuit_id> 404."""
1293 1
        self.napp.mongo_controller.get_circuit.return_value = None
1294 1
        url = f'{self.base_endpoint}/v2/evc/1234'
1295 1
        response = await self.api_client.get(url)
1296
        assert response.status_code == 404
1297
1298 1
    @patch('httpx.post')
1299 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
1300 1
    @patch('napps.kytos.mef_eline.scheduler.Scheduler.add')
1301 1
    @patch('napps.kytos.mef_eline.controllers.ELineController.update_evc')
1302 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1303 1
    @patch('napps.kytos.mef_eline.models.evc.EVC._validate')
1304 1
    @patch('kytos.core.Controller.get_interface_by_id')
1305 1
    @patch('napps.kytos.mef_eline.models.path.Path.is_valid')
1306 1
    @patch('napps.kytos.mef_eline.models.evc.EVCDeploy.deploy')
1307 1
    @patch('napps.kytos.mef_eline.main.Main._uni_from_dict')
1308 1
    @patch('napps.kytos.mef_eline.main.EVC.as_dict')
1309 1
    async def test_update_circuit(
1310
        self,
1311
        evc_as_dict_mock,
1312
        uni_from_dict_mock,
1313
        evc_deploy,
1314
        _is_valid_mock,
1315
        interface_by_id_mock,
1316
        _mock_validate,
1317
        _mongo_controller_upsert_mock,
1318
        _mongo_controller_update_mock,
1319
        _sched_add_mock,
1320
        mock_use_uni_tags,
1321
        httpx_mock
1322
    ):
1323
        """Test update a circuit circuit."""
1324 1
        self.napp.controller.loop = asyncio.get_running_loop()
1325 1
        mock_use_uni_tags.return_value = True
1326 1
        evc_deploy.return_value = True
1327 1
        interface_by_id_mock.return_value = get_uni_mocked().interface
1328 1
        unis = [
1329
            get_uni_mocked(switch_dpid="00:00:00:00:00:00:00:01"),
1330
            get_uni_mocked(switch_dpid="00:00:00:00:00:00:00:02"),
1331
        ]
1332 1
        uni_from_dict_mock.side_effect = 2 * unis
1333
1334 1
        response = MagicMock()
1335 1
        response.status_code = 201
1336 1
        httpx_mock.return_value = response
1337
1338 1
        payloads = [
1339
            {
1340
                "name": "my evc1",
1341
                "uni_a": {
1342
                    "interface_id": "00:00:00:00:00:00:00:01:1",
1343
                    "tag": {"tag_type": 'vlan', "value": 80},
1344
                },
1345
                "uni_z": {
1346
                    "interface_id": "00:00:00:00:00:00:00:02:2",
1347
                    "tag": {"tag_type": 'vlan', "value": 1},
1348
                },
1349
                "dynamic_backup_path": True,
1350
            },
1351
            {
1352
                "primary_path": [
1353
                    {
1354
                        "endpoint_a": {"id": "00:00:00:00:00:00:00:01:1"},
1355
                        "endpoint_b": {"id": "00:00:00:00:00:00:00:02:2"},
1356
                    }
1357
                ]
1358
            },
1359
            {
1360
                "sb_priority": 3
1361
            },
1362
            {
1363
                # It works only with 'enabled' and not with 'enable'
1364
                "enabled": True
1365
            },
1366
            {
1367
                "sb_priority": 100
1368
            }
1369
        ]
1370
1371 1
        evc_as_dict_mock.return_value = payloads[0]
1372 1
        response = await self.api_client.post(
1373
            f"{self.base_endpoint}/v2/evc/",
1374
            json=payloads[0],
1375
        )
1376 1
        assert 201 == response.status_code
1377
1378 1
        evc_deploy.reset_mock()
1379 1
        evc_as_dict_mock.return_value = payloads[1]
1380 1
        current_data = response.json()
1381 1
        circuit_id = current_data["circuit_id"]
1382 1
        response = await self.api_client.patch(
1383
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1384
            json=payloads[1],
1385
        )
1386
        # evc_deploy.assert_called_once()
1387 1
        assert 200 == response.status_code
1388
1389 1
        evc_deploy.reset_mock()
1390 1
        evc_as_dict_mock.return_value = payloads[2]
1391 1
        response = await self.api_client.patch(
1392
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1393
            json=payloads[2],
1394
        )
1395 1
        evc_deploy.assert_not_called()
1396 1
        assert 200 == response.status_code
1397
1398 1
        evc_deploy.reset_mock()
1399 1
        evc_as_dict_mock.return_value = payloads[3]
1400 1
        response = await self.api_client.patch(
1401
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1402
            json=payloads[3],
1403
        )
1404 1
        evc_deploy.assert_called_once()
1405 1
        assert 200 == response.status_code
1406
1407 1
        evc_deploy.reset_mock()
1408 1
        response = await self.api_client.patch(
1409
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1410
            content=b'{"priority":5,}',
1411
            headers={"Content-Type": "application/json"}
1412
        )
1413
        evc_deploy.assert_not_called()
1414
        assert 400 == response.status_code
1415
1416
        response = await self.api_client.patch(
1417
            f"{self.base_endpoint}/v2/evc/1234",
1418
            json=payloads[1],
1419
        )
1420
        current_data = response.json()
1421
        expected_data = "circuit_id 1234 not found"
1422
        assert current_data["description"] == expected_data
1423
        assert 404 == response.status_code
1424
1425
        self.napp.circuits[circuit_id]._active = False
1426
        evc_deploy.reset_mock()
1427
        response = await self.api_client.patch(
1428
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1429
            json=payloads[4]
1430
        )
1431 1
        assert 200 == response.status_code
1432 1
        evc_deploy.assert_called_once()
1433
1434 1 View Code Duplication
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1435 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
1436 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
1437 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
1438 1
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
1439 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
1440 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1441 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
1442 1
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
1443 1
    async def test_update_circuit_invalid_json(
1444
        self,
1445
        evc_as_dict_mock,
1446
        validate_mock,
1447
        mongo_controller_upsert_mock,
1448
        uni_from_dict_mock,
1449
        sched_add_mock,
1450
        evc_deploy_mock,
1451
        mock_use_uni_tags,
1452
        mock_tags_equal,
1453
        mock_check_duplicate
1454
    ):
1455
        """Test update a circuit circuit."""
1456 1
        self.napp.controller.loop = asyncio.get_running_loop()
1457 1
        validate_mock.return_value = True
1458 1
        mongo_controller_upsert_mock.return_value = True
1459 1
        sched_add_mock.return_value = True
1460 1
        evc_deploy_mock.return_value = True
1461 1
        mock_use_uni_tags.return_value = True
1462 1
        mock_tags_equal.return_value = True
1463 1
        mock_check_duplicate.return_value = True
1464 1
        uni1 = create_autospec(UNI)
1465 1
        uni2 = create_autospec(UNI)
1466 1
        uni1.interface = create_autospec(Interface)
1467 1
        uni2.interface = create_autospec(Interface)
1468 1
        uni1.interface.switch = "00:00:00:00:00:00:00:01"
1469 1
        uni2.interface.switch = "00:00:00:00:00:00:00:02"
1470 1
        uni_from_dict_mock.side_effect = [uni1, uni2, uni1, uni2]
1471
1472 1
        payload1 = {
1473
            "name": "my evc1",
1474
            "uni_a": {
1475
                "interface_id": "00:00:00:00:00:00:00:01:1",
1476
                "tag": {"tag_type": 'vlan', "value": 80},
1477
            },
1478
            "uni_z": {
1479
                "interface_id": "00:00:00:00:00:00:00:02:2",
1480
                "tag": {"tag_type": 'vlan', "value": 1},
1481
            },
1482
            "dynamic_backup_path": True,
1483
        }
1484
1485 1
        payload2 = {
1486
            "dynamic_backup_path": False,
1487
        }
1488
1489 1
        evc_as_dict_mock.return_value = payload1
1490 1
        response = await self.api_client.post(
1491
            f"{self.base_endpoint}/v2/evc/",
1492
            json=payload1
1493
        )
1494 1
        assert 201 == response.status_code
1495
1496 1
        evc_as_dict_mock.return_value = payload2
1497 1
        current_data = response.json()
1498 1
        circuit_id = current_data["circuit_id"]
1499 1
        response = await self.api_client.patch(
1500
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1501
            json=payload2
1502
        )
1503
        current_data = response.json()
1504
        assert 400 == response.status_code
1505
        assert "must have a primary path or" in current_data["description"]
1506
1507 1
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
1508 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
1509 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
1510 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
1511 1
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
1512 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
1513 1
    @patch("napps.kytos.mef_eline.main.Main._link_from_dict")
1514 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1515 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
1516 1
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
1517 1
    @patch("napps.kytos.mef_eline.models.path.Path.is_valid")
1518 1
    async def test_update_circuit_invalid_path(
1519
        self,
1520
        is_valid_mock,
1521
        evc_as_dict_mock,
1522
        validate_mock,
1523
        mongo_controller_upsert_mock,
1524
        link_from_dict_mock,
1525
        uni_from_dict_mock,
1526
        sched_add_mock,
1527
        evc_deploy_mock,
1528
        mock_use_uni_tags,
1529
        mock_tags_equal,
1530
        mock_check_duplicate
1531
    ):
1532
        """Test update a circuit circuit."""
1533 1
        self.napp.controller.loop = asyncio.get_running_loop()
1534 1
        is_valid_mock.side_effect = InvalidPath("error")
1535 1
        validate_mock.return_value = True
1536 1
        mongo_controller_upsert_mock.return_value = True
1537 1
        sched_add_mock.return_value = True
1538 1
        evc_deploy_mock.return_value = True
1539 1
        mock_use_uni_tags.return_value = True
1540 1
        link_from_dict_mock.return_value = 1
1541 1
        mock_tags_equal.return_value = True
1542 1
        mock_check_duplicate.return_value = True
1543 1
        uni1 = create_autospec(UNI)
1544 1
        uni2 = create_autospec(UNI)
1545 1
        uni1.interface = create_autospec(Interface)
1546 1
        uni2.interface = create_autospec(Interface)
1547 1
        uni1.interface.switch = "00:00:00:00:00:00:00:01"
1548 1
        uni2.interface.switch = "00:00:00:00:00:00:00:02"
1549 1
        uni_from_dict_mock.side_effect = [uni1, uni2, uni1, uni2]
1550
1551 1
        payload1 = {
1552
            "name": "my evc1",
1553
            "uni_a": {
1554
                "interface_id": "00:00:00:00:00:00:00:01:1",
1555
                "tag": {"tag_type": 'vlan', "value": 80},
1556
            },
1557
            "uni_z": {
1558
                "interface_id": "00:00:00:00:00:00:00:02:2",
1559
                "tag": {"tag_type": 'vlan', "value": 1},
1560
            },
1561
            "dynamic_backup_path": True,
1562
        }
1563
1564 1
        payload2 = {
1565
            "primary_path": [
1566
                {
1567
                    "endpoint_a": {"id": "00:00:00:00:00:00:00:01:1"},
1568
                    "endpoint_b": {"id": "00:00:00:00:00:00:00:02:2"},
1569
                }
1570
            ]
1571
        }
1572
1573 1
        evc_as_dict_mock.return_value = payload1
1574 1
        response = await self.api_client.post(
1575
            f"{self.base_endpoint}/v2/evc/",
1576
            json=payload1,
1577
        )
1578 1
        assert 201 == response.status_code
1579
1580 1
        evc_as_dict_mock.return_value = payload2
1581 1
        current_data = response.json()
1582 1
        circuit_id = current_data["circuit_id"]
1583 1
        response = await self.api_client.patch(
1584
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1585
            json=payload2,
1586
        )
1587
        current_data = response.json()
1588
        expected_data = "primary_path is not a valid path: error"
1589
        assert 400 == response.status_code
1590
        assert current_data["description"] == expected_data
1591
1592 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._get_unis_use_tags")
1593 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
1594 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1595 1
    @patch('napps.kytos.mef_eline.models.evc.EVC._validate')
1596 1
    @patch('napps.kytos.mef_eline.models.evc.EVCDeploy.deploy')
1597 1
    @patch('napps.kytos.mef_eline.main.Main._uni_from_dict')
1598 1
    async def test_update_disabled_intra_switch(
1599
        self,
1600
        uni_from_dict_mock,
1601
        evc_deploy,
1602
        _mock_validate,
1603
        _mongo_controller_upsert_mock,
1604
        mock_use_uni_tags,
1605
        mock_get_unis
1606
    ):
1607
        """Test update a circuit that result in an intra-switch EVC
1608
        with disabled switches or interfaces"""
1609 1
        evc_deploy.return_value = True
1610 1
        _mock_validate.return_value = True
1611 1
        _mongo_controller_upsert_mock.return_value = True
1612 1
        mock_use_uni_tags.return_value = True
1613 1
        self.napp.controller.loop = asyncio.get_running_loop()
1614
        # Interfaces from get_uni_mocked() are disabled
1615 1
        uni_a = get_uni_mocked(
1616
            switch_dpid="00:00:00:00:00:00:00:01",
1617
            switch_id="00:00:00:00:00:00:00:01"
1618
        )
1619 1
        uni_z = get_uni_mocked(
1620
            switch_dpid="00:00:00:00:00:00:00:02",
1621
            switch_id="00:00:00:00:00:00:00:02"
1622
        )
1623 1
        unis = [uni_a, uni_z]
1624 1
        uni_from_dict_mock.side_effect = 2 * unis
1625
1626 1
        evc_payload = {
1627
            "name": "Intra-EVC",
1628
            "dynamic_backup_path": True,
1629
            "uni_a": {
1630
                "tag": {"value": 101, "tag_type": 'vlan'},
1631
                "interface_id": "00:00:00:00:00:00:00:02:2"
1632
            },
1633
            "uni_z": {
1634
                "tag": {"value": 101, "tag_type": 'vlan'},
1635
                "interface_id": "00:00:00:00:00:00:00:01:1"
1636
            }
1637
        }
1638
1639
        # With this update the EVC will be intra-switch
1640 1
        update_payload = {
1641
            "uni_z": {
1642
                "tag": {"value": 101, "tag_type": 'vlan'},
1643
                "interface_id": "00:00:00:00:00:00:00:02:1"
1644
            }
1645
        }
1646
        # Same mocks = intra-switch
1647 1
        mock_get_unis.return_value = [uni_z, uni_z]
1648 1
        response = await self.api_client.post(
1649
            f"{self.base_endpoint}/v2/evc/",
1650
            json=evc_payload,
1651
        )
1652 1
        assert 201 == response.status_code
1653 1
        current_data = response.json()
1654 1
        circuit_id = current_data["circuit_id"]
1655
1656 1
        response = await self.api_client.patch(
1657
            f"{self.base_endpoint}/v2/evc/{circuit_id}",
1658
            json=update_payload,
1659
        )
1660
        assert 409 == response.status_code
1661
        description = "00:00:00:00:00:00:00:02:1 is disabled"
1662
        assert description in response.json()["description"]
1663
1664 1
    def test_link_from_dict_non_existent_intf(self):
1665
        """Test _link_from_dict non existent intf."""
1666 1
        self.napp.controller.get_interface_by_id = MagicMock(return_value=None)
1667 1
        link_dict = {
1668
            "endpoint_a": {"id": "a"},
1669
            "endpoint_b": {"id": "b"}
1670
        }
1671 1
        with pytest.raises(ValueError):
1672 1
            self.napp._link_from_dict(link_dict)
1673
1674 1
    def test_uni_from_dict_non_existent_intf(self):
1675
        """Test _link_from_dict non existent intf."""
1676 1
        self.napp.controller.get_interface_by_id = MagicMock(return_value=None)
1677 1
        uni_dict = {
1678
            "interface_id": "aaa",
1679
        }
1680 1
        with pytest.raises(ValueError):
1681 1
            self.napp._uni_from_dict(uni_dict)
1682
1683 1 View Code Duplication
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1684 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
1685 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
1686 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
1687 1
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
1688 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
1689 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
1690 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1691 1
    async def test_update_evc_no_json_mime(
1692
        self,
1693
        mongo_controller_upsert_mock,
1694
        validate_mock,
1695
        uni_from_dict_mock,
1696
        sched_add_mock,
1697
        evc_deploy_mock,
1698
        mock_use_uni_tags,
1699
        mock_tags_equal,
1700
        mock_check_duplicate
1701
    ):
1702
        """Test update a circuit with wrong mimetype."""
1703 1
        self.napp.controller.loop = asyncio.get_running_loop()
1704 1
        validate_mock.return_value = True
1705 1
        sched_add_mock.return_value = True
1706 1
        evc_deploy_mock.return_value = True
1707 1
        mock_use_uni_tags.return_value = True
1708 1
        mock_tags_equal.return_value = True
1709 1
        mock_check_duplicate.return_value = True
1710 1
        uni1 = create_autospec(UNI)
1711 1
        uni2 = create_autospec(UNI)
1712 1
        uni1.interface = create_autospec(Interface)
1713 1
        uni2.interface = create_autospec(Interface)
1714 1
        uni1.interface.switch = "00:00:00:00:00:00:00:01"
1715 1
        uni2.interface.switch = "00:00:00:00:00:00:00:02"
1716 1
        uni_from_dict_mock.side_effect = [uni1, uni2, uni1, uni2]
1717 1
        mongo_controller_upsert_mock.return_value = True
1718
1719 1
        payload1 = {
1720
            "name": "my evc1",
1721
            "uni_a": {
1722
                "interface_id": "00:00:00:00:00:00:00:01:1",
1723
                "tag": {"tag_type": 'vlan', "value": 80},
1724
            },
1725
            "uni_z": {
1726
                "interface_id": "00:00:00:00:00:00:00:02:2",
1727
                "tag": {"tag_type": 'vlan', "value": 1},
1728
            },
1729
            "dynamic_backup_path": True,
1730
        }
1731
1732 1
        payload2 = {"dynamic_backup_path": False}
1733
1734 1
        response = await self.api_client.post(
1735
            f"{self.base_endpoint}/v2/evc/",
1736
            json=payload1,
1737
        )
1738 1
        assert 201 == response.status_code
1739
1740 1
        current_data = response.json()
1741 1
        circuit_id = current_data["circuit_id"]
1742 1
        response = await self.api_client.patch(
1743
            f"{self.base_endpoint}/v2/evc/{circuit_id}", data=payload2
1744
        )
1745
        current_data = response.json()
1746
        assert 415 == response.status_code
1747
        assert "application/json" in current_data["description"]
1748
1749 1
    async def test_delete_no_evc(self):
1750
        """Test delete when EVC does not exist."""
1751 1
        url = f"{self.base_endpoint}/v2/evc/123"
1752 1
        response = await self.api_client.delete(url)
1753
        current_data = response.json()
1754
        expected_data = "circuit_id 123 not found"
1755
        assert current_data["description"] == expected_data
1756
        assert 404 == response.status_code
1757
1758 1
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
1759 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
1760 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.remove_uni_tags")
1761 1
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
1762 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.remove_current_flows")
1763 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
1764 1
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
1765 1
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
1766 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1767 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
1768 1
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
1769 1
    async def test_delete_archived_evc(
1770
        self,
1771
        evc_as_dict_mock,
1772
        validate_mock,
1773
        mongo_controller_upsert_mock,
1774
        uni_from_dict_mock,
1775
        sched_add_mock,
1776
        evc_deploy_mock,
1777
        remove_current_flows_mock,
1778
        mock_use_uni,
1779
        mock_remove_tags,
1780
        mock_tags_equal,
1781
        mock_check_duplicate
1782
    ):
1783
        """Try to delete an archived EVC"""
1784 1
        self.napp.controller.loop = asyncio.get_running_loop()
1785 1
        validate_mock.return_value = True
1786 1
        mongo_controller_upsert_mock.return_value = True
1787 1
        sched_add_mock.return_value = True
1788 1
        evc_deploy_mock.return_value = True
1789 1
        mock_use_uni.return_value = True
1790 1
        mock_tags_equal.return_value = True
1791 1
        mock_check_duplicate.return_value = True
1792 1
        uni1 = create_autospec(UNI)
1793 1
        uni2 = create_autospec(UNI)
1794 1
        uni1.interface = create_autospec(Interface)
1795 1
        uni2.interface = create_autospec(Interface)
1796 1
        uni1.interface.switch = "00:00:00:00:00:00:00:01"
1797 1
        uni2.interface.switch = "00:00:00:00:00:00:00:02"
1798 1
        uni_from_dict_mock.side_effect = [uni1, uni2, uni1, uni2]
1799
1800 1
        payload1 = {
1801
            "name": "my evc1",
1802
            "uni_a": {
1803
                "interface_id": "00:00:00:00:00:00:00:01:1",
1804
                "tag": {"tag_type": 'vlan', "value": 80},
1805
            },
1806
            "uni_z": {
1807
                "interface_id": "00:00:00:00:00:00:00:02:2",
1808
                "tag": {"tag_type": 'vlan', "value": 1},
1809
            },
1810
            "dynamic_backup_path": True,
1811
        }
1812
1813 1
        evc_as_dict_mock.return_value = payload1
1814 1
        response = await self.api_client.post(
1815
            f"{self.base_endpoint}/v2/evc/",
1816
            json=payload1
1817
        )
1818 1
        assert 201 == response.status_code
1819 1
        assert len(self.napp.circuits) == 1
1820 1
        current_data = response.json()
1821 1
        circuit_id = current_data["circuit_id"]
1822 1
        self.napp.circuits[circuit_id].archive()
1823
1824 1
        response = await self.api_client.delete(
1825
            f"{self.base_endpoint}/v2/evc/{circuit_id}"
1826
        )
1827 1
        assert 200 == response.status_code
1828 1
        assert mock_remove_tags.call_count == 0
1829 1
        assert remove_current_flows_mock.call_count == 0
1830 1
        assert len(self.napp.circuits) == 0
1831
1832 1
        response = await self.api_client.delete(
1833
            f"{self.base_endpoint}/v2/evc/{circuit_id}"
1834
        )
1835
        current_data = response.json()
1836
        expected_data = f"circuit_id {circuit_id} not found"
1837
        assert current_data["description"] == expected_data
1838
        assert 404 == response.status_code
1839
        assert len(self.napp.circuits) == 0
1840
1841 1
    @patch("napps.kytos.mef_eline.main.emit_event")
1842 1
    async def test_delete_circuit(self, emit_event_mock):
1843
        """Test delete_circuit"""
1844 1
        evc = MagicMock()
1845 1
        evc.archived = False
1846 1
        circuit_id = '1'
1847 1
        self.napp.circuits[circuit_id] = evc
1848 1
        response = await self.api_client.delete(
1849
            f"{self.base_endpoint}/v2/evc/{circuit_id}"
1850
        )
1851 1
        assert 200 == response.status_code
1852 1
        assert evc.deactivate.call_count == 1
1853 1
        assert evc.disable.call_count == 1
1854 1
        evc.remove_current_flows.assert_called_once_with(
1855
            sync=False
1856
        )
1857 1
        evc.remove_failover_flows.assert_called_once_with(
1858
            sync=False
1859
        )
1860 1
        assert evc.archive.call_count == 1
1861 1
        assert evc.remove_uni_tags.call_count == 1
1862 1
        assert evc.sync.call_count == 1
1863 1
        assert not self.napp.circuits
1864 1
        assert emit_event_mock.call_count == 1
1865
1866 1
    def test_handle_link_up(self):
1867
        """Test handle_link_up method."""
1868 1
        evc_mock = create_autospec(EVC)
1869 1
        evc_mock.service_level, evc_mock.creation_time = 0, 1
1870 1
        evc_mock.is_enabled = MagicMock(side_effect=[
1871
            True, False, True, True, True
1872
        ])
1873 1
        evc_mock.lock = MagicMock()
1874 1
        evc_mock.archived = False
1875 1
        evcs = [evc_mock, evc_mock, evc_mock]
1876 1
        event = KytosEvent(name="test", content={"link": "abc"})
1877 1
        self.napp.circuits = dict(zip(["1", "2", "3"], evcs))
1878 1
        self.napp.handle_link_up(event)
1879 1
        assert evc_mock.handle_link_up.call_count == 2
1880 1
        evc_mock.handle_link_up.assert_called_with("abc")
1881
1882 1
    @patch("time.sleep", return_value=None)
1883 1
    @patch("napps.kytos.mef_eline.utils.emit_event")
1884 1
    @patch("napps.kytos.mef_eline.main.emit_event")
1885 1
    def test_handle_link_down(
1886
        self, emit_main_mock, emit_utils_mock, _
1887
    ):
1888
        """Test handle_link_down method."""
1889 1
        uni = create_autospec(UNI)
1890 1
        evc1 = MagicMock(id="1", service_level=0, creation_time=1,
1891
                         metadata="mock", _active="true", _enabled="true",
1892
                         uni_a=uni, uni_z=uni)
1893 1
        evc1.name = "name"
1894 1
        evc1.is_affected_by_link.return_value = True
1895 1
        evc1.handle_link_down.return_value = True
1896 1
        evc1.failover_path = None
1897 1
        evc2 = MagicMock(id="2", service_level=6, creation_time=1)
1898 1
        evc2.is_affected_by_link.return_value = False
1899 1
        evc2.is_failover_path_affected_by_link.return_value = True
1900 1
        evc2.as_dict.return_value = {"id": "2"}
1901 1
        evc3 = MagicMock(id="3", service_level=5, creation_time=1,
1902
                         metadata="mock", _active="true", _enabled="true",
1903
                         uni_a=uni, uni_z=uni)
1904 1
        evc3.name = "name"
1905 1
        evc3.is_affected_by_link.return_value = True
1906 1
        evc3.handle_link_down.return_value = True
1907 1
        evc3.failover_path = None
1908 1
        evc4 = MagicMock(id="4", service_level=4, creation_time=1,
1909
                         metadata="mock", _active="true", _enabled="true",
1910
                         uni_a=uni, uni_z=uni)
1911 1
        evc4.name = "name"
1912 1
        evc4.is_affected_by_link.return_value = True
1913 1
        evc4.is_failover_path_affected_by_link.return_value = False
1914 1
        evc4.failover_path = ["2"]
1915 1
        evc4.get_failover_flows.return_value = {
1916
            "2": ["flow1", "flow2"],
1917
            "3": ["flow3", "flow4", "flow5", "flow6"],
1918
        }
1919 1
        evc4.as_dict.return_value = {"id": "4"}
1920 1
        evc5 = MagicMock(id="5", service_level=7, creation_time=1)
1921 1
        evc5.is_affected_by_link.return_value = True
1922 1
        evc5.is_failover_path_affected_by_link.return_value = False
1923 1
        evc5.failover_path = ["3"]
1924 1
        evc5.get_failover_flows.return_value = {
1925
            "4": ["flow7", "flow8"],
1926
            "5": ["flow9", "flow10"],
1927
        }
1928 1
        evc5.as_dict.return_value = {"id": "5"}
1929 1
        evc6 = MagicMock(id="6", service_level=8, creation_time=1,
1930
                         metadata="mock", _active="true", _enabled="true",
1931
                         uni_a=uni, uni_z=uni)
1932 1
        evc6.name = "name"
1933 1
        evc6.is_affected_by_link.return_value = True
1934 1
        evc6.is_failover_path_affected_by_link.return_value = False
1935 1
        evc6.failover_path = ["3"]
1936 1
        evc6.get_failover_flows.side_effect = AttributeError("err")
1937 1
        link = MagicMock(id="123")
1938 1
        event = KytosEvent(name="test", content={"link": link})
1939 1
        self.napp.circuits = {"1": evc1, "2": evc2, "3": evc3, "4": evc4,
1940
                              "5": evc5, "6": evc6}
1941 1
        self.napp.handle_link_down(event)
1942
1943 1
        assert evc5.service_level > evc4.service_level
1944
        # evc5 batched flows should be sent first
1945 1
        emit_utils_mock.assert_has_calls([
1946
            call(
1947
                self.napp.controller,
1948
                context="kytos.flow_manager",
1949
                name="flows.install",
1950
                content={
1951
                    "dpid": "4",
1952
                    "flow_dict": {"flows": ["flow7", "flow8"]},
1953
                    'force': True,
1954
                }
1955
            ),
1956
            call(
1957
                self.napp.controller,
1958
                context="kytos.flow_manager",
1959
                name="flows.install",
1960
                content={
1961
                    "dpid": "5",
1962
                    "flow_dict": {"flows": ["flow9", "flow10"]},
1963
                    'force': True,
1964
                }
1965
            ),
1966
            call(
1967
                self.napp.controller,
1968
                context="kytos.flow_manager",
1969
                name="flows.install",
1970
                content={
1971
                    "dpid": "2",
1972
                    "flow_dict": {"flows": ["flow1", "flow2"]},
1973
                    'force': True,
1974
                }
1975
            ),
1976
            call(
1977
                self.napp.controller,
1978
                context="kytos.flow_manager",
1979
                name="flows.install",
1980
                content={
1981
                    "dpid": "3",
1982
                    "flow_dict": {
1983
                        "flows": ["flow3", "flow4", "flow5", "flow6"]
1984
                    },
1985
                    'force': True,
1986
                }
1987
            )
1988
        ])
1989 1
        event_name = "evc_affected_by_link_down"
1990 1
        assert evc3.service_level > evc1.service_level
1991
        # evc3 should be handled before evc1
1992 1
        emit_main_mock.assert_has_calls([
1993
            call(self.napp.controller, event_name, content={
1994
                "link": link,
1995
                "id": "6",
1996
                "evc_id": "6",
1997
                "name": "name",
1998
                "metadata": "mock",
1999
                "active": "true",
2000
                "enabled": "true",
2001
                "uni_a": uni.as_dict(),
2002
                "uni_z": uni.as_dict(),
2003
            }),
2004
            call(self.napp.controller, event_name, content={
2005
                "link": link,
2006
                "evc_id": "3",
2007
                "id": "3",
2008
                "name": "name",
2009
                "metadata": "mock",
2010
                "active": "true",
2011
                "enabled": "true",
2012
                "uni_a": uni.as_dict(),
2013
                "uni_z": uni.as_dict(),
2014
            }),
2015
            call(self.napp.controller, event_name, content={
2016
                "link": link,
2017
                "evc_id": "1",
2018
                "id": "1",
2019
                "name": "name",
2020
                "metadata": "mock",
2021
                "active": "true",
2022
                "enabled": "true",
2023
                "uni_a": uni.as_dict(),
2024
                "uni_z": uni.as_dict(),
2025
            }),
2026
        ])
2027 1
        self.napp.mongo_controller.update_evcs.assert_called_with(
2028
            [{"id": "5"}, {"id": "4"}, {"id": "2"}]
2029
        )
2030 1
        event_name = "failover_link_down"
2031 1
        assert emit_main_mock.call_args_list[0][0][1] == event_name
2032
2033 1
    @patch("napps.kytos.mef_eline.main.emit_event")
2034 1
    def test_handle_evc_affected_by_link_down(self, emit_event_mock):
2035
        """Test handle_evc_affected_by_link_down method."""
2036 1
        uni = create_autospec(UNI)
2037 1
        evc1 = MagicMock(
2038
            id="1",
2039
            metadata="data_mocked",
2040
            _active="true",
2041
            _enabled="false",
2042
            uni_a=uni,
2043
            uni_z=uni,
2044
        )
2045 1
        evc1.name = "name_mocked"
2046 1
        evc1.handle_link_down.return_value = True
2047 1
        evc2 = MagicMock(
2048
            id="2",
2049
            metadata="mocked_data",
2050
            _active="false",
2051
            _enabled="true",
2052
            uni_a=uni,
2053
            uni_z=uni,
2054
        )
2055 1
        evc2.name = "mocked_name"
2056 1
        evc2.handle_link_down.return_value = False
2057 1
        self.napp.circuits = {"1": evc1, "2": evc2}
2058
2059 1
        event = KytosEvent(name="e1", content={
2060
            "evc_id": "3",
2061
            "link": MagicMock(),
2062
        })
2063 1
        self.napp.handle_evc_affected_by_link_down(event)
2064 1
        emit_event_mock.assert_not_called()
2065 1
        event.content["evc_id"] = "1"
2066 1
        self.napp.handle_evc_affected_by_link_down(event)
2067 1
        emit_event_mock.assert_called_with(
2068
            self.napp.controller, "redeployed_link_down", content={
2069
                "id": "1",
2070
                "evc_id": "1",
2071
                "name": "name_mocked",
2072
                "metadata": "data_mocked",
2073
                "active": "true",
2074
                "enabled": "false",
2075
                "uni_a": uni.as_dict(),
2076
                "uni_z": uni.as_dict(),
2077
            }
2078
        )
2079
2080 1
        event.content["evc_id"] = "2"
2081 1
        self.napp.handle_evc_affected_by_link_down(event)
2082 1
        emit_event_mock.assert_called_with(
2083
            self.napp.controller, "error_redeploy_link_down", content={
2084
                "evc_id": "2",
2085
                "id": "2",
2086
                "name": "mocked_name",
2087
                "metadata": "mocked_data",
2088
                "active": "false",
2089
                "enabled": "true",
2090
                "uni_a": uni.as_dict(),
2091
                "uni_z": uni.as_dict(),
2092
            }
2093
        )
2094
2095 1
    def test_cleanup_evcs_old_path(self, monkeypatch):
2096
        """Test handle_cleanup_evcs_old_path method."""
2097 1
        current_path, map_evc_content, emit_event = [
2098
            MagicMock(), MagicMock(), MagicMock()
2099
        ]
2100 1
        send_flows, merge_flows = MagicMock(), MagicMock()
2101 1
        monkeypatch.setattr(
2102
            "napps.kytos.mef_eline.main.map_evc_event_content",
2103
            map_evc_content
2104
        )
2105 1
        monkeypatch.setattr(
2106
            "napps.kytos.mef_eline.main.emit_event",
2107
            emit_event
2108
        )
2109 1
        monkeypatch.setattr(
2110
            "napps.kytos.mef_eline.main.send_flow_mods_event",
2111
            send_flows
2112
        )
2113 1
        monkeypatch.setattr(
2114
            "napps.kytos.mef_eline.main.merge_flow_dicts",
2115
            merge_flows
2116
        )
2117 1
        merge_flows.return_value = ['1', '2']
2118 1
        evc1 = create_autospec(EVC, id="1", old_path=["1"],
2119
                               current_path=current_path, lock=MagicMock())
2120 1
        evc2 = create_autospec(EVC, id="2", old_path=["2"],
2121
                               current_path=current_path, lock=MagicMock())
2122 1
        evc3 = create_autospec(EVC, id="3", old_path=[],
2123
                               current_path=[], lock=MagicMock())
2124
2125 1
        event = KytosEvent(name="e1", content={"evcs": [evc1, evc2, evc3]})
2126 1
        self.napp.handle_cleanup_evcs_old_path(event)
2127 1
        evc1._prepare_nni_flows.assert_called_with(["1"])
2128 1
        evc1._prepare_uni_flows.assert_called_with(["1"], skip_in=True)
2129 1
        evc2._prepare_nni_flows.assert_called_with(["2"])
2130 1
        evc2._prepare_uni_flows.assert_called_with(["2"], skip_in=True)
2131 1
        evc3._prepare_nni_flows.assert_not_called()
2132 1
        evc3._prepare_uni_flows.assert_not_called()
2133 1
        assert emit_event.call_count == 1
2134 1
        assert emit_event.call_args[0][1] == "failover_old_path"
2135 1
        assert len(emit_event.call_args[1]["content"]) == 2
2136 1
        assert send_flows.call_count == 1
2137 1
        assert send_flows.call_args[0][1] == ['1', '2']
2138 1
        assert send_flows.call_args[0][2] == 'delete'
2139
2140 1
    async def test_add_metadata(self):
2141
        """Test method to add metadata"""
2142 1
        self.napp.controller.loop = asyncio.get_running_loop()
2143 1
        evc_mock = create_autospec(EVC)
2144 1
        evc_mock.metadata = {}
2145 1
        evc_mock.id = 1234
2146 1
        self.napp.circuits = {"1234": evc_mock}
2147
2148 1
        payload = {"metadata1": 1, "metadata2": 2}
2149 1
        response = await self.api_client.post(
2150
            f"{self.base_endpoint}/v2/evc/1234/metadata",
2151
            json=payload
2152
        )
2153
2154 1
        assert response.status_code == 201
2155 1
        evc_mock.extend_metadata.assert_called_with(payload)
2156
2157 1
    async def test_add_metadata_malformed_json(self):
2158
        """Test method to add metadata with a malformed json"""
2159 1
        self.napp.controller.loop = asyncio.get_running_loop()
2160 1
        payload = b'{"metadata1": 1, "metadata2": 2,}'
2161 1
        response = await self.api_client.post(
2162
            f"{self.base_endpoint}/v2/evc/1234/metadata",
2163
            content=payload,
2164
            headers={"Content-Type": "application/json"}
2165
        )
2166
2167
        assert response.status_code == 400
2168
        assert "body contains invalid API" in response.json()["description"]
2169
2170 1
    async def test_add_metadata_no_body(self):
2171
        """Test method to add metadata with no body"""
2172 1
        self.napp.controller.loop = asyncio.get_running_loop()
2173 1
        response = await self.api_client.post(
2174
            f"{self.base_endpoint}/v2/evc/1234/metadata"
2175
        )
2176
        assert response.status_code == 400
2177
        assert response.json()["description"] == \
2178
            "Missing required request body"
2179
2180 1
    async def test_add_metadata_no_evc(self):
2181
        """Test method to add metadata with no evc"""
2182 1
        self.napp.controller.loop = asyncio.get_running_loop()
2183 1
        payload = {"metadata1": 1, "metadata2": 2}
2184 1
        response = await self.api_client.post(
2185
            f"{self.base_endpoint}/v2/evc/1234/metadata",
2186
            json=payload,
2187
        )
2188
        assert response.status_code == 404
2189
        assert response.json()["description"] == \
2190
            "circuit_id 1234 not found."
2191
2192 1
    async def test_add_metadata_wrong_content_type(self):
2193
        """Test method to add metadata with wrong content type"""
2194 1
        self.napp.controller.loop = asyncio.get_running_loop()
2195 1
        payload = {"metadata1": 1, "metadata2": 2}
2196 1
        response = await self.api_client.post(
2197
            f"{self.base_endpoint}/v2/evc/1234/metadata",
2198
            data=payload,
2199
            headers={"Content-Type": "application/xml"}
2200
        )
2201
        assert response.status_code == 415
2202
        assert "application/xml" in response.json()["description"]
2203
2204 1
    async def test_get_metadata(self):
2205
        """Test method to get metadata"""
2206 1
        evc_mock = create_autospec(EVC)
2207 1
        evc_mock.metadata = {'metadata1': 1, 'metadata2': 2}
2208 1
        evc_mock.id = 1234
2209 1
        self.napp.circuits = {"1234": evc_mock}
2210
2211 1
        response = await self.api_client.get(
2212
            f"{self.base_endpoint}/v2/evc/1234/metadata",
2213
        )
2214 1
        assert response.status_code == 200
2215 1
        assert response.json() == {"metadata": evc_mock.metadata}
2216
2217 1
    async def test_delete_metadata(self):
2218
        """Test method to delete metadata"""
2219 1
        evc_mock = create_autospec(EVC)
2220 1
        evc_mock.metadata = {'metadata1': 1, 'metadata2': 2}
2221 1
        evc_mock.id = 1234
2222 1
        self.napp.circuits = {"1234": evc_mock}
2223
2224 1
        response = await self.api_client.delete(
2225
            f"{self.base_endpoint}/v2/evc/1234/metadata/metadata1",
2226
        )
2227 1
        assert response.status_code == 200
2228
2229 1
    async def test_delete_metadata_no_evc(self):
2230
        """Test method to delete metadata with no evc"""
2231 1
        response = await self.api_client.delete(
2232
            f"{self.base_endpoint}/v2/evc/1234/metadata/metadata1",
2233
        )
2234
        assert response.status_code == 404
2235
        assert response.json()["description"] == \
2236
            "circuit_id 1234 not found."
2237
2238 1
    @patch('napps.kytos.mef_eline.main.Main._load_evc')
2239 1
    def test_load_all_evcs(self, load_evc_mock):
2240
        """Test load_evcs method"""
2241 1
        mock_circuits = {
2242
            'circuits': {
2243
                1: 'circuit_1',
2244
                2: 'circuit_2',
2245
                3: 'circuit_3',
2246
                4: 'circuit_4'
2247
            }
2248
        }
2249 1
        self.napp.mongo_controller.get_circuits.return_value = mock_circuits
2250 1
        self.napp.circuits = {2: 'circuit_2', 3: 'circuit_3'}
2251 1
        self.napp.load_all_evcs()
2252 1
        load_evc_mock.assert_has_calls([call('circuit_1'), call('circuit_4')])
2253 1
        assert self.napp.controller.buffers.app.put.call_count > 1
2254 1
        call_args = self.napp.controller.buffers.app.put.call_args[0]
2255 1
        assert call_args[0].name == "kytos/mef_eline.evcs_loaded"
2256 1
        assert dict(call_args[0].content) == mock_circuits["circuits"]
2257 1
        timeout_d = {"timeout": 1}
2258 1
        assert self.napp.controller.buffers.app.put.call_args[1] == timeout_d
2259
2260 1
    @patch('napps.kytos.mef_eline.main.Main._evc_from_dict')
2261 1
    def test_load_evc(self, evc_from_dict_mock):
2262
        """Test _load_evc method"""
2263
        # pylint: disable=protected-access
2264
        # case 1: early return with ValueError exception
2265 1
        evc_from_dict_mock.side_effect = ValueError("err")
2266 1
        evc_dict = MagicMock()
2267 1
        assert not self.napp._load_evc(evc_dict)
2268
2269
        # case 2: early return with KytosTagError exception
2270 1
        evc_from_dict_mock.side_effect = KytosTagError("")
2271 1
        assert not self.napp._load_evc(evc_dict)
2272
2273
        # case 3: archived evc
2274 1
        evc = MagicMock()
2275 1
        evc.archived = True
2276 1
        evc_from_dict_mock.side_effect = None
2277 1
        evc_from_dict_mock.return_value = evc
2278 1
        assert not self.napp._load_evc(evc_dict)
2279
2280
        # case 4: success creating
2281 1
        evc.archived = False
2282 1
        evc.id = 1
2283 1
        self.napp.sched = MagicMock()
2284
2285 1
        result = self.napp._load_evc(evc_dict)
2286 1
        assert result == evc
2287 1
        self.napp.sched.add.assert_called_with(evc)
2288 1
        assert self.napp.circuits[1] == evc
2289
2290 1
    def test_handle_flow_mod_error(self):
2291
        """Test handle_flow_mod_error method"""
2292 1
        flow = MagicMock()
2293 1
        flow.cookie = 0xaa00000000000011
2294 1
        event = MagicMock()
2295 1
        event.content = {'flow': flow, 'error_command': 'add'}
2296 1
        evc = create_autospec(EVC)
2297 1
        evc.remove_current_flows = MagicMock()
2298 1
        evc.lock = MagicMock()
2299 1
        self.napp.circuits = {"00000000000011": evc}
2300 1
        self.napp.handle_flow_mod_error(event)
2301 1
        evc.remove_current_flows.assert_called_once()
2302
2303 1
    @patch("kytos.core.Controller.get_interface_by_id")
2304 1
    def test_uni_from_dict(self, _get_interface_by_id_mock):
2305
        """Test _uni_from_dict method."""
2306
        # pylint: disable=protected-access
2307
        # case1: early return on empty dict
2308 1
        assert not self.napp._uni_from_dict(None)
2309
2310
        # case2: invalid interface raises ValueError
2311 1
        _get_interface_by_id_mock.return_value = None
2312 1
        uni_dict = {
2313
            "interface_id": "00:01:1",
2314
            "tag": {"tag_type": 'vlan', "value": 81},
2315
        }
2316 1
        with pytest.raises(ValueError):
2317 1
            self.napp._uni_from_dict(uni_dict)
2318
2319
        # case3: success creation
2320 1
        uni_mock = get_uni_mocked(switch_id="00:01")
2321 1
        _get_interface_by_id_mock.return_value = uni_mock.interface
2322 1
        uni = self.napp._uni_from_dict(uni_dict)
2323 1
        assert uni == uni_mock
2324
2325
        # case4: success creation of tag list
2326 1
        uni_dict["tag"]["value"] = [[1, 10]]
2327 1
        uni = self.napp._uni_from_dict(uni_dict)
2328 1
        assert isinstance(uni.user_tag, TAGRange)
2329
2330
        # case5: success creation without tag
2331 1
        uni_mock.user_tag = None
2332 1
        del uni_dict["tag"]
2333 1
        uni = self.napp._uni_from_dict(uni_dict)
2334 1
        assert uni == uni_mock
2335
2336 1
    def test_handle_flow_delete(self):
2337
        """Test handle_flow_delete method"""
2338 1
        flow = MagicMock()
2339 1
        flow.cookie = 0xaa00000000000011
2340 1
        event = MagicMock()
2341 1
        event.content = {'flow': flow}
2342 1
        evc = create_autospec(EVC)
2343 1
        evc.set_flow_removed_at = MagicMock()
2344 1
        self.napp.circuits = {"00000000000011": evc}
2345 1
        self.napp.handle_flow_delete(event)
2346 1
        evc.set_flow_removed_at.assert_called_once()
2347
2348 1 View Code Duplication
    async def test_add_bulk_metadata(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2349
        """Test add_bulk_metadata method"""
2350 1
        self.napp.controller.loop = asyncio.get_running_loop()
2351 1
        evc_mock = create_autospec(EVC)
2352 1
        evc_mock.id = 1234
2353 1
        self.napp.circuits = {"1234": evc_mock}
2354 1
        payload = {
2355
            "circuit_ids": ["1234"],
2356
            "metadata1": 1,
2357
            "metadata2": 2
2358
        }
2359 1
        response = await self.api_client.post(
2360
            f"{self.base_endpoint}/v2/evc/metadata",
2361
            json=payload
2362
        )
2363 1
        assert response.status_code == 201
2364 1
        args = self.napp.mongo_controller.update_evcs_metadata.call_args[0]
2365 1
        ids = payload.pop("circuit_ids")
2366 1
        assert args[0] == ids
2367 1
        assert args[1] == payload
2368 1
        assert args[2] == "add"
2369 1
        calls = self.napp.mongo_controller.update_evcs_metadata.call_count
2370 1
        assert calls == 1
2371 1
        evc_mock.extend_metadata.assert_called_with(payload)
2372
2373 1
    async def test_add_bulk_metadata_empty_list(self):
2374
        """Test add_bulk_metadata method empty list"""
2375 1
        self.napp.controller.loop = asyncio.get_running_loop()
2376 1
        evc_mock = create_autospec(EVC)
2377 1
        evc_mock.id = 1234
2378 1
        self.napp.circuits = {"1234": evc_mock}
2379 1
        payload = {
2380
            "circuit_ids": [],
2381
            "metadata1": 1,
2382
            "metadata2": 2
2383
        }
2384 1
        response = await self.api_client.post(
2385
            f"{self.base_endpoint}/v2/evc/metadata",
2386
            json=payload
2387
        )
2388
        assert response.status_code == 400
2389
        assert "invalid" in response.json()["description"]
2390
2391 1
    async def test_add_bulk_metadata_no_id(self):
2392
        """Test add_bulk_metadata with unknown evc id"""
2393 1
        self.napp.controller.loop = asyncio.get_running_loop()
2394 1
        evc_mock = create_autospec(EVC)
2395 1
        evc_mock.id = 1234
2396 1
        self.napp.circuits = {"1234": evc_mock}
2397 1
        payload = {
2398
            "circuit_ids": ["1234", "4567"]
2399
        }
2400 1
        response = await self.api_client.post(
2401
            f"{self.base_endpoint}/v2/evc/metadata",
2402
            json=payload
2403
        )
2404
        assert response.status_code == 404
2405
2406 1
    async def test_add_bulk_metadata_no_circuits(self):
2407
        """Test add_bulk_metadata without circuit_ids"""
2408 1
        self.napp.controller.loop = asyncio.get_running_loop()
2409 1
        evc_mock = create_autospec(EVC)
2410 1
        evc_mock.id = 1234
2411 1
        self.napp.circuits = {"1234": evc_mock}
2412 1
        payload = {
2413
            "metadata": "data"
2414
        }
2415 1
        response = await self.api_client.post(
2416
            f"{self.base_endpoint}/v2/evc/metadata",
2417
            json=payload
2418
        )
2419
        assert response.status_code == 400
2420
2421 1 View Code Duplication
    async def test_delete_bulk_metadata(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2422
        """Test delete_metadata method"""
2423 1
        self.napp.controller.loop = asyncio.get_running_loop()
2424 1
        evc_mock = create_autospec(EVC)
2425 1
        evc_mock.id = 1234
2426 1
        self.napp.circuits = {"1234": evc_mock}
2427 1
        payload = {
2428
            "circuit_ids": ["1234"]
2429
        }
2430 1
        response = await self.api_client.request(
2431
            "DELETE",
2432
            f"{self.base_endpoint}/v2/evc/metadata/metadata1",
2433
            json=payload
2434
        )
2435 1
        assert response.status_code == 200
2436 1
        args = self.napp.mongo_controller.update_evcs_metadata.call_args[0]
2437 1
        assert args[0] == payload["circuit_ids"]
2438 1
        assert args[1] == {"metadata1": ""}
2439 1
        assert args[2] == "del"
2440 1
        calls = self.napp.mongo_controller.update_evcs_metadata.call_count
2441 1
        assert calls == 1
2442 1
        assert evc_mock.remove_metadata.call_count == 1
2443
2444 1
    async def test_delete_bulk_metadata_error(self):
2445
        """Test bulk_delete_metadata with ciruit erroring"""
2446 1
        self.napp.controller.loop = asyncio.get_running_loop()
2447 1
        evc_mock = create_autospec(EVC)
2448 1
        evcs = [evc_mock, evc_mock]
2449 1
        self.napp.circuits = dict(zip(["1", "2"], evcs))
2450 1
        payload = {"circuit_ids": ["1", "2", "3"]}
2451 1
        response = await self.api_client.request(
2452
            "DELETE",
2453
            f"{self.base_endpoint}/v2/evc/metadata/metadata1",
2454
            json=payload
2455
        )
2456
        assert response.status_code == 404, response.data
2457
        assert response.json()["description"] == ["3"]
2458
2459 1
    async def test_use_uni_tags(self):
2460
        """Test _use_uni_tags"""
2461 1
        self.napp.controller.loop = asyncio.get_running_loop()
2462 1
        evc_mock = create_autospec(EVC)
2463 1
        evc_mock.uni_a = "uni_a_mock"
2464 1
        evc_mock.uni_z = "uni_z_mock"
2465 1
        self.napp._use_uni_tags(evc_mock)
2466 1
        assert evc_mock._use_uni_vlan.call_count == 2
2467 1
        assert evc_mock._use_uni_vlan.call_args[0][0] == evc_mock.uni_z
2468
2469
        # One UNI tag is not available
2470 1
        evc_mock._use_uni_vlan.side_effect = [KytosTagError(""), None]
2471 1
        with pytest.raises(KytosTagError):
2472 1
            self.napp._use_uni_tags(evc_mock)
2473 1
        assert evc_mock._use_uni_vlan.call_count == 3
2474 1
        assert evc_mock.make_uni_vlan_available.call_count == 0
2475
2476 1
        evc_mock._use_uni_vlan.side_effect = [None, KytosTagError("")]
2477 1
        with pytest.raises(KytosTagError):
2478 1
            self.napp._use_uni_tags(evc_mock)
2479 1
        assert evc_mock._use_uni_vlan.call_count == 5
2480 1
        assert evc_mock.make_uni_vlan_available.call_count == 1
2481
2482 1
    def test_check_no_tag_duplication(self):
2483
        """Test _check_no_tag_duplication"""
2484 1
        evc = MagicMock()
2485 1
        evc.check_no_tag_duplicate = MagicMock()
2486 1
        evc.archived = False
2487 1
        evc.id = "1"
2488 1
        self.napp.circuits = {"1": evc}
2489 1
        evc_id = "2"
2490 1
        uni_a = get_uni_mocked(valid=True)
2491 1
        uni_z = get_uni_mocked(valid=True)
2492 1
        self.napp._check_no_tag_duplication(evc_id, uni_a, uni_z)
2493 1
        assert evc.check_no_tag_duplicate.call_count == 0
2494
2495 1
        uni_a.user_tag = None
2496 1
        uni_z.user_tag = None
2497 1
        self.napp._check_no_tag_duplication(evc_id, uni_a, uni_z)
2498 1
        assert evc.check_no_tag_duplicate.call_count == 2
2499
2500 1
        self.napp._check_no_tag_duplication(evc_id, uni_a, None)
2501 1
        assert evc.check_no_tag_duplicate.call_count == 3
2502
2503 1
        self.napp._check_no_tag_duplication(evc_id, None, None)
2504 1
        assert evc.check_no_tag_duplicate.call_count == 3
2505
2506 1
    @patch("napps.kytos.mef_eline.main.time")
2507 1
    @patch("napps.kytos.mef_eline.main.Main.handle_interface_link_up")
2508 1
    @patch("napps.kytos.mef_eline.main.Main.handle_interface_link_down")
2509 1
    def test_handle_on_interface_link_change(
2510
        self,
2511
        mock_down,
2512
        mock_up,
2513
        mock_time
2514
    ):
2515
        """Test handle_on_interface_link_change"""
2516 1
        mock_time.sleep.return_value = True
2517 1
        mock_intf = Mock()
2518 1
        mock_intf.id = "mock_intf"
2519
2520
        # Created/link_up
2521 1
        name = '.*.switch.interface.created'
2522 1
        content = {"interface": mock_intf}
2523 1
        event = KytosEvent(name=name, content=content)
2524 1
        self.napp.handle_on_interface_link_change(event)
2525 1
        assert mock_down.call_count == 0
2526 1
        assert mock_up.call_count == 1
2527
2528
        # Deleted/link_down
2529 1
        name = '.*.switch.interface.deleted'
2530 1
        event = KytosEvent(name=name, content=content)
2531 1
        self.napp.handle_on_interface_link_change(event)
2532 1
        assert mock_down.call_count == 1
2533 1
        assert mock_up.call_count == 1
2534
2535
        # Event delay
2536 1
        self.napp._intf_events[mock_intf.id]["last_acquired"] = "mock_time"
2537 1
        for _ in range(1, 6):
2538 1
            self.napp.handle_on_interface_link_change(event)
2539 1
        assert mock_down.call_count == 1
2540 1
        assert mock_up.call_count == 1
2541
2542 1
        self.napp._intf_events[mock_intf.id].pop("last_acquired")
2543 1
        self.napp.handle_on_interface_link_change(event)
2544 1
        assert mock_down.call_count == 2
2545 1
        assert mock_up.call_count == 1
2546
2547
        # Out of order event
2548 1
        event = KytosEvent(name=name, content=content)
2549 1
        self.napp._intf_events[mock_intf.id]["event"] = Mock(timestamp=now())
2550
2551 1
        self.napp.handle_on_interface_link_change(event)
2552 1
        assert mock_down.call_count == 2
2553
        assert mock_up.call_count == 1
2554