TestOpenflow13.test_send_packet_out()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
""" Tests for /backends/openflow13.py """
2
3 1
from unittest.mock import MagicMock, patch
4
5 1
from napps.amlight.sdntrace import settings
6 1
from napps.amlight.sdntrace.backends.openflow13 import (
7
    packet_in,
8
    send_packet_out,
9
)
10
11
12
# pylint: disable=too-many-public-methods, too-many-lines
13 1
class TestOpenflow13:
14
    """Unit tests for backends.openflow13 functions"""
15
16 1
    def test_packet_in_with_color(self):
17
        """Test packet in with color ee:ee:ee:ee:ee:01."""
18 1
        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 1
        packet_in_msg = MagicMock()
23 1
        packet_in_msg.data.value = raw
24 1
        packet_in_msg.in_port.value = 1
25
26 1
        event = MagicMock()
27 1
        event.source.switch = "ee:ee:ee:ee:ee:01"
28 1
        event.message.in_port = 1
29
30 1
        ethernet, in_port, switch = packet_in(event, packet_in_msg)
31
32 1
        assert ethernet.source.value == "ee:ee:ee:ee:ee:01"
33 1
        assert in_port == 1
34 1
        assert switch == "ee:ee:ee:ee:ee:01"
35
36 1
    def test_normal_packet_in(self):
37
        """Test packet in without color."""
38 1
        raw = b"\x00\x15\xaf\xd58\x98\x00\x1f:>\x9a\xcf\x08\x00testdata"
39
        # Ethernet(destination='00:15:af:d5:38:98',
40
        #          source='00:1f:3a:3e:9a:cf', ether_type=0x800,
41
        #          data=b'testdata')
42 1
        packet_in_msg = MagicMock()
43 1
        packet_in_msg.data.value = raw
44 1
        packet_in_msg.in_port.value = 1
45
46 1
        event = MagicMock()
47 1
        event.source.switch = "00:1f:3a:3e:9a:cf"
48
49 1
        ethernet, in_port, switch = packet_in(event, packet_in_msg)
50
51 1
        assert ethernet == 0
52 1
        assert in_port == 0
53 1
        assert switch == 0
54
55 1
    def test_packet_out_with_color(self):
56
        """Test packet in with color ee:ee:ee:ee:ee:01."""
57 1
        data = b"\x00\x15\xaf\xd58\x98\xee\xee\xee\xee\xee\x01\x08\x00testdata"
58
        # Ethernet(destination='00:15:af:d5:38:98',
59
        #          source='ee:ee:ee:ee:ee:01', ether_type=0x800,
60
        #          data=b'testdata')
61 1
        controller = MagicMock()
62 1
        switch = MagicMock()
63 1
        switch.connection.value = "00:15:af:d5:38:98"
64 1
        port = 1
65
66 1
        send_packet_out(controller, switch, port, data)
67
68
        # Verify that the controller sent the packet_out
69 1
        controller.buffers.msg_out.put.assert_called_once()
70 1
        called_event = controller.buffers.msg_out.put.call_args.args[0]
71
72
        # Verify the packet_out values
73 1
        assert called_event.name == "kytos/of_lldp.messages.out.ofpt_packet_out"
74 1
        assert called_event.content["destination"].value == switch.connection.value
75 1
        assert called_event.content["message"].in_port == port
76 1
        assert called_event.content["message"].data == data
77 1
        assert called_event.content["message"].actions[0].port == settings.OFPP_TABLE_13
78
79 1
    @patch("napps.amlight.sdntrace.backends.openflow13.of_msg_prio")
80 1
    def test_send_packet_out(self, mock_of_msg_prio) -> None:
81
        """Test send_packet_out."""
82 1
        controller = MagicMock()
83 1
        send_packet_out(controller, MagicMock(), MagicMock(), MagicMock())
84 1
        assert controller.buffers.msg_out.put.call_count == 1
85
        assert mock_of_msg_prio.call_count == 1
86