Skip to content

Commit

Permalink
Force either primary path or dynamic path on EVC
Browse files Browse the repository at this point in the history
When creating an EVC, either a primary path is defined
or dynamic path is allowed.
Fix kytos#49
  • Loading branch information
ajoaoff committed Aug 17, 2021
1 parent 4aff4ff commit c1bd2fb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
81 changes: 69 additions & 12 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,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')
Expand All @@ -314,10 +315,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)
Expand Down Expand Up @@ -347,7 +350,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),
Expand All @@ -368,7 +372,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()

Expand Down Expand Up @@ -470,19 +475,22 @@ 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')
@patch('napps.kytos.mef_eline.models.EVC._validate')
@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)
Expand All @@ -507,7 +515,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
Expand All @@ -524,6 +533,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 = {
Expand Down Expand Up @@ -1161,6 +1210,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()
Expand Down Expand Up @@ -1191,7 +1241,8 @@ def test_update_circuit(self, *args):
"tag_type": 1,
"value": 1
}
}
},
"dynamic_backup_path": True
},
{
"primary_path": [
Expand Down Expand Up @@ -1263,6 +1314,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')
Expand All @@ -1272,11 +1324,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)
Expand All @@ -1301,11 +1354,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
Expand All @@ -1325,6 +1379,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')
Expand All @@ -1334,11 +1389,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)
Expand All @@ -1363,11 +1419,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
Expand Down

0 comments on commit c1bd2fb

Please sign in to comment.