Passed
Pull Request — master (#114)
by Aldo
04:31
created

TestTracePath.test_get_packet_in_empty()   A

Complexity

Conditions 1

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 21
nop 3
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 1
rs 9.376
c 0
b 0
f 0
1
"""
2
    Test tracing.trace_entries
3
"""
4
5 1
from unittest.mock import MagicMock, patch
6 1
import time
7 1
import pytest
8 1
from napps.amlight.sdntrace.tracing.trace_msg import TraceMsg
9 1
from napps.amlight.sdntrace.tracing.trace_manager import TraceManager
10 1
from napps.amlight.sdntrace.tracing.tracer import TracePath
11 1
from napps.amlight.sdntrace.tracing.rest import FormatRest
12 1
from napps.amlight.sdntrace.shared.switches import Switches
13
14 1
from kytos.lib.helpers import get_controller_mock
15
16
17
# pylint: disable=too-many-public-methods, too-many-lines,
18
# pylint: disable=protected-access, too-many-locals
19 1
class TestTracePath:
20
    """Test all combinations for DPID"""
21
22 1
    @pytest.fixture(autouse=True)
23 1
    def commomn_patches(self, request):
24
        """This function handles setup and cleanup for patches"""
25
        # This fixture sets up the common patches,
26
        # and a finalizer is added using addfinalizer to stop
27
        # the common patches after each test. This ensures that the cleanup
28
        # is performed after each test, and additional patch decorators
29
        # can be used within individual test functions.
30
31 1
        patcher = patch("kytos.core.helpers.run_on_thread", lambda x: x)
32 1
        mock_patch = patcher.start()
33
34 1
        _ = request.function.__name__
35
36 1
        def cleanup():
37 1
            patcher.stop()
38
39 1
        request.addfinalizer(cleanup)
40 1
        return mock_patch
41
42 1
    def setup_method(self):
43
        """Set up before each test method"""
44 1
        TraceManager.run_traces = MagicMock()
45 1
        self.trace_manager = TraceManager(controller=get_controller_mock())
46
47
        # This variable is used to initiate the Singleton class so
48
        # these tests can run on their own.
49 1
        self._auxiliar = Switches(MagicMock())
50
51 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
52 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
53 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
54 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop")
55 1
    async def test_tracepath(
56
        self,
57
        mock_trace_loop,
58
        mock_get_switch,
59
        mock_aswitch_colors,
60
        mock_switch_colors,
61
    ):
62
        """Test tracepath initial result item. Mocking tracepath loop
63
        to get just one step."""
64 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
65 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
66
67 1
        switch = MagicMock()
68 1
        switch.dpid = "00:00:00:00:00:00:00:01"
69 1
        mock_get_switch.return_value = switch
70
71
        # Mock tracepath loop to create only the initial result item
72 1
        mock_trace_loop.return_value = True
73
74
        # Trace id to recover the result
75 1
        trace_id = 111
76
77
        # Creating trace entries
78 1
        eth = {"dl_vlan": 100}
79 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
80 1
        switch = {"switch": dpid, "eth": eth}
81 1
        entries = {"trace": switch}
82 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
83 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
84 1
        tracer.tracepath()
85
86
        # Retrieve trace result created from tracepath
87 1
        result = self.trace_manager.get_result(trace_id)
88
89 1
        assert result["request_id"] == 111
90 1
        assert len(result["result"]) == 1
91 1
        assert result["result"][0]["type"] == "starting"
92 1
        assert result["result"][0]["dpid"] == dpid["dpid"]
93 1
        assert result["result"][0]["port"] == dpid["in_port"]
94
95 1
        assert result["request"]["trace"]["switch"]["dpid"] == dpid["dpid"]
96 1
        assert result["request"]["trace"]["switch"]["in_port"] == dpid["in_port"]
97 1
        assert result["request"]["trace"]["eth"]["dl_vlan"] == eth["dl_vlan"]
98 1
        assert mock_switch_colors.call_count == 1
99 1
        assert mock_aswitch_colors.call_count == 1
100
101 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
102 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
103 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
104 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop")
105 1
    async def test_tracepath_result(
106
        self, mock_trace_loop, mock_get_switch, mock_switch_colors, mock_aswitch_colors
107
    ):
108
        """Test tracepath initial result item. Patching the tracepath loop
109
        to test multiple steps."""
110 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
111 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
112
113
        # Patch Switches.get_switch
114 1
        def wrap_get_switch(dpid):
115 1
            switch = MagicMock()
116 1
            switch.dpid = dpid
117 1
            return switch
118
119 1
        mock_get_switch.side_effect = wrap_get_switch
120
121
        # Trace id to recover the result
122 1
        trace_id = 111
123
124
        # Creating trace entries
125 1
        eth = {"dl_vlan": 100}
126 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
127 1
        switch = {"switch": dpid, "eth": eth}
128 1
        entries = {"trace": switch}
129 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
130
131 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
132
133
        # Patch tracepath loop to create a second result item
134
        # pylint: disable=unused-argument
135 1
        def wrap_tracepath_loop(entries, color, switch):
136 1
            rest = FormatRest()
137 1
            rest.add_trace_step(
138
                tracer.trace_result,
139
                trace_type="trace",
140
                dpid="00:00:00:00:00:00:00:03",
141
                port=3,
142
            )
143 1
            return True
144
145 1
        mock_trace_loop.side_effect = wrap_tracepath_loop
146
147
        # Execute tracepath
148 1
        tracer.tracepath()
149
150
        # Retrieve trace result created from tracepath
151 1
        result = self.trace_manager.get_result(trace_id)
152
153 1
        assert result["request_id"] == 111
154 1
        assert len(result["result"]) == 2
155 1
        assert result["result"][0]["type"] == "starting"
156 1
        assert result["result"][0]["dpid"] == dpid["dpid"]
157 1
        assert result["result"][0]["port"] == dpid["in_port"]
158
159 1
        assert result["result"][1]["type"] == "trace"
160 1
        assert result["result"][1]["dpid"] == "00:00:00:00:00:00:00:03"
161 1
        assert result["result"][1]["port"] == 3
162
163 1
        assert result["request"]["trace"]["switch"]["dpid"] == dpid["dpid"]
164 1
        assert result["request"]["trace"]["switch"]["in_port"] == dpid["in_port"]
165 1
        assert result["request"]["trace"]["eth"]["dl_vlan"] == eth["dl_vlan"]
166 1
        assert mock_switch_colors.call_count == 1
167 1
        assert mock_aswitch_colors.call_count == 1
168
169 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
170 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
171 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop")
172 1
    @patch("napps.amlight.sdntrace.tracing.tracer.send_packet_out")
173 1
    async def test_send_trace_probe(
174
        self,
175
        mock_send_packet_out,
176
        mock_trace_loop,
177
        mock_get_switch,
178
        mock_aswitch_colors,
179
    ):
180
        """Test send_trace_probe send and receive."""
181 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
182
183 1
        mock_send_packet_out.return_value = True
184
185 1
        switch_obj = MagicMock()
186 1
        switch_obj.dpid = "00:00:00:00:00:00:00:01"
187 1
        mock_get_switch.return_value = switch_obj
188
189
        # Mock tracepath loop to create only the initial result item
190 1
        mock_trace_loop.return_value = True
191
192
        # Creating a fake packet in
193 1
        msg = TraceMsg()
194 1
        msg.request_id = 111
195 1
        pkt_in = {}
196 1
        pkt_in["dpid"] = "00:00:00:00:00:00:00:01"
197 1
        pkt_in["in_port"] = 1
198 1
        pkt_in["msg"] = msg
199 1
        pkt_in["ethernet"] = "fake_ethernet_object"
200 1
        pkt_in["event"] = "fake_event_object"
201 1
        self.trace_manager._trace_pkt_in[msg.request_id].sync_q.put(pkt_in)
202
203
        # Trace id to recover the result
204 1
        trace_id = 111
205
206
        # Creating trace entries
207 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
208 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
209 1
        switch = {"switch": dpid, "eth": eth}
210 1
        entries = {"trace": switch}
211 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
212
213 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
214
215 1
        in_port = 1
216 1
        probe_pkt = MagicMock()
217
218 1
        result = tracer.send_trace_probe(switch_obj, in_port, probe_pkt)
219
220 1
        mock_send_packet_out.assert_called_once()
221
222 1
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
223 1
        assert result[0]["port"] == 1
224 1
        assert result[1] == "fake_event_object"
225 1
        mock_aswitch_colors.assert_called_once()
226
227 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
228 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
229 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop")
230 1
    @patch("napps.amlight.sdntrace.tracing.tracer.send_packet_out")
231 1
    async def test_send_trace_probe_timeout(
232
        self,
233
        mock_send_packet_out,
234
        mock_trace_loop,
235
        mock_get_switch,
236
        mock_aswitch_colors,
237
    ):
238
        """Test send_trace_probe with timeout."""
239 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
240 1
        mock_send_packet_out.return_value = True
241
242 1
        switch_obj = MagicMock()
243 1
        switch_obj.dpid = "00:00:00:00:00:00:00:01"
244 1
        mock_get_switch.return_value = switch_obj
245
246
        # Mock tracepath loop to create only the initial result item
247 1
        mock_trace_loop.return_value = True
248
249
        # Creating a fake packet in
250 1
        self.trace_manager.trace_pkt_in = []
251
252
        # Trace id to recover the result
253 1
        trace_id = 111
254
255
        # Creating trace entries
256 1
        timeout = 1
257 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
258 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
259 1
        switch = {"switch": dpid, "eth": eth, "timeout": timeout}
260 1
        entries = {"trace": switch}
261 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
262
263 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
264
265 1
        in_port = 1
266 1
        probe_pkt = MagicMock()
267
268 1
        expected_time = timeout * 3
269 1
        start_time = time.time()
270 1
        result = tracer.send_trace_probe(switch_obj, in_port, probe_pkt)
271 1
        actual_time = time.time() - start_time
272
273 1
        assert mock_send_packet_out.call_count == 3
274 1
        assert expected_time < actual_time, "Trace was too fast."
275
276 1
        assert result[0] == "timeout"
277 1
        assert result[1] is False
278 1
        mock_aswitch_colors.assert_called_once()
279
280 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
281 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
282 1
    async def test_check_loop(self, mock_get_switch, mock_aswitch_colors):
283
        """Test check_loop with loop detection."""
284 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
285
286
        # Patch Switches.get_switch
287 1
        def wrap_get_switch(dpid):
288 1
            switch = MagicMock()
289 1
            switch.dpid = dpid
290 1
            return switch
291
292 1
        mock_get_switch.side_effect = wrap_get_switch
293
294
        # Trace id to recover the result
295 1
        trace_id = 111
296
297
        # Creating trace entries
298 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
299 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
300 1
        switch = {"switch": dpid, "eth": eth}
301 1
        entries = {"trace": switch}
302 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
303
304 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
305
306
        # Patch tracepath loop to create a second result item
307 1
        rest = FormatRest()
308 1
        rest.add_trace_step(
309
            tracer.trace_result,
310
            trace_type="trace",
311
            dpid="00:00:00:00:00:00:00:01",
312
            port=1,
313
        )
314 1
        rest.add_trace_step(
315
            tracer.trace_result,
316
            trace_type="trace",
317
            dpid="00:00:00:00:00:00:00:02",
318
            port=2,
319
        )
320 1
        rest.add_trace_step(
321
            tracer.trace_result,
322
            trace_type="trace",
323
            dpid="00:00:00:00:00:00:00:03",
324
            port=3,
325
        )
326 1
        rest.add_trace_step(
327
            tracer.trace_result,
328
            trace_type="trace",
329
            dpid="00:00:00:00:00:00:00:01",
330
            port=1,
331
        )
332
333 1
        result = tracer.check_loop()
334 1
        mock_aswitch_colors.assert_called_once()
335 1
        assert result is True
336
337 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
338 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
339 1
    async def test_check_loop_false(self, mock_get_switch, mock_aswitch_colors):
340
        """Test check_loop with no loop detection."""
341 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
342
343
        # Patch Switches.get_switch
344 1
        def wrap_get_switch(dpid):
345 1
            switch = MagicMock()
346 1
            switch.dpid = dpid
347 1
            return switch
348
349 1
        mock_get_switch.side_effect = wrap_get_switch
350
351
        # Trace id to recover the result
352 1
        trace_id = 111
353
354
        # Creating trace entries
355 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
356 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
357 1
        switch = {"switch": dpid, "eth": eth}
358 1
        entries = {"trace": switch}
359 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
360
361 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
362
363
        # Patch tracepath loop to create a second result item
364 1
        rest = FormatRest()
365 1
        rest.add_trace_step(
366
            tracer.trace_result,
367
            trace_type="trace",
368
            dpid="00:00:00:00:00:00:00:01",
369
            port=1,
370
        )
371 1
        rest.add_trace_step(
372
            tracer.trace_result,
373
            trace_type="trace",
374
            dpid="00:00:00:00:00:00:00:02",
375
            port=2,
376
        )
377
378 1
        result = tracer.check_loop()
379 1
        mock_aswitch_colors.assert_called_once()
380 1
        assert result == 0
381
382 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
383 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
384 1
    async def test_check_loop_port_different(
385
        self, mock_get_switch, mock_aswitch_colors
386
    ):
387
        """Test check_loop with same switch and different port."""
388 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
389
390
        # Patch Switches.get_switch
391 1
        def wrap_get_switch(dpid):
392 1
            switch = MagicMock()
393 1
            switch.dpid = dpid
394 1
            return switch
395
396 1
        mock_get_switch.side_effect = wrap_get_switch
397
398
        # Trace id to recover the result
399 1
        trace_id = 111
400
401
        # Creating trace entries
402 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
403 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
404 1
        switch = {"switch": dpid, "eth": eth}
405 1
        entries = {"trace": switch}
406 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
407
408 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
409
410
        # Patch tracepath loop to create a second result item
411 1
        rest = FormatRest()
412 1
        rest.add_trace_step(
413
            tracer.trace_result,
414
            trace_type="trace",
415
            dpid="00:00:00:00:00:00:00:01",
416
            port=1,
417
        )
418 1
        rest.add_trace_step(
419
            tracer.trace_result,
420
            trace_type="trace",
421
            dpid="00:00:00:00:00:00:00:02",
422
            port=2,
423
        )
424 1
        rest.add_trace_step(
425
            tracer.trace_result,
426
            trace_type="trace",
427
            dpid="00:00:00:00:00:00:00:01",
428
            port=10,
429
        )
430
431 1
        result = tracer.check_loop()
432 1
        mock_aswitch_colors.assert_called_once()
433 1
        assert result == 0
434
435 1 View Code Duplication
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
436 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
437 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
438 1
    @patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet")
439 1
    async def test_tracepath_loop(
440
        self,
441
        mock_next_packet,
442
        mock_probe,
443
        mock_get_switch,
444
        mock_aswitch_colors,
445
    ):
446
        """Test tracepath loop method. This test force the return
447
        after one normal trace."""
448 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
449
450
        # Patch Switches.get_switch
451 1
        def wrap_get_switch(dpid):
452 1
            switch = MagicMock()
453 1
            switch.dpid = dpid
454 1
            return switch
455
456 1
        mock_get_switch.side_effect = wrap_get_switch
457
458 1
        mock_probe.return_value = [
459
            {"dpid": "00:00:00:00:00:00:00:01", "port": 1},
460
            "fake_event_object",
461
        ]
462
463
        # Trace id to recover the result
464 1
        trace_id = 111
465
466
        # Creating trace entries
467 1
        eth = {"dl_vlan": 100}
468 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
469 1
        switch = {"switch": dpid, "eth": eth}
470 1
        entries = {"trace": switch}
471 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
472
473 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
474
475
        # Mock the next packt to stop the trace loop
476
        # pylint: disable=unused-argument
477 1
        def wrap_next_packet(entries, result, packet_in):
478 1
            tracer.trace_ended = True
479 1
            return "", "", ""
480
481 1
        mock_next_packet.side_effect = wrap_next_packet
482
483 1
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
484
485
        # Execute tracepath
486 1
        tracer.tracepath_loop(trace_entries, color, switch)
487 1
        result = tracer.trace_result
488
489 1
        mock_probe.assert_called_once()
490 1
        mock_aswitch_colors.assert_called_once()
491 1
        assert result[0]["type"] == "trace"
492 1
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
493
494 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
495 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
496 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
497 1
    async def test_tracepath_loop_timeout(
498
        self,
499
        mock_probe,
500
        mock_get_switch,
501
        mock_aswitch_colors,
502
    ):
503
        """Test tracepath loop method finishing with timeout."""
504 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
505
506
        # Patch Switches.get_switch
507 1
        def wrap_get_switch(dpid):
508 1
            switch = MagicMock()
509 1
            switch.dpid = dpid
510 1
            return switch
511
512 1
        mock_get_switch.side_effect = wrap_get_switch
513
514 1
        mock_probe.return_value = ["timeout", "fake_event_object"]
515
516
        # Trace id to recover the result
517 1
        trace_id = 111
518
519
        # Creating trace entries
520 1
        eth = {"dl_vlan": 100}
521 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
522 1
        switch = {"switch": dpid, "eth": eth}
523 1
        entries = {"trace": switch}
524 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
525
526 1
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
527
528
        # Execute tracepath
529 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
530 1
        tracer.tracepath_loop(trace_entries, color, switch)
531
532 1
        result = tracer.trace_result
533
534 1
        mock_probe.assert_called_once()
535 1
        mock_aswitch_colors.assert_called_once()
536 1
        assert result[0]["type"] == "last"
537 1
        assert result[0]["reason"] == "done"
538 1
        assert result[0]["msg"] == "none"
539
540 1 View Code Duplication
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
541 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
542 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
543 1
    @patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet")
544 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.check_loop")
545 1
    async def test_tracepath_loop_with_loop(
546
        self,
547
        mock_check_loop,
548
        mock_next_packet,
549
        mock_probe,
550
        mock_get_switch,
551
        mock_aswitch_colors,
552
    ):
553
        """Test tracepath loop method finishing with a loop."""
554 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
555 1
        mock_check_loop.return_value = True
556
557
        # Patch Switches.get_switch
558 1
        def wrap_get_switch(dpid):
559 1
            switch = MagicMock()
560 1
            switch.dpid = dpid
561 1
            return switch
562
563 1
        mock_get_switch.side_effect = wrap_get_switch
564
565 1
        mock_probe.return_value = [
566
            {"dpid": "00:00:00:00:00:00:00:01", "port": 1},
567
            "fake_event_object",
568
        ]
569
570
        # Trace id to recover the result
571 1
        trace_id = 111
572
573
        # Creating trace entries
574 1
        eth = {"dl_vlan": 100}
575 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
576 1
        switch = {"switch": dpid, "eth": eth}
577 1
        entries = {"trace": switch}
578 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
579
580 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
581
582
        # Mock the next packt to stop the trace loop
583
        # pylint: disable=unused-argument
584 1
        def wrap_next_packet(entries, result, packet_in):
585
            tracer.trace_ended = True
586
            return "", "", ""
587
588 1
        mock_next_packet.side_effect = wrap_next_packet
589
590 1
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
591
592
        # Execute tracepath
593 1
        tracer.tracepath_loop(trace_entries, color, switch)
594 1
        result = tracer.trace_result
595
596 1
        mock_check_loop.assert_called_once()
597 1
        mock_next_packet.assert_not_called()
598 1
        mock_probe.assert_called_once()
599 1
        assert mock_get_switch.call_count == 3
600 1
        mock_aswitch_colors.assert_called_once()
601
602 1
        assert result[0]["type"] == "trace"
603 1
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
604
605 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
606 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
607 1
    async def test_get_packet_in_empty(
608
        self,
609
        mock_get_switch,
610
        mock_aswitch_colors,
611
    ):
612
        """Test get_packet_in when no packet in was caught."""
613
614 1
        def wrap_get_switch(dpid):
615 1
            switch = MagicMock()
616 1
            switch.dpid = dpid
617 1
            return switch
618
619 1
        mock_get_switch.side_effect = wrap_get_switch
620 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
621 1
        trace_id = 111
622 1
        eth = {"dl_vlan": 100}
623 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
624 1
        switch = {"switch": dpid, "eth": eth}
625 1
        entries = {"trace": switch}
626 1
        trace_entries = await self.trace_manager.is_entry_valid(entries)
627 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
628
629
        # No packetIn
630 1
        self.trace_manager._trace_pkt_in = {}
631
        assert tracer.get_packet_in() is None
632