Completed
Push — master ( ae1c36...646d2b )
by Humberto
25s queued 14s
created

TestInterface.test_interface_use_tags()   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 1
dl 0
loc 16
rs 9.95
c 0
b 0
f 0
1
"""Interface tests."""
2
import logging
3
import unittest
4
from unittest.mock import Mock
5
6
from pyof.v0x04.common.port import PortFeatures
7
8
from kytos.core.interface import Interface, TAG, TAGType
9
from kytos.core.switch import Switch
10
11
logging.basicConfig(level=logging.CRITICAL)
12
13
14
class TestInterface(unittest.TestCase):
15
    """Test Interfaces."""
16
17
    def setUp(self):
18
        """Create interface object."""
19
        self.iface = self._get_v0x04_iface()
20
21
    @staticmethod
22
    def _get_v0x04_iface(*args, **kwargs):
23
        """Create a v0x04 interface object with optional extra arguments."""
24
        switch = Switch('dpid')
25
        switch.connection = Mock()
26
        switch.connection.protocol.version = 0x04
27
        return Interface('name', 42, switch, *args, **kwargs)
28
29
    def test_speed_feature_none(self):
30
        """When port's current features is None."""
31
        self.iface.features = None
32
        self.assertIsNone(self.iface.speed)
33
        self.assertEqual('', self.iface.get_hr_speed())
34
35
    def test_speed_feature_zero(self):
36
        """When port's current features is 0. E.g. port 65534."""
37
        self.iface.features = 0
38
        self.assertIsNone(self.iface.speed)
39
        self.assertEqual('', self.iface.get_hr_speed())
40
41
    def test_1_tera_speed(self):
42
        """1Tb link."""
43
        self.iface.features = PortFeatures.OFPPF_1TB_FD
44
        self.assertEqual(10**12 / 8, self.iface.speed)
45
        self.assertEqual('1 Tbps', self.iface.get_hr_speed())
46
47
    def test_100_giga_speed(self):
48
        """100Gb link."""
49
        self.iface.features = PortFeatures.OFPPF_100GB_FD
50
        self.assertEqual(100 * 10**9 / 8, self.iface.speed)
51
        self.assertEqual('100 Gbps', self.iface.get_hr_speed())
52
53
    def test_40_giga_speed(self):
54
        """40Gb link."""
55
        self.iface.features = PortFeatures.OFPPF_40GB_FD
56
        self.assertEqual(40 * 10**9 / 8, self.iface.speed)
57
        self.assertEqual('40 Gbps', self.iface.get_hr_speed())
58
59
    def test_10_giga_speed(self):
60
        """10Gb link."""
61
        self.iface.features = PortFeatures.OFPPF_10GB_FD
62
        self.assertEqual(10 * 10**9 / 8, self.iface.speed)
63
        self.assertEqual('10 Gbps', self.iface.get_hr_speed())
64
65
    def test_1_giga_speed(self):
66
        """1Gb link."""
67
        self.iface.features = PortFeatures.OFPPF_1GB_FD
68
        self.assertEqual(10**9 / 8, self.iface.speed)
69
        self.assertEqual('1 Gbps', self.iface.get_hr_speed())
70
71
    def test_100_mega_speed(self):
72
        """100Mb link."""
73
        self.iface.features = PortFeatures.OFPPF_100MB_FD
74
        self.assertEqual(100 * 10**6 / 8, self.iface.speed)
75
        self.assertEqual('100 Mbps', self.iface.get_hr_speed())
76
77
    def test_10_mega_speed(self):
78
        """10Mb link."""
79
        self.iface.features = PortFeatures.OFPPF_10MB_FD
80
        self.assertEqual(10 * 10**6 / 8, self.iface.speed)
81
        self.assertEqual('10 Mbps', self.iface.get_hr_speed())
82
83
    def test_speed_setter(self):
84
        """Should return speed that was set and not features'."""
85
        expected_speed = 12345
86
        self.iface.features = PortFeatures.OFPPF_10MB_FD
87
        self.iface.set_custom_speed(expected_speed)
88
        actual_speed = self.iface.speed
89
        self.assertEqual(expected_speed, actual_speed)
90
91
    def test_speed_in_constructor(self):
92
        """Custom speed should override features'."""
93
        expected_speed = 6789
94
        iface = self._get_v0x04_iface(speed=expected_speed,
95
                                      features=PortFeatures.OFPPF_10MB_FD)
96
        actual_speed = iface.speed
97
        self.assertEqual(expected_speed, actual_speed)
98
99
    def test_remove_custom_speed(self):
100
        """Should return features' speed again when custom's becomes None."""
101
        custom_speed = 101112
102
        of_speed = 10 * 10**6 / 8
103
        iface = self._get_v0x04_iface(speed=custom_speed,
104
                                      features=PortFeatures.OFPPF_10MB_FD)
105
        self.assertEqual(custom_speed, iface.speed)
106
        iface.set_custom_speed(None)
107
        self.assertEqual(of_speed, iface.speed)
108
109
    def test_interface_available_tags(self):
110
        """Test available_tags on Interface class."""
111
        default_range = [vlan for vlan in range(1, 4096)]
112
        intf_values = [tag.value for tag in self.iface.available_tags]
113
        self.assertListEqual(intf_values, default_range)
114
115
        custom_range = [vlan for vlan in range(100, 199)]
116
        self.iface.set_available_tags(custom_range)
117
        intf_values = [tag.value for tag in self.iface.available_tags]
118
        self.assertListEqual(intf_values, custom_range)
119
120
    def test_all_available_tags(self):
121
        """Test all available_tags on Interface class."""
122
        max_range = 4096
123
124
        for i in range(1, max_range):
125
            next_tag = self.iface.get_next_available_tag()
126
            self.assertIs(type(next_tag), TAG)
127
            self.assertEqual(next_tag.value, max_range - i)
128
129
        next_tag = self.iface.get_next_available_tag()
130
        self.assertEqual(next_tag, False)
131
132
    def test_interface_is_tag_available(self):
133
        """Test is_tag_available on Interface class."""
134
        max_range = 4096
135
        for i in range(1, max_range):
136
            tag = TAG(TAGType.VLAN, i)
137
138
            next_tag = self.iface.is_tag_available(tag)
139
            self.assertTrue(next_tag)
140
141
        # test lower limit
142
        tag = TAG(TAGType.VLAN, 0)
143
        self.assertFalse(self.iface.is_tag_available(tag))
144
        # test upper limit
145
        tag = TAG(TAGType.VLAN, max_range)
146
        self.assertFalse(self.iface.is_tag_available(tag))
147
148
    def test_interface_use_tags(self):
149
        """Test all use_tag on Interface class."""
150
151
        tag = TAG(TAGType.VLAN, 100)
152
        # check use tag for the first time
153
        is_success = self.iface.use_tag(tag)
154
        self.assertTrue(is_success)
155
156
        # check use tag for the second time
157
        is_success = self.iface.use_tag(tag)
158
        self.assertFalse(is_success)
159
160
        # check use tag after returning the tag to the pool
161
        self.iface.make_tag_available(tag)
162
        is_success = self.iface.use_tag(tag)
163
        self.assertTrue(is_success)
164