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