Passed
Pull Request — master (#84)
by Vinicius
02:44
created

build.tests.unit.test_topo_controller   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 180
dl 0
loc 260
rs 10
c 0
b 0
f 0
wmc 27

25 Methods

Rating   Name   Duplication   Size   Complexity  
A TestTopoController.test_enable_switch() 0 8 1
A TestTopoController.test_add_switch_metadata() 0 9 1
A TestTopoController.test_get_topology() 0 7 1
A TestTopoController.test_get_links() 0 7 1
A TestTopoController.test_enable_interface() 0 8 1
A TestTopoController.test_get_interfaces() 0 10 1
A TestTopoController.test_delete_switch_metadata() 0 9 1
A TestTopoController.test_deactivate_interface() 0 8 1
A TestTopoController.test_disable_interface() 0 8 1
A TestTopoController.test_disable_switch() 0 8 1
A TestTopoController.test_activate_interface() 0 8 1
A TestTopoController.test_get_switches() 0 8 1
A TestTopoController.test_boostrap_indexes() 0 12 1
A TestTopoController.test_deactivate_switch() 0 8 1
A TestTopoController.setUp() 0 6 1
A TestTopoController.test_add_link_metadata() 0 10 1
A TestTopoController.test_enable_link() 0 8 1
A TestTopoController.test_bulk_upsert_interface_details() 0 13 2
A TestTopoController.test_delete_interface_metadata_key() 0 9 1
A TestTopoController.test_add_interface_metadata() 0 9 1
A TestTopoController.test_upsert_switch() 0 10 2
A TestTopoController.test_get_interfaces_details() 0 6 1
A TestTopoController.test_disable_link() 0 8 1
A TestTopoController.test_delete_link_metadata_key() 0 9 1
A TestTopoController.test_upsert_link() 0 12 1
1
"""Module to TopoController."""
2
3
from unittest import TestCase
4
from unittest.mock import MagicMock
5
6
from pymongo.operations import UpdateOne
7
8
from napps.kytos.topology.controllers import TopoController
9
from napps.kytos.topology.db.models import LinkDoc, SwitchDoc
10
11
12
class TestTopoController(TestCase):  # pylint: disable=too-many-public-methods
13
    """Test the Main class."""
14
15
    def setUp(self) -> None:
16
        """Execute steps before each tests."""
17
        self.topo = TopoController(MagicMock())
18
        self.dpid = "00:00:00:00:00:00:00:01"
19
        self.interface_id = f"{self.dpid}:1"
20
        self.link_id = "some_id"
21
22
    def test_boostrap_indexes(self) -> None:
23
        """Test_boostrap_indexes."""
24
        self.topo.bootstrap_indexes()
25
26
        expected_indexes = [
27
            ("switches", [("interfaces.id", 1)]),
28
            ("links", [("endpoints.id", 1)]),
29
        ]
30
        mock = self.topo.mongo.bootstrap_index
31
        assert mock.call_count == len(expected_indexes)
32
        indexes = [(v[0][0], v[0][1]) for v in mock.call_args_list]
33
        assert expected_indexes == indexes
34
35
    def test_get_topology(self) -> None:
36
        """Test_get_topology."""
37
        self.topo.get_switches = MagicMock()
38
        self.topo.get_links = MagicMock()
39
        assert "topology" in self.topo.get_topology()
40
        assert self.topo.get_switches.call_count == 1
41
        assert self.topo.get_links.call_count == 1
42
43
    def test_get_links(self) -> None:
44
        """test_get_links."""
45
        assert "links" in self.topo.get_links()
46
        assert self.topo.db.links.aggregate.call_count == 1
47
        arg = self.topo.db.links.aggregate.call_args[0]
48
        assert arg[0] == [{"$sort": {"_id": 1}},
49
                          {"$project": LinkDoc.projection()}]
50
51
    def test_get_switches(self) -> None:
52
        """test_get_switches."""
53
        assert "switches" in self.topo.get_switches()
54
        assert self.topo.db.switches.aggregate.call_count == 1
55
        arg = self.topo.db.switches.aggregate.call_args[0]
56
        assert arg[0] == [
57
            {"$sort": {"_id": 1}},
58
            {"$project": SwitchDoc.projection()},
59
        ]
60
61
    def test_get_interfaces(self) -> None:
62
        """test_get_interfaces."""
63
        assert "interfaces" in self.topo.get_interfaces()
64
        assert self.topo.db.switches.aggregate.call_count == 1
65
        arg = self.topo.db.switches.aggregate.call_args[0]
66
        assert arg[0] == [
67
            {"$sort": {"_id": 1}},
68
            {"$project": {"interfaces": 1, "_id": 0}},
69
            {"$unwind": "$interfaces"},
70
            {"$replaceRoot": {"newRoot": "$interfaces"}},
71
        ]
72
73
    def test_enable_switch(self) -> None:
74
        """test_enable_switch."""
75
        self.topo.enable_switch(self.dpid)
76
77
        self.topo.db.switches.find_one_and_update.assert_called()
78
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
79
        assert arg1 == {"_id": self.dpid}
80
        assert arg2["$set"]["enabled"]
81
82
    def test_disable_switch(self) -> None:
83
        """test_disable_switch."""
84
        self.topo.disable_switch(self.dpid)
85
86
        self.topo.db.switches.find_one_and_update.assert_called()
87
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
88
        assert arg1 == {"_id": self.dpid}
89
        assert not arg2["$set"]["enabled"]
90
91
    def test_deactivate_switch(self) -> None:
92
        """test_deactivate_switch."""
93
        self.topo.deactivate_switch(self.dpid)
94
95
        self.topo.db.switches.find_one_and_update.assert_called()
96
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
97
        assert arg1 == {"_id": self.dpid}
98
        assert not arg2["$set"]["active"]
99
100
    def test_add_switch_metadata(self) -> None:
101
        """test_add_switch_metadata."""
102
        metadata = {"some": "value"}
103
        self.topo.add_switch_metadata(self.dpid, metadata)
104
105
        self.topo.db.switches.find_one_and_update.assert_called()
106
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
107
        assert arg1 == {"_id": self.dpid}
108
        assert arg2["$set"]["metadata.some"] == "value"
109
110
    def test_delete_switch_metadata(self) -> None:
111
        """test_delete_switch_metadata."""
112
        key = "some"
113
        self.topo.delete_switch_metadata_key(self.dpid, key)
114
115
        self.topo.db.switches.find_one_and_update.assert_called()
116
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
117
        assert arg1 == {"_id": self.dpid}
118
        assert arg2["$unset"][f"metadata.{key}"] == ""
119
120
    def test_enable_interface(self) -> None:
121
        """test_enable_interface."""
122
        self.topo.enable_interface(self.interface_id)
123
124
        self.topo.db.switches.find_one_and_update.assert_called()
125
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
126
        assert arg1 == {"interfaces.id": self.interface_id}
127
        assert arg2["$set"]["interfaces.$.enabled"]
128
129
    def test_disable_interface(self) -> None:
130
        """test_disable_interface."""
131
        self.topo.disable_interface(self.interface_id)
132
133
        self.topo.db.switches.find_one_and_update.assert_called()
134
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
135
        assert arg1 == {"interfaces.id": self.interface_id}
136
        assert not arg2["$set"]["interfaces.$.enabled"]
137
138
    def test_activate_interface(self) -> None:
139
        """test_activate_interface."""
140
        self.topo.activate_interface(self.interface_id)
141
142
        self.topo.db.switches.find_one_and_update.assert_called()
143
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
144
        assert arg1 == {"interfaces.id": self.interface_id}
145
        assert arg2["$set"]["interfaces.$.active"]
146
147
    def test_deactivate_interface(self) -> None:
148
        """test_deactivate_interface."""
149
        self.topo.deactivate_interface(self.interface_id)
150
151
        self.topo.db.switches.find_one_and_update.assert_called()
152
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
153
        assert arg1 == {"interfaces.id": self.interface_id}
154
        assert not arg2["$set"]["interfaces.$.active"]
155
156
    def test_add_interface_metadata(self) -> None:
157
        """test_add_interface_metadata."""
158
        metadata = {"some": "value"}
159
        self.topo.add_interface_metadata(self.interface_id, metadata)
160
161
        self.topo.db.switches.find_one_and_update.assert_called()
162
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
163
        assert arg1 == {"interfaces.id": self.interface_id}
164
        assert arg2["$set"]["interfaces.$.metadata.some"] == "value"
165
166
    def test_delete_interface_metadata_key(self) -> None:
167
        """test_delete_interface_metadata."""
168
        key = "some"
169
        self.topo.delete_interface_metadata_key(self.interface_id, key)
170
171
        self.topo.db.switches.find_one_and_update.assert_called()
172
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
173
        assert arg1 == {"interfaces.id": self.interface_id}
174
        assert arg2["$unset"][f"interfaces.$.metadata.{key}"] == ""
175
176
    def test_enable_link(self) -> None:
177
        """test_enable_link."""
178
        self.topo.enable_link(self.link_id)
179
180
        self.topo.db.links.find_one_and_update.assert_called()
181
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
182
        assert arg1 == {"_id": self.link_id}
183
        assert arg2["$set"]["enabled"]
184
185
    def test_disable_link(self) -> None:
186
        """test_disable_link."""
187
        self.topo.disable_link(self.link_id)
188
189
        self.topo.db.links.find_one_and_update.assert_called()
190
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
191
        assert arg1 == {"_id": self.link_id}
192
        assert not arg2["$set"]["enabled"]
193
194
    def test_add_link_metadata(self) -> None:
195
        """test_add_link_metadata."""
196
        key = "some_key"
197
        value = "some_value"
198
        self.topo.add_link_metadata(self.link_id, {key: value})
199
200
        self.topo.db.links.find_one_and_update.assert_called()
201
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
202
        assert arg1 == {"_id": self.link_id}
203
        assert arg2["$set"][f"metadata.{key}"] == value
204
205
    def test_delete_link_metadata_key(self) -> None:
206
        """test_delete_link_metadata_key."""
207
        key = "some_key"
208
        self.topo.delete_link_metadata_key(self.link_id, key)
209
210
        self.topo.db.links.find_one_and_update.assert_called()
211
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
212
        assert arg1 == {"_id": self.link_id}
213
        assert arg2["$unset"][f"metadata.{key}"] == ""
214
215
    def test_get_interfaces_details(self) -> None:
216
        """test_get_insterfaces_details."""
217
        interfaces_ids = ["1", "2", "3"]
218
        self.topo.get_interfaces_details(interfaces_ids)
219
        self.topo.db.interface_details.aggregate.assert_called_with(
220
            [{"$match": {"_id": {"$in": interfaces_ids}}}]
221
        )
222
223
    def test_bulk_upsert_interface_details(self) -> None:
224
        """test_bulk_upsert_interface_details."""
225
        ids_details = [
226
            ("1", {"_id": "1", "available_vlans": [1]}),
227
            ("2", {"_id": "2", "available_vlans": [2]}),
228
        ]
229
        self.topo.bulk_upsert_interface_details(ids_details)
230
231
        self.topo.db_client.start_session.assert_called()
232
        arg1 = self.topo.db.interface_details.bulk_write.call_args[0]
233
        assert len(arg1[0]) == len(ids_details)
234
        for item in arg1[0]:
235
            assert isinstance(item, UpdateOne)
236
237
    def test_upsert_switch(self) -> None:
238
        """test_upsert_switch."""
239
        switch_dict = {"enabled": True, "active": True, "_id": self.dpid}
240
        self.topo.upsert_switch(self.dpid, switch_dict)
241
242
        self.topo.db.switches.find_one_and_update.assert_called()
243
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
244
        assert arg1 == {"_id": self.dpid}
245
        for key, value in switch_dict.items():
246
            assert arg2["$set"][key] == value
247
248
    def test_upsert_link(self) -> None:
249
        """test_upsert_link."""
250
        link_dict = {
251
            "_id": self.link_id,
252
            "enabled": True,
253
            "active": True,
254
            "endpoint_a": {"id": "00:00:00:00:00:00:00:01:1"},
255
            "endpoint_b": {"id": "00:00:00:00:00:00:00:02:01"},
256
        }
257
        self.topo.upsert_link(self.link_id, link_dict)
258
        assert self.topo.db.switches.find_one_and_update.call_count == 2
259
        assert self.topo.db.links.find_one_and_update.call_count == 1
260