Passed
Pull Request — master (#42)
by Jose
02:25
created

TestUserSpeed.test_port_speed()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""Test UserSpeed."""
2
import logging
3
import unittest
4
from pathlib import Path
5
from unittest.mock import mock_open, patch  # noqa (isort conflict)
6
7
from napps.kytos.of_stats.user_speed import UserSpeed
8
9
logging.basicConfig(level=logging.CRITICAL)
10
11
12
class TestUserSpeed(unittest.TestCase):
13
    """Test UserSpeed."""
14
15
    def setUp(self):
16
        """Path().exists() mock."""
17
        patcher = patch.object(Path, 'exists', return_value=True)
18
        self.file_exists = patcher.start()
19
        self.addCleanup(patcher.stop)
20
21
    def set_file_content(self, content):
22
        """Mock for the user configuration file."""
23
        open_method = mock_open(read_data=content)
24
        patcher = patch.object(Path, 'open', open_method)
25
        patcher.start()
26
        self.addCleanup(patcher.stop)
27
28
    def test_no_file(self):
29
        """Speed should be s None without user file."""
30
        self.file_exists.return_value = False
31
        user = UserSpeed()
32
        self.assertIsNone(user.get_speed('dpid'))
33
34
    def test_default(self):
35
        """Test the default for all switches."""
36
        self.set_file_content('{"default": 42000000000}')
37
        self.assertEqual(42 * 10**9, UserSpeed().get_speed('dpid'))
38
        self.assertEqual(42 * 10**9, UserSpeed().get_speed('dpid', 4))
39
40
    def test_no_dpid_no_default(self):
41
        """Return None when no default and no dpid is found."""
42
        self.set_file_content('{"dpid": 42}')
43
        self.assertIsNone(UserSpeed().get_speed('dpid2'))
44
        self.assertIsNone(UserSpeed().get_speed('dpid2', 4))
45
46
    def test_dpid_default(self):
47
        """Return default speed for dpid."""
48
        self.set_file_content('{"dpid": {"default": 42000000000}}')
49
        self.assertEqual(42 * 10**9, UserSpeed().get_speed('dpid'))
50
        self.assertEqual(42 * 10**9, UserSpeed().get_speed('dpid', 1))
51
52
    def test_port_speed(self):
53
        """Return the speed defined for that port, ignoring defaults."""
54
        self.set_file_content(
55
            '{"default": 1, "dpid": {"default": 2000000000, "4": 3000000000}}'
56
        )
57
        self.assertEqual(3 * 10 ** 9, UserSpeed().get_speed("dpid", 4))
58