Code Duplication    Length = 58-64 lines in 2 locations

tests/unit/tracing/test_tracer.py 2 locations

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