Completed
Push — master ( 912aad...faaf9a )
by Beraldo
15s queued 10s
created

TestInterface.test_interface_available_tags()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 1
dl 0
loc 10
rs 10
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
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