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

build.tests.unit.test_topo_controller   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 192
dl 0
loc 278
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_link_metadata() 0 10 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_link() 0 8 1
A TestTopoController.test_enable_interface() 0 8 1
A TestTopoController.test_get_interfaces() 0 10 1
A TestTopoController.test_bulk_upsert_interface_details() 0 13 2
A TestTopoController.test_delete_switch_metadata() 0 9 1
A TestTopoController.test_deactivate_interface() 0 8 1
A TestTopoController.test_delete_interface_metadata_key() 0 9 1
A TestTopoController.test_disable_interface() 0 8 1
A TestTopoController.test_add_interface_metadata() 0 9 1
A TestTopoController.test_disable_switch() 0 8 1
A TestTopoController.test_activate_interface() 0 8 1
A TestTopoController.test_upsert_switch() 0 10 2
A TestTopoController.test_get_switches() 0 8 1
A TestTopoController.test_disable_interface_lldp() 0 8 1
A TestTopoController.test_get_interfaces_details() 0 6 1
A TestTopoController.test_enable_interface_lldp() 0 8 1
A TestTopoController.test_disable_link() 0 8 1
A TestTopoController.test_delete_link_metadata_key() 0 9 1
A TestTopoController.test_boostrap_indexes() 0 12 1
A TestTopoController.test_deactivate_switch() 0 8 1
A TestTopoController.test_upsert_link() 0 12 1
A TestTopoController.setUp() 0 6 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_enable_interface_lldp(self) -> None:
157
        """test_enable_interface_lldp."""
158
        self.topo.enable_interface_lldp(self.interface_id)
159
160
        self.topo.db.switches.find_one_and_update.assert_called()
161
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
162
        assert arg1 == {"interfaces.id": self.interface_id}
163
        assert arg2["$set"]["interfaces.$.lldp"]
164
165
    def test_disable_interface_lldp(self) -> None:
166
        """test_disable_interface_lldp."""
167
        self.topo.disable_interface_lldp(self.interface_id)
168
169
        self.topo.db.switches.find_one_and_update.assert_called()
170
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
171
        assert arg1 == {"interfaces.id": self.interface_id}
172
        assert not arg2["$set"]["interfaces.$.lldp"]
173
174
    def test_add_interface_metadata(self) -> None:
175
        """test_add_interface_metadata."""
176
        metadata = {"some": "value"}
177
        self.topo.add_interface_metadata(self.interface_id, metadata)
178
179
        self.topo.db.switches.find_one_and_update.assert_called()
180
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
181
        assert arg1 == {"interfaces.id": self.interface_id}
182
        assert arg2["$set"]["interfaces.$.metadata.some"] == "value"
183
184
    def test_delete_interface_metadata_key(self) -> None:
185
        """test_delete_interface_metadata."""
186
        key = "some"
187
        self.topo.delete_interface_metadata_key(self.interface_id, key)
188
189
        self.topo.db.switches.find_one_and_update.assert_called()
190
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
191
        assert arg1 == {"interfaces.id": self.interface_id}
192
        assert arg2["$unset"][f"interfaces.$.metadata.{key}"] == ""
193
194
    def test_enable_link(self) -> None:
195
        """test_enable_link."""
196
        self.topo.enable_link(self.link_id)
197
198
        self.topo.db.links.find_one_and_update.assert_called()
199
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
200
        assert arg1 == {"_id": self.link_id}
201
        assert arg2["$set"]["enabled"]
202
203
    def test_disable_link(self) -> None:
204
        """test_disable_link."""
205
        self.topo.disable_link(self.link_id)
206
207
        self.topo.db.links.find_one_and_update.assert_called()
208
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
209
        assert arg1 == {"_id": self.link_id}
210
        assert not arg2["$set"]["enabled"]
211
212
    def test_add_link_metadata(self) -> None:
213
        """test_add_link_metadata."""
214
        key = "some_key"
215
        value = "some_value"
216
        self.topo.add_link_metadata(self.link_id, {key: value})
217
218
        self.topo.db.links.find_one_and_update.assert_called()
219
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
220
        assert arg1 == {"_id": self.link_id}
221
        assert arg2["$set"][f"metadata.{key}"] == value
222
223
    def test_delete_link_metadata_key(self) -> None:
224
        """test_delete_link_metadata_key."""
225
        key = "some_key"
226
        self.topo.delete_link_metadata_key(self.link_id, key)
227
228
        self.topo.db.links.find_one_and_update.assert_called()
229
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
230
        assert arg1 == {"_id": self.link_id}
231
        assert arg2["$unset"][f"metadata.{key}"] == ""
232
233
    def test_get_interfaces_details(self) -> None:
234
        """test_get_insterfaces_details."""
235
        interfaces_ids = ["1", "2", "3"]
236
        self.topo.get_interfaces_details(interfaces_ids)
237
        self.topo.db.interface_details.aggregate.assert_called_with(
238
            [{"$match": {"_id": {"$in": interfaces_ids}}}]
239
        )
240
241
    def test_bulk_upsert_interface_details(self) -> None:
242
        """test_bulk_upsert_interface_details."""
243
        ids_details = [
244
            ("1", {"_id": "1", "available_vlans": [1]}),
245
            ("2", {"_id": "2", "available_vlans": [2]}),
246
        ]
247
        self.topo.bulk_upsert_interface_details(ids_details)
248
249
        self.topo.db_client.start_session.assert_called()
250
        arg1 = self.topo.db.interface_details.bulk_write.call_args[0]
251
        assert len(arg1[0]) == len(ids_details)
252
        for item in arg1[0]:
253
            assert isinstance(item, UpdateOne)
254
255
    def test_upsert_switch(self) -> None:
256
        """test_upsert_switch."""
257
        switch_dict = {"enabled": True, "active": True, "_id": self.dpid}
258
        self.topo.upsert_switch(self.dpid, switch_dict)
259
260
        self.topo.db.switches.find_one_and_update.assert_called()
261
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
262
        assert arg1 == {"_id": self.dpid}
263
        for key, value in switch_dict.items():
264
            assert arg2["$set"][key] == value
265
266
    def test_upsert_link(self) -> None:
267
        """test_upsert_link."""
268
        link_dict = {
269
            "_id": self.link_id,
270
            "enabled": True,
271
            "active": True,
272
            "endpoint_a": {"id": "00:00:00:00:00:00:00:01:1"},
273
            "endpoint_b": {"id": "00:00:00:00:00:00:00:02:01"},
274
        }
275
        self.topo.upsert_link(self.link_id, link_dict)
276
        assert self.topo.db.switches.find_one_and_update.call_count == 2
277
        assert self.topo.db.links.find_one_and_update.call_count == 1
278