Passed
Pull Request — master (#84)
by Vinicius
03:50
created

build.tests.unit.test_topo_controller   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 294
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 204
dl 0
loc 294
rs 10
c 0
b 0
f 0
wmc 29

27 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_activate_link() 0 13 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_deactivate_link() 0 13 1
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_activate_link(self) -> None:
157
        """test activate_link."""
158
        mock = MagicMock()
159
        self.topo._update_link = mock
160
        self.topo.activate_link(self.link_id, last_status_change=1,
161
                                last_status_is_active=True)
162
        self.topo._update_link.assert_called_with(
163
            self.link_id,
164
            {
165
                "$set": {
166
                    "metadata.last_status_change": 1,
167
                    "metadata.last_status_is_active": True,
168
                    "active": True,
169
                }
170
            },
171
        )
172
173
    def test_deactivate_link(self) -> None:
174
        """test deactivate_link."""
175
        mock = MagicMock()
176
        self.topo._update_link = mock
177
        self.topo.deactivate_link(self.link_id, last_status_change=1,
178
                                  last_status_is_active=False)
179
        self.topo._update_link.assert_called_with(
180
            self.link_id,
181
            {
182
                "$set": {
183
                    "metadata.last_status_change": 1,
184
                    "metadata.last_status_is_active": False,
185
                    "active": False,
186
                }
187
            },
188
        )
189
190
    def test_add_interface_metadata(self) -> None:
191
        """test_add_interface_metadata."""
192
        metadata = {"some": "value"}
193
        self.topo.add_interface_metadata(self.interface_id, metadata)
194
195
        self.topo.db.switches.find_one_and_update.assert_called()
196
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
197
        assert arg1 == {"interfaces.id": self.interface_id}
198
        assert arg2["$set"]["interfaces.$.metadata.some"] == "value"
199
200
    def test_delete_interface_metadata_key(self) -> None:
201
        """test_delete_interface_metadata."""
202
        key = "some"
203
        self.topo.delete_interface_metadata_key(self.interface_id, key)
204
205
        self.topo.db.switches.find_one_and_update.assert_called()
206
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
207
        assert arg1 == {"interfaces.id": self.interface_id}
208
        assert arg2["$unset"][f"interfaces.$.metadata.{key}"] == ""
209
210
    def test_enable_link(self) -> None:
211
        """test_enable_link."""
212
        self.topo.enable_link(self.link_id)
213
214
        self.topo.db.links.find_one_and_update.assert_called()
215
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
216
        assert arg1 == {"_id": self.link_id}
217
        assert arg2["$set"]["enabled"]
218
219
    def test_disable_link(self) -> None:
220
        """test_disable_link."""
221
        self.topo.disable_link(self.link_id)
222
223
        self.topo.db.links.find_one_and_update.assert_called()
224
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
225
        assert arg1 == {"_id": self.link_id}
226
        assert not arg2["$set"]["enabled"]
227
228
    def test_add_link_metadata(self) -> None:
229
        """test_add_link_metadata."""
230
        key = "some_key"
231
        value = "some_value"
232
        self.topo.add_link_metadata(self.link_id, {key: value})
233
234
        self.topo.db.links.find_one_and_update.assert_called()
235
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
236
        assert arg1 == {"_id": self.link_id}
237
        assert arg2["$set"][f"metadata.{key}"] == value
238
239
    def test_delete_link_metadata_key(self) -> None:
240
        """test_delete_link_metadata_key."""
241
        key = "some_key"
242
        self.topo.delete_link_metadata_key(self.link_id, key)
243
244
        self.topo.db.links.find_one_and_update.assert_called()
245
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
246
        assert arg1 == {"_id": self.link_id}
247
        assert arg2["$unset"][f"metadata.{key}"] == ""
248
249
    def test_get_interfaces_details(self) -> None:
250
        """test_get_insterfaces_details."""
251
        interfaces_ids = ["1", "2", "3"]
252
        self.topo.get_interfaces_details(interfaces_ids)
253
        self.topo.db.interface_details.aggregate.assert_called_with(
254
            [{"$match": {"_id": {"$in": interfaces_ids}}}]
255
        )
256
257
    def test_bulk_upsert_interface_details(self) -> None:
258
        """test_bulk_upsert_interface_details."""
259
        ids_details = [
260
            ("1", {"_id": "1", "available_vlans": [1]}),
261
            ("2", {"_id": "2", "available_vlans": [2]}),
262
        ]
263
        self.topo.bulk_upsert_interface_details(ids_details)
264
265
        self.topo.db_client.start_session.assert_called()
266
        arg1 = self.topo.db.interface_details.bulk_write.call_args[0]
267
        assert len(arg1[0]) == len(ids_details)
268
        for item in arg1[0]:
269
            assert isinstance(item, UpdateOne)
270
271
    def test_upsert_switch(self) -> None:
272
        """test_upsert_switch."""
273
        switch_dict = {"enabled": True, "active": True, "_id": self.dpid}
274
        self.topo.upsert_switch(self.dpid, switch_dict)
275
276
        self.topo.db.switches.find_one_and_update.assert_called()
277
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
278
        assert arg1 == {"_id": self.dpid}
279
        for key, value in switch_dict.items():
280
            assert arg2["$set"][key] == value
281
282
    def test_upsert_link(self) -> None:
283
        """test_upsert_link."""
284
        link_dict = {
285
            "_id": self.link_id,
286
            "enabled": True,
287
            "active": True,
288
            "endpoint_a": {"id": "00:00:00:00:00:00:00:01:1"},
289
            "endpoint_b": {"id": "00:00:00:00:00:00:00:02:01"},
290
        }
291
        self.topo.upsert_link(self.link_id, link_dict)
292
        assert self.topo.db.switches.find_one_and_update.call_count == 2
293
        assert self.topo.db.links.find_one_and_update.call_count == 1
294