1
|
|
|
""" |
2
|
|
|
Test tracing.trace_entries |
3
|
|
|
""" |
4
|
1 |
|
from unittest.mock import MagicMock, patch |
5
|
1 |
|
import pytest |
6
|
1 |
|
from napps.amlight.sdntrace.tracing.trace_msg import TraceMsg |
7
|
1 |
|
from napps.amlight.sdntrace.tracing.trace_manager import TraceManager |
8
|
1 |
|
from napps.amlight.sdntrace.tracing.tracer import TracePath |
9
|
1 |
|
from napps.amlight.sdntrace.shared.switches import Switches |
10
|
1 |
|
from napps.amlight.sdntrace.tracing.rest import FormatRest |
11
|
|
|
|
12
|
1 |
|
from kytos.lib.helpers import ( |
13
|
|
|
get_interface_mock, |
14
|
|
|
get_link_mock, |
15
|
|
|
get_switch_mock, |
16
|
|
|
get_controller_mock, |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
# pylint: disable=too-many-public-methods, too-many-lines, |
21
|
|
|
# pylint: disable=protected-access, too-many-locals |
22
|
1 |
|
class TestTracePath: |
23
|
|
|
"""Test all combinations for DPID""" |
24
|
|
|
|
25
|
1 |
|
@pytest.fixture(autouse=True) |
26
|
1 |
|
def commomn_patches(self, request): |
27
|
|
|
"""This function handles setup and cleanup for patches""" |
28
|
|
|
# This fixture sets up the common patches, |
29
|
|
|
# and a finalizer is added using addfinalizer to stop |
30
|
|
|
# the common patches after each test. This ensures that the cleanup |
31
|
|
|
# is performed after each test, and additional patch decorators |
32
|
|
|
# can be used within individual test functions. |
33
|
|
|
|
34
|
1 |
|
patcher = patch("kytos.core.helpers.run_on_thread", lambda x: x) |
35
|
1 |
|
mock_patch = patcher.start() |
36
|
|
|
|
37
|
1 |
|
_ = request.function.__name__ |
38
|
|
|
|
39
|
1 |
|
def cleanup(): |
40
|
1 |
|
patcher.stop() |
41
|
|
|
|
42
|
1 |
|
request.addfinalizer(cleanup) |
43
|
1 |
|
return mock_patch |
44
|
|
|
|
45
|
1 |
|
def setup_method(self): |
46
|
|
|
"""Set up before each test method""" |
47
|
1 |
|
self.create_basic_switches(get_controller_mock()) |
48
|
1 |
|
self.trace_manager = TraceManager(controller=get_controller_mock()) |
49
|
|
|
|
50
|
1 |
View Code Duplication |
@classmethod |
|
|
|
|
51
|
1 |
|
def create_basic_switches(cls, controller): |
52
|
|
|
"""Create basic mock switches for Kytos controller.""" |
53
|
1 |
|
dpid_a = "00:00:00:00:00:00:00:01" |
54
|
1 |
|
dpid_b = "00:00:00:00:00:00:00:02" |
55
|
|
|
|
56
|
1 |
|
mock_switch_a = get_switch_mock(dpid_a, 0x04) |
57
|
1 |
|
mock_switch_b = get_switch_mock(dpid_b, 0x04) |
58
|
1 |
|
mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a) |
59
|
1 |
|
mock_interface_b = get_interface_mock("s2-eth1", 1, mock_switch_b) |
60
|
|
|
|
61
|
1 |
|
mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
62
|
1 |
|
mock_link.id = "cf0f4071be4" |
63
|
1 |
|
mock_switch_a.id = dpid_a |
64
|
1 |
|
mock_switch_a.as_dict.return_value = {"metadata": {}} |
65
|
1 |
|
mock_switch_b.id = dpid_b |
66
|
1 |
|
mock_switch_b.as_dict.return_value = {"metadata": {}} |
67
|
|
|
|
68
|
1 |
|
controller.switches = {dpid_a: mock_switch_a, dpid_b: mock_switch_b} |
69
|
|
|
|
70
|
1 |
|
Switches(controller.switches) |
71
|
|
|
|
72
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
73
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
74
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
75
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop") |
76
|
1 |
|
def test_tracepath( |
77
|
|
|
self, mock_trace_loop, mock_get_switch, mock_color, mock_switch_colors |
78
|
|
|
): |
79
|
|
|
"""Test tracepath initial result item. Mocking tracepath loop |
80
|
|
|
to get just one step.""" |
81
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
82
|
|
|
|
83
|
1 |
|
switch = MagicMock() |
84
|
1 |
|
switch.dpid = "00:00:00:00:00:00:00:01" |
85
|
1 |
|
mock_get_switch.return_value = switch |
86
|
|
|
|
87
|
|
|
# Mock tracepath loop to create only the initial result item |
88
|
1 |
|
mock_trace_loop.return_value = True |
89
|
|
|
|
90
|
|
|
# Trace id to recover the result |
91
|
1 |
|
trace_id = 111 |
92
|
|
|
|
93
|
|
|
# Creating trace entries |
94
|
1 |
|
eth = {"dl_vlan": 100} |
95
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
96
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
97
|
1 |
|
entries = {"trace": switch} |
98
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
99
|
|
|
|
100
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
101
|
1 |
|
tracer.tracepath() |
102
|
|
|
|
103
|
|
|
# Retrieve trace result created from tracepath |
104
|
1 |
|
result = self.trace_manager.get_result(trace_id) |
105
|
|
|
|
106
|
1 |
|
assert result["request_id"] == 111 |
107
|
1 |
|
assert len(result["result"]) == 1 |
108
|
1 |
|
assert result["result"][0]["type"] == "starting" |
109
|
1 |
|
assert result["result"][0]["dpid"] == dpid["dpid"] |
110
|
1 |
|
assert result["result"][0]["port"] == dpid["in_port"] |
111
|
|
|
|
112
|
1 |
|
assert result["request"]["trace"]["switch"]["dpid"] == dpid["dpid"] |
113
|
1 |
|
assert result["request"]["trace"]["switch"]["in_port"] == dpid["in_port"] |
114
|
1 |
|
assert result["request"]["trace"]["eth"]["dl_vlan"] == eth["dl_vlan"] |
115
|
1 |
|
assert mock_color.call_count == 2 |
116
|
1 |
|
assert mock_switch_colors.call_count == 2 |
117
|
|
|
|
118
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
119
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
120
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
121
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop") |
122
|
1 |
|
def test_tracepath_result( |
123
|
|
|
self, mock_trace_loop, mock_get_switch, mock_color, mock_switch_colors |
124
|
|
|
): |
125
|
|
|
"""Test tracepath initial result item. Patching the tracepath loop |
126
|
|
|
to test multiple steps.""" |
127
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
128
|
|
|
|
129
|
|
|
# Patch Switches.get_switch |
130
|
1 |
|
def wrap_get_switch(dpid): |
131
|
1 |
|
switch = MagicMock() |
132
|
1 |
|
switch.dpid = dpid |
133
|
1 |
|
return switch |
134
|
|
|
|
135
|
1 |
|
mock_get_switch.side_effect = wrap_get_switch |
136
|
|
|
|
137
|
|
|
# Trace id to recover the result |
138
|
1 |
|
trace_id = 111 |
139
|
|
|
|
140
|
|
|
# Creating trace entries |
141
|
1 |
|
eth = {"dl_vlan": 100} |
142
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
143
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
144
|
1 |
|
entries = {"trace": switch} |
145
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
146
|
|
|
|
147
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
148
|
|
|
|
149
|
|
|
# Patch tracepath loop to create a second result item |
150
|
|
|
# pylint: disable=unused-argument |
151
|
1 |
|
def wrap_tracepath_loop(entries, color, switch): |
152
|
1 |
|
rest = FormatRest() |
153
|
1 |
|
rest.add_trace_step( |
154
|
|
|
tracer.trace_result, |
155
|
|
|
trace_type="trace", |
156
|
|
|
dpid="00:00:00:00:00:00:00:03", |
157
|
|
|
port=3, |
158
|
|
|
) |
159
|
1 |
|
return True |
160
|
|
|
|
161
|
1 |
|
mock_trace_loop.side_effect = wrap_tracepath_loop |
162
|
|
|
|
163
|
|
|
# Execute tracepath |
164
|
1 |
|
tracer.tracepath() |
165
|
|
|
|
166
|
|
|
# Retrieve trace result created from tracepath |
167
|
1 |
|
result = self.trace_manager.get_result(trace_id) |
168
|
|
|
|
169
|
1 |
|
assert result["request_id"] == 111 |
170
|
1 |
|
assert len(result["result"]) == 2 |
171
|
1 |
|
assert result["result"][0]["type"] == "starting" |
172
|
1 |
|
assert result["result"][0]["dpid"] == dpid["dpid"] |
173
|
1 |
|
assert result["result"][0]["port"] == dpid["in_port"] |
174
|
|
|
|
175
|
1 |
|
assert result["result"][1]["type"] == "trace" |
176
|
1 |
|
assert result["result"][1]["dpid"] == "00:00:00:00:00:00:00:03" |
177
|
1 |
|
assert result["result"][1]["port"] == 3 |
178
|
|
|
|
179
|
1 |
|
assert result["request"]["trace"]["switch"]["dpid"] == dpid["dpid"] |
180
|
1 |
|
assert result["request"]["trace"]["switch"]["in_port"] == dpid["in_port"] |
181
|
1 |
|
assert result["request"]["trace"]["eth"]["dl_vlan"] == eth["dl_vlan"] |
182
|
1 |
|
assert mock_color.call_count == 2 |
183
|
1 |
|
assert mock_switch_colors.call_count == 2 |
184
|
|
|
|
185
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
186
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
187
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
188
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop") |
189
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.send_packet_out") |
190
|
1 |
|
def test_send_trace_probe( |
191
|
|
|
self, |
192
|
|
|
mock_send_packet_out, |
193
|
|
|
mock_trace_loop, |
194
|
|
|
mock_get_switch, |
195
|
|
|
mock_color, |
196
|
|
|
mock_switch_colors, |
197
|
|
|
): |
198
|
|
|
"""Test send_trace_probe send and receive.""" |
199
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
200
|
|
|
|
201
|
1 |
|
mock_send_packet_out.return_value = True |
202
|
|
|
|
203
|
1 |
|
switch_obj = MagicMock() |
204
|
1 |
|
switch_obj.dpid = "00:00:00:00:00:00:00:01" |
205
|
1 |
|
mock_get_switch.return_value = switch_obj |
206
|
|
|
|
207
|
|
|
# Mock tracepath loop to create only the initial result item |
208
|
1 |
|
mock_trace_loop.return_value = True |
209
|
|
|
|
210
|
|
|
# Creating a fake packet in |
211
|
1 |
|
msg = TraceMsg() |
212
|
1 |
|
msg.request_id = 111 |
213
|
1 |
|
pkt_in = {} |
214
|
1 |
|
pkt_in["dpid"] = "00:00:00:00:00:00:00:01" |
215
|
1 |
|
pkt_in["in_port"] = 1 |
216
|
1 |
|
pkt_in["msg"] = msg |
217
|
1 |
|
pkt_in["ethernet"] = "fake_ethernet_object" |
218
|
1 |
|
pkt_in["event"] = "fake_event_object" |
219
|
1 |
|
self.trace_manager.trace_pkt_in = [pkt_in] |
220
|
|
|
|
221
|
|
|
# Trace id to recover the result |
222
|
1 |
|
trace_id = 111 |
223
|
|
|
|
224
|
|
|
# Creating trace entries |
225
|
1 |
|
eth = {"dl_vlan": 100, "dl_type": 2048} |
226
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
227
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
228
|
1 |
|
entries = {"trace": switch} |
229
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
230
|
|
|
|
231
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
232
|
|
|
|
233
|
1 |
|
in_port = 1 |
234
|
1 |
|
probe_pkt = MagicMock() |
235
|
|
|
|
236
|
1 |
|
result = tracer.send_trace_probe(switch_obj, in_port, probe_pkt) |
237
|
|
|
|
238
|
1 |
|
mock_send_packet_out.assert_called_once() |
239
|
|
|
|
240
|
1 |
|
assert result[0]["dpid"] == "00:00:00:00:00:00:00:01" |
241
|
1 |
|
assert result[0]["port"] == 1 |
242
|
1 |
|
assert result[1] == "fake_event_object" |
243
|
1 |
|
mock_color.assert_called_once() |
244
|
1 |
|
mock_switch_colors.assert_called_once() |
245
|
|
|
|
246
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
247
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
248
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
249
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop") |
250
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.send_packet_out") |
251
|
1 |
|
def test_send_trace_probe_timeout( |
252
|
|
|
self, |
253
|
|
|
mock_send_packet_out, |
254
|
|
|
mock_trace_loop, |
255
|
|
|
mock_get_switch, |
256
|
|
|
mock_color, |
257
|
|
|
mock_switch_colors, |
258
|
|
|
): |
259
|
|
|
"""Test send_trace_probe with timeout.""" |
260
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
261
|
1 |
|
mock_send_packet_out.return_value = True |
262
|
|
|
|
263
|
1 |
|
switch_obj = MagicMock() |
264
|
1 |
|
switch_obj.dpid = "00:00:00:00:00:00:00:01" |
265
|
1 |
|
mock_get_switch.return_value = switch_obj |
266
|
|
|
|
267
|
|
|
# Mock tracepath loop to create only the initial result item |
268
|
1 |
|
mock_trace_loop.return_value = True |
269
|
|
|
|
270
|
|
|
# Creating a fake packet in |
271
|
1 |
|
self.trace_manager.trace_pkt_in = [] |
272
|
|
|
|
273
|
|
|
# Trace id to recover the result |
274
|
1 |
|
trace_id = 111 |
275
|
|
|
|
276
|
|
|
# Creating trace entries |
277
|
1 |
|
eth = {"dl_vlan": 100, "dl_type": 2048} |
278
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
279
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
280
|
1 |
|
entries = {"trace": switch} |
281
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
282
|
|
|
|
283
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
284
|
|
|
|
285
|
1 |
|
in_port = 1 |
286
|
1 |
|
probe_pkt = MagicMock() |
287
|
|
|
|
288
|
1 |
|
result = tracer.send_trace_probe(switch_obj, in_port, probe_pkt) |
289
|
|
|
|
290
|
1 |
|
assert mock_send_packet_out.call_count == 3 |
291
|
|
|
|
292
|
1 |
|
assert result[0] == "timeout" |
293
|
1 |
|
assert result[1] is False |
294
|
1 |
|
mock_color.assert_called_once() |
295
|
1 |
|
mock_switch_colors.assert_called_once() |
296
|
|
|
|
297
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
298
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
299
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
300
|
1 |
|
def test_check_loop(self, mock_get_switch, mock_color, mock_switch_colors): |
301
|
|
|
"""Test check_loop with loop detection.""" |
302
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
303
|
|
|
|
304
|
|
|
# Patch Switches.get_switch |
305
|
1 |
|
def wrap_get_switch(dpid): |
306
|
1 |
|
switch = MagicMock() |
307
|
1 |
|
switch.dpid = dpid |
308
|
1 |
|
return switch |
309
|
|
|
|
310
|
1 |
|
mock_get_switch.side_effect = wrap_get_switch |
311
|
|
|
|
312
|
|
|
# Trace id to recover the result |
313
|
1 |
|
trace_id = 111 |
314
|
|
|
|
315
|
|
|
# Creating trace entries |
316
|
1 |
|
eth = {"dl_vlan": 100, "dl_type": 2048} |
317
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
318
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
319
|
1 |
|
entries = {"trace": switch} |
320
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
321
|
|
|
|
322
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
323
|
|
|
|
324
|
|
|
# Patch tracepath loop to create a second result item |
325
|
1 |
|
rest = FormatRest() |
326
|
1 |
|
rest.add_trace_step( |
327
|
|
|
tracer.trace_result, |
328
|
|
|
trace_type="trace", |
329
|
|
|
dpid="00:00:00:00:00:00:00:01", |
330
|
|
|
port=1, |
331
|
|
|
) |
332
|
1 |
|
rest.add_trace_step( |
333
|
|
|
tracer.trace_result, |
334
|
|
|
trace_type="trace", |
335
|
|
|
dpid="00:00:00:00:00:00:00:02", |
336
|
|
|
port=2, |
337
|
|
|
) |
338
|
1 |
|
rest.add_trace_step( |
339
|
|
|
tracer.trace_result, |
340
|
|
|
trace_type="trace", |
341
|
|
|
dpid="00:00:00:00:00:00:00:03", |
342
|
|
|
port=3, |
343
|
|
|
) |
344
|
1 |
|
rest.add_trace_step( |
345
|
|
|
tracer.trace_result, |
346
|
|
|
trace_type="trace", |
347
|
|
|
dpid="00:00:00:00:00:00:00:01", |
348
|
|
|
port=1, |
349
|
|
|
) |
350
|
|
|
|
351
|
1 |
|
result = tracer.check_loop() |
352
|
1 |
|
mock_color.assert_called_once() |
353
|
1 |
|
mock_switch_colors.assert_called_once() |
354
|
1 |
|
assert result is True |
355
|
|
|
|
356
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
357
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
358
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
359
|
1 |
|
def test_check_loop_false(self, mock_get_switch, mock_color, mock_switch_colors): |
360
|
|
|
"""Test check_loop with no loop detection.""" |
361
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
362
|
|
|
|
363
|
|
|
# Patch Switches.get_switch |
364
|
1 |
|
def wrap_get_switch(dpid): |
365
|
1 |
|
switch = MagicMock() |
366
|
1 |
|
switch.dpid = dpid |
367
|
1 |
|
return switch |
368
|
|
|
|
369
|
1 |
|
mock_get_switch.side_effect = wrap_get_switch |
370
|
|
|
|
371
|
|
|
# Trace id to recover the result |
372
|
1 |
|
trace_id = 111 |
373
|
|
|
|
374
|
|
|
# Creating trace entries |
375
|
1 |
|
eth = {"dl_vlan": 100, "dl_type": 2048} |
376
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
377
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
378
|
1 |
|
entries = {"trace": switch} |
379
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
380
|
|
|
|
381
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
382
|
|
|
|
383
|
|
|
# Patch tracepath loop to create a second result item |
384
|
1 |
|
rest = FormatRest() |
385
|
1 |
|
rest.add_trace_step( |
386
|
|
|
tracer.trace_result, |
387
|
|
|
trace_type="trace", |
388
|
|
|
dpid="00:00:00:00:00:00:00:01", |
389
|
|
|
port=1, |
390
|
|
|
) |
391
|
1 |
|
rest.add_trace_step( |
392
|
|
|
tracer.trace_result, |
393
|
|
|
trace_type="trace", |
394
|
|
|
dpid="00:00:00:00:00:00:00:02", |
395
|
|
|
port=2, |
396
|
|
|
) |
397
|
|
|
|
398
|
1 |
|
result = tracer.check_loop() |
399
|
1 |
|
mock_color.assert_called_once() |
400
|
1 |
|
mock_switch_colors.assert_called_once() |
401
|
1 |
|
assert result == 0 |
402
|
|
|
|
403
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
404
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
405
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
406
|
1 |
|
def test_check_loop_port_different( |
407
|
|
|
self, mock_get_switch, mock_color, mock_switch_colors |
408
|
|
|
): |
409
|
|
|
"""Test check_loop with same switch and different port.""" |
410
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
411
|
|
|
|
412
|
|
|
# Patch Switches.get_switch |
413
|
1 |
|
def wrap_get_switch(dpid): |
414
|
1 |
|
switch = MagicMock() |
415
|
1 |
|
switch.dpid = dpid |
416
|
1 |
|
return switch |
417
|
|
|
|
418
|
1 |
|
mock_get_switch.side_effect = wrap_get_switch |
419
|
|
|
|
420
|
|
|
# Trace id to recover the result |
421
|
1 |
|
trace_id = 111 |
422
|
|
|
|
423
|
|
|
# Creating trace entries |
424
|
1 |
|
eth = {"dl_vlan": 100, "dl_type": 2048} |
425
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
426
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
427
|
1 |
|
entries = {"trace": switch} |
428
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
429
|
|
|
|
430
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
431
|
|
|
|
432
|
|
|
# Patch tracepath loop to create a second result item |
433
|
1 |
|
rest = FormatRest() |
434
|
1 |
|
rest.add_trace_step( |
435
|
|
|
tracer.trace_result, |
436
|
|
|
trace_type="trace", |
437
|
|
|
dpid="00:00:00:00:00:00:00:01", |
438
|
|
|
port=1, |
439
|
|
|
) |
440
|
1 |
|
rest.add_trace_step( |
441
|
|
|
tracer.trace_result, |
442
|
|
|
trace_type="trace", |
443
|
|
|
dpid="00:00:00:00:00:00:00:02", |
444
|
|
|
port=2, |
445
|
|
|
) |
446
|
1 |
|
rest.add_trace_step( |
447
|
|
|
tracer.trace_result, |
448
|
|
|
trace_type="trace", |
449
|
|
|
dpid="00:00:00:00:00:00:00:01", |
450
|
|
|
port=10, |
451
|
|
|
) |
452
|
|
|
|
453
|
1 |
|
result = tracer.check_loop() |
454
|
1 |
|
mock_color.assert_called_once() |
455
|
1 |
|
mock_switch_colors.assert_called_once() |
456
|
1 |
|
assert result == 0 |
457
|
|
|
|
458
|
1 |
View Code Duplication |
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
|
|
|
|
459
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
460
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
461
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe") |
462
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet") |
463
|
1 |
|
def test_tracepath_loop( |
464
|
|
|
self, |
465
|
|
|
mock_next_packet, |
466
|
|
|
mock_probe, |
467
|
|
|
mock_get_switch, |
468
|
|
|
mock_color, |
469
|
|
|
mock_switch_colors, |
470
|
|
|
): |
471
|
|
|
"""Test tracepath loop method. This test force the return |
472
|
|
|
after one normal trace.""" |
473
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
474
|
|
|
|
475
|
|
|
# Patch Switches.get_switch |
476
|
1 |
|
def wrap_get_switch(dpid): |
477
|
1 |
|
switch = MagicMock() |
478
|
1 |
|
switch.dpid = dpid |
479
|
1 |
|
return switch |
480
|
|
|
|
481
|
1 |
|
mock_get_switch.side_effect = wrap_get_switch |
482
|
|
|
|
483
|
1 |
|
mock_probe.return_value = [ |
484
|
|
|
{"dpid": "00:00:00:00:00:00:00:01", "port": 1}, |
485
|
|
|
"fake_event_object", |
486
|
|
|
] |
487
|
|
|
|
488
|
|
|
# Trace id to recover the result |
489
|
1 |
|
trace_id = 111 |
490
|
|
|
|
491
|
|
|
# Creating trace entries |
492
|
1 |
|
eth = {"dl_vlan": 100} |
493
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
494
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
495
|
1 |
|
entries = {"trace": switch} |
496
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
497
|
|
|
|
498
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
499
|
|
|
|
500
|
|
|
# Mock the next packt to stop the trace loop |
501
|
|
|
# pylint: disable=unused-argument |
502
|
1 |
|
def wrap_next_packet(entries, result, packet_in): |
503
|
1 |
|
tracer.trace_ended = True |
504
|
1 |
|
return "", "", "" |
505
|
|
|
|
506
|
1 |
|
mock_next_packet.side_effect = wrap_next_packet |
507
|
|
|
|
508
|
1 |
|
color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"} |
509
|
|
|
|
510
|
|
|
# Execute tracepath |
511
|
1 |
|
tracer.tracepath_loop(trace_entries, color, switch) |
512
|
1 |
|
result = tracer.trace_result |
513
|
|
|
|
514
|
1 |
|
mock_probe.assert_called_once() |
515
|
1 |
|
mock_color.assert_called_once() |
516
|
1 |
|
mock_switch_colors.assert_called_once() |
517
|
1 |
|
assert result[0]["type"] == "trace" |
518
|
1 |
|
assert result[0]["dpid"] == "00:00:00:00:00:00:00:01" |
519
|
|
|
|
520
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
521
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
522
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
523
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe") |
524
|
1 |
|
def test_tracepath_loop_timeout( |
525
|
|
|
self, mock_probe, mock_get_switch, mock_color, mock_switch_colors |
526
|
|
|
): |
527
|
|
|
"""Test tracepath loop method finishing with timeout.""" |
528
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
529
|
|
|
|
530
|
|
|
# Patch Switches.get_switch |
531
|
1 |
|
def wrap_get_switch(dpid): |
532
|
1 |
|
switch = MagicMock() |
533
|
1 |
|
switch.dpid = dpid |
534
|
1 |
|
return switch |
535
|
|
|
|
536
|
1 |
|
mock_get_switch.side_effect = wrap_get_switch |
537
|
|
|
|
538
|
1 |
|
mock_probe.return_value = ["timeout", "fake_event_object"] |
539
|
|
|
|
540
|
|
|
# Trace id to recover the result |
541
|
1 |
|
trace_id = 111 |
542
|
|
|
|
543
|
|
|
# Creating trace entries |
544
|
1 |
|
eth = {"dl_vlan": 100} |
545
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
546
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
547
|
1 |
|
entries = {"trace": switch} |
548
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
549
|
|
|
|
550
|
1 |
|
color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"} |
551
|
|
|
|
552
|
|
|
# Execute tracepath |
553
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
554
|
1 |
|
tracer.tracepath_loop(trace_entries, color, switch) |
555
|
|
|
|
556
|
1 |
|
result = tracer.trace_result |
557
|
|
|
|
558
|
1 |
|
mock_probe.assert_called_once() |
559
|
1 |
|
mock_color.assert_called_once() |
560
|
1 |
|
mock_switch_colors.assert_called_once() |
561
|
1 |
|
assert result[0]["type"] == "last" |
562
|
1 |
|
assert result[0]["reason"] == "done" |
563
|
1 |
|
assert result[0]["msg"] == "none" |
564
|
|
|
|
565
|
1 |
View Code Duplication |
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
|
|
|
|
566
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
567
|
1 |
|
@patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch") |
568
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe") |
569
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet") |
570
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.tracer.TracePath.check_loop") |
571
|
1 |
|
def test_tracepath_loop_with_loop( |
572
|
|
|
self, |
573
|
|
|
mock_check_loop, |
574
|
|
|
mock_next_packet, |
575
|
|
|
mock_probe, |
576
|
|
|
mock_get_switch, |
577
|
|
|
mock_color, |
578
|
|
|
mock_switch_colors, |
579
|
|
|
): |
580
|
|
|
"""Test tracepath loop method finishing with a loop.""" |
581
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
582
|
1 |
|
mock_check_loop.return_value = True |
583
|
|
|
|
584
|
|
|
# Patch Switches.get_switch |
585
|
1 |
|
def wrap_get_switch(dpid): |
586
|
1 |
|
switch = MagicMock() |
587
|
1 |
|
switch.dpid = dpid |
588
|
1 |
|
return switch |
589
|
|
|
|
590
|
1 |
|
mock_get_switch.side_effect = wrap_get_switch |
591
|
|
|
|
592
|
1 |
|
mock_probe.return_value = [ |
593
|
|
|
{"dpid": "00:00:00:00:00:00:00:01", "port": 1}, |
594
|
|
|
"fake_event_object", |
595
|
|
|
] |
596
|
|
|
|
597
|
|
|
# Trace id to recover the result |
598
|
1 |
|
trace_id = 111 |
599
|
|
|
|
600
|
|
|
# Creating trace entries |
601
|
1 |
|
eth = {"dl_vlan": 100} |
602
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
603
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
604
|
1 |
|
entries = {"trace": switch} |
605
|
1 |
|
trace_entries = self.trace_manager.is_entry_valid(entries) |
606
|
|
|
|
607
|
1 |
|
tracer = TracePath(self.trace_manager, trace_id, trace_entries) |
608
|
|
|
|
609
|
|
|
# Mock the next packt to stop the trace loop |
610
|
|
|
# pylint: disable=unused-argument |
611
|
1 |
|
def wrap_next_packet(entries, result, packet_in): |
612
|
|
|
tracer.trace_ended = True |
613
|
|
|
return "", "", "" |
614
|
|
|
|
615
|
1 |
|
mock_next_packet.side_effect = wrap_next_packet |
616
|
|
|
|
617
|
1 |
|
color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"} |
618
|
|
|
|
619
|
|
|
# Execute tracepath |
620
|
1 |
|
tracer.tracepath_loop(trace_entries, color, switch) |
621
|
1 |
|
result = tracer.trace_result |
622
|
|
|
|
623
|
1 |
|
mock_check_loop.assert_called_once() |
624
|
1 |
|
mock_next_packet.assert_not_called() |
625
|
1 |
|
mock_probe.assert_called_once() |
626
|
1 |
|
assert mock_get_switch.call_count == 3 |
627
|
1 |
|
mock_color.assert_called_once() |
628
|
1 |
|
mock_switch_colors.assert_called_once() |
629
|
|
|
|
630
|
1 |
|
assert result[0]["type"] == "trace" |
631
|
|
|
assert result[0]["dpid"] == "00:00:00:00:00:00:00:01" |
632
|
|
|
|