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