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