1
|
|
|
"""Test Graph methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, call, patch |
4
|
|
|
|
5
|
|
|
from napps.kytos.pathfinder.graph import KytosGraph |
6
|
|
|
from tests.helpers import get_topology_mock |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class TestGraph(TestCase): |
10
|
|
|
"""Tests for the Main class.""" |
11
|
|
|
|
12
|
|
|
def setUp(self): |
13
|
|
|
"""Execute steps before each tests.""" |
14
|
|
|
self.kytos_graph = KytosGraph() |
15
|
|
|
|
16
|
|
|
@patch('networkx.Graph.clear') |
17
|
|
|
def test_clear(self, mock_nx_clear): |
18
|
|
|
"""Test clear.""" |
19
|
|
|
self.kytos_graph.clear() |
20
|
|
|
mock_nx_clear.assert_called() |
21
|
|
|
|
22
|
|
|
@patch('napps.kytos.pathfinder.graph.KytosGraph.update_links') |
23
|
|
|
@patch('napps.kytos.pathfinder.graph.KytosGraph.update_nodes') |
24
|
|
|
@patch('networkx.Graph.clear') |
25
|
|
|
def test_update_topology(self, *args): |
26
|
|
|
"""Test update topology.""" |
27
|
|
|
(mock_nx_clear, mock_update_nodes, mock_update_links) = args |
28
|
|
|
|
29
|
|
|
topology = get_topology_mock() |
30
|
|
|
self.kytos_graph.update_topology(topology) |
31
|
|
|
|
32
|
|
|
mock_nx_clear.assert_called() |
33
|
|
|
mock_update_nodes.assert_called_with(topology.switches) |
34
|
|
|
mock_update_links.assert_called_with(topology.links) |
35
|
|
|
|
36
|
|
|
@patch('networkx.Graph.add_edge') |
37
|
|
|
@patch('networkx.Graph.add_node') |
38
|
|
|
def test_update_nodes(self, *args): |
39
|
|
|
"""Test update nodes.""" |
40
|
|
|
(mock_nx_add_node, mock_nx_add_edge) = args |
41
|
|
|
|
42
|
|
|
topology = get_topology_mock() |
43
|
|
|
self.kytos_graph.update_nodes(topology.switches) |
44
|
|
|
switch = topology.switches["00:00:00:00:00:00:00:01"] |
45
|
|
|
|
46
|
|
|
calls = [call(switch.id)] |
47
|
|
|
calls += [call(interface.id) |
48
|
|
|
for interface in switch.interfaces.values()] |
49
|
|
|
mock_nx_add_node.assert_has_calls(calls) |
50
|
|
|
|
51
|
|
|
calls = [call(switch.id, interface.id) |
52
|
|
|
for interface in switch.interfaces.values()] |
53
|
|
|
mock_nx_add_edge.assert_has_calls(calls) |
54
|
|
|
|
55
|
|
|
@patch('napps.kytos.pathfinder.graph.KytosGraph._set_default_metadata') |
56
|
|
|
def test_update_links(self, mock_set_default_metadata): |
57
|
|
|
"""Test update nodes.""" |
58
|
|
|
topology = get_topology_mock() |
59
|
|
|
self.kytos_graph.graph = MagicMock() |
60
|
|
|
self.kytos_graph.update_links(topology.links) |
61
|
|
|
|
62
|
|
|
keys = [] |
63
|
|
|
all_metadata = [link.metadata for link in topology.links.values()] |
64
|
|
|
for metadata in all_metadata: |
65
|
|
|
keys.extend(key for key in metadata.keys()) |
66
|
|
|
mock_set_default_metadata.assert_called_with(keys) |
67
|
|
|
|
68
|
|
|
def test_remove_switch_hops(self): |
69
|
|
|
"""Test remove switch hops.""" |
70
|
|
|
circuit = {"hops": ["00:00:00:00:00:00:00:01:1", |
71
|
|
|
"00:00:00:00:00:00:00:01", |
72
|
|
|
"00:00:00:00:00:00:00:01:2"]} |
73
|
|
|
expected_circuit = {"hops": ["00:00:00:00:00:00:00:01:1", |
74
|
|
|
"00:00:00:00:00:00:00:01:2"]} |
75
|
|
|
self.kytos_graph._remove_switch_hops(circuit) |
76
|
|
|
self.assertEqual(circuit, expected_circuit) |
77
|
|
|
|
78
|
|
|
@patch('networkx.shortest_simple_paths') |
79
|
|
|
def test_shortest_paths(self, mock_shortest_simple_paths): |
80
|
|
|
"""Test shortest paths.""" |
81
|
|
|
path = ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:01", |
82
|
|
|
"00:00:00:00:00:00:00:01:2"] |
83
|
|
|
mock_shortest_simple_paths.return_value = path |
84
|
|
|
source, dest = "00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:2" |
85
|
|
|
shortest_paths = self.kytos_graph.shortest_paths(source, dest) |
86
|
|
|
mock_shortest_simple_paths.assert_called_with(self.kytos_graph.graph, |
87
|
|
|
source, dest, None) |
88
|
|
|
self.assertEqual(path, shortest_paths) |
89
|
|
|
|