1
|
|
|
""" |
2
|
|
|
Test tracing.trace_entries |
3
|
|
|
""" |
4
|
|
|
|
5
|
1 |
|
from unittest.mock import MagicMock, patch |
6
|
1 |
|
from pyof.foundation.network_types import Ethernet |
7
|
1 |
|
import pytest |
8
|
|
|
|
9
|
1 |
|
from napps.amlight.sdntrace.tracing import trace_pkt |
10
|
1 |
|
from napps.amlight.sdntrace.shared.switches import Switches |
11
|
1 |
|
from napps.amlight.sdntrace.tracing.trace_entries import TraceEntries |
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, protected-access |
21
|
1 |
|
class TestTracePkt: |
22
|
|
|
"""Test all combinations for DPID""" |
23
|
|
|
|
24
|
1 |
|
@pytest.fixture(autouse=True) |
25
|
1 |
|
def commomn_patches(self, request): |
26
|
|
|
"""This function handles setup and cleanup for patches""" |
27
|
|
|
# This fixture sets up the common patches, |
28
|
|
|
# and a finalizer is added using addfinalizer to stop |
29
|
|
|
# the common patches after each test. This ensures that the cleanup |
30
|
|
|
# is performed after each test, and additional patch decorators |
31
|
|
|
# can be used within individual test functions. |
32
|
|
|
|
33
|
1 |
|
patcher = patch("kytos.core.helpers.run_on_thread", lambda x: x) |
34
|
1 |
|
mock_patch = patcher.start() |
35
|
|
|
|
36
|
1 |
|
_ = request.function.__name__ |
37
|
|
|
|
38
|
1 |
|
def cleanup(): |
39
|
1 |
|
patcher.stop() |
40
|
|
|
|
41
|
1 |
|
request.addfinalizer(cleanup) |
42
|
1 |
|
return mock_patch |
43
|
|
|
|
44
|
1 |
|
def setup_method(self): |
45
|
|
|
"""Set up before each test method""" |
46
|
1 |
|
self.create_basic_switches(get_controller_mock()) |
47
|
|
|
|
48
|
1 |
View Code Duplication |
@classmethod |
|
|
|
|
49
|
1 |
|
def create_basic_switches(cls, controller): |
50
|
|
|
"""Create basic mock switches for Kytos controller.""" |
51
|
1 |
|
dpid_a = "00:00:00:00:00:00:00:01" |
52
|
1 |
|
dpid_b = "00:00:00:00:00:00:00:02" |
53
|
|
|
|
54
|
1 |
|
mock_switch_a = get_switch_mock(dpid_a, 0x04) |
55
|
1 |
|
mock_switch_b = get_switch_mock(dpid_b, 0x04) |
56
|
1 |
|
mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a) |
57
|
1 |
|
mock_interface_b = get_interface_mock("s2-eth1", 1, mock_switch_b) |
58
|
|
|
|
59
|
1 |
|
mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
60
|
1 |
|
mock_link.id = "cf0f4071be4" |
61
|
1 |
|
mock_switch_a.id = dpid_a |
62
|
1 |
|
mock_switch_a.as_dict.return_value = {"metadata": {}} |
63
|
1 |
|
mock_switch_b.id = dpid_b |
64
|
1 |
|
mock_switch_b.as_dict.return_value = {"metadata": {}} |
65
|
|
|
|
66
|
1 |
|
controller.switches = {dpid_a: mock_switch_a, dpid_b: mock_switch_b} |
67
|
|
|
|
68
|
1 |
|
Switches(controller.switches) |
69
|
|
|
|
70
|
1 |
View Code Duplication |
@patch("napps.amlight.sdntrace.shared.extd_nw_types.randrange") |
|
|
|
|
71
|
1 |
|
def test_generate_trace_pkt_tcp(self, mock_rand): |
72
|
|
|
"""Test trace manager new trace creation.""" |
73
|
1 |
|
mock_rand.return_value = 0 |
74
|
1 |
|
eth = {"dl_vlan": 100} |
75
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
76
|
1 |
|
ip = {"nw_proto": 6} |
77
|
1 |
|
tp = {"tp_src": 1, "tp_dst": 2} |
78
|
1 |
|
switch = {"switch": dpid, "eth": eth, "ip": ip, "tp": tp} |
79
|
1 |
|
entries = {"trace": switch} |
80
|
|
|
|
81
|
1 |
|
trace_entries = TraceEntries() |
82
|
1 |
|
trace_entries.load_entries(entries) |
83
|
1 |
|
r_id = 999 |
84
|
|
|
|
85
|
|
|
# Coloring REST response |
86
|
1 |
|
color = {"color_value": "ee:ee:ee:ee:ee:01"} |
87
|
|
|
|
88
|
|
|
# new_trace does not check duplicated request. |
89
|
1 |
|
in_port, pkt = trace_pkt.generate_trace_pkt(trace_entries, color, r_id) |
90
|
|
|
|
91
|
1 |
|
assert entries["trace"]["switch"]["in_port"] == in_port |
92
|
1 |
|
assert ( |
93
|
|
|
pkt == b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
94
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00\x84\x00\x00\x00" |
95
|
|
|
b"\x00\xff\x06\xb7o\x01\x01\x01\x01\x01\x01\x01\x02" |
96
|
|
|
b"\x00\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00P" |
97
|
|
|
b"\x02\x00\x00\xb5\x9f\x00\x00\x80\x04\x95Q\x00\x00" |
98
|
|
|
b"\x00\x00\x00\x00\x00\x8c(napps.amlight.sdntrace." |
99
|
|
|
b"tracing.trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
100
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7\x03sb." |
101
|
|
|
) |
102
|
|
|
|
103
|
1 |
View Code Duplication |
@patch("napps.amlight.sdntrace.shared.extd_nw_types.randrange") |
|
|
|
|
104
|
1 |
|
def test_generate_trace_pkt_udp(self, mock_rand): |
105
|
|
|
"""Test trace manager new trace creation.""" |
106
|
1 |
|
mock_rand.return_value = 0 |
107
|
1 |
|
eth = {"dl_vlan": 100} |
108
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
109
|
1 |
|
ip = {"nw_proto": 17} |
110
|
1 |
|
tp = {"tp_src": 1, "tp_dst": 2} |
111
|
1 |
|
switch = {"switch": dpid, "eth": eth, "ip": ip, "tp": tp} |
112
|
1 |
|
entries = {"trace": switch} |
113
|
|
|
|
114
|
1 |
|
trace_entries = TraceEntries() |
115
|
1 |
|
trace_entries.load_entries(entries) |
116
|
1 |
|
r_id = 999 |
117
|
|
|
|
118
|
|
|
# Coloring REST response |
119
|
1 |
|
color = {"color_value": "ee:ee:ee:ee:ee:01"} |
120
|
|
|
|
121
|
|
|
# new_trace does not check duplicated request. |
122
|
1 |
|
in_port, pkt = trace_pkt.generate_trace_pkt(trace_entries, color, r_id) |
123
|
1 |
|
assert entries["trace"]["switch"]["in_port"] == in_port |
124
|
1 |
|
assert ( |
125
|
|
|
pkt == b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
126
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00x\x00\x00\x00\x00" |
127
|
|
|
b"\xff\x11\xb7p\x01\x01\x01\x01\x01\x01\x01\x02\x00" |
128
|
|
|
b"\x01\x00\x02\x00\x08\x05\x9b\x80\x04\x95Q\x00\x00" |
129
|
|
|
b"\x00\x00\x00\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
130
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)\x81\x94}\x94" |
131
|
|
|
b"\x8c\x0b_request_id\x94M\xe7\x03sb." |
132
|
|
|
) |
133
|
|
|
|
134
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
135
|
1 |
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
136
|
1 |
|
def test_get_node_color_from_dpid(self, mock_color, mock_switch_colors): |
137
|
|
|
"""Test get color from dpid.""" |
138
|
1 |
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
139
|
|
|
|
140
|
1 |
|
switch, color = trace_pkt._get_node_color_from_dpid("00:00:00:00:00:00:00:01") |
141
|
|
|
|
142
|
1 |
|
assert switch.dpid == "00:00:00:00:00:00:00:01" |
143
|
1 |
|
assert color == "ee:ee:ee:ee:ee:01" |
144
|
1 |
|
mock_color.assert_called_once() |
145
|
1 |
|
mock_switch_colors.assert_called_once() |
146
|
|
|
|
147
|
1 |
|
def test_get_node_color_unknown_dpid(self): |
148
|
|
|
"""Test get color from unknown dpid.""" |
149
|
1 |
|
switch, color = trace_pkt._get_node_color_from_dpid("99:99:99:99:99:99:99:99") |
150
|
|
|
|
151
|
1 |
|
assert switch == 0 |
152
|
1 |
|
assert color == 0 |
153
|
|
|
|
154
|
1 |
|
def test_get_vlan_from_pkt(self): |
155
|
|
|
"""Test get color from unknown dpid.""" |
156
|
1 |
|
pkt = ( |
157
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
158
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00" |
159
|
|
|
b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01" |
160
|
|
|
b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00" |
161
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
162
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
163
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
164
|
|
|
b"\x03sb." |
165
|
|
|
) |
166
|
|
|
|
167
|
1 |
|
vid = trace_pkt._get_vlan_from_pkt(pkt) |
168
|
|
|
|
169
|
1 |
|
assert vid == 100 |
170
|
|
|
|
171
|
1 |
|
def test_process_packet(self): |
172
|
|
|
"""Test process packet to find the trace message.""" |
173
|
1 |
|
pkt = ( |
174
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
175
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00" |
176
|
|
|
b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01" |
177
|
|
|
b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00" |
178
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
179
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
180
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
181
|
|
|
b"\x03sb." |
182
|
|
|
) |
183
|
1 |
|
expected = ( |
184
|
|
|
b"\x80\x04\x95Q\x00\x00\x00\x00\x00" |
185
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
186
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
187
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
188
|
|
|
b"\x03sb." |
189
|
|
|
) |
190
|
|
|
|
191
|
1 |
|
ethernet = Ethernet() |
192
|
1 |
|
ethernet.unpack(pkt) |
193
|
|
|
|
194
|
1 |
|
trace_msg = trace_pkt.process_packet(ethernet) |
195
|
1 |
|
assert trace_msg == expected |
196
|
|
|
|
197
|
1 |
|
def test_process_packet_tcp(self): |
198
|
|
|
"""Test process TCP packet to find the trace message.""" |
199
|
1 |
|
pkt = ( |
200
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
201
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00\x84\x00\x00\x00" |
202
|
|
|
b"\x00\xff\x06\xb7o\x01\x01\x01\x01\x01\x01\x01\x02" |
203
|
|
|
b"\x00\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00P" |
204
|
|
|
b"\x02\x00\x00\xb5\x9f\x00\x00\x80\x04\x95Q\x00\x00" |
205
|
|
|
b"\x00\x00\x00\x00\x00\x8c(napps.amlight.sdntrace." |
206
|
|
|
b"tracing.trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
207
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7\x03sb." |
208
|
|
|
) |
209
|
1 |
|
expected = ( |
210
|
|
|
b"\x80\x04\x95Q\x00\x00\x00\x00\x00" |
211
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
212
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
213
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
214
|
|
|
b"\x03sb." |
215
|
|
|
) |
216
|
1 |
|
ethernet = Ethernet() |
217
|
1 |
|
ethernet.unpack(pkt) |
218
|
1 |
|
trace_msg = trace_pkt.process_packet(ethernet) |
219
|
1 |
|
assert trace_msg == expected |
220
|
|
|
|
221
|
1 |
|
def test_process_packet_udp(self): |
222
|
|
|
"""Test process UDP packet to find the trace message.""" |
223
|
1 |
|
pkt = ( |
224
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
225
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00x\x00\x00\x00\x00" |
226
|
|
|
b"\xff\x11\xb7p\x01\x01\x01\x01\x01\x01\x01\x02\x00" |
227
|
|
|
b"\x01\x00\x02\x00\x08\x05\x9b\x80\x04\x95Q\x00\x00" |
228
|
|
|
b"\x00\x00\x00\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
229
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)\x81\x94}\x94" |
230
|
|
|
b"\x8c\x0b_request_id\x94M\xe7\x03sb." |
231
|
|
|
) |
232
|
1 |
|
expected = ( |
233
|
|
|
b"\x80\x04\x95Q\x00\x00\x00\x00\x00" |
234
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
235
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
236
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
237
|
|
|
b"\x03sb." |
238
|
|
|
) |
239
|
1 |
|
ethernet = Ethernet() |
240
|
1 |
|
ethernet.unpack(pkt) |
241
|
1 |
|
trace_msg = trace_pkt.process_packet(ethernet) |
242
|
1 |
|
assert trace_msg == expected |
243
|
|
|
|
244
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.trace_pkt._get_node_color_from_dpid") |
245
|
1 |
|
def test_prepare_next_packet(self, mock_get_color): |
246
|
|
|
"""Test trace prepare next packet.""" |
247
|
1 |
|
color_switch = MagicMock() |
248
|
1 |
|
color_switch.dpid = "00:00:00:00:00:00:00:01" |
249
|
1 |
|
mock_get_color.return_value = [color_switch, "ee:ee:ee:ee:ee:01"] |
250
|
|
|
|
251
|
1 |
|
eth = {"dl_vlan": 100} |
252
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
253
|
1 |
|
switch = {"switch": dpid, "eth": eth} |
254
|
1 |
|
entries = {"trace": switch} |
255
|
|
|
|
256
|
1 |
|
trace_entries = TraceEntries() |
257
|
1 |
|
trace_entries.load_entries(entries) |
258
|
|
|
|
259
|
1 |
|
raw = ( |
260
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
261
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00" |
262
|
|
|
b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01" |
263
|
|
|
b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00" |
264
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
265
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
266
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
267
|
|
|
b"\x03sb." |
268
|
|
|
) |
269
|
|
|
|
270
|
1 |
|
packet_in_msg = MagicMock() |
271
|
1 |
|
packet_in_msg.data.value = raw |
272
|
1 |
|
packet_in_msg.in_port.value = 1 |
273
|
1 |
|
packet_in_msg.header.version = 1 |
274
|
|
|
|
275
|
1 |
|
event = MagicMock() |
276
|
1 |
|
event.source.switch = "00:00:00:00:00:00:00:02" |
277
|
1 |
|
event.content = {"message": packet_in_msg} |
278
|
|
|
|
279
|
1 |
|
pkt_in = { |
280
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
281
|
|
|
"in_port": 1, |
282
|
|
|
"msg": "", |
283
|
|
|
"ethernet": "", |
284
|
|
|
"event": event, |
285
|
|
|
} |
286
|
1 |
|
result = {"dpid": pkt_in["dpid"], "port": pkt_in["in_port"]} |
287
|
|
|
|
288
|
|
|
# result = [result_trace, result_color, result_switch] |
289
|
1 |
|
result = trace_pkt.prepare_next_packet(trace_entries, result, event) |
290
|
|
|
|
291
|
1 |
|
assert result[0] == trace_entries |
292
|
1 |
|
assert result[1] == mock_get_color()[1] |
293
|
1 |
|
assert result[2].dpid == color_switch.dpid |
294
|
|
|
|
295
|
1 |
|
@patch("napps.amlight.sdntrace.tracing.trace_pkt._get_node_color_from_dpid") |
296
|
1 |
|
def test_prepare_next_packet_no_vlan(self, mock_get_color): |
297
|
|
|
"""Test trace prepare next packet.""" |
298
|
1 |
|
color_switch = MagicMock() |
299
|
1 |
|
color_switch.dpid = "00:00:00:00:00:00:00:01" |
300
|
1 |
|
mock_get_color.return_value = [color_switch, "ee:ee:ee:ee:ee:01"] |
301
|
1 |
|
ip = {"nw_src": "0.0.0.1", "nw_dst": "0.0.0.2", "nw_tos": 5, "nw_proto": 6} |
302
|
1 |
|
tp = {"tp_src": 1, "tp_dst": 2} |
303
|
1 |
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
304
|
1 |
|
switch = {"switch": dpid, "ip": ip, "tp": tp} |
305
|
1 |
|
entries = {"trace": switch} |
306
|
|
|
|
307
|
1 |
|
trace_entries = TraceEntries() |
308
|
1 |
|
trace_entries.load_entries(entries) |
309
|
|
|
|
310
|
1 |
|
raw = ( |
311
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
312
|
|
|
b"\x01\x08\x00E\x14\x00\x84\x00\x00\x00\x00\xff" |
313
|
|
|
b"\x06\xbb]\x00\x00\x00\x01\x00\x00\x00\x02\x00" |
314
|
|
|
b"\x01\x00\x02\x19,Jh\x00\x00\x00\x00P\x02\x00" |
315
|
|
|
b"\x00\xe4\xbd\x00\x00\x80\x04\x95Q\x00\x00\x00" |
316
|
|
|
b"\x00\x00\x00\x00\x8c(napps.amlight.sdntrace." |
317
|
|
|
b"tracing.trace_msg\x94\x8c\x08TraceMsg\x94\x93" |
318
|
|
|
b"\x94)\x81\x94}\x94\x8c\x0b_request_id\x94M6usb." |
319
|
|
|
) |
320
|
|
|
|
321
|
1 |
|
packet_in_msg = MagicMock() |
322
|
1 |
|
packet_in_msg.data.value = raw |
323
|
1 |
|
packet_in_msg.in_port.value = 1 |
324
|
1 |
|
packet_in_msg.header.version = 1 |
325
|
|
|
|
326
|
1 |
|
event = MagicMock() |
327
|
1 |
|
event.source.switch = "00:00:00:00:00:00:00:02" |
328
|
1 |
|
event.content = {"message": packet_in_msg} |
329
|
|
|
|
330
|
1 |
|
result = {"dpid": "00:00:00:00:00:00:00:01"} |
331
|
|
|
|
332
|
1 |
|
result = trace_pkt.prepare_next_packet(trace_entries, result, event) |
333
|
|
|
|
334
|
|
|
assert result[0].dl_vlan == 0 |
335
|
|
|
|