1
|
|
|
"""Test Flow serializer for OF 1.0 methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from napps.kytos.flow_manager.serializers.v0x04 import FlowSerializer13 |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestFlowSerializer13(TestCase): |
9
|
|
|
"""Test the FlowSerializer13 class.""" |
10
|
|
|
|
11
|
|
|
def setUp(self): |
12
|
|
|
"""Execute steps before each tests.""" |
13
|
|
|
self.napp = FlowSerializer13() |
14
|
|
|
|
15
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
16
|
|
|
'_actions_from_list') |
17
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
18
|
|
|
'_match_from_dict') |
19
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.InstructionApplyAction') |
20
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowMod') |
21
|
|
|
def test_from_dict(self, *args): |
22
|
|
|
"""Test from_dict method.""" |
23
|
|
|
(mock_flow_mod, mock_instruction, mock_match_from_dict, |
24
|
|
|
mock_actions_from_list) = args |
25
|
|
|
flow_mod = MagicMock() |
26
|
|
|
flow_mod.instructions = [] |
27
|
|
|
flow_mod.match.oxm_match_fields = [] |
28
|
|
|
instruction = MagicMock() |
29
|
|
|
|
30
|
|
|
mock_flow_mod.return_value = flow_mod |
31
|
|
|
mock_instruction.return_value = instruction |
32
|
|
|
mock_match_from_dict.return_value = 'match_from_dict' |
33
|
|
|
mock_actions_from_list.return_value = 'actions' |
34
|
|
|
|
35
|
|
|
self.napp.flow_attributes = {'any'} |
36
|
|
|
dictionary = {'any': 'any_data', |
37
|
|
|
'match': 'match_data', |
38
|
|
|
'actions': 'actions_data'} |
39
|
|
|
return_flow_mod = self.napp.from_dict(dictionary) |
40
|
|
|
|
41
|
|
|
self.assertEqual(return_flow_mod.any, 'any_data') |
42
|
|
|
self.assertEqual(return_flow_mod.match.oxm_match_fields, |
43
|
|
|
[list('match_from_dict')]) |
44
|
|
|
self.assertEqual(return_flow_mod.instructions, [instruction]) |
45
|
|
|
mock_match_from_dict.assert_called_with('match_data') |
46
|
|
|
mock_actions_from_list.assert_called_with('actions_data') |
47
|
|
|
instruction.actions.extend.assert_called_with(list('actions')) |
48
|
|
|
|
49
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.IPAddress') |
50
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.HWAddress') |
51
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.OxmTLV') |
52
|
|
|
def test_match_from_dict(self, *args): |
53
|
|
|
"""Test _match_from_dict method.""" |
54
|
|
|
(mock_oxm_tlv, mock_hw_address, mock_ip_address) = args |
55
|
|
|
hw_address = MagicMock() |
56
|
|
|
hw_address.pack.return_value = 'hw_address' |
57
|
|
|
mock_hw_address.return_value = hw_address |
58
|
|
|
ip_address = MagicMock() |
59
|
|
|
ip_address.pack.return_value = 'ip_address' |
60
|
|
|
mock_ip_address.return_value = ip_address |
61
|
|
|
|
62
|
|
|
dictionary = {'dl_vlan_pcp': 0, 'nw_proto': 1, 'dl_vlan': 2, |
63
|
|
|
'dl_src': 3, 'dl_dst': 4, 'nw_src': 5, 'nw_dst': 6, |
64
|
|
|
'in_port': 7, 'dl_type': 8} |
65
|
|
|
|
66
|
|
|
tlv_generator = self.napp._match_from_dict(dictionary) |
67
|
|
|
|
68
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, b'\x00') |
69
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, b'\x01') |
70
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, b'\x10\x02') |
71
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, 'hw_address') |
72
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, 'hw_address') |
73
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, 'ip_address') |
74
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, 'ip_address') |
75
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, b'\x00\x00\x00\x07') |
76
|
|
|
self.assertEqual(next(tlv_generator).oxm_value, b'\x00\x08') |
77
|
|
|
|
78
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
79
|
|
|
'_action_from_dict') |
80
|
|
|
def test_actions_from_list(self, mock_action_from_dict): |
81
|
|
|
"""Test _actions_from_list method.""" |
82
|
|
|
mock_action_from_dict.side_effect = [None, 'action_2'] |
83
|
|
|
actions = ['action_1', 'action_2'] |
84
|
|
|
|
85
|
|
|
generator = self.napp._actions_from_list(actions) |
86
|
|
|
return_action = next(generator) |
87
|
|
|
|
88
|
|
|
self.assertEqual(return_action, 'action_2') |
89
|
|
|
|
90
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.ActionPopVLAN') |
91
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.ActionPush') |
92
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.ActionOutput') |
93
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.ActionSetField') |
94
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
95
|
|
|
'_create_vlan_tlv') |
96
|
|
|
def test_action_from_dict(self, *args): |
97
|
|
|
"""Test _action_from_dict method.""" |
98
|
|
|
(mock_create_vlan_tlv, mock_action_set_field, mock_action_output, |
99
|
|
|
mock_action_push, mock_action_pop_vlan) = args |
100
|
|
|
|
101
|
|
|
mock_create_vlan_tlv.return_value = 'tlv' |
102
|
|
|
mock_action_set_field.return_value = 'action_set_field' |
103
|
|
|
mock_action_output.return_value = 'action_output' |
104
|
|
|
mock_action_push.return_value = 'action_push' |
105
|
|
|
mock_action_pop_vlan.return_value = 'action_pop_vlan' |
106
|
|
|
|
107
|
|
|
action_1 = {'action_type': 'set_vlan', 'vlan_id': 'id'} |
108
|
|
|
return_1 = self.napp._action_from_dict(action_1) |
109
|
|
|
mock_create_vlan_tlv.assert_called_with(vlan_id='id') |
110
|
|
|
mock_action_set_field.assert_called_with(field='tlv') |
111
|
|
|
self.assertEqual(return_1, 'action_set_field') |
112
|
|
|
|
113
|
|
|
action_2 = {'action_type': 'output', 'port': 'controller'} |
114
|
|
|
return_2 = self.napp._action_from_dict(action_2) |
115
|
|
|
mock_action_output.assert_called_with(port=0xfffffffd) |
116
|
|
|
self.assertEqual(return_2, 'action_output') |
117
|
|
|
|
118
|
|
|
action_3 = {'action_type': 'output', 'port': 'any'} |
119
|
|
|
return_3 = self.napp._action_from_dict(action_3) |
120
|
|
|
mock_action_output.assert_called_with(port='any') |
121
|
|
|
self.assertEqual(return_3, 'action_output') |
122
|
|
|
|
123
|
|
|
action_4 = {'action_type': 'push_vlan', 'tag_type': 's'} |
124
|
|
|
return_4 = self.napp._action_from_dict(action_4) |
125
|
|
|
mock_action_push.assert_called_with(action_type=17, ethertype=0x88a8) |
126
|
|
|
self.assertEqual(return_4, 'action_push') |
127
|
|
|
|
128
|
|
|
action_5 = {'action_type': 'push_vlan', 'tag_type': 'c'} |
129
|
|
|
return_5 = self.napp._action_from_dict(action_5) |
130
|
|
|
mock_action_push.assert_called_with(action_type=17, ethertype=0x8100) |
131
|
|
|
self.assertEqual(return_5, 'action_push') |
132
|
|
|
|
133
|
|
|
action_6 = {'action_type': 'pop_vlan'} |
134
|
|
|
return_6 = self.napp._action_from_dict(action_6) |
135
|
|
|
self.assertEqual(return_6, 'action_pop_vlan') |
136
|
|
|
|
137
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.OxmTLV') |
138
|
|
|
def test_create_vlan_tlv(self, mock_oxm_tlv): |
139
|
|
|
"""Test _create_vlan_tlv method.""" |
140
|
|
|
tlv = self.napp._create_vlan_tlv(1) |
141
|
|
|
|
142
|
|
|
self.assertEqual(tlv.oxm_field, 6) |
143
|
|
|
self.assertEqual(tlv.oxm_value, b'\x10\x01') |
144
|
|
|
|
145
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
146
|
|
|
'_actions_to_list') |
147
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
148
|
|
|
'_match_to_dict') |
149
|
|
|
def test_to_dict(self, *args): |
150
|
|
|
"""Test to_dict method.""" |
151
|
|
|
(mock_match_to_dict, mock_actions_to_list) = args |
152
|
|
|
mock_match_to_dict.return_value = 'match_to_dict' |
153
|
|
|
mock_actions_to_list.return_value = 'actions_to_list' |
154
|
|
|
|
155
|
|
|
flow_stats = MagicMock() |
156
|
|
|
flow_stats.flow_attr = MagicMock() |
157
|
|
|
self.napp.flow_attributes = {'flow_attr'} |
158
|
|
|
|
159
|
|
|
flow_dict = self.napp.to_dict(flow_stats) |
160
|
|
|
|
161
|
|
|
expected = {'flow_attr': flow_stats.flow_attr.value, |
162
|
|
|
'match': 'match_to_dict', |
163
|
|
|
'actions': 'actions_to_list'} |
164
|
|
|
|
165
|
|
|
self.assertEqual(flow_dict, expected) |
166
|
|
|
|
167
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.HWAddress') |
168
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.IPAddress') |
169
|
|
|
def test_match_to_dict(self, *args): |
170
|
|
|
"""Test _match_to_dict method.""" |
171
|
|
|
(mock_ip_address, mock_hw_address) = args |
172
|
|
|
ip_address = MagicMock() |
173
|
|
|
hw_address = MagicMock() |
174
|
|
|
mock_ip_address.return_value = ip_address |
175
|
|
|
mock_hw_address.return_value = hw_address |
176
|
|
|
|
177
|
|
|
flow_stats = MagicMock() |
178
|
|
|
flow_stats.match.oxm_match_fields = [] |
179
|
|
|
for oxm_field in [0, 6, 4, 3, 11, 12]: |
180
|
|
|
field = MagicMock() |
181
|
|
|
field.oxm_field = oxm_field |
182
|
|
|
flow_stats.match.oxm_match_fields.append(field) |
183
|
|
|
|
184
|
|
|
match_dict = self.napp._match_to_dict(flow_stats) |
185
|
|
|
expected = {'in_port': 0, 'dl_vlan': 0, 'dl_src': str(hw_address), |
186
|
|
|
'dl_dst': str(hw_address), 'nw_src': str(ip_address), |
187
|
|
|
'nw_dst': str(ip_address)} |
188
|
|
|
|
189
|
|
|
self.assertEqual(match_dict, expected) |
190
|
|
|
|
191
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
192
|
|
|
'_action_to_dict') |
193
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x04.FlowSerializer13.' |
194
|
|
|
'_filter_actions') |
195
|
|
|
def test_actions_to_list(self, *args): |
196
|
|
|
"""Test _actions_to_list method.""" |
197
|
|
|
(mock_filter_actions, mock_action_to_dict) = args |
198
|
|
|
mock_filter_actions.return_value = ['action'] |
199
|
|
|
mock_action_to_dict.return_value = 'action_dict' |
200
|
|
|
flow_stats = MagicMock() |
201
|
|
|
|
202
|
|
|
actions_list = self.napp._actions_to_list(flow_stats) |
203
|
|
|
|
204
|
|
|
mock_filter_actions.assert_called_with(flow_stats) |
205
|
|
|
mock_action_to_dict.assert_called_with('action') |
206
|
|
|
self.assertEqual(actions_list, ['action_dict']) |
207
|
|
|
|
208
|
|
|
def test_action_to_dict(self): |
209
|
|
|
"""Test _action_to_dict method.""" |
210
|
|
|
action_1 = MagicMock() |
211
|
|
|
action_1.action_type = 25 |
212
|
|
|
action_1.field.oxm_field = 6 |
213
|
|
|
action_1.field.oxm_value = MagicMock() |
214
|
|
|
return_1 = self.napp._action_to_dict(action_1) |
215
|
|
|
|
216
|
|
|
action_2 = MagicMock() |
217
|
|
|
action_2.action_type = 0 |
218
|
|
|
action_2.port = 0xfffffffd |
219
|
|
|
return_2 = self.napp._action_to_dict(action_2) |
220
|
|
|
expected = {'action_type': 'output', 'port': 'controller'} |
221
|
|
|
self.assertEqual(return_2, expected) |
222
|
|
|
|
223
|
|
|
action_3 = MagicMock() |
224
|
|
|
action_3.action_type = 0 |
225
|
|
|
action_3.port.value = 'any' |
226
|
|
|
return_3 = self.napp._action_to_dict(action_3) |
227
|
|
|
expected = {'action_type': 'output', 'port': 'any'} |
228
|
|
|
self.assertEqual(return_3, expected) |
229
|
|
|
|
230
|
|
|
action_4 = MagicMock() |
231
|
|
|
action_4.action_type = 17 |
232
|
|
|
action_4.ethertype = 0x88a8 |
233
|
|
|
return_4 = self.napp._action_to_dict(action_4) |
234
|
|
|
expected = {'action_type': 'push_vlan', 'tag_type': 's'} |
235
|
|
|
self.assertEqual(return_4, expected) |
236
|
|
|
|
237
|
|
|
action_5 = MagicMock() |
238
|
|
|
action_5.action_type = 17 |
239
|
|
|
action_5.ethertype = 'any' |
240
|
|
|
return_5 = self.napp._action_to_dict(action_5) |
241
|
|
|
expected = {'action_type': 'push_vlan', 'tag_type': 'c'} |
242
|
|
|
self.assertEqual(return_5, expected) |
243
|
|
|
|
244
|
|
|
action_6 = MagicMock() |
245
|
|
|
action_6.action_type = 18 |
246
|
|
|
return_6 = self.napp._action_to_dict(action_6) |
247
|
|
|
expected = {'action_type': 'pop_vlan'} |
248
|
|
|
self.assertEqual(return_6, expected) |
249
|
|
|
|
250
|
|
|
action_7 = MagicMock() |
251
|
|
|
action_7.action_type = 'any' |
252
|
|
|
return_7 = self.napp._action_to_dict(action_7) |
253
|
|
|
self.assertEqual(return_7, {}) |
254
|
|
|
|