1
|
|
|
"""Test Main methods.""" |
2
|
|
|
|
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import patch |
5
|
|
|
|
6
|
|
|
from kytos.core.events import KytosEvent |
7
|
|
|
from kytos.lib.helpers import get_controller_mock, get_test_client |
8
|
|
|
|
9
|
|
|
# from napps.kytos.pathfinder.main import Main |
10
|
|
|
from main import Main |
11
|
|
|
from tests.helpers import get_topology_mock, get_topology_with_metadata |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
# pylint: disable=protected-access |
15
|
|
|
class TestMain(TestCase): |
16
|
|
|
"""Tests for the Main class.""" |
17
|
|
|
|
18
|
|
|
def setUp(self): |
19
|
|
|
"""Execute steps before each tests.""" |
20
|
|
|
self.napp = Main(get_controller_mock()) |
21
|
|
|
|
22
|
|
|
def test_update_topology_success_case(self): |
23
|
|
|
"""Test update topology method to success case.""" |
24
|
|
|
topology = get_topology_mock() |
25
|
|
|
event = KytosEvent(name='kytos.topology.updated', |
26
|
|
|
content={'topology': topology}) |
27
|
|
|
self.napp.update_topology(event) |
28
|
|
|
|
29
|
|
|
self.assertEqual(self.napp._topology, topology) |
30
|
|
|
|
31
|
|
|
def test_update_topology_failure_case(self): |
32
|
|
|
"""Test update topology method to failure case.""" |
33
|
|
|
event = KytosEvent(name='kytos.topology.updated') |
34
|
|
|
self.napp.update_topology(event) |
35
|
|
|
|
36
|
|
|
self.assertIsNone(self.napp._topology) |
37
|
|
|
|
38
|
|
|
def setting_shortest_path_mocked(self, mock_shortest_paths): |
39
|
|
|
"""Set the primary elements needed to test the retrieving |
40
|
|
|
process of the shortest path under a mocked approach.""" |
41
|
|
|
self.napp._topology = get_topology_mock() |
42
|
|
|
path = ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:1"] |
43
|
|
|
mock_shortest_paths.return_value = [path] |
44
|
|
|
|
45
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
46
|
|
|
|
47
|
|
|
return api, path |
48
|
|
|
|
49
|
|
|
@patch('graph.KytosGraph.shortest_paths') |
50
|
|
|
def test_shortest_path_path_response(self, mock_shortest_paths): |
51
|
|
|
"""Test shortest path.""" |
52
|
|
|
api, path = self.setting_shortest_path_mocked(mock_shortest_paths) |
53
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2" |
54
|
|
|
data = {"source": "00:00:00:00:00:00:00:01:1", |
55
|
|
|
"destination": "00:00:00:00:00:00:00:02:1", |
56
|
|
|
"desired_links": ["1"], |
57
|
|
|
"undesired_links": None} |
58
|
|
|
response = api.open(url, method='POST', json=data) |
59
|
|
|
|
60
|
|
|
expected_response = {'paths': [{'hops': path}]} |
61
|
|
|
self.assertEqual(response.json, expected_response) |
62
|
|
|
|
63
|
|
|
@patch('graph.KytosGraph.shortest_paths') |
64
|
|
|
def test_shortest_path_response_status_code(self, mock_shortest_paths): |
65
|
|
|
"""Test shortest path.""" |
66
|
|
|
api, _ = self.setting_shortest_path_mocked(mock_shortest_paths) |
67
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2" |
68
|
|
|
data = {"source": "00:00:00:00:00:00:00:01:1", |
69
|
|
|
"destination": "00:00:00:00:00:00:00:02:1", |
70
|
|
|
"desired_links": ["1"], |
71
|
|
|
"undesired_links": None} |
72
|
|
|
response = api.open(url, method='POST', json=data) |
73
|
|
|
|
74
|
|
|
self.assertEqual(response.status_code, 200) |
75
|
|
|
|
76
|
|
|
def setting_shortest_constrained_path_mocked(self, mock_constrained_flexible_paths): |
77
|
|
|
"""Set the primary elements needed to test the retrieving process |
78
|
|
|
of the shortest constrained path under a mocked approach.""" |
79
|
|
|
source = "00:00:00:00:00:00:00:01:1" |
80
|
|
|
destination = "00:00:00:00:00:00:00:02:1" |
81
|
|
|
path = [source, destination] |
82
|
|
|
base_metrics = {"ownership": "bob"} |
83
|
|
|
fle_metrics = {"delay": 30} |
84
|
|
|
metrics = {**base_metrics, **fle_metrics} |
85
|
|
|
mock_constrained_flexible_paths.return_value = [ |
86
|
|
|
{"paths": [path], "metrics": metrics}] |
87
|
|
|
|
88
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
89
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2/" + \ |
90
|
|
|
"best-constrained-paths" |
91
|
|
|
data = {"source": "00:00:00:00:00:00:00:01:1", |
92
|
|
|
"destination": "00:00:00:00:00:00:00:02:1", |
93
|
|
|
"base_metrics": {"ownership": "bob"}, |
94
|
|
|
"flexible_metrics": {"delay": 30}, |
95
|
|
|
"minimum_flexible_hits": 1} |
96
|
|
|
response = api.open(url, method='POST', json=data) |
97
|
|
|
|
98
|
|
|
return response, metrics, path |
99
|
|
|
|
100
|
|
|
@patch('graph.KytosGraph.constrained_flexible_paths', autospec=True) |
101
|
|
|
def test_shortest_constrained_path_response(self, mock_constrained_flexible_paths): |
102
|
|
|
"""Test constrained flexible paths.""" |
103
|
|
|
response, metrics, path = \ |
104
|
|
|
self.setting_shortest_constrained_path_mocked( |
105
|
|
|
mock_constrained_flexible_paths) |
106
|
|
|
expected_response = [{"metrics": metrics, "paths": [path]}] |
107
|
|
|
|
108
|
|
|
self.assertEqual(response.json, expected_response) |
109
|
|
|
|
110
|
|
|
# @patch('napps.kytos.pathfinder.graph.KytosGraph.' + |
111
|
|
|
# 'constrained_flexible_paths', autospec=True) |
112
|
|
|
@patch('graph.KytosGraph.constrained_flexible_paths', autospec=True) |
113
|
|
|
def test_shortest_constrained_path_response_status_code(self, mock_constrained_flexible_paths): |
114
|
|
|
"""Test constrained flexible paths.""" |
115
|
|
|
response, _, _ = \ |
116
|
|
|
self.setting_shortest_constrained_path_mocked( |
117
|
|
|
mock_constrained_flexible_paths) |
118
|
|
|
|
119
|
|
|
self.assertEqual(response.status_code, 200) |
120
|
|
|
|
121
|
|
|
def test_filter_paths_response_on_desired(self): |
122
|
|
|
"""Test filter paths.""" |
123
|
|
|
self.napp._topology = get_topology_mock() |
124
|
|
|
paths = [{"hops": ["00:00:00:00:00:00:00:01:1", |
125
|
|
|
"00:00:00:00:00:00:00:02:1"]}] |
126
|
|
|
desired, undesired = ["1"], None |
127
|
|
|
|
128
|
|
|
filtered_paths = self.napp._filter_paths(paths, desired, undesired) |
129
|
|
|
self.assertEqual(filtered_paths, paths) |
130
|
|
|
|
131
|
|
|
def test_filter_paths_response_on_undesired(self): |
132
|
|
|
"""Test filter paths.""" |
133
|
|
|
self.napp._topology = get_topology_mock() |
134
|
|
|
paths = [{"hops": ["00:00:00:00:00:00:00:01:2", |
135
|
|
|
"00:00:00:00:00:00:00:03:1"]}] |
136
|
|
|
desired, undesired = None, ["2"] |
137
|
|
|
filtered_paths = self.napp._filter_paths(paths, desired, undesired) |
138
|
|
|
self.assertEqual(filtered_paths, []) |
139
|
|
|
|
140
|
|
|
def setting_path(self): |
141
|
|
|
"""Set the primary elements needed to test the topology |
142
|
|
|
update process under a "real-simulated" scenario.""" |
143
|
|
|
topology = get_topology_with_metadata() |
144
|
|
|
event = KytosEvent(name='kytos.topology.updated', |
145
|
|
|
content={'topology': topology}) |
146
|
|
|
self.napp.update_topology(event) |
147
|
|
|
|
148
|
|
|
def test_shortest_path(self): |
149
|
|
|
"""Test shortest path.""" |
150
|
|
|
self.setting_path() |
151
|
|
|
|
152
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
153
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2/" + \ |
154
|
|
|
"path-exact-delay" |
155
|
|
|
|
156
|
|
|
data = {"source": "User1", |
157
|
|
|
"destination": "User4", |
158
|
|
|
"delay": 30} |
159
|
|
|
|
160
|
|
|
response = api.open(url, method='POST', json=data) |
161
|
|
|
paths = [{"path": ["User1", "User1:4", "User4:3", "User4"], |
162
|
|
|
"total_delay": 105}, |
163
|
|
|
{"path": ["User1", "User1:1", "S1:2", "S1", "S1:1", |
164
|
|
|
"S2:1", "S2", "S2:2", "User4:1", "User4"], |
165
|
|
|
"total_delay": 116}] |
166
|
|
|
|
167
|
|
|
self.assertEqual(paths, response.json['Exact Path Result']) |
168
|
|
|
|
169
|
|
|
def setting_shortest_path_exception(self): |
170
|
|
|
"""Set the primary elements needed to test the |
171
|
|
|
shortest path behavior under exception actions.""" |
172
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
173
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2/" + \ |
174
|
|
|
"path-exact-delay" |
175
|
|
|
|
176
|
|
|
data = {"source": "User1", |
177
|
|
|
"destination": "User4", |
178
|
|
|
"delay": 30} |
179
|
|
|
|
180
|
|
|
return api, url, data |
181
|
|
|
|
182
|
|
|
def test_shortest_path_broader_exception(self): |
183
|
|
|
"""Test shortest path.""" |
184
|
|
|
api, url, data = self.setting_shortest_path_exception() |
185
|
|
|
with patch('graph.KytosGraph.exact_path', side_effect=Exception): |
186
|
|
|
response = api.open(url, method='POST', json=data) |
187
|
|
|
|
188
|
|
|
self.assertIsNotNone(response.json.get('exception')) |
189
|
|
|
|
190
|
|
|
def test_shortest_path_specific_exception(self): |
191
|
|
|
"""Test shortest path.""" |
192
|
|
|
api, url, data = self.setting_shortest_path_exception() |
193
|
|
|
|
194
|
|
|
with patch('graph.KytosGraph.exact_path', side_effect=TypeError): |
195
|
|
|
response = api.open(url, method='POST', json=data) |
196
|
|
|
|
197
|
|
|
self.assertIsNotNone(response.json.get('exception')) |
198
|
|
|
|
199
|
|
|
def setting_shortest_constrained_path_exception(self, side_effect): |
200
|
|
|
"""Set the primary elements needed to test the shortest |
201
|
|
|
constrained path behavior under exception actions.""" |
202
|
|
|
self.setting_path() |
203
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
204
|
|
|
|
205
|
|
|
with patch('graph.KytosGraph.constrained_flexible_paths', side_effect=side_effect): |
206
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2/" + \ |
207
|
|
|
"best-constrained-paths" |
208
|
|
|
|
209
|
|
|
data = {"source": "00:00:00:00:00:00:00:01:1", |
210
|
|
|
"destination": "00:00:00:00:00:00:00:02:1", |
211
|
|
|
"base_metrics": {"ownership": "bob"}, |
212
|
|
|
"flexible_metrics": {"delay": 30}, |
213
|
|
|
"minimum_flexible_hits": 1} |
214
|
|
|
|
215
|
|
|
response = api.open(url, method='POST', json=data) |
216
|
|
|
|
217
|
|
|
return response |
218
|
|
|
|
219
|
|
|
def test_shortest_constrained_path_400_exception(self): |
220
|
|
|
"""Test shortest path.""" |
221
|
|
|
response = self.setting_shortest_constrained_path_exception(TypeError) |
222
|
|
|
|
223
|
|
|
self.assertEqual(response.status_code, 400) |
224
|
|
|
|
225
|
|
|
def test_shortest_constrained_path_500_exception(self): |
226
|
|
|
"""Test shortest path.""" |
227
|
|
|
response = self.setting_shortest_constrained_path_exception(Exception) |
228
|
|
|
|
229
|
|
|
self.assertEqual(response.status_code, 500) |
230
|
|
|
|