1
|
|
|
""" |
2
|
|
|
Test tracing.trace_entries |
3
|
|
|
""" |
4
|
|
|
from unittest import TestCase |
5
|
|
|
from unittest.mock import MagicMock, patch |
6
|
|
|
from pyof.foundation.network_types import Ethernet |
7
|
|
|
|
8
|
|
|
from napps.amlight.sdntrace.tracing import trace_pkt |
9
|
|
|
from napps.amlight.sdntrace.shared.switches import Switches |
10
|
|
|
from napps.amlight.sdntrace.tracing.trace_entries import TraceEntries |
11
|
|
|
from kytos.lib.helpers import ( |
12
|
|
|
get_interface_mock, |
13
|
|
|
get_link_mock, |
14
|
|
|
get_switch_mock, |
15
|
|
|
get_controller_mock, |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
# pylint: disable=too-many-public-methods, too-many-lines, protected-access |
20
|
|
|
class TestTracePkt(TestCase): |
21
|
|
|
"""Test all combinations for DPID""" |
22
|
|
|
|
23
|
|
|
def setUp(self): |
24
|
|
|
self.create_basic_switches(get_controller_mock()) |
25
|
|
|
|
26
|
|
|
# The decorator run_on_thread is patched, so methods that listen |
27
|
|
|
# for events do not run on threads while tested. |
28
|
|
|
# Decorators have to be patched before the methods that are |
29
|
|
|
# decorated with them are imported. |
30
|
|
|
patch("kytos.core.helpers.run_on_thread", lambda x: x).start() |
31
|
|
|
|
32
|
|
|
self.addCleanup(patch.stopall) |
33
|
|
|
|
34
|
|
View Code Duplication |
@classmethod |
|
|
|
|
35
|
|
|
def create_basic_switches(cls, controller): |
36
|
|
|
"""Create basic mock switches for Kytos controller.""" |
37
|
|
|
dpid_a = "00:00:00:00:00:00:00:01" |
38
|
|
|
dpid_b = "00:00:00:00:00:00:00:02" |
39
|
|
|
|
40
|
|
|
mock_switch_a = get_switch_mock(dpid_a, 0x04) |
41
|
|
|
mock_switch_b = get_switch_mock(dpid_b, 0x04) |
42
|
|
|
mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a) |
43
|
|
|
mock_interface_b = get_interface_mock("s2-eth1", 1, mock_switch_b) |
44
|
|
|
|
45
|
|
|
mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
46
|
|
|
mock_link.id = "cf0f4071be4" |
47
|
|
|
mock_switch_a.id = dpid_a |
48
|
|
|
mock_switch_a.as_dict.return_value = {"metadata": {}} |
49
|
|
|
mock_switch_b.id = dpid_b |
50
|
|
|
mock_switch_b.as_dict.return_value = {"metadata": {}} |
51
|
|
|
|
52
|
|
|
controller.switches = {dpid_a: mock_switch_a, dpid_b: mock_switch_b} |
53
|
|
|
|
54
|
|
|
Switches(controller.switches) |
55
|
|
|
|
56
|
|
|
def test_generate_trace_pkt_tcp(self): |
57
|
|
|
"""Test trace manager new trace creation.""" |
58
|
|
|
eth = {"dl_vlan": 100} |
59
|
|
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
60
|
|
|
switch = {"switch": dpid, "eth": eth} |
61
|
|
|
entries = {"trace": switch} |
62
|
|
|
|
63
|
|
|
trace_entries = TraceEntries() |
64
|
|
|
trace_entries.load_entries(entries) |
65
|
|
|
r_id = 999 |
66
|
|
|
|
67
|
|
|
# Coloring REST response |
68
|
|
|
color = {"color_value": "ee:ee:ee:ee:ee:01"} |
69
|
|
|
|
70
|
|
|
# new_trace does not check duplicated request. |
71
|
|
|
in_port, pkt = trace_pkt.generate_trace_pkt(trace_entries, color, r_id) |
72
|
|
|
|
73
|
|
|
self.assertEqual(entries["trace"]["switch"]["in_port"], in_port) |
74
|
|
|
self.assertEqual( |
75
|
|
|
pkt, |
76
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
77
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00" |
78
|
|
|
b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01" |
79
|
|
|
b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00" |
80
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
81
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
82
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
83
|
|
|
b"\x03sb.", |
84
|
|
|
) |
85
|
|
|
|
86
|
|
|
@patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color") |
87
|
|
|
@patch("napps.amlight.sdntrace.shared.colors.Colors._get_colors") |
88
|
|
|
def test_get_node_color_from_dpid(self, mock_color, mock_switch_colors): |
89
|
|
|
"""Test get color from dpid.""" |
90
|
|
|
mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01" |
91
|
|
|
|
92
|
|
|
switch, color = trace_pkt._get_node_color_from_dpid( |
93
|
|
|
"00:00:00:00:00:00:00:01" |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
self.assertEqual(switch.dpid, "00:00:00:00:00:00:00:01") |
97
|
|
|
self.assertEqual(color, "ee:ee:ee:ee:ee:01") |
98
|
|
|
mock_color.assert_called_once() |
99
|
|
|
mock_switch_colors.assert_called_once() |
100
|
|
|
|
101
|
|
|
def test_get_node_color_unknown_dpid(self): |
102
|
|
|
"""Test get color from unknown dpid.""" |
103
|
|
|
switch, color = trace_pkt._get_node_color_from_dpid( |
104
|
|
|
"99:99:99:99:99:99:99:99" |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
self.assertEqual(switch, 0) |
108
|
|
|
self.assertEqual(color, 0) |
109
|
|
|
|
110
|
|
|
def test_get_vlan_from_pkt(self): |
111
|
|
|
"""Test get color from unknown dpid.""" |
112
|
|
|
pkt = ( |
113
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
114
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00" |
115
|
|
|
b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01" |
116
|
|
|
b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00" |
117
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
118
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
119
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
120
|
|
|
b"\x03sb." |
121
|
|
|
) |
122
|
|
|
|
123
|
|
|
vid = trace_pkt._get_vlan_from_pkt(pkt) |
124
|
|
|
|
125
|
|
|
self.assertEqual(vid, 100) |
126
|
|
|
|
127
|
|
|
def test_process_packet(self): |
128
|
|
|
"""Test process packet to find the trace message.""" |
129
|
|
|
pkt = ( |
130
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
131
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00" |
132
|
|
|
b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01" |
133
|
|
|
b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00" |
134
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
135
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
136
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
137
|
|
|
b"\x03sb." |
138
|
|
|
) |
139
|
|
|
expected = ( |
140
|
|
|
b"\x80\x04\x95Q\x00\x00\x00\x00\x00" |
141
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
142
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
143
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
144
|
|
|
b"\x03sb." |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
ethernet = Ethernet() |
148
|
|
|
ethernet.unpack(pkt) |
149
|
|
|
|
150
|
|
|
trace_msg = trace_pkt.process_packet(ethernet) |
151
|
|
|
self.assertEqual(trace_msg, expected) |
152
|
|
|
|
153
|
|
|
@patch( |
154
|
|
|
"napps.amlight.sdntrace.tracing.trace_pkt._get_node_color_from_dpid" |
155
|
|
|
) |
156
|
|
|
def test_prepare_next_packet(self, mock_get_color): |
157
|
|
|
"""Test trace prepare next packet.""" |
158
|
|
|
color_switch = MagicMock() |
159
|
|
|
color_switch.dpid = "00:00:00:00:00:00:00:01" |
160
|
|
|
mock_get_color.return_value = [color_switch, "ee:ee:ee:ee:ee:01"] |
161
|
|
|
|
162
|
|
|
eth = {"dl_vlan": 100} |
163
|
|
|
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1} |
164
|
|
|
switch = {"switch": dpid, "eth": eth} |
165
|
|
|
entries = {"trace": switch} |
166
|
|
|
|
167
|
|
|
trace_entries = TraceEntries() |
168
|
|
|
trace_entries.load_entries(entries) |
169
|
|
|
|
170
|
|
|
raw = ( |
171
|
|
|
b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee" |
172
|
|
|
b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00" |
173
|
|
|
b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01" |
174
|
|
|
b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00" |
175
|
|
|
b"\x00\x00\x8c(napps.amlight.sdntrace.tracing." |
176
|
|
|
b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)" |
177
|
|
|
b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7" |
178
|
|
|
b"\x03sb." |
179
|
|
|
) |
180
|
|
|
|
181
|
|
|
packet_in_msg = MagicMock() |
182
|
|
|
packet_in_msg.data.value = raw |
183
|
|
|
packet_in_msg.in_port.value = 1 |
184
|
|
|
packet_in_msg.header.version = 1 |
185
|
|
|
|
186
|
|
|
event = MagicMock() |
187
|
|
|
event.source.switch = "00:00:00:00:00:00:00:02" |
188
|
|
|
event.content = {"message": packet_in_msg} |
189
|
|
|
|
190
|
|
|
pkt_in = { |
191
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
192
|
|
|
"in_port": 1, |
193
|
|
|
"msg": "", |
194
|
|
|
"ethernet": "", |
195
|
|
|
"event": event, |
196
|
|
|
} |
197
|
|
|
result = {"dpid": pkt_in["dpid"], "port": pkt_in["in_port"]} |
198
|
|
|
|
199
|
|
|
# result = [result_trace, result_color, result_switch] |
200
|
|
|
result = trace_pkt.prepare_next_packet(trace_entries, result, event) |
201
|
|
|
|
202
|
|
|
self.assertEqual(result[0], trace_entries) |
203
|
|
|
self.assertEqual(result[1], mock_get_color()[1]) |
204
|
|
|
self.assertEqual(result[2].dpid, color_switch.dpid) |
205
|
|
|
|