Passed
Pull Request — master (#20)
by Rogerio
04:59 queued 11s
created

build.tests.unit.backends.test_openflow10   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 83
Duplicated Lines 78.31 %

Importance

Changes 0
Metric Value
eloc 48
dl 65
loc 83
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A TestOpenflow10.test_packet_in_with_color() 18 18 1
A TestOpenflow10.test_normal_packet_in() 18 18 1
A TestOpenflow10.test_packet_out_with_color() 29 29 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
""" Tests for /backends/openflow10.py """
2
from unittest import TestCase
3
from unittest.mock import MagicMock
4
5
from napps.amlight.sdntrace import settings
6
from napps.amlight.sdntrace.backends.openflow10 import (
7
    packet_in,
8
    send_packet_out,
9
)
10
11
12
# pylint: disable=too-many-public-methods, too-many-lines
13
class TestOpenflow10(TestCase):
14
    """Unit tests for /backends/openflow10.py functions."""
15
16 View Code Duplication
    def test_packet_in_with_color(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
17
        """Test packet in with color ee:ee:ee:ee:ee:01."""
18
        raw = b"\x00\x15\xaf\xd58\x98\xee\xee\xee\xee\xee\x01\x08\x00testdata"
19
        # Ethernet(destination='00:15:af:d5:38:98',
20
        #          source='ee:ee:ee:ee:ee:01', ether_type=0x800,
21
        #          data=b'testdata')
22
        packet_in_msg = MagicMock()
23
        packet_in_msg.data.value = raw
24
        packet_in_msg.in_port.value = 1
25
26
        event = MagicMock()
27
        event.source.switch = "ee:ee:ee:ee:ee:01"
28
29
        ethernet, in_port, switch = packet_in(event, packet_in_msg)
30
31
        self.assertEqual(ethernet.source.value, "ee:ee:ee:ee:ee:01")
32
        self.assertEqual(in_port, 1)
33
        self.assertEqual(switch, "ee:ee:ee:ee:ee:01")
34
35 View Code Duplication
    def test_normal_packet_in(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
36
        """Test packet in without color."""
37
        raw = b"\x00\x15\xaf\xd58\x98\x00\x1f:>\x9a\xcf\x08\x00testdata"
38
        # Ethernet(destination='00:15:af:d5:38:98',
39
        #          source='00:1f:3a:3e:9a:cf', ether_type=0x800,
40
        #          data=b'testdata')
41
        packet_in_msg = MagicMock()
42
        packet_in_msg.data.value = raw
43
        packet_in_msg.in_port.value = 1
44
45
        event = MagicMock()
46
        event.source.switch = "00:1f:3a:3e:9a:cf"
47
48
        ethernet, in_port, switch = packet_in(event, packet_in_msg)
49
50
        self.assertEqual(ethernet, 0)
51
        self.assertEqual(in_port, 0)
52
        self.assertEqual(switch, 0)
53
54 View Code Duplication
    def test_packet_out_with_color(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
55
        """Test packet in with color ee:ee:ee:ee:ee:01."""
56
        data = b"\x00\x15\xaf\xd58\x98\xee\xee\xee\xee\xee\x01\x08\x00testdata"
57
        # Ethernet(destination='00:15:af:d5:38:98',
58
        #          source='ee:ee:ee:ee:ee:01', ether_type=0x800,
59
        #          data=b'testdata')
60
        controller = MagicMock()
61
        switch = MagicMock()
62
        switch.connection.value = "00:15:af:d5:38:98"
63
        port = 1
64
65
        send_packet_out(controller, switch, port, data)
66
67
        # Verify that the controller sent the packet_out
68
        controller.buffers.msg_out.put.assert_called_once()
69
        called_event = controller.buffers.msg_out.put.call_args.args[0]
70
71
        # Verify the packet_out values
72
        self.assertEqual(
73
            called_event.name, "kytos/of_lldp.messages.out.ofpt_packet_out"
74
        )
75
        self.assertEqual(
76
            called_event.content["destination"].value, switch.connection.value
77
        )
78
        self.assertEqual(called_event.content["message"].in_port, port)
79
        self.assertEqual(called_event.content["message"].data, data)
80
        self.assertEqual(
81
            called_event.content["message"].actions[0].port,
82
            settings.OFPP_TABLE,
83
        )
84