Passed
Push — master ( 127cfb...14f54d )
by Vinicius
02:06 queued 12s
created

build.tests.unit.test_liveness_controller   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A TestLivenessController.test_enable_interfaces() 0 11 1
A TestLivenessController.test_boostrap_indexes() 0 9 1
A TestLivenessController.test_get_enabled_interfaces() 0 9 1
A TestLivenessController.test_upsert_interfaces() 0 6 1
A TestLivenessController.test_disable_interfaces() 0 11 1
1
"""Module to test LivenessController."""
2
# pylint: disable=invalid-name,relative-beyond-top-level,no-self-use
3
4
from unittest.mock import MagicMock
5
6
7
class TestLivenessController:  # pylint: disable=too-many-public-methods
8
    """Test the Main class."""
9
10
    def test_boostrap_indexes(self, liveness_controller) -> None:
11
        """Test_boostrap_indexes."""
12
        liveness_controller.bootstrap_indexes()
13
14
        expected_indexes = [("liveness", [("enabled", 1)])]
15
        mock = liveness_controller.mongo.bootstrap_index
16
        assert mock.call_count == len(expected_indexes)
17
        indexes = [(v[0][0], v[0][1]) for v in mock.call_args_list]
18
        assert expected_indexes == indexes
19
20
    def test_get_enabled_interfaces(self, liveness_controller) -> None:
21
        """Test get_enabled_interfaces."""
22
        liveness_controller.get_enabled_interfaces()
23
        assert liveness_controller.db.liveness.aggregate.call_count == 1
24
        call_args = liveness_controller.db.liveness.aggregate.call_args
25
        assert call_args[0][0] == [
26
            {"$match": {"enabled": True}},
27
            {"$sort": {"_id": 1}},
28
            {"$project": {"_id": 0}},
29
        ]
30
31
    def test_upsert_interfaces(self, liveness_controller) -> None:
32
        """Test upsert_interfaces."""
33
        intf_ids = ["00:00:00:00:00:00:00:01:2", "00:00:00:00:00:00:00:02:2"]
34
        intf_dicts = [{"enabled": True}, {"enabled": True}]
35
        liveness_controller.upsert_interfaces(intf_ids, intf_dicts)
36
        assert liveness_controller.db.liveness.bulk_write.call_count == 1
37
38
    def test_enable_interfaces(self, liveness_controller) -> None:
39
        """Test enable_interfaces."""
40
        intf_ids = ["00:00:00:00:00:00:00:01:2", "00:00:00:00:00:00:00:02:2"]
41
        liveness_controller.upsert_interfaces = MagicMock()
42
        upsert_mock = liveness_controller.upsert_interfaces
43
        liveness_controller.enable_interfaces(intf_ids)
44
        assert upsert_mock.call_count == 1
45
        upsert_mock.assert_called_with(
46
            intf_ids,
47
            [{"enabled": True}, {"enabled": True}],
48
            upsert=True,
49
        )
50
51
    def test_disable_interfaces(self, liveness_controller) -> None:
52
        """Test disable_interfaces."""
53
        intf_ids = ["00:00:00:00:00:00:00:01:2", "00:00:00:00:00:00:00:02:2"]
54
        liveness_controller.upsert_interfaces = MagicMock()
55
        upsert_mock = liveness_controller.upsert_interfaces
56
        liveness_controller.disable_interfaces(intf_ids)
57
        assert upsert_mock.call_count == 1
58
        upsert_mock.assert_called_with(
59
            intf_ids,
60
            [{"enabled": False}, {"enabled": False}],
61
            upsert=False,
62
        )
63