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