1
|
|
|
"""Module to test the automate.py.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import patch, MagicMock |
4
|
|
|
|
5
|
|
|
from napps.amlight.sdntrace_cp.automate import Automate |
6
|
|
|
|
7
|
|
|
from kytos.lib.helpers import get_switch_mock |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
# pylint: disable=too-many-public-methods, duplicate-code, protected-access |
11
|
|
|
class TestAutomate(TestCase): |
12
|
|
|
"""Test class Automate.""" |
13
|
|
|
|
14
|
|
View Code Duplication |
@patch("napps.amlight.sdntrace_cp.automate.Automate.find_circuits") |
|
|
|
|
15
|
|
|
def test_get_circuit(self, mock_find_circuits): |
16
|
|
|
"""Verify get circuit success.""" |
17
|
|
|
formatted = [ |
18
|
|
|
{ |
19
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
20
|
|
|
"in_port": 3, |
21
|
|
|
"in_vlan": 100, |
22
|
|
|
"out_port": 2, |
23
|
|
|
"out_vlan": 200, |
24
|
|
|
}, |
25
|
|
|
{ |
26
|
|
|
"dpid": "00:00:00:00:00:00:00:02", |
27
|
|
|
"in_port": 3, |
28
|
|
|
"in_vlan": 200, |
29
|
|
|
"out_port": 2, |
30
|
|
|
"out_vlan": 100, |
31
|
|
|
}, |
32
|
|
|
{ |
33
|
|
|
"dpid": "00:00:00:00:00:00:00:03", |
34
|
|
|
"in_port": 3, |
35
|
|
|
"in_vlan": 100, |
36
|
|
|
"out_port": 2, |
37
|
|
|
"out_vlan": 200, |
38
|
|
|
}, |
39
|
|
|
] |
40
|
|
|
|
41
|
|
|
circuits = [] |
42
|
|
|
circuits.append({"circuit": formatted, "entries": []}) |
43
|
|
|
|
44
|
|
|
circuit = { |
45
|
|
|
"dpid_a": circuits[0]["circuit"][0]["dpid"], |
46
|
|
|
"port_a": circuits[0]["circuit"][0]["in_port"], |
47
|
|
|
"vlan_a": circuits[0]["circuit"][0]["in_vlan"], |
48
|
|
|
"dpid_z": circuits[0]["circuit"][2]["dpid"], |
49
|
|
|
"port_z": circuits[0]["circuit"][2]["out_port"], |
50
|
|
|
"vlan_z": circuits[0]["circuit"][2]["out_vlan"], |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
tracer = MagicMock() |
54
|
|
|
automate = Automate(tracer) |
55
|
|
|
mock_find_circuits.return_value = circuits |
56
|
|
|
result = automate.get_circuit(circuit) |
57
|
|
|
|
58
|
|
|
mock_find_circuits.assert_called_once() |
59
|
|
|
self.assertEqual(result, formatted) |
60
|
|
|
|
61
|
|
View Code Duplication |
@patch("napps.amlight.sdntrace_cp.automate.Automate.find_circuits") |
|
|
|
|
62
|
|
|
def test_get_circuit_not_found(self, mock_find_circuits): |
63
|
|
|
"""Verify get circuit not finding a circuit.""" |
64
|
|
|
|
65
|
|
|
formatted = [ |
66
|
|
|
{ |
67
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
68
|
|
|
"in_port": 3, |
69
|
|
|
"in_vlan": 100, |
70
|
|
|
"out_port": 2, |
71
|
|
|
"out_vlan": 200, |
72
|
|
|
}, |
73
|
|
|
{ |
74
|
|
|
"dpid": "00:00:00:00:00:00:00:02", |
75
|
|
|
"in_port": 3, |
76
|
|
|
"in_vlan": 200, |
77
|
|
|
"out_port": 2, |
78
|
|
|
"out_vlan": 100, |
79
|
|
|
}, |
80
|
|
|
{ |
81
|
|
|
"dpid": "00:00:00:00:00:00:00:03", |
82
|
|
|
"in_port": 3, |
83
|
|
|
"in_vlan": 100, |
84
|
|
|
"out_port": 2, |
85
|
|
|
"out_vlan": 200, |
86
|
|
|
}, |
87
|
|
|
] |
88
|
|
|
|
89
|
|
|
circuits = [] |
90
|
|
|
circuits.append({"circuit": formatted, "entries": []}) |
91
|
|
|
|
92
|
|
|
circuit = { |
93
|
|
|
"dpid_a": circuits[0]["circuit"][0]["dpid"], |
94
|
|
|
"port_a": circuits[0]["circuit"][0]["in_port"], |
95
|
|
|
"vlan_a": circuits[0]["circuit"][0]["in_vlan"], |
96
|
|
|
"dpid_z": circuits[0]["circuit"][0]["dpid"], |
97
|
|
|
"port_z": circuits[0]["circuit"][0]["out_port"], |
98
|
|
|
"vlan_z": circuits[0]["circuit"][0]["out_vlan"], |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
tracer = MagicMock() |
102
|
|
|
automate = Automate(tracer) |
103
|
|
|
automate._circuits = circuits |
104
|
|
|
|
105
|
|
|
result = automate.get_circuit(circuit) |
106
|
|
|
|
107
|
|
|
mock_find_circuits.assert_called_once() |
108
|
|
|
self.assertIsNone(result) |
109
|
|
|
|
110
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.Automate.find_circuits") |
111
|
|
|
def test_get_circuit_empty(self, mock_find_circuits): |
112
|
|
|
"""Verify get circuit with empty circuits""" |
113
|
|
|
circuit = { |
114
|
|
|
"dpid_a": "00:00:00:00:00:00:00:01", |
115
|
|
|
"port_a": 1, |
116
|
|
|
"vlan_a": 100, |
117
|
|
|
"dpid_z": "00:00:00:00:00:00:00:03", |
118
|
|
|
"port_z": 2, |
119
|
|
|
"vlan_z": 200, |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
tracer = MagicMock() |
123
|
|
|
automate = Automate(tracer) |
124
|
|
|
automate._circuits = [] |
125
|
|
|
|
126
|
|
|
result = automate.get_circuit(circuit) |
127
|
|
|
|
128
|
|
|
mock_find_circuits.assert_called_once() |
129
|
|
|
self.assertIsNone(result) |
130
|
|
|
|
131
|
|
|
def test_check_step(self): |
132
|
|
|
"""Verify check_step success.""" |
133
|
|
|
trace_step = {"dpid": "00:00:00:00:00:00:00:01", "port": 1} |
134
|
|
|
circuit_step = {} |
135
|
|
|
circuit_step["dpid"] = trace_step["dpid"] |
136
|
|
|
circuit_step["in_port"] = trace_step["port"] |
137
|
|
|
|
138
|
|
|
tracer = MagicMock() |
139
|
|
|
automate = Automate(tracer) |
140
|
|
|
result = automate.check_step(circuit_step, trace_step) |
141
|
|
|
|
142
|
|
|
self.assertTrue(result) |
143
|
|
|
|
144
|
|
View Code Duplication |
def test_check_step_wront_dpid(self): |
|
|
|
|
145
|
|
|
"""Verify if check_step fail with different dpid.""" |
146
|
|
|
trace_step = {"dpid": "00:00:00:00:00:00:00:01", "port": 1} |
147
|
|
|
circuit_step = {} |
148
|
|
|
circuit_step["dpid"] = "00:00:00:00:00:00:00:02" |
149
|
|
|
circuit_step["in_port"] = trace_step["port"] |
150
|
|
|
|
151
|
|
|
tracer = MagicMock() |
152
|
|
|
automate = Automate(tracer) |
153
|
|
|
result = automate.check_step(circuit_step, trace_step) |
154
|
|
|
|
155
|
|
|
self.assertFalse(result) |
156
|
|
|
|
157
|
|
View Code Duplication |
def test_check_step_wrong_port(self): |
|
|
|
|
158
|
|
|
"""Verify if check_step fail with different port.""" |
159
|
|
|
trace_step = {"dpid": "00:00:00:00:00:00:00:01", "port": 1} |
160
|
|
|
circuit_step = {} |
161
|
|
|
circuit_step["dpid"] = trace_step["dpid"] |
162
|
|
|
circuit_step["in_port"] = 2 |
163
|
|
|
|
164
|
|
|
tracer = MagicMock() |
165
|
|
|
automate = Automate(tracer) |
166
|
|
|
result = automate.check_step(circuit_step, trace_step) |
167
|
|
|
|
168
|
|
|
self.assertFalse(result) |
169
|
|
|
|
170
|
|
View Code Duplication |
@patch("napps.amlight.sdntrace_cp.automate.Automate.get_circuit") |
|
|
|
|
171
|
|
|
def test_check_trace(self, mock_get_circuit): |
172
|
|
|
"""Verify _check_trace with trace finding a valid circuit.""" |
173
|
|
|
circuit_steps = [ |
174
|
|
|
{ |
175
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
176
|
|
|
"in_port": 3, |
177
|
|
|
}, |
178
|
|
|
{ |
179
|
|
|
"dpid": "00:00:00:00:00:00:00:02", |
180
|
|
|
"in_port": 3, |
181
|
|
|
}, |
182
|
|
|
] |
183
|
|
|
mock_get_circuit.return_value = circuit_steps |
184
|
|
|
trace = [ |
185
|
|
|
{ |
186
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
187
|
|
|
"port": 3, |
188
|
|
|
}, |
189
|
|
|
{ |
190
|
|
|
"dpid": "00:00:00:00:00:00:00:02", |
191
|
|
|
"port": 3, |
192
|
|
|
}, |
193
|
|
|
{ |
194
|
|
|
"dpid": "00:00:00:00:00:00:00:03", |
195
|
|
|
"port": 3, |
196
|
|
|
}, |
197
|
|
|
] |
198
|
|
|
|
199
|
|
|
circuit = { |
200
|
|
|
"dpid_a": "00:00:00:00:00:00:00:01", |
201
|
|
|
"port_a": 1, |
202
|
|
|
"dpid_z": "00:00:00:00:00:00:00:03", |
203
|
|
|
"port_z": 2, |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
tracer = MagicMock() |
207
|
|
|
automate = Automate(tracer) |
208
|
|
|
result = automate._check_trace(circuit, trace) |
209
|
|
|
|
210
|
|
|
mock_get_circuit.assert_called_once() |
211
|
|
|
self.assertTrue(result) |
212
|
|
|
|
213
|
|
|
# pylint: disable=duplicate-code |
214
|
|
View Code Duplication |
@patch("napps.amlight.sdntrace_cp.automate.Automate.get_circuit") |
|
|
|
|
215
|
|
|
def test_check_trace__short_trace( |
216
|
|
|
self, mock_get_circuit |
217
|
|
|
): |
218
|
|
|
"""Verify _check_trace if lenght of circuit steps is different from |
219
|
|
|
lenght of trace steps""" |
220
|
|
|
circuit_steps = [ |
221
|
|
|
{ |
222
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
223
|
|
|
"in_port": 3, |
224
|
|
|
}, |
225
|
|
|
{ |
226
|
|
|
"dpid": "00:00:00:00:00:00:00:02", |
227
|
|
|
"in_port": 3, |
228
|
|
|
}, |
229
|
|
|
] |
230
|
|
|
mock_get_circuit.return_value = circuit_steps |
231
|
|
|
|
232
|
|
|
trace = [ |
233
|
|
|
{ |
234
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
235
|
|
|
"port": 3, |
236
|
|
|
}, |
237
|
|
|
{ |
238
|
|
|
"dpid": "00:00:00:00:00:00:00:03", |
239
|
|
|
"port": 3, |
240
|
|
|
}, |
241
|
|
|
] |
242
|
|
|
|
243
|
|
|
circuit = { |
244
|
|
|
"dpid_a": "00:00:00:00:00:00:00:01", |
245
|
|
|
"port_a": 1, |
246
|
|
|
"dpid_z": "00:00:00:00:00:00:00:03", |
247
|
|
|
"port_z": 2, |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
tracer = MagicMock() |
251
|
|
|
automate = Automate(tracer) |
252
|
|
|
result = automate._check_trace(circuit, trace) |
253
|
|
|
|
254
|
|
|
mock_get_circuit.assert_called_once() |
255
|
|
|
self.assertFalse(result) |
256
|
|
|
|
257
|
|
View Code Duplication |
@patch("napps.amlight.sdntrace_cp.automate.Automate.get_circuit") |
|
|
|
|
258
|
|
|
def test_check_trace__wrong_steps( |
259
|
|
|
self, mock_get_circuit |
260
|
|
|
): |
261
|
|
|
"""Verify _check_trace with circuit steps different |
262
|
|
|
from trace steps""" |
263
|
|
|
circuit_steps = [ |
264
|
|
|
{ |
265
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
266
|
|
|
"in_port": 3, |
267
|
|
|
}, |
268
|
|
|
{ |
269
|
|
|
"dpid": "00:00:00:00:00:00:00:02", |
270
|
|
|
"in_port": 3, |
271
|
|
|
}, |
272
|
|
|
] |
273
|
|
|
mock_get_circuit.return_value = circuit_steps |
274
|
|
|
|
275
|
|
|
trace = [ |
276
|
|
|
{ |
277
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
278
|
|
|
"port": 3, |
279
|
|
|
}, |
280
|
|
|
{ |
281
|
|
|
"dpid": "00:00:00:00:00:00:00:05", |
282
|
|
|
"port": 3, |
283
|
|
|
}, |
284
|
|
|
{ |
285
|
|
|
"dpid": "00:00:00:00:00:00:00:03", |
286
|
|
|
"port": 3, |
287
|
|
|
}, |
288
|
|
|
] |
289
|
|
|
|
290
|
|
|
circuit = { |
291
|
|
|
"dpid_a": "00:00:00:00:00:00:00:01", |
292
|
|
|
"port_a": 1, |
293
|
|
|
"dpid_z": "00:00:00:00:00:00:00:03", |
294
|
|
|
"port_z": 2, |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
tracer = MagicMock() |
298
|
|
|
automate = Automate(tracer) |
299
|
|
|
result = automate._check_trace(circuit, trace) |
300
|
|
|
|
301
|
|
|
mock_get_circuit.assert_called_once() |
302
|
|
|
self.assertFalse(result) |
303
|
|
|
|
304
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.Automate.get_circuit") |
305
|
|
|
def test_check_trace__no_steps(self, mock_get_circuit): |
306
|
|
|
"""Verify _check_trace with empty circuit steps.""" |
307
|
|
|
circuit_steps = [] |
308
|
|
|
mock_get_circuit.return_value = circuit_steps |
309
|
|
|
|
310
|
|
|
trace = MagicMock() |
311
|
|
|
|
312
|
|
|
circuit = { |
313
|
|
|
"dpid_a": "00:00:00:00:00:00:00:01", |
314
|
|
|
"port_a": 1, |
315
|
|
|
"dpid_z": "00:00:00:00:00:00:00:03", |
316
|
|
|
"port_z": 2, |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
tracer = MagicMock() |
320
|
|
|
automate = Automate(tracer) |
321
|
|
|
result = automate._check_trace(circuit, trace) |
322
|
|
|
|
323
|
|
|
mock_get_circuit.assert_called_once() |
324
|
|
|
self.assertFalse(result) |
325
|
|
|
|
326
|
|
|
@patch("napps.amlight.sdntrace_cp.utils.requests") |
327
|
|
|
def test_run_traces__empty(self, mock_request): |
328
|
|
|
"""Test run_traces with empty circuits.""" |
329
|
|
|
tracer = MagicMock() |
330
|
|
|
automate = Automate(tracer) |
331
|
|
|
|
332
|
|
|
mock_json = MagicMock() |
333
|
|
|
mock_request.get.return_value = mock_json |
334
|
|
|
|
335
|
|
|
result = automate.run_traces() |
336
|
|
|
|
337
|
|
|
tracer.tracepath.assert_not_called() |
338
|
|
|
self.assertEqual(result, []) |
339
|
|
|
|
340
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.Automate.find_circuits") |
341
|
|
|
def test_run_traces(self, mock_find_circuits): |
342
|
|
|
"""Test run_traces runnin tracepaths for all circuits.""" |
343
|
|
|
trace_result = [ |
344
|
|
|
{ |
345
|
|
|
"in": { |
346
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
347
|
|
|
"port": 1, |
348
|
|
|
"time": "2022-06-21 21:32:14.420100", |
349
|
|
|
"type": "starting", |
350
|
|
|
} |
351
|
|
|
}, |
352
|
|
|
{ |
353
|
|
|
"in": { |
354
|
|
|
"dpid": "00:00:00:00:00:00:00:03", |
355
|
|
|
"port": 3, |
356
|
|
|
"time": "2022-06-21 21:32:14.420200", |
357
|
|
|
"type": "trace", |
358
|
|
|
"vlan": 100, |
359
|
|
|
}, |
360
|
|
|
"out": {"port": 2, "vlan": 200}, |
361
|
|
|
}, |
362
|
|
|
] |
363
|
|
|
|
364
|
|
|
tracer = MagicMock() |
365
|
|
|
tracer.tracepath.return_value = trace_result |
366
|
|
|
|
367
|
|
|
automate = Automate(tracer) |
368
|
|
|
circuits = [ |
369
|
|
|
{ |
370
|
|
|
"circuit": { |
371
|
|
|
"dpid_a": "00:00:00:00:00:00:00:01", |
372
|
|
|
"port_a": 1, |
373
|
|
|
"dpid_z": "00:00:00:00:00:00:00:03", |
374
|
|
|
"port_z": 2, |
375
|
|
|
}, |
376
|
|
|
"entries": { |
377
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
378
|
|
|
"in_port": 1, |
379
|
|
|
"vlan_vid": [100], |
380
|
|
|
}, |
381
|
|
|
}, |
382
|
|
|
{ |
383
|
|
|
"circuit": { |
384
|
|
|
"dpid_a": "00:00:00:00:00:00:00:02", |
385
|
|
|
"port_a": 1, |
386
|
|
|
"dpid_z": "00:00:00:00:00:00:00:04", |
387
|
|
|
"port_z": 2, |
388
|
|
|
}, |
389
|
|
|
"entries": { |
390
|
|
|
"dpid": "00:00:00:00:00:00:00:02", |
391
|
|
|
"in_port": 1, |
392
|
|
|
"vlan_vid": [100], |
393
|
|
|
}, |
394
|
|
|
}, |
395
|
|
|
] |
396
|
|
|
|
397
|
|
|
mock_find_circuits.return_value = circuits |
398
|
|
|
result = automate.run_traces() |
399
|
|
|
|
400
|
|
|
self.assertEqual(tracer.tracepath.call_count, 2) |
401
|
|
|
self.assertEqual(result[0], circuits[0]) |
402
|
|
|
self.assertEqual(result[1], circuits[1]) |
403
|
|
|
|
404
|
|
|
@patch("napps.amlight.sdntrace_cp.utils.requests") |
405
|
|
|
def test_find_circuits(self, mock_request): |
406
|
|
|
"""Test find_circuits successfully finding circuits |
407
|
|
|
for all switches.""" |
408
|
|
|
trace_result = [ |
409
|
|
|
{ |
410
|
|
|
"in": { |
411
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
412
|
|
|
"port": 1, |
413
|
|
|
"time": "2022-06-21 21:32:14.420100", |
414
|
|
|
"type": "starting", |
415
|
|
|
} |
416
|
|
|
}, |
417
|
|
|
{ |
418
|
|
|
"in": { |
419
|
|
|
"dpid": "00:00:00:00:00:00:00:03", |
420
|
|
|
"port": 3, |
421
|
|
|
"time": "2022-06-21 21:32:14.420200", |
422
|
|
|
"type": "trace", |
423
|
|
|
"vlan": 100, |
424
|
|
|
}, |
425
|
|
|
"out": {"port": 2, "vlan": 200}, |
426
|
|
|
}, |
427
|
|
|
] |
428
|
|
|
tracer = MagicMock() |
429
|
|
|
tracer.tracepath.return_value = trace_result |
430
|
|
|
|
431
|
|
|
automate = Automate(tracer) |
432
|
|
|
switches = [ |
433
|
|
|
get_switch_mock("00:00:00:00:00:00:00:01", 0x04), |
434
|
|
|
get_switch_mock("00:00:00:00:00:00:00:02", 0x04), |
435
|
|
|
get_switch_mock("00:00:00:00:00:00:00:03", 0x04), |
436
|
|
|
get_switch_mock("00:00:00:00:00:00:00:04", 0x04), |
437
|
|
|
] |
438
|
|
|
switches_dict = {} |
439
|
|
|
stored = {} |
440
|
|
|
for switch in switches: |
441
|
|
|
flow = {'flow': {}} |
442
|
|
|
flow['flow']['match'] = {"in_port": 1} |
443
|
|
|
action = {} |
444
|
|
|
action['action_type'] = "output" |
445
|
|
|
action['port'] = 1 |
446
|
|
|
flow['flow']['actions'] = [action] |
447
|
|
|
flows = [flow] |
448
|
|
|
stored[switch.dpid] = flows |
449
|
|
|
switches_dict[switch.dpid] = switch |
450
|
|
|
|
451
|
|
|
automate._tracer.controller.switches = switches_dict |
452
|
|
|
|
453
|
|
|
mock_json = MagicMock() |
454
|
|
|
mock_json.json.return_value = stored |
455
|
|
|
mock_request.get.return_value = mock_json |
456
|
|
|
|
457
|
|
|
circuits = automate.find_circuits() |
458
|
|
|
|
459
|
|
|
self.assertIsNotNone(circuits) |
460
|
|
|
|
461
|
|
|
self.assertEqual(len(circuits), 4) |
462
|
|
|
for item in circuits: |
463
|
|
|
self.assertEqual( |
464
|
|
|
item["circuit"][0]["dpid"], trace_result[0]["in"]["dpid"] |
465
|
|
|
) |
466
|
|
|
self.assertEqual( |
467
|
|
|
item["circuit"][0]["in_port"], trace_result[0]["in"]["port"] |
468
|
|
|
) |
469
|
|
|
self.assertEqual( |
470
|
|
|
item["circuit"][1]["dpid"], trace_result[1]["in"]["dpid"] |
471
|
|
|
) |
472
|
|
|
self.assertEqual( |
473
|
|
|
item["circuit"][1]["in_port"], trace_result[1]["in"]["port"] |
474
|
|
|
) |
475
|
|
|
self.assertEqual( |
476
|
|
|
item["circuit"][1]["in_vlan"], trace_result[1]["in"]["vlan"] |
477
|
|
|
) |
478
|
|
|
self.assertEqual( |
479
|
|
|
item["circuit"][1]["out_port"], trace_result[1]["out"]["port"] |
480
|
|
|
) |
481
|
|
|
self.assertEqual( |
482
|
|
|
item["circuit"][1]["out_vlan"], trace_result[1]["out"]["vlan"] |
483
|
|
|
) |
484
|
|
|
|
485
|
|
|
self.assertEqual( |
486
|
|
|
circuits[0]["entries"]["trace"]["switch"]["dpid"], |
487
|
|
|
"00:00:00:00:00:00:00:01", |
488
|
|
|
) |
489
|
|
|
self.assertEqual( |
490
|
|
|
circuits[1]["entries"]["trace"]["switch"]["dpid"], |
491
|
|
|
"00:00:00:00:00:00:00:02", |
492
|
|
|
) |
493
|
|
|
self.assertEqual( |
494
|
|
|
circuits[2]["entries"]["trace"]["switch"]["dpid"], |
495
|
|
|
"00:00:00:00:00:00:00:03", |
496
|
|
|
) |
497
|
|
|
self.assertEqual( |
498
|
|
|
circuits[3]["entries"]["trace"]["switch"]["dpid"], |
499
|
|
|
"00:00:00:00:00:00:00:04", |
500
|
|
|
) |
501
|
|
|
|
502
|
|
|
@patch("napps.amlight.sdntrace_cp.utils.requests") |
503
|
|
|
def test_find_circuits__empty(self, mock_request): |
504
|
|
|
"""Test find_circuits without switches.""" |
505
|
|
|
tracer = MagicMock() |
506
|
|
|
|
507
|
|
|
automate = Automate(tracer) |
508
|
|
|
|
509
|
|
|
mock_json = MagicMock() |
510
|
|
|
mock_request.get.return_value = mock_json |
511
|
|
|
|
512
|
|
|
circuits = automate.find_circuits() |
513
|
|
|
|
514
|
|
|
self.assertEqual(circuits, []) |
515
|
|
|
|
516
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.requests") |
517
|
|
|
def test_run_important_traces__empty(self, mock_requests): |
518
|
|
|
"""Test run_important_traces with empty circuits to run.""" |
519
|
|
|
tracer = MagicMock() |
520
|
|
|
|
521
|
|
|
automate = Automate(tracer) |
522
|
|
|
automate.run_important_traces() |
523
|
|
|
|
524
|
|
|
mock_requests.assert_not_called() |
525
|
|
|
|
526
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.requests") |
527
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.settings") |
528
|
|
|
@patch("napps.amlight.sdntrace_cp.utils.requests") |
529
|
|
|
def test_run_important_traces( |
530
|
|
|
self, |
531
|
|
|
mock_request_get, |
532
|
|
|
mock_settings, |
533
|
|
|
mock_requests |
534
|
|
|
): |
535
|
|
|
"""Test run_important_traces if control plane trace result is |
536
|
|
|
different from the data plane trace.""" |
537
|
|
|
mock_settings.IMPORTANT_CIRCUITS = [ |
538
|
|
|
{"dpid_a": "00:00:00:00:00:00:00:01", "port_a": 1, "vlan_a": 100} |
539
|
|
|
] |
540
|
|
|
|
541
|
|
|
mock_json = MagicMock() |
542
|
|
|
mock_json.json.return_value = { |
543
|
|
|
"result": [{"type": "starting"}, {"type": "last"}] |
544
|
|
|
} |
545
|
|
|
mock_requests.get.return_value = mock_json |
546
|
|
|
|
547
|
|
|
def side_effect(event): |
548
|
|
|
self.assertTrue( |
549
|
|
|
event.content["message"]["source"], "amlight/sdntrace_cp" |
550
|
|
|
) |
551
|
|
|
self.assertTrue( |
552
|
|
|
event.content["message"]["m_body"], |
553
|
|
|
"Trace in data plane different from trace in control plane " |
554
|
|
|
"for circuit {'dpid_a': '00:00:00:00:00:00:00:01', " |
555
|
|
|
"'port_a': 1, 'vlan_a': 100}" |
556
|
|
|
) |
557
|
|
|
|
558
|
|
|
tracer = MagicMock() |
559
|
|
|
tracer.controller.buffers.app.put.side_effect = side_effect |
560
|
|
|
|
561
|
|
|
flow = { |
562
|
|
|
'flow': { |
563
|
|
|
'match': {"in_port": 1}, |
564
|
|
|
'actions': [ |
565
|
|
|
{'action_type': "output", 'port': 1} |
566
|
|
|
] |
567
|
|
|
} |
568
|
|
|
} |
569
|
|
|
stored = { |
570
|
|
|
"00:00:00:00:00:00:00:01": [flow], |
571
|
|
|
"00:00:00:00:00:00:00:02": [flow], |
572
|
|
|
"00:00:00:00:00:00:00:03": [flow], |
573
|
|
|
"00:00:00:00:00:00:00:04": [flow] |
574
|
|
|
} |
575
|
|
|
mock_json = MagicMock() |
576
|
|
|
mock_json.json.return_value = stored |
577
|
|
|
mock_request_get.get.return_value = mock_json |
578
|
|
|
|
579
|
|
|
automate = Automate(tracer) |
580
|
|
|
automate.run_important_traces() |
581
|
|
|
|
582
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.requests") |
583
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.settings") |
584
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.Automate._check_trace") |
585
|
|
|
def test_run_important_traces__success( |
586
|
|
|
self, mock_check_trace, mock_settings, mock_requests |
587
|
|
|
): |
588
|
|
|
"""Verify run_important_traces if control plane trace result |
589
|
|
|
is the same result from the data plane trace.""" |
590
|
|
|
# Patch to check trace with stored circuit |
591
|
|
|
mock_check_trace.return_value = True |
592
|
|
|
|
593
|
|
|
mock_settings.IMPORTANT_CIRCUITS = [ |
594
|
|
|
{ |
595
|
|
|
"dpid_a": "00:00:00:00:00:00:00:01", |
596
|
|
|
"port_a": 1, |
597
|
|
|
"vlan_a": 100, |
598
|
|
|
"dpid_z": "00:00:00:00:00:00:00:02", |
599
|
|
|
"port_z": 2, |
600
|
|
|
"vlan_z": 100, |
601
|
|
|
}, |
602
|
|
|
] |
603
|
|
|
|
604
|
|
|
mock_json = MagicMock() |
605
|
|
|
mock_json.json.return_value = { |
606
|
|
|
"result": [ |
607
|
|
|
{ |
608
|
|
|
"dpid": "00:00:00:00:00:00:00:01", |
609
|
|
|
"port": 1, |
610
|
|
|
"type": "starting", |
611
|
|
|
}, |
612
|
|
|
{"dpid": "00:00:00:00:00:00:00:03", "port": 1, "type": "last"}, |
613
|
|
|
] |
614
|
|
|
} |
615
|
|
|
mock_requests.get.return_value = mock_json |
616
|
|
|
|
617
|
|
|
tracer = MagicMock() |
618
|
|
|
tracer.controller.buffers.app.put.side_effect = None |
619
|
|
|
|
620
|
|
|
automate = Automate(tracer) |
621
|
|
|
automate.run_important_traces() |
622
|
|
|
|
623
|
|
|
# Check if important trace dont trigger the event |
624
|
|
|
# It means that the CP trace is the same to the DP trace |
625
|
|
|
tracer.controller.buffers.app.put.assert_not_called() |
626
|
|
|
|
627
|
|
|
def test_schedule_id(self): |
628
|
|
|
"""Test schedule_id with proper id type""" |
629
|
|
|
tracer = MagicMock() |
630
|
|
|
automate = Automate(tracer) |
631
|
|
|
try: |
632
|
|
|
automate.schedule_id('mock_id') |
633
|
|
|
self.assertEqual(len(automate.ids), 1) |
634
|
|
|
except AttributeError: |
635
|
|
|
self.fail('schedule_id() raised an error') |
636
|
|
|
|
637
|
|
|
def test_schedule_id_fail(self): |
638
|
|
|
"""Test schedule_id with non-string id""" |
639
|
|
|
tracer = MagicMock() |
640
|
|
|
automate = Automate(tracer) |
641
|
|
|
with self.assertRaises(AttributeError): |
642
|
|
|
automate.schedule_id(1) |
643
|
|
|
|
644
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.settings") |
645
|
|
|
def test_schedule_traces(self, mock_settings): |
646
|
|
|
"""Test schedule_traces with the arguments from settings""" |
647
|
|
|
mock_settings.TRIGGER_SCHEDULE_TRACES = True |
648
|
|
|
mock_settings.SCHEDULE_ARGS = {'seconds': 120} |
649
|
|
|
mock_settings.SCHEDULE_TRIGGER = 'interval' |
650
|
|
|
tracer = MagicMock() |
651
|
|
|
automate = Automate(tracer) |
652
|
|
|
try: |
653
|
|
|
job = automate.schedule_traces(mock_settings) |
654
|
|
|
except AttributeError: |
655
|
|
|
self.fail("automate.schedule_traces() raised an error") |
656
|
|
|
self.assertIsNotNone(job) |
657
|
|
|
|
658
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.settings") |
659
|
|
|
def test_schedule_traces_fail(self, mock_settings): |
660
|
|
|
"""Test schedule_traces with wrong arguments from settings""" |
661
|
|
|
mock_settings.TRIGGER_SCHEDULE_TRACES = True |
662
|
|
|
mock_settings.SCHEDULE_ARGS = 120 |
663
|
|
|
mock_settings.SCHEDULE_TRIGGER = {'interval'} |
664
|
|
|
tracer = MagicMock() |
665
|
|
|
automate = Automate(tracer) |
666
|
|
|
with self.assertRaises(AttributeError): |
667
|
|
|
automate.schedule_traces(mock_settings) |
668
|
|
|
|
669
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.settings") |
670
|
|
|
def test_schedule_important_traces(self, mock_settings): |
671
|
|
|
""" |
672
|
|
|
Test schedule_important_traces with the arguments from settings |
673
|
|
|
""" |
674
|
|
|
mock_settings.TRIGGER_IMPORTANT_CIRCUITS = True |
675
|
|
|
mock_settings.IMPORTANT_CIRCUITS_ARGS = {'seconds': 20} |
676
|
|
|
mock_settings.IMPORTANT_CIRCUITS_TRIGGER = 'interval' |
677
|
|
|
tracer = MagicMock() |
678
|
|
|
automate = Automate(tracer) |
679
|
|
|
try: |
680
|
|
|
job = automate.schedule_important_traces(mock_settings) |
681
|
|
|
except AttributeError: |
682
|
|
|
self.fail("automate.schedule_important_traces() raised an error") |
683
|
|
|
self.assertIsNotNone(job) |
684
|
|
|
|
685
|
|
|
@patch("napps.amlight.sdntrace_cp.automate.settings") |
686
|
|
|
def test_schedule_important_traces_fail(self, mock_settings): |
687
|
|
|
""" |
688
|
|
|
Test schedule_important_traces with wrong arguments from settings |
689
|
|
|
""" |
690
|
|
|
mock_settings.TRIGGER_IMPORTANT_CIRCUITS = True |
691
|
|
|
mock_settings.IMPORTANT_CIRCUITS_ARGS = 20 |
692
|
|
|
mock_settings.IMPORTANT_CIRCUITS_TRIGGER = {'interval'} |
693
|
|
|
tracer = MagicMock() |
694
|
|
|
automate = Automate(tracer) |
695
|
|
|
with self.assertRaises(AttributeError): |
696
|
|
|
automate.schedule_important_traces(mock_settings) |
697
|
|
|
|
698
|
|
|
def test_unschedule_ids(self): |
699
|
|
|
"""Test unschedule_ids with existent ids""" |
700
|
|
|
tracer = MagicMock() |
701
|
|
|
automate = Automate(tracer) |
702
|
|
|
automate.scheduler = MagicMock() |
703
|
|
|
automate.schedule_id('mock_id') |
704
|
|
|
automate.schedule_id('id_mocked') |
705
|
|
|
self.assertEqual(len(automate.ids), 2) |
706
|
|
|
id_ = {'mock_id'} |
707
|
|
|
automate.unschedule_ids(id_set=id_) |
708
|
|
|
self.assertEqual(len(automate.ids), 1) |
709
|
|
|
automate.unschedule_ids() |
710
|
|
|
self.assertEqual(len(automate.ids), 0) |
711
|
|
|
|