TestTracePath.test_check_loop_port_different()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 54
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 38
nop 4
dl 0
loc 54
ccs 25
cts 25
cp 1
crap 1
rs 8.968
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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