def test_add_switch_metadata(self, mock_metadata_changes): """Test add_switch_metadata.""" dpid = "00:00:00:00:00:00:00:01" mock_switch = get_switch_mock(dpid) self.napp.controller.switches = {dpid: mock_switch} api = get_test_client(self.napp.controller, self.napp) payload = {"data": "A"} url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' response = api.post(url, data=json.dumps(payload), content_type='application/json') self.assertEqual(response.status_code, 201, response.data) mock_metadata_changes.assert_called() # fail case dpid = "00:00:00:00:00:00:00:02" url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' response = api.post(url, data=json.dumps(payload), content_type='application/json') self.assertEqual(response.status_code, 404, response.data)
def test_rest_list_with_dpid(self): """Test list rest method with dpid.""" flow_dict = { "priority": 13, "cookie": 84114964, "command": "add", "match": { "dl_dst": "00:15:af:d5:38:98" }, } flow_1 = MagicMock() flow_1.as_dict.return_value = flow_dict self.switch_01.flows.append(flow_1) api = get_test_client(self.napp.controller, self.napp) url = f'{self.API_URL}/v2/flows/00:00:00:00:00:00:00:01' response = api.get(url) expected = {'00:00:00:00:00:00:00:01': {'flows': [flow_dict]}} self.assertEqual(response.json, expected) self.assertEqual(response.status_code, 200)
def test_add_link_metadata(self, mock_metadata_changes): """Test add_link_metadata.""" mock_link = MagicMock(Link) mock_link.metadata = "A" self.napp.links = {'1': mock_link} payload = {"metadata": "A"} api = get_test_client(self.napp.controller, self.napp) link_id = 1 url = f'{self.server_name_url}/v3/links/{link_id}/metadata' response = api.post(url, data=json.dumps(payload), content_type='application/json') self.assertEqual(response.status_code, 201, response.data) mock_metadata_changes.assert_called() # fail case link_id = 2 url = f'{self.server_name_url}/v3/links/{link_id}/metadata' response = api.post(url, data=json.dumps(payload), content_type='application/json') self.assertEqual(response.status_code, 404, response.data)
def test_rest_add_and_delete_with_dpid(self, mock_install_flows): """Test add and delete rest method with dpid.""" api = get_test_client(self.napp.controller, self.napp) for method in ['flows', 'delete']: url_1 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:01' url_2 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:02' url_3 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:03' response_1 = api.post(url_1) response_2 = api.post(url_1, json={'data': '123'}) response_3 = api.post(url_2, json={'data': '123'}) response_4 = api.post(url_3, json={'data': '123'}) self.assertEqual(response_1.status_code, 404) self.assertEqual(response_2.status_code, 200) if method == 'flows': self.assertEqual(response_3.status_code, 404) else: self.assertEqual(response_3.status_code, 200) self.assertEqual(response_4.status_code, 404) self.assertEqual(mock_install_flows.call_count, 3)
def test_shortest_path(self): """Test shortest path.""" self.setting_path() api = get_test_client(self.napp.controller, self.napp) url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2/" + \ "path-exact-delay" data = {"source": "User1", "destination": "User4", "delay": 30} response = api.open(url, method='POST', json=data) paths = [{ "path": ["User1", "User1:4", "User4:3", "User4"], "total_delay": 105 }, { "path": [ "User1", "User1:1", "S1:2", "S1", "S1:1", "S2:1", "S2", "S2:2", "User4:1", "User4" ], "total_delay": 116 }] self.assertEqual(paths, response.json['Exact Path Result'])
def test_get_topology(self): """Test get_topology.""" dpid_a = "00:00:00:00:00:00:00:01" dpid_b = "00:00:00:00:00:00:00:02" expected = { "topology": { "switches": { "00:00:00:00:00:00:00:01": { "metadata": { "lat": "0.0", "lng": "-30.0" } }, "00:00:00:00:00:00:00:02": { "metadata": { "lat": "0.0", "lng": "-30.0" } } }, "links": { "cf0f4071be4": { "id": "cf0f4071be4" } } } } mock_switch_a = get_switch_mock(dpid_a, 0x04) mock_switch_b = get_switch_mock(dpid_b, 0x04) mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) mock_link = get_link_mock(mock_interface_a, mock_interface_b) mock_link.id = 'cf0f4071be4' mock_switch_a.id = dpid_a mock_switch_a.as_dict.return_value = { 'metadata': { 'lat': '0.0', 'lng': '-30.0' } } mock_switch_b.id = dpid_b mock_switch_b.as_dict.return_value = { 'metadata': { 'lat': '0.0', 'lng': '-30.0' } } self.napp.controller.switches = { dpid_a: mock_switch_a, dpid_b: mock_switch_b } self.napp.links = {"cf0f4071be4": mock_link} mock_link.as_dict.return_value = {"id": "cf0f4071be4"} api = get_test_client(self.napp.controller, self.napp) url = f'{self.server_name_url}/v3/' response = api.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.data), expected)
def test_restore_network_status(self, mock_storehouse): """Test restore_network_status.""" dpid = '00:00:00:00:00:00:00:01' dpid_b = '00:00:00:00:00:00:00:02' mock_switch_a = get_switch_mock(dpid) mock_switch_b = get_switch_mock(dpid_b) mock_interface_a_1 = get_interface_mock('s1-eth1', 1, mock_switch_a) mock_interface_a_2 = get_interface_mock('s1-eth2', 2, mock_switch_b) mock_interface_b_1 = get_interface_mock('s2-eth1', 1, mock_switch_b) mock_interface_b_2 = get_interface_mock('s2-eth2', 2, mock_switch_b) mock_link = get_link_mock(mock_interface_a_1, mock_interface_b_1) mock_switch_a.interfaces = { 1: mock_interface_a_1, 2: mock_interface_a_2 } mock_switch_b.interfaces = { 1: mock_interface_b_1, 2: mock_interface_b_2 } self.napp.controller.switches = { dpid: mock_switch_a, dpid_b: mock_switch_b } self.napp.links = {'4d42dc08522': mock_link} status = { 'network_status': { 'id': 'network_status', 'links': { '4d42dc08522': { 'enabled': True, 'endpoint_a': { 'switch': '00:00:00:00:00:00:00:01', 'id': '00:00:00:00:00:00:00:00:1' }, 'endpoint_b': { 'switch': '00:00:00:00:00:00:00:01', 'id': '00:00:00:00:00:00:00:00:2' } } }, 'switches': { '00:00:00:00:00:00:00:01': { 'dpid': '00:00:00:00:00:00:00:01', 'enabled': True, 'id': '00:00:00:00:00:00:00:01', 'interfaces': { '00:00:00:00:00:00:00:01:1': { 'enabled': True, 'lldp': True, 'id': '00:00:00:00:00:00:00:00:1', } } } } } } status_disable = { 'network_status': { 'id': 'network_status', 'links': { '4d42dc08522': { 'enabled': False, 'endpoint_a': { 'switch': '00:00:00:00:00:00:00:01', 'id': '00:00:00:00:00:00:00:00:1' }, 'endpoint_b': { 'switch': '00:00:00:00:00:00:00:01', 'id': '00:00:00:00:00:00:00:00:2' } } }, 'switches': { '00:00:00:00:00:00:00:01': { 'dpid': '00:00:00:00:00:00:00:01', 'enabled': False, 'id': '00:00:00:00:00:00:00:01', 'interfaces': { '00:00:00:00:00:00:00:01:1': { 'enabled': False, 'lldp': False, 'id': '00:00:00:00:00:00:00:01:1', } } } } } } api = get_test_client(self.napp.controller, self.napp) mock_storehouse.side_effect = [{}, status, status_disable] # fail case url = f'{self.server_name_url}/v3/restore' response = api.get(url) self.assertEqual(response.status_code, 404, response.data) # enable url = f'{self.server_name_url}/v3/restore' response = api.get(url) self.assertEqual(response.status_code, 200, response.data) self.assertEqual(mock_switch_a.enable.call_count, 1) self.assertEqual(mock_interface_a_1.enable.call_count, 1) self.assertEqual(mock_interface_a_1.lldp, True) self.assertEqual(mock_link.enable.call_count, 1) # disable url = f'{self.server_name_url}/v3/restore' response = api.get(url) self.assertEqual(response.status_code, 200, response.data) self.assertEqual(mock_switch_a.disable.call_count, 1) self.assertEqual(mock_interface_a_1.disable.call_count, 1) self.assertEqual(mock_interface_a_1.lldp, False) self.assertEqual(mock_link.disable.call_count, 1)