Passed
Pull Request — master (#246)
by Italo Valcy
03:24
created

TestUtils.test_uni_to_str()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""Module to test the utls.py file."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock
4
5
from napps.kytos.mef_eline.utils import (
6
    compare_endpoint_trace, uni_to_str, compare_uni_out_trace
7
)
8
9
10
# pylint: disable=too-many-public-methods, too-many-lines
11
class TestUtils(TestCase):
12
    """Test utility functions."""
13
14
    def test_compare_endpoint_trace(self):
15
        """Test method compare_endpoint_trace"""
16
17
        trace = {"dpid": "1234", "port": 2, "vlan": 123}
18
19
        switch1 = MagicMock()
20
        switch1.dpid = "1234"
21
        switch2 = MagicMock()
22
        switch2.dpid = "2345"
23
24
        endpoint = MagicMock()
25
        endpoint.port_number = 2
26
        vlan = 123
27
28
        for switch, expected in ((switch1, True), (switch2, False)):
29
            with self.subTest(switch=switch, expected=expected):
30
                endpoint.switch = switch
31
                self.assertEqual(
32
                    compare_endpoint_trace(endpoint, vlan, trace), expected
33
                )
34
                self.assertEqual(
35
                    compare_endpoint_trace(endpoint, None, trace), expected
36
                )
37
38
    def test_uni_to_str(self):
39
        """Test uni_to_str method"""
40
        uni = MagicMock()
41
        uni.interface.switch.dpid = "00:00:00:00:00:00:00:01"
42
        uni.interface.port_number = 1
43
        uni.user_tag.value = 2
44
        self.assertEqual(uni_to_str(uni), "00:00:00:00:00:00:00:01:1:2")
45
46
        # without user_tag
47
        uni.user_tag = None
48
        self.assertEqual(uni_to_str(uni), "00:00:00:00:00:00:00:01:1")
49
50
    def test_compare_uni_out_trace(self):
51
        """Test compare_uni_out_trace method."""
52
        # case1: trace without 'out' info, should return True
53
        uni = MagicMock()
54
        self.assertTrue(compare_uni_out_trace(uni, {}))
55
56
        # case2: trace with valid port and VLAN, should return True
57
        uni.interface.port_number = 1
58
        uni.user_tag.value = 123
59
        trace = {"out": {"port": 1, "vlan": 123}}
60
        self.assertTrue(compare_uni_out_trace(uni, trace))
61
62
        # case3: UNI has VLAN but trace dont have, should return False
63
        trace = {"out": {"port": 1}}
64
        self.assertFalse(compare_uni_out_trace(uni, trace))
65
66
        # case4: UNI and trace dont have VLAN should return True
67
        uni.user_tag = None
68
        self.assertTrue(compare_uni_out_trace(uni, trace))
69
70
        # case5: UNI dont have VLAN but trace has, should return False
71
        trace = {"out": {"port": 1, "vlan": 123}}
72
        self.assertFalse(compare_uni_out_trace(uni, trace))
73