def get_topology_with_metadata_mock(): """Create a topology with metadata.""" switches = {} interfaces = {} links = {} i = 0 links_to_interfaces, links_to_metadata, switches_to_interface_counts = topology_setting( ) for switch in switches_to_interface_counts: switches[switch] = get_switch_mock(switch) for key, value in switches_to_interface_counts.items(): switches[key].interfaces = {} for interface in _get_interfaces(value, switches[key]): switches[key].interfaces[interface.id] = interface interfaces[interface.id] = interface for interfaces_str in links_to_interfaces: interface_a = interfaces[interfaces_str[0]] interface_b = interfaces[interfaces_str[1]] links[str(i)] = get_link_mock(interface_a, interface_b) links[str(i)].metadata = links_to_metadata[i] i += 1 topology = MagicMock() topology.links = links topology.switches = switches return topology
def test_link_mock(self): """Test link mock.""" endpoint_a = MagicMock() endpoint_b = MagicMock() link_mock = get_link_mock(endpoint_a, endpoint_b) self.assertEqual(link_mock.endpoint_a, endpoint_a) self.assertEqual(link_mock.endpoint_b, endpoint_b) self.assertEqual(link_mock.metadata, {"A": 0})
def get_topology_mock(): """Create a default topology.""" switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01) interface_a1 = get_interface_mock("s1-eth1", 1, switch_a) interface_a2 = get_interface_mock("s1-eth2", 2, switch_a) interface_b1 = get_interface_mock("s2-eth1", 1, switch_b) interface_b2 = get_interface_mock("s2-eth2", 2, switch_b) interface_c1 = get_interface_mock("s3-eth1", 1, switch_c) interface_c2 = get_interface_mock("s3-eth2", 2, switch_c) switch_a.interfaces = { interface_a1.id: interface_a1, interface_a2.id: interface_a2 } switch_b.interfaces = { interface_b1.id: interface_b1, interface_b2.id: interface_b2 } switch_c.interfaces = { interface_c1.id: interface_c1, interface_c2.id: interface_c2 } link_1 = get_link_mock(interface_a1, interface_b1) link_2 = get_link_mock(interface_a2, interface_c1) link_3 = get_link_mock(interface_b2, interface_c2) topology = MagicMock() topology.links = {"1": link_1, "2": link_2, "3": link_3} topology.switches = { switch_a.dpid: switch_a, switch_b.dpid: switch_b, switch_c.dpid: switch_c } return topology
def test_get_link_from_interface(self): """Test _get_link_from_interface.""" mock_switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) mock_switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 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_interface_c = get_interface_mock('s2-eth1', 2, mock_switch_b) mock_link = get_link_mock(mock_interface_a, mock_interface_b) self.napp.links = {'0e2b5d7bc858b9f38db11b69': mock_link} response = self.napp._get_link_from_interface(mock_interface_a) self.assertEqual(response, mock_link) response = self.napp._get_link_from_interface(mock_interface_c) self.assertEqual(response, None)
def test_fail_restore_link(self): """Test fail restore_link.""" dpid = '00:00:00:00:00:00:00:01' dpid_b = '00:00:00:00:00:00:00:02' link_id = '4d42dc08522' link_id_fail = '4cd52' 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_b_1 = get_interface_mock('s2-eth1', 1, mock_switch_b) mock_link = get_link_mock(mock_interface_a_1, mock_interface_b_1) mock_link.id = link_id self.napp.links = {link_id: mock_link} self.napp.links_state = {link_id: {"enabled": True}} with self.assertRaises(RestoreError): self.napp._restore_link(link_id_fail) self.napp.links_state = {link_id_fail: {"enabled": True}} with self.assertRaises(RestoreError): self.napp._restore_link(link_id_fail)
def test_restore_links(self): """Test restore_link.""" dpid = '00:00:00:00:00:00:00:01' dpid_b = '00:00:00:00:00:00:00:02' link_id = '4d42dc08522' 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_b_1 = get_interface_mock('s2-eth1', 1, mock_switch_b) mock_link = get_link_mock(mock_interface_a_1, mock_interface_b_1) mock_link.id = link_id self.napp.links = {link_id: mock_link} self.napp.links_state = {link_id: {'enabled': True}} # enable link self.napp.restore_network_status(mock_link) self.assertEqual(mock_link.enable.call_count, 1) # disable link self.napp.links_state = {link_id: {"enabled": False}} self.napp._verified_links = [] self.napp.restore_network_status(mock_link) self.assertEqual(mock_link.disable.call_count, 1)
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)