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 graph import KytosGraph |
7
|
|
|
from tests.helpers import (get_filter_links_fake, get_topology_mock, |
8
|
|
|
get_topology_with_metadata_mock) |
9
|
|
|
|
10
|
|
|
# pylint: disable=arguments-differ, protected-access |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class TestGraph(TestCase): |
14
|
|
|
"""Tests for the Main class.""" |
15
|
|
|
|
16
|
|
|
@patch('networkx.Graph') |
17
|
|
|
def setUp(self, mock_graph): |
18
|
|
|
"""Execute steps before each tests.""" |
19
|
|
|
self.mock_graph = mock_graph.return_value |
20
|
|
|
self.kytos_graph = KytosGraph() |
21
|
|
|
|
22
|
|
|
def test_clear(self): |
23
|
|
|
"""Test clear.""" |
24
|
|
|
self.kytos_graph.clear() |
25
|
|
|
|
26
|
|
|
self.mock_graph.clear.assert_called() |
27
|
|
|
|
28
|
|
|
def setting_update_topology(self, *args): |
29
|
|
|
"""Set the primary elements needed to |
30
|
|
|
test the topology update process.""" |
31
|
|
|
(mock_update_nodes, mock_update_links) = args |
32
|
|
|
topology = get_topology_mock() |
33
|
|
|
self.kytos_graph.update_topology(topology) |
34
|
|
|
|
35
|
|
|
self.mock_graph.clear.assert_called() |
36
|
|
|
|
37
|
|
|
return mock_update_nodes, mock_update_links, topology |
38
|
|
|
|
39
|
|
|
# @patch('napps.kytos.pathfinder.graph.KytosGraph.update_links') |
40
|
|
|
# @patch('napps.kytos.pathfinder.graph.KytosGraph.update_nodes') |
41
|
|
|
@patch('graph.KytosGraph.update_links') |
42
|
|
|
@patch('graph.KytosGraph.update_nodes') |
43
|
|
|
def test_update_topology_switches(self, *args): |
44
|
|
|
"""Test update topology.""" |
45
|
|
|
mock_update_nodes, _, topology = self.setting_update_topology(*args) |
46
|
|
|
mock_update_nodes.assert_called_with(topology.switches) |
47
|
|
|
|
48
|
|
|
# @patch('napps.kytos.pathfinder.graph.KytosGraph.update_links') |
49
|
|
|
# @patch('napps.kytos.pathfinder.graph.KytosGraph.update_nodes') |
50
|
|
|
@patch('graph.KytosGraph.update_links') |
51
|
|
|
@patch('graph.KytosGraph.update_nodes') |
52
|
|
|
def test_update_topology_links(self, *args): |
53
|
|
|
"""Test update topology.""" |
54
|
|
|
_, mock_update_links, topology = self.setting_update_topology(*args) |
55
|
|
|
mock_update_links.assert_called_with(topology.links) |
56
|
|
|
|
57
|
|
|
def test_update_nodes(self): |
58
|
|
|
"""Test update nodes.""" |
59
|
|
|
topology = get_topology_mock() |
60
|
|
|
self.kytos_graph.update_nodes(topology.switches) |
61
|
|
|
switch = topology.switches["00:00:00:00:00:00:00:01"] |
62
|
|
|
|
63
|
|
|
calls = [call(switch.id)] |
64
|
|
|
calls += [call(interface.id) |
65
|
|
|
for interface in switch.interfaces.values()] |
66
|
|
|
self.mock_graph.add_node.assert_has_calls(calls) |
67
|
|
|
|
68
|
|
|
calls = [call(switch.id, interface.id) |
69
|
|
|
for interface in switch.interfaces.values()] |
70
|
|
|
|
71
|
|
|
self.mock_graph.add_edge.assert_has_calls(calls) |
72
|
|
|
|
73
|
|
|
def test_update_nodes_2(self): |
74
|
|
|
"""Test update nodes.""" |
75
|
|
|
|
76
|
|
|
effect = MagicMock(side_effect=AttributeError) |
77
|
|
|
|
78
|
|
|
topology = get_topology_mock() |
79
|
|
|
with self.assertRaises(Exception): |
80
|
|
|
with patch.object(self.mock_graph, 'add_node', effect): |
81
|
|
|
self.kytos_graph.update_nodes(topology.switches) |
82
|
|
|
|
83
|
|
|
self.assertRaises(AttributeError) |
84
|
|
|
|
85
|
|
|
def test_remove_switch_hops(self): |
86
|
|
|
"""Test remove switch hops.""" |
87
|
|
|
circuit = {"hops": ["00:00:00:00:00:00:00:01:1", |
88
|
|
|
"00:00:00:00:00:00:00:01", |
89
|
|
|
"00:00:00:00:00:00:00:01:2"]} |
90
|
|
|
|
91
|
|
|
self.kytos_graph._remove_switch_hops(circuit) |
92
|
|
|
|
93
|
|
|
expected_circuit = {"hops": ["00:00:00:00:00:00:00:01:1", |
94
|
|
|
"00:00:00:00:00:00:00:01:2"]} |
95
|
|
|
self.assertEqual(circuit, expected_circuit) |
96
|
|
|
|
97
|
|
|
@patch('networkx.shortest_simple_paths', return_value=["any"]) |
98
|
|
|
def test_shortest_paths(self, mock_shortest_simple_paths): |
99
|
|
|
"""Test shortest paths.""" |
100
|
|
|
source, dest = "00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:2" |
101
|
|
|
shortest_paths = self.kytos_graph.shortest_paths(source, dest) |
102
|
|
|
|
103
|
|
|
mock_shortest_simple_paths.assert_called_with(self.kytos_graph.graph, |
104
|
|
|
source, dest, None) |
105
|
|
|
self.assertEqual(shortest_paths, ["any"]) |
106
|
|
|
|
107
|
|
|
@patch('graph.combinations', autospec=True) |
108
|
|
|
def test_constrained_flexible_paths(self, mock_combinations): |
109
|
|
|
"""Test shortest constrained paths.""" |
110
|
|
|
source, dest = "00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:2" |
111
|
|
|
minimum_hits = 1 |
112
|
|
|
base_metrics = {"apple": 1} |
113
|
|
|
flexible_metrics = {"pear": 2} |
114
|
|
|
mock_combinations.return_value = [(('pear', 2),)] |
115
|
|
|
filtered_links = ["a", "b", "c"] |
116
|
|
|
filtered_links_without_metadata = filtered_links[0:2] |
117
|
|
|
constrained_shortest_paths = [["path1"], ["path2"]] |
118
|
|
|
|
119
|
|
|
self.kytos_graph._constrained_shortest_paths = MagicMock( |
120
|
|
|
return_value=constrained_shortest_paths) |
121
|
|
|
self.kytos_graph._filter_links = MagicMock( |
122
|
|
|
side_effect=get_filter_links_fake) |
123
|
|
|
shortest_paths = self.kytos_graph.constrained_flexible_paths( |
124
|
|
|
source, dest, minimum_hits, base=base_metrics, |
125
|
|
|
flexible=flexible_metrics) |
126
|
|
|
|
127
|
|
|
self.kytos_graph._constrained_shortest_paths.assert_has_calls([ |
128
|
|
|
call(source, dest, filtered_links_without_metadata)]) |
129
|
|
|
self.kytos_graph._filter_links.assert_called() |
130
|
|
|
self.assertEqual(shortest_paths, [ |
131
|
|
|
{"paths": constrained_shortest_paths, "metrics": |
132
|
|
|
{"apple": 1, "pear": 2}}]) |
133
|
|
|
|
134
|
|
|
def test_get_link_metadata(self): |
135
|
|
|
"""Test metadata retrieval.""" |
136
|
|
|
topology = get_topology_with_metadata_mock() |
137
|
|
|
self.kytos_graph.update_nodes(topology.switches) |
138
|
|
|
self.kytos_graph.update_links(topology.links) |
139
|
|
|
endpoint_a = "S1:1" |
140
|
|
|
endpoint_b = "S2:1" |
141
|
|
|
metadata = {"reliability": 5, "bandwidth": 100, "delay": 105} |
142
|
|
|
self.kytos_graph.get_link_metadata = MagicMock(return_value=metadata) |
143
|
|
|
|
144
|
|
|
result = self.kytos_graph.get_link_metadata( |
145
|
|
|
endpoint_a, endpoint_b) |
146
|
|
|
|
147
|
|
|
self.assertEqual(result, metadata) |
148
|
|
|
|