Passed
Pull Request — master (#114)
by Aldo
03:58
created

TestTracePath.test_tracepath()   A

Complexity

Conditions 1

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 35
nop 5
dl 0
loc 50
ccs 30
cts 30
cp 1
crap 1
rs 9.0399
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 = self.trace_manager.is_entry_valid(entries)
83
84 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
85 1
        await tracer.tracepath()
86
87
        # Retrieve trace result created from tracepath
88 1
        result = self.trace_manager.get_result(trace_id)
89
90 1
        assert result["request_id"] == 111
91 1
        assert len(result["result"]) == 1
92 1
        assert result["result"][0]["type"] == "starting"
93 1
        assert result["result"][0]["dpid"] == dpid["dpid"]
94 1
        assert result["result"][0]["port"] == dpid["in_port"]
95
96 1
        assert result["request"]["trace"]["switch"]["dpid"] == dpid["dpid"]
97 1
        assert result["request"]["trace"]["switch"]["in_port"] == dpid["in_port"]
98 1
        assert result["request"]["trace"]["eth"]["dl_vlan"] == eth["dl_vlan"]
99 1
        assert mock_switch_colors.call_count == 1
100 1
        assert mock_aswitch_colors.call_count == 1
101
102 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.aget_switch_color")
103 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
104 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
105 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop")
106 1
    async def test_tracepath_result(
107
        self, mock_trace_loop, mock_get_switch, mock_switch_colors, mock_aswitch_colors
108
    ):
109
        """Test tracepath initial result item. Patching the tracepath loop
110
        to test multiple steps."""
111 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
112 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
113
114
        # Patch Switches.get_switch
115 1
        def wrap_get_switch(dpid):
116 1
            switch = MagicMock()
117 1
            switch.dpid = dpid
118 1
            return switch
119
120 1
        mock_get_switch.side_effect = wrap_get_switch
121
122
        # Trace id to recover the result
123 1
        trace_id = 111
124
125
        # Creating trace entries
126 1
        eth = {"dl_vlan": 100}
127 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
128 1
        switch = {"switch": dpid, "eth": eth}
129 1
        entries = {"trace": switch}
130 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
131
132 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
133
134
        # Patch tracepath loop to create a second result item
135
        # pylint: disable=unused-argument
136 1
        def wrap_tracepath_loop(entries, color, switch):
137 1
            rest = FormatRest()
138 1
            rest.add_trace_step(
139
                tracer.trace_result,
140
                trace_type="trace",
141
                dpid="00:00:00:00:00:00:00:03",
142
                port=3,
143
            )
144 1
            return True
145
146 1
        mock_trace_loop.side_effect = wrap_tracepath_loop
147
148
        # Execute tracepath
149 1
        await tracer.tracepath()
150
151
        # Retrieve trace result created from tracepath
152 1
        result = self.trace_manager.get_result(trace_id)
153
154 1
        assert result["request_id"] == 111
155 1
        assert len(result["result"]) == 2
156 1
        assert result["result"][0]["type"] == "starting"
157 1
        assert result["result"][0]["dpid"] == dpid["dpid"]
158 1
        assert result["result"][0]["port"] == dpid["in_port"]
159
160 1
        assert result["result"][1]["type"] == "trace"
161 1
        assert result["result"][1]["dpid"] == "00:00:00:00:00:00:00:03"
162 1
        assert result["result"][1]["port"] == 3
163
164 1
        assert result["request"]["trace"]["switch"]["dpid"] == dpid["dpid"]
165 1
        assert result["request"]["trace"]["switch"]["in_port"] == dpid["in_port"]
166 1
        assert result["request"]["trace"]["eth"]["dl_vlan"] == eth["dl_vlan"]
167 1
        assert mock_switch_colors.call_count == 1
168 1
        assert mock_aswitch_colors.call_count == 1
169
170 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
171 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
172 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop")
173 1
    @patch("napps.amlight.sdntrace.tracing.tracer.send_packet_out")
174 1
    async def test_send_trace_probe(
175
        self,
176
        mock_send_packet_out,
177
        mock_trace_loop,
178
        mock_get_switch,
179
        mock_switch_colors,
180
    ):
181
        """Test send_trace_probe send and receive."""
182 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
183
184 1
        mock_send_packet_out.return_value = True
185
186 1
        switch_obj = MagicMock()
187 1
        switch_obj.dpid = "00:00:00:00:00:00:00:01"
188 1
        mock_get_switch.return_value = switch_obj
189
190
        # Mock tracepath loop to create only the initial result item
191 1
        mock_trace_loop.return_value = True
192
193
        # Creating a fake packet in
194 1
        msg = TraceMsg()
195 1
        msg.request_id = 111
196 1
        pkt_in = {}
197 1
        pkt_in["dpid"] = "00:00:00:00:00:00:00:01"
198 1
        pkt_in["in_port"] = 1
199 1
        pkt_in["msg"] = msg
200 1
        pkt_in["ethernet"] = "fake_ethernet_object"
201 1
        pkt_in["event"] = "fake_event_object"
202 1
        self.trace_manager.trace_pkt_in = [pkt_in]
203
204
        # Trace id to recover the result
205 1
        trace_id = 111
206
207
        # Creating trace entries
208 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
209 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
210 1
        switch = {"switch": dpid, "eth": eth}
211 1
        entries = {"trace": switch}
212 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
213
214 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
215
216 1
        in_port = 1
217 1
        probe_pkt = MagicMock()
218
219 1
        result = await tracer.send_trace_probe(switch_obj, in_port, probe_pkt)
220
221 1
        mock_send_packet_out.assert_called_once()
222
223 1
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
224 1
        assert result[0]["port"] == 1
225 1
        assert result[1] == "fake_event_object"
226 1
        mock_switch_colors.assert_called_once()
227
228 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
229 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
230 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.tracepath_loop")
231 1
    @patch("napps.amlight.sdntrace.tracing.tracer.send_packet_out")
232 1
    async def test_send_trace_probe_timeout(
233
        self,
234
        mock_send_packet_out,
235
        mock_trace_loop,
236
        mock_get_switch,
237
        mock_aswitch_colors,
238
    ):
239
        """Test send_trace_probe with timeout."""
240 1
        mock_aswitch_colors.return_value = "ee:ee:ee:ee:ee:01"
241 1
        mock_send_packet_out.return_value = True
242
243 1
        switch_obj = MagicMock()
244 1
        switch_obj.dpid = "00:00:00:00:00:00:00:01"
245 1
        mock_get_switch.return_value = switch_obj
246
247
        # Mock tracepath loop to create only the initial result item
248 1
        mock_trace_loop.return_value = True
249
250
        # Creating a fake packet in
251 1
        self.trace_manager.trace_pkt_in = []
252
253
        # Trace id to recover the result
254 1
        trace_id = 111
255
256
        # Creating trace entries
257 1
        timeout = 1
258 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
259 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
260 1
        switch = {"switch": dpid, "eth": eth, "timeout": timeout}
261 1
        entries = {"trace": switch}
262 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
263
264 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
265
266 1
        in_port = 1
267 1
        probe_pkt = MagicMock()
268
269 1
        expected_time = timeout * 3
270 1
        start_time = time.time()
271 1
        result = await tracer.send_trace_probe(switch_obj, in_port, probe_pkt)
272 1
        actual_time = time.time() - start_time
273
274 1
        assert mock_send_packet_out.call_count == 3
275 1
        assert expected_time < actual_time, "Trace was too fast."
276
277 1
        assert result[0] == "timeout"
278 1
        assert result[1] is False
279 1
        mock_aswitch_colors.assert_called_once()
280
281 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
282 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
283 1
    def test_check_loop(self, mock_get_switch, mock_switch_colors):
284
        """Test check_loop with loop detection."""
285 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
286
287
        # Patch Switches.get_switch
288 1
        def wrap_get_switch(dpid):
289 1
            switch = MagicMock()
290 1
            switch.dpid = dpid
291 1
            return switch
292
293 1
        mock_get_switch.side_effect = wrap_get_switch
294
295
        # Trace id to recover the result
296 1
        trace_id = 111
297
298
        # Creating trace entries
299 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
300 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
301 1
        switch = {"switch": dpid, "eth": eth}
302 1
        entries = {"trace": switch}
303 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
304
305 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
306
307
        # Patch tracepath loop to create a second result item
308 1
        rest = FormatRest()
309 1
        rest.add_trace_step(
310
            tracer.trace_result,
311
            trace_type="trace",
312
            dpid="00:00:00:00:00:00:00:01",
313
            port=1,
314
        )
315 1
        rest.add_trace_step(
316
            tracer.trace_result,
317
            trace_type="trace",
318
            dpid="00:00:00:00:00:00:00:02",
319
            port=2,
320
        )
321 1
        rest.add_trace_step(
322
            tracer.trace_result,
323
            trace_type="trace",
324
            dpid="00:00:00:00:00:00:00:03",
325
            port=3,
326
        )
327 1
        rest.add_trace_step(
328
            tracer.trace_result,
329
            trace_type="trace",
330
            dpid="00:00:00:00:00:00:00:01",
331
            port=1,
332
        )
333
334 1
        result = tracer.check_loop()
335 1
        mock_switch_colors.assert_called_once()
336 1
        assert result is True
337
338 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
339 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
340 1
    def test_check_loop_false(self, mock_get_switch, mock_switch_colors):
341
        """Test check_loop with no loop detection."""
342 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
343
344
        # Patch Switches.get_switch
345 1
        def wrap_get_switch(dpid):
346 1
            switch = MagicMock()
347 1
            switch.dpid = dpid
348 1
            return switch
349
350 1
        mock_get_switch.side_effect = wrap_get_switch
351
352
        # Trace id to recover the result
353 1
        trace_id = 111
354
355
        # Creating trace entries
356 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
357 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
358 1
        switch = {"switch": dpid, "eth": eth}
359 1
        entries = {"trace": switch}
360 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
361
362 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
363
364
        # Patch tracepath loop to create a second result item
365 1
        rest = FormatRest()
366 1
        rest.add_trace_step(
367
            tracer.trace_result,
368
            trace_type="trace",
369
            dpid="00:00:00:00:00:00:00:01",
370
            port=1,
371
        )
372 1
        rest.add_trace_step(
373
            tracer.trace_result,
374
            trace_type="trace",
375
            dpid="00:00:00:00:00:00:00:02",
376
            port=2,
377
        )
378
379 1
        result = tracer.check_loop()
380 1
        mock_switch_colors.assert_called_once()
381 1
        assert result == 0
382
383 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
384 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
385 1
    def test_check_loop_port_different(self, mock_get_switch, mock_switch_colors):
386
        """Test check_loop with same switch and different port."""
387 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
388
389
        # Patch Switches.get_switch
390 1
        def wrap_get_switch(dpid):
391 1
            switch = MagicMock()
392 1
            switch.dpid = dpid
393 1
            return switch
394
395 1
        mock_get_switch.side_effect = wrap_get_switch
396
397
        # Trace id to recover the result
398 1
        trace_id = 111
399
400
        # Creating trace entries
401 1
        eth = {"dl_vlan": 100, "dl_type": 2048}
402 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
403 1
        switch = {"switch": dpid, "eth": eth}
404 1
        entries = {"trace": switch}
405 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
406
407 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
408
409
        # Patch tracepath loop to create a second result item
410 1
        rest = FormatRest()
411 1
        rest.add_trace_step(
412
            tracer.trace_result,
413
            trace_type="trace",
414
            dpid="00:00:00:00:00:00:00:01",
415
            port=1,
416
        )
417 1
        rest.add_trace_step(
418
            tracer.trace_result,
419
            trace_type="trace",
420
            dpid="00:00:00:00:00:00:00:02",
421
            port=2,
422
        )
423 1
        rest.add_trace_step(
424
            tracer.trace_result,
425
            trace_type="trace",
426
            dpid="00:00:00:00:00:00:00:01",
427
            port=10,
428
        )
429
430 1
        result = tracer.check_loop()
431 1
        mock_switch_colors.assert_called_once()
432 1
        assert result == 0
433
434 1 View Code Duplication
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
435 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
436 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
437 1
    @patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet")
438 1
    async def test_tracepath_loop(
439
        self,
440
        mock_next_packet,
441
        mock_probe,
442
        mock_get_switch,
443
        mock_switch_colors,
444
    ):
445
        """Test tracepath loop method. This test force the return
446
        after one normal trace."""
447 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
448
449
        # Patch Switches.get_switch
450 1
        def wrap_get_switch(dpid):
451 1
            switch = MagicMock()
452 1
            switch.dpid = dpid
453 1
            return switch
454
455 1
        mock_get_switch.side_effect = wrap_get_switch
456
457 1
        mock_probe.return_value = [
458
            {"dpid": "00:00:00:00:00:00:00:01", "port": 1},
459
            "fake_event_object",
460
        ]
461
462
        # Trace id to recover the result
463 1
        trace_id = 111
464
465
        # Creating trace entries
466 1
        eth = {"dl_vlan": 100}
467 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
468 1
        switch = {"switch": dpid, "eth": eth}
469 1
        entries = {"trace": switch}
470 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
471
472 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
473
474
        # Mock the next packt to stop the trace loop
475
        # pylint: disable=unused-argument
476 1
        def wrap_next_packet(entries, result, packet_in):
477 1
            tracer.trace_ended = True
478 1
            return "", "", ""
479
480 1
        mock_next_packet.side_effect = wrap_next_packet
481
482 1
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
483
484
        # Execute tracepath
485 1
        await tracer.tracepath_loop(trace_entries, color, switch)
486 1
        result = tracer.trace_result
487
488 1
        mock_probe.assert_called_once()
489 1
        mock_switch_colors.assert_called_once()
490 1
        assert result[0]["type"] == "trace"
491 1
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
492
493 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
494 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
495 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
496 1
    async def test_tracepath_loop_timeout(
497
        self,
498
        mock_probe,
499
        mock_get_switch,
500
        mock_switch_colors,
501
    ):
502
        """Test tracepath loop method finishing with timeout."""
503 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
504
505
        # Patch Switches.get_switch
506 1
        def wrap_get_switch(dpid):
507 1
            switch = MagicMock()
508 1
            switch.dpid = dpid
509 1
            return switch
510
511 1
        mock_get_switch.side_effect = wrap_get_switch
512
513 1
        mock_probe.return_value = ["timeout", "fake_event_object"]
514
515
        # Trace id to recover the result
516 1
        trace_id = 111
517
518
        # Creating trace entries
519 1
        eth = {"dl_vlan": 100}
520 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
521 1
        switch = {"switch": dpid, "eth": eth}
522 1
        entries = {"trace": switch}
523 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
524
525 1
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
526
527
        # Execute tracepath
528 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
529 1
        await tracer.tracepath_loop(trace_entries, color, switch)
530
531 1
        result = tracer.trace_result
532
533 1
        mock_probe.assert_called_once()
534 1
        mock_switch_colors.assert_called_once()
535 1
        assert result[0]["type"] == "last"
536 1
        assert result[0]["reason"] == "done"
537 1
        assert result[0]["msg"] == "none"
538
539 1 View Code Duplication
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
540 1
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
541 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
542 1
    @patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet")
543 1
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.check_loop")
544 1
    async def test_tracepath_loop_with_loop(
545
        self,
546
        mock_check_loop,
547
        mock_next_packet,
548
        mock_probe,
549
        mock_get_switch,
550
        mock_switch_colors,
551
    ):
552
        """Test tracepath loop method finishing with a loop."""
553 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
554 1
        mock_check_loop.return_value = True
555
556
        # Patch Switches.get_switch
557 1
        def wrap_get_switch(dpid):
558 1
            switch = MagicMock()
559 1
            switch.dpid = dpid
560 1
            return switch
561
562 1
        mock_get_switch.side_effect = wrap_get_switch
563
564 1
        mock_probe.return_value = [
565
            {"dpid": "00:00:00:00:00:00:00:01", "port": 1},
566
            "fake_event_object",
567
        ]
568
569
        # Trace id to recover the result
570 1
        trace_id = 111
571
572
        # Creating trace entries
573 1
        eth = {"dl_vlan": 100}
574 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
575 1
        switch = {"switch": dpid, "eth": eth}
576 1
        entries = {"trace": switch}
577 1
        trace_entries = self.trace_manager.is_entry_valid(entries)
578
579 1
        tracer = TracePath(self.trace_manager, trace_id, trace_entries)
580
581
        # Mock the next packt to stop the trace loop
582
        # pylint: disable=unused-argument
583 1
        def wrap_next_packet(entries, result, packet_in):
584
            tracer.trace_ended = True
585
            return "", "", ""
586
587 1
        mock_next_packet.side_effect = wrap_next_packet
588
589 1
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
590
591
        # Execute tracepath
592 1
        await tracer.tracepath_loop(trace_entries, color, switch)
593 1
        result = tracer.trace_result
594
595 1
        mock_check_loop.assert_called_once()
596 1
        mock_next_packet.assert_not_called()
597 1
        mock_probe.assert_called_once()
598 1
        assert mock_get_switch.call_count == 3
599 1
        mock_switch_colors.assert_called_once()
600
601 1
        assert result[0]["type"] == "trace"
602
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
603