Skip to content

Commit

Permalink
Always deploy EVC if UNIs are in the same switch (#218)
Browse files Browse the repository at this point in the history
* Always deploy EVC if UNIs are in the same switch
* Change tests to reflect the use of the 'switch' in deploy method
  • Loading branch information
Antonio Francisco authored Feb 18, 2021
1 parent 32c10f9 commit ececab6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
3 changes: 2 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ def deploy_to_backup_path(self):
if success:
return True

if self.dynamic_backup_path:
if self.dynamic_backup_path or \
self.uni_a.interface.switch == self.uni_z.interface.switch:
return self.deploy_to_path()

return False
Expand Down
38 changes: 37 additions & 1 deletion tests/unit/models/test_evc_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,42 @@ def test_deploy_fail(self, *args):
self.assertEqual(sync_mock.call_count, 1)
self.assertFalse(deployed)

@patch('napps.kytos.mef_eline.models.EVC.deploy_to_path')
@patch('napps.kytos.mef_eline.models.EVC.discover_new_paths')
def test_deploy_to_backup_path1(self, discover_new_paths_mocked,
deploy_to_path_mocked):
"""Test deployment when dynamic_backup_path is False in same switch"""
uni_a = get_uni_mocked(interface_port=2, tag_value=82,
is_valid=True)
uni_z = get_uni_mocked(interface_port=3, tag_value=83,
is_valid=True)

switch = Mock(spec=Switch)
uni_a.interface.switch = switch
uni_z.interface.switch = switch

attributes = {
"controller": get_controller_mock(),
"name": "custom_name",
"uni_a": uni_a,
"uni_z": uni_z,
"enabled": True,
"dynamic_backup_path": False
}

evc = EVC(**attributes)
discover_new_paths_mocked.return_value = []
deploy_to_path_mocked.return_value = True

# storehouse initialization mock
evc._storehouse.box = Mock() # pylint: disable=protected-access
evc._storehouse.box.data = {} # pylint: disable=protected-access

deployed = evc.deploy_to_backup_path()

deploy_to_path_mocked.assert_called_once_with()
self.assertEqual(deployed, True)

@patch('requests.post')
@patch('napps.kytos.mef_eline.storehouse.StoreHouse.save_evc')
@patch('napps.kytos.mef_eline.models.log')
Expand Down Expand Up @@ -498,7 +534,7 @@ def test_deploy_without_path_case1(self, *args):
"uni_a": uni_a,
"uni_z": uni_z,
"enabled": True,
"dynamic_backup_path": True
"dynamic_backup_path": False
}

dynamic_backup_path = Path([
Expand Down
45 changes: 29 additions & 16 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,19 @@ def test_circuit_with_invalid_id(self, storehouse_data_mock):
@patch('napps.kytos.mef_eline.models.EVC._validate')
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

validate_mock.return_value = True
save_evc_mock.return_value = True
uni_from_dict_mock.side_effect = ['uni_a', 'uni_z']
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]
evc_as_dict_mock.return_value = {}
sched_add_mock.return_value = True
storehouse_data_mock.return_value = {}
Expand Down Expand Up @@ -360,8 +367,8 @@ def test_create_a_circuit_case_1(self, *args):
validate_mock.assert_called_once()
validate_mock.assert_called_with(frequency='* * * * *',
name='my evc1',
uni_a='uni_a',
uni_z='uni_z')
uni_a=uni1,
uni_z=uni2)
# verify save method is called
save_evc_mock.assert_called_once()

Expand Down Expand Up @@ -426,11 +433,16 @@ def test_create_circuit_already_enabled(self, *args):
validate_mock.return_value = True
save_evc_mock.return_value = True
sched_add_mock.return_value = True
uni_from_dict_mock.side_effect = ['uni_a', 'uni_z', 'uni_a', 'uni_z']
payload1 = {'name': 'circuit_1'}
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]

api = self.get_app_test_client(self.napp)
payload2 = {
payload = {
"name": "my evc1",
"uni_a": {
"interface_id": "00:00:00:00:00:00:00:01:1",
Expand All @@ -448,20 +460,14 @@ def test_create_circuit_already_enabled(self, *args):
}
}

evc_as_dict_mock.return_value = payload1
response = api.post(f'{self.server_name_url}/v2/evc/',
data=json.dumps(payload1),
content_type='application/json')
self.assertEqual(201, response.status_code)

evc_as_dict_mock.return_value = payload2
evc_as_dict_mock.return_value = payload
response = api.post(f'{self.server_name_url}/v2/evc/',
data=json.dumps(payload2),
data=json.dumps(payload),
content_type='application/json')
self.assertEqual(201, response.status_code)

response = api.post(f'{self.server_name_url}/v2/evc/',
data=json.dumps(payload2),
data=json.dumps(payload),
content_type='application/json')
current_data = json.loads(response.data)
expected_data = 'The EVC already exists.'
Expand Down Expand Up @@ -1212,13 +1218,20 @@ def test_update_circuit(self, *args):
@patch('napps.kytos.mef_eline.main.EVC.as_dict')
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

validate_mock.return_value = True
save_evc_mock.return_value = True
sched_add_mock.return_value = True
uni_from_dict_mock.side_effect = ['uni_a', 'uni_z', 'uni_a', 'uni_z']
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]

api = self.get_app_test_client(self.napp)
payload1 = {
Expand Down

0 comments on commit ececab6

Please sign in to comment.