def test_update_topology_success_case(self): """Test update topology method to success case.""" topology = get_topology_mock() event = KytosEvent(name='kytos.topology.updated', content={'topology': topology}) self.napp.update_topology(event) self.assertEqual(self.napp._topology, topology)
def test_filter_paths_response_on_undesired(self): """Test filter paths.""" self.napp._topology = get_topology_mock() paths = [{ "hops": ["00:00:00:00:00:00:00:01:2", "00:00:00:00:00:00:00:03:1"] }] desired, undesired = None, ["2"] filtered_paths = self.napp._filter_paths(paths, desired, undesired) self.assertEqual(filtered_paths, [])
def test_update_topology(self, *args): """Test update topology.""" (mock_update_nodes, mock_update_links) = args topology = get_topology_mock() self.kytos_graph.update_topology(topology) self.mock_graph.clear.assert_called() mock_update_nodes.assert_called_with(topology.switches) mock_update_links.assert_called_with(topology.links)
def setting_update_topology(self, *args): """Set the primary elements needed to test the topology update process.""" (mock_update_nodes, mock_update_links) = args topology = get_topology_mock() self.kytos_graph.update_topology(topology) self.mock_graph.clear.assert_called() return mock_update_nodes, mock_update_links, topology
def setting_shortest_path_mocked(self, mock_shortest_paths): """Set the primary elements needed to test the retrieving process of the shortest path under a mocked approach.""" self.napp._topology = get_topology_mock() path = ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:1"] mock_shortest_paths.return_value = [path] api = get_test_client(self.napp.controller, self.napp) return api, path
def test_filter_paths_response_on_desired(self): """Test filter paths.""" self.napp._topology = get_topology_mock() paths = [{ "hops": ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:1"] }] desired, undesired = ["1"], None filtered_paths = self.napp._filter_paths(paths, desired, undesired) self.assertEqual(filtered_paths, paths)
def test_update_links(self, mock_set_default_metadata): """Test update nodes.""" topology = get_topology_mock() self.kytos_graph.update_links(topology.links) keys = [] all_metadata = [link.metadata for link in topology.links.values()] for metadata in all_metadata: keys.extend(key for key in metadata.keys()) mock_set_default_metadata.assert_called_with(keys)
def test_update_nodes_2(self): """Test update nodes.""" effect = MagicMock(side_effect=AttributeError) topology = get_topology_mock() with self.assertRaises(Exception): with patch.object(self.mock_graph, 'add_node', effect): self.kytos_graph.update_nodes(topology.switches) self.assertRaises(AttributeError)
def setUp(self): """Execute steps before each tests.""" self.server_name_url = 'http://127.0.0.1:8181/api/kytos/of_lldp' patch('kytos.core.helpers.run_on_thread', lambda x: x).start() # pylint: disable=bad-option-value, import-outside-toplevel from napps.kytos.of_lldp.main import Main self.addCleanup(patch.stopall) self.topology = get_topology_mock() controller = get_controller_mock() controller.switches = self.topology.switches self.napp = Main(controller)
def test_update_nodes(self): """Test update nodes.""" topology = get_topology_mock() self.kytos_graph.update_nodes(topology.switches) switch = topology.switches["00:00:00:00:00:00:00:01"] calls = [call(switch.id)] calls += [call(interface.id) for interface in switch.interfaces.values()] self.mock_graph.add_node.assert_has_calls(calls) calls = [call(switch.id, interface.id) for interface in switch.interfaces.values()] self.mock_graph.add_edge.assert_has_calls(calls)
def test_shortest_path(self, mock_shortest_paths): """Test shortest path.""" self.napp._topology = get_topology_mock() path = ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:1"] mock_shortest_paths.return_value = [path] api = get_test_client(self.napp.controller, self.napp) url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2" data = { "source": "00:00:00:00:00:00:00:01:1", "destination": "00:00:00:00:00:00:00:02:1", "desired_links": ["1"], "undesired_links": None } response = api.open(url, method='POST', json=data) expected_response = {'paths': [{'hops': path}]} self.assertEqual(response.json, expected_response) self.assertEqual(response.status_code, 200)