Code Duplication    Length = 58-64 lines in 2 locations

tests/unit/tracing/test_tracer.py 2 locations

@@ 539-602 (lines=64) @@
536
        assert result[0]["reason"] == "done"
537
        assert result[0]["msg"] == "none"
538
539
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
540
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
541
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
542
    @patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet")
543
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.check_loop")
544
    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
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
554
        mock_check_loop.return_value = True
555
556
        # Patch Switches.get_switch
557
        def wrap_get_switch(dpid):
558
            switch = MagicMock()
559
            switch.dpid = dpid
560
            return switch
561
562
        mock_get_switch.side_effect = wrap_get_switch
563
564
        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
        trace_id = 111
571
572
        # Creating trace entries
573
        eth = {"dl_vlan": 100}
574
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
575
        switch = {"switch": dpid, "eth": eth}
576
        entries = {"trace": switch}
577
        trace_entries = self.trace_manager.is_entry_valid(entries)
578
579
        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
        def wrap_next_packet(entries, result, packet_in):
584
            tracer.trace_ended = True
585
            return "", "", ""
586
587
        mock_next_packet.side_effect = wrap_next_packet
588
589
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
590
591
        # Execute tracepath
592
        await tracer.tracepath_loop(trace_entries, color, switch)
593
        result = tracer.trace_result
594
595
        mock_check_loop.assert_called_once()
596
        mock_next_packet.assert_not_called()
597
        mock_probe.assert_called_once()
598
        assert mock_get_switch.call_count == 3
599
        mock_switch_colors.assert_called_once()
600
601
        assert result[0]["type"] == "trace"
602
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
603
@@ 434-491 (lines=58) @@
431
        mock_switch_colors.assert_called_once()
432
        assert result == 0
433
434
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
435
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")
436
    @patch("napps.amlight.sdntrace.tracing.tracer.TracePath.send_trace_probe")
437
    @patch("napps.amlight.sdntrace.tracing.tracer.prepare_next_packet")
438
    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
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
448
449
        # Patch Switches.get_switch
450
        def wrap_get_switch(dpid):
451
            switch = MagicMock()
452
            switch.dpid = dpid
453
            return switch
454
455
        mock_get_switch.side_effect = wrap_get_switch
456
457
        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
        trace_id = 111
464
465
        # Creating trace entries
466
        eth = {"dl_vlan": 100}
467
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
468
        switch = {"switch": dpid, "eth": eth}
469
        entries = {"trace": switch}
470
        trace_entries = self.trace_manager.is_entry_valid(entries)
471
472
        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
        def wrap_next_packet(entries, result, packet_in):
477
            tracer.trace_ended = True
478
            return "", "", ""
479
480
        mock_next_packet.side_effect = wrap_next_packet
481
482
        color = {"color_field": "dl_src", "color_value": "ee:ee:ee:ee:01:2c"}
483
484
        # Execute tracepath
485
        await tracer.tracepath_loop(trace_entries, color, switch)
486
        result = tracer.trace_result
487
488
        mock_probe.assert_called_once()
489
        mock_switch_colors.assert_called_once()
490
        assert result[0]["type"] == "trace"
491
        assert result[0]["dpid"] == "00:00:00:00:00:00:00:01"
492
493
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
494
    @patch("napps.amlight.sdntrace.shared.switches.Switches.get_switch")