diff --git a/main.py b/main.py index 3390a864..3e6a8340 100644 --- a/main.py +++ b/main.py @@ -154,6 +154,11 @@ def create_circuit(self): log.debug('create_circuit result %s %s', result, 409) raise Conflict(result) + if not evc.primary_path and evc.dynamic_backup_path is False: + result = "The EVC must have a primary path or allow dynamic paths." + log.debug('create_circuit result %s %s', result, 400) + raise BadRequest(result) + # store circuit in dictionary self.circuits[evc.id] = evc diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 665bc33e..22b97179 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -307,6 +307,7 @@ def test_circuit_with_invalid_id(self, storehouse_data_mock): self.assertEqual(json.loads(response.data)['description'], expected_result) + @patch('napps.kytos.mef_eline.models.EVC.deploy') @patch('napps.kytos.mef_eline.storehouse.StoreHouse.get_data') @patch('napps.kytos.mef_eline.scheduler.Scheduler.add') @patch('napps.kytos.mef_eline.main.Main._uni_from_dict') @@ -317,10 +318,12 @@ def test_create_a_circuit_case_1(self, *args): """Test create a new circuit.""" # pylint: disable=too-many-locals (validate_mock, evc_as_dict_mock, save_evc_mock, - uni_from_dict_mock, sched_add_mock, storehouse_data_mock) = args + uni_from_dict_mock, sched_add_mock, storehouse_data_mock, + evc_deploy_mock) = args validate_mock.return_value = True save_evc_mock.return_value = True + evc_deploy_mock.return_value = True uni1 = create_autospec(UNI) uni2 = create_autospec(UNI) uni1.interface = create_autospec(Interface) @@ -350,7 +353,8 @@ def test_create_a_circuit_case_1(self, *args): "tag_type": 1, "value": 1 } - } + }, + "dynamic_backup_path": True } response = api.post(url, data=json.dumps(payload), @@ -371,7 +375,8 @@ def test_create_a_circuit_case_1(self, *args): validate_mock.assert_called_with(frequency='* * * * *', name='my evc1', uni_a=uni1, - uni_z=uni2) + uni_z=uni2, + dynamic_backup_path=True) # verify save method is called save_evc_mock.assert_called_once() @@ -473,6 +478,7 @@ def test_create_a_circuit_case_4(self, uni_from_dict_mock): self.assertEqual(400, response.status_code, response.data) self.assertEqual(current_data['description'], expected_data) + @patch('napps.kytos.mef_eline.models.EVC.deploy') @patch('napps.kytos.mef_eline.scheduler.Scheduler.add') @patch('napps.kytos.mef_eline.main.Main._uni_from_dict') @patch('napps.kytos.mef_eline.storehouse.StoreHouse.save_evc') @@ -480,12 +486,14 @@ def test_create_a_circuit_case_4(self, uni_from_dict_mock): @patch('napps.kytos.mef_eline.main.EVC.as_dict') def test_create_circuit_already_enabled(self, *args): """Test create an already created circuit.""" + # pylint: disable=too-many-locals (evc_as_dict_mock, validate_mock, save_evc_mock, - uni_from_dict_mock, sched_add_mock) = args + uni_from_dict_mock, sched_add_mock, evc_deploy_mock) = args validate_mock.return_value = True save_evc_mock.return_value = True sched_add_mock.return_value = True + evc_deploy_mock.return_value = True uni1 = create_autospec(UNI) uni2 = create_autospec(UNI) uni1.interface = create_autospec(Interface) @@ -510,7 +518,8 @@ def test_create_circuit_already_enabled(self, *args): "tag_type": 1, "value": 1 } - } + }, + "dynamic_backup_path": True } evc_as_dict_mock.return_value = payload @@ -527,6 +536,46 @@ def test_create_circuit_already_enabled(self, *args): self.assertEqual(current_data['description'], expected_data) self.assertEqual(409, response.status_code) + @patch('napps.kytos.mef_eline.main.Main._uni_from_dict') + def test_create_circuit_case_5(self, uni_from_dict_mock): + """Test when neither primary path nor dynamic_backup_path is set.""" + api = self.get_app_test_client(self.napp) + url = f'{self.server_name_url}/v2/evc/' + uni1 = create_autospec(UNI) + uni2 = create_autospec(UNI) + uni1.interface = create_autospec(Interface) + uni2.interface = create_autospec(Interface) + uni1.interface.switch = '00:00:00:00:00:00:00:01' + uni2.interface.switch = '00:00:00:00:00:00:00:02' + uni_from_dict_mock.side_effect = [uni1, uni2, uni1, uni2] + + payload = { + "name": "my evc1", + "frequency": "* * * * *", + "uni_a": { + "interface_id": "00:00:00:00:00:00:00:01:1", + "tag": { + "tag_type": 1, + "value": 80 + } + }, + "uni_z": { + "interface_id": "00:00:00:00:00:00:00:02:2", + "tag": { + "tag_type": 1, + "value": 1 + } + } + } + + response = api.post(url, data=json.dumps(payload), + content_type='application/json') + current_data = json.loads(response.data) + expected = 'The EVC must have a primary path or allow dynamic paths.' + + self.assertEqual(400, response.status_code, response.data) + self.assertEqual(current_data['description'], expected) + def test_load_circuits_by_interface(self): """Test if existing circuits are correctly loaded to the cache.""" stored_circuits = { @@ -1197,6 +1246,7 @@ def test_delete_schedule_archived(self, *args): @patch('napps.kytos.mef_eline.main.EVC.as_dict') def test_update_circuit(self, *args): """Test update a circuit circuit.""" + # pylint: disable=too-many-locals (evc_as_dict_mock, uni_from_dict_mock, evc_deploy, *mocks, requests_mock) = args response = MagicMock() @@ -1227,7 +1277,8 @@ def test_update_circuit(self, *args): "tag_type": 1, "value": 1 } - } + }, + "dynamic_backup_path": True }, { "primary_path": [ @@ -1299,6 +1350,7 @@ def test_update_circuit(self, *args): evc_deploy.assert_not_called() self.assertEqual(405, response.status_code) + @patch('napps.kytos.mef_eline.models.EVC.deploy') @patch('napps.kytos.mef_eline.scheduler.Scheduler.add') @patch('napps.kytos.mef_eline.main.Main._uni_from_dict') @patch('napps.kytos.mef_eline.storehouse.StoreHouse.save_evc') @@ -1308,11 +1360,12 @@ def test_update_circuit_invalid_json(self, *args): """Test update a circuit circuit.""" # pylint: disable=too-many-locals (evc_as_dict_mock, validate_mock, save_evc_mock, - uni_from_dict_mock, sched_add_mock) = args + uni_from_dict_mock, sched_add_mock, evc_deploy_mock) = args validate_mock.return_value = True save_evc_mock.return_value = True sched_add_mock.return_value = True + evc_deploy_mock.return_value = True uni1 = create_autospec(UNI) uni2 = create_autospec(UNI) uni1.interface = create_autospec(Interface) @@ -1337,11 +1390,12 @@ def test_update_circuit_invalid_json(self, *args): "tag_type": 1, "value": 1 } - } + }, + "dynamic_backup_path": True } payload2 = { - "dynamic_backup_path": True, + "dynamic_backup_path": False, } evc_as_dict_mock.return_value = payload1 @@ -1361,6 +1415,7 @@ def test_update_circuit_invalid_json(self, *args): self.assertEqual(current_data['description'], expected_data) self.assertEqual(400, response.status_code) + @patch('napps.kytos.mef_eline.models.EVC.deploy') @patch('napps.kytos.mef_eline.scheduler.Scheduler.add') @patch('napps.kytos.mef_eline.main.Main._uni_from_dict') @patch('napps.kytos.mef_eline.storehouse.StoreHouse.save_evc') @@ -1370,11 +1425,12 @@ def test_update_evc_no_json_mime(self, *args): """Test update a circuit with wrong mimetype.""" # pylint: disable=too-many-locals (evc_as_dict_mock, validate_mock, save_evc_mock, - uni_from_dict_mock, sched_add_mock) = args + uni_from_dict_mock, sched_add_mock, evc_deploy_mock) = args validate_mock.return_value = True save_evc_mock.return_value = True sched_add_mock.return_value = True + evc_deploy_mock.return_value = True uni1 = create_autospec(UNI) uni2 = create_autospec(UNI) uni1.interface = create_autospec(Interface) @@ -1399,11 +1455,12 @@ def test_update_evc_no_json_mime(self, *args): "tag_type": 1, "value": 1 } - } + }, + "dynamic_backup_path": True } payload2 = { - "dynamic_backup_path": True + "dynamic_backup_path": False } evc_as_dict_mock.return_value = payload1