Passed
Pull Request — master (#651)
by Carlos Eduardo
02:24
created

TestInterface._get_v0x04_iface()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
c 1
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.switch import Interface, Switch
9
10
logging.basicConfig(level=logging.CRITICAL)
11
12
13
class TestInterface(unittest.TestCase):
14
    """Test Interfaces."""
15
16
    def setUp(self):
17
        """Create interface object."""
18
        self.iface = self._get_v0x04_iface()
19
20
    @staticmethod
21
    def _get_v0x04_iface(*args, **kwargs):
22
        """Create a v0x04 interface object with optional extra arguments."""
23
        switch = Switch('dpid')
24
        switch.connection = Mock()
25
        switch.connection.protocol.version = 0x04
26
        return Interface('name', 42, switch, *args, **kwargs)
27
28
    def test_speed_feature_none(self):
29
        """When port's current features is None."""
30
        self.iface.features = None
31
        self.assertIsNone(self.iface.speed)
32
        self.assertEqual('', self.iface.get_hr_speed())
33
34
    def test_speed_feature_zero(self):
35
        """When port's current features is 0. E.g. port 65534."""
36
        self.iface.features = 0
37
        self.assertIsNone(self.iface.speed)
38
        self.assertEqual('', self.iface.get_hr_speed())
39
40
    def test_1_tera_speed(self):
41
        """1Tb link."""
42
        self.iface.features = PortFeatures.OFPPF_1TB_FD
43
        self.assertEqual(10**12 / 8, self.iface.speed)
44
        self.assertEqual('1 Tbps', self.iface.get_hr_speed())
45
46
    def test_100_giga_speed(self):
47
        """100Gb link."""
48
        self.iface.features = PortFeatures.OFPPF_100GB_FD
49
        self.assertEqual(100 * 10**9 / 8, self.iface.speed)
50
        self.assertEqual('100 Gbps', self.iface.get_hr_speed())
51
52
    def test_40_giga_speed(self):
53
        """40Gb link."""
54
        self.iface.features = PortFeatures.OFPPF_40GB_FD
55
        self.assertEqual(40 * 10**9 / 8, self.iface.speed)
56
        self.assertEqual('40 Gbps', self.iface.get_hr_speed())
57
58
    def test_10_giga_speed(self):
59
        """10Gb link."""
60
        self.iface.features = PortFeatures.OFPPF_10GB_FD
61
        self.assertEqual(10 * 10**9 / 8, self.iface.speed)
62
        self.assertEqual('10 Gbps', self.iface.get_hr_speed())
63
64
    def test_1_giga_speed(self):
65
        """1Gb link."""
66
        self.iface.features = PortFeatures.OFPPF_1GB_FD
67
        self.assertEqual(10**9 / 8, self.iface.speed)
68
        self.assertEqual('1 Gbps', self.iface.get_hr_speed())
69
70
    def test_100_mega_speed(self):
71
        """100Mb link."""
72
        self.iface.features = PortFeatures.OFPPF_100MB_FD
73
        self.assertEqual(100 * 10**6 / 8, self.iface.speed)
74
        self.assertEqual('100 Mbps', self.iface.get_hr_speed())
75
76
    def test_10_mega_speed(self):
77
        """10Mb link."""
78
        self.iface.features = PortFeatures.OFPPF_10MB_FD
79
        self.assertEqual(10 * 10**6 / 8, self.iface.speed)
80
        self.assertEqual('10 Mbps', self.iface.get_hr_speed())
81
82
    def test_speed_setter(self):
83
        """Should return speed that was set and not features'."""
84
        expected_speed = 12345
85
        self.iface.features = PortFeatures.OFPPF_10MB_FD
86
        self.iface.set_custom_speed(expected_speed)
87
        actual_speed = self.iface.speed
88
        self.assertEqual(expected_speed, actual_speed)
89
90
    def test_speed_in_constructor(self):
91
        """Custom speed should override features'."""
92
        expected_speed = 6789
93
        iface = self._get_v0x04_iface(speed=expected_speed,
94
                                      features=PortFeatures.OFPPF_10MB_FD)
95
        actual_speed = iface.speed
96
        self.assertEqual(expected_speed, actual_speed)
97
98
    def test_remove_custom_speed(self):
99
        """Should return features' speed again when custom's becomes None."""
100
        custom_speed = 101112
101
        of_speed = 10 * 10**6 / 8
102
        iface = self._get_v0x04_iface(speed=custom_speed,
103
                                      features=PortFeatures.OFPPF_10MB_FD)
104
        self.assertEqual(custom_speed, iface.speed)
105
        iface.set_custom_speed(None)
106
        self.assertEqual(of_speed, iface.speed)
107