build.tests.unit.backends.test_of_parser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 91
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A TestOFParser.test_process_packet_in() 0 20 1
A TestOFParser.test_process_of13_packet_in_patched() 0 11 1
A TestOFParser.test_process_packet_in_error() 0 15 1
A TestOFParser.test_process_of13_packet_out_patched() 0 13 1
A TestOFParser.test_process_packet_out_error() 0 13 1
1
""" Tests for /backends/of_parser.py """
2
3 1
from unittest.mock import MagicMock, patch
4
5 1
from napps.amlight.sdntrace.backends.of_parser import (
6
    process_packet_in,
7
    send_packet_out,
8
)
9
10
11
# pylint: disable=too-many-public-methods, too-many-lines
12 1
class TestOFParser:
13
    """Unit tests for backends.of_parser functions"""
14
15 1
    def test_process_packet_in(self):
16
        """Test process packet in for openflow13 with data."""
17 1
        raw = b"\x00\x15\xaf\xd58\x98\xee\xee\xee\xee\xee\x01\x08\x00testdata"
18
        # Ethernet(destination='00:15:af:d5:38:98',
19
        #          source='ee:ee:ee:ee:ee:01', ether_type=0x800,
20
        #          data=b'testdata')
21 1
        packet_in_msg = MagicMock()
22 1
        packet_in_msg.data.value = raw
23
24 1
        event = MagicMock()
25 1
        event.source.switch = "ee:ee:ee:ee:ee:01"
26 1
        event.content = {"message": packet_in_msg}
27 1
        event.content["message"].header.version.value = 4
28 1
        event.message.in_port = 1
29
30 1
        ethernet, in_port, switch = process_packet_in(event)
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
    @patch("napps.amlight.sdntrace.backends.openflow13.packet_in")
37 1
    def test_process_of13_packet_in_patched(self, mock_of13_packet_in):
38
        """Test process packet in for openflow13."""
39
40 1
        event = MagicMock()
41 1
        event.content = {"message": MagicMock()}
42 1
        event.content["message"].header.version.value = 4
43
44 1
        process_packet_in(event)
45
46 1
        mock_of13_packet_in.assert_called_once()
47
48 1
    @patch("napps.amlight.sdntrace.backends.openflow13.packet_in")
49 1
    def test_process_packet_in_error(self, mock_of13_packet_in):
50
        """Test process packet in for openflow13."""
51
52 1
        event = MagicMock()
53 1
        event.content = {"message": MagicMock()}
54 1
        event.content["message"].header.version.value = 9999
55
56 1
        ethernet, in_port, switch = process_packet_in(event)
57
58 1
        assert ethernet == 0
59 1
        assert in_port == 0
60 1
        assert switch == 0
61
62 1
        mock_of13_packet_in.assert_not_called()
63
64 1
    @patch("napps.amlight.sdntrace.backends.openflow13.send_packet_out")
65 1
    def test_process_of13_packet_out_patched(self, mock_of13_packet_out):
66
        """Test process packet out for openflow13."""
67 1
        controller = MagicMock()
68 1
        switch = MagicMock()
69 1
        switch.features.header.version.value = 4
70
71 1
        port = MagicMock()
72 1
        data = MagicMock()
73
74 1
        send_packet_out(controller, switch, port, data)
75
76 1
        mock_of13_packet_out.assert_called_once()
77
78 1
    @patch("napps.amlight.sdntrace.backends.openflow13.send_packet_out")
79 1
    def test_process_packet_out_error(self, mock_of13_packet_out):
80
        """Test process packet out for openflow13."""
81 1
        controller = MagicMock()
82 1
        switch = MagicMock()
83 1
        switch.features.header.version.value = 999
84
85 1
        port = MagicMock()
86 1
        data = MagicMock()
87
88 1
        send_packet_out(controller, switch, port, data)
89
90
        mock_of13_packet_out.assert_not_called()
91