1
|
|
|
"""Test INTManager""" |
2
|
|
|
|
3
|
1 |
|
import pytest |
4
|
|
|
|
5
|
1 |
|
from unittest.mock import AsyncMock, MagicMock |
6
|
1 |
|
from napps.kytos.telemetry_int.exceptions import ProxyPortSameSourceIntraEVC |
7
|
1 |
|
from napps.kytos.telemetry_int.exceptions import ProxyPortShared |
8
|
1 |
|
from napps.kytos.telemetry_int.managers.int import INTManager |
9
|
1 |
|
from napps.kytos.telemetry_int import exceptions |
10
|
1 |
|
from kytos.core.common import EntityStatus |
11
|
|
|
|
12
|
1 |
|
from kytos.lib.helpers import ( |
13
|
|
|
get_interface_mock, |
14
|
|
|
get_controller_mock, |
15
|
|
|
get_switch_mock, |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
class TestINTManager: |
20
|
|
|
"""TestINTManager.""" |
21
|
|
|
|
22
|
1 |
|
def test_get_proxy_port_or_raise(self) -> None: |
23
|
|
|
"""Test proxy_port_or_raise.""" |
24
|
1 |
|
dpid_a = "00:00:00:00:00:00:00:01" |
25
|
1 |
|
mock_switch_a = get_switch_mock(dpid_a, 0x04) |
26
|
1 |
|
mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a) |
27
|
1 |
|
mock_interface_a.metadata = {} |
28
|
1 |
|
intf_id = f"{dpid_a}:1" |
29
|
1 |
|
controller = get_controller_mock() |
30
|
1 |
|
evc_id = "3766c105686749" |
31
|
1 |
|
int_manager = INTManager(controller) |
32
|
|
|
|
33
|
|
|
# Initially the mocked interface and switch hasn't been associated in the ctrllr |
34
|
1 |
|
with pytest.raises(exceptions.ProxyPortNotFound) as exc: |
35
|
1 |
|
int_manager.get_proxy_port_or_raise(intf_id, evc_id) |
36
|
1 |
|
assert f"interface {intf_id} not found" in str(exc) |
37
|
|
|
|
38
|
|
|
# Now, proxy_port still hasn't been set yet |
39
|
1 |
|
controller.get_interface_by_id = lambda x: mock_interface_a |
40
|
1 |
|
with pytest.raises(exceptions.ProxyPortNotFound) as exc: |
41
|
1 |
|
int_manager.get_proxy_port_or_raise(intf_id, evc_id) |
42
|
1 |
|
assert f"proxy_port metadata not found in {intf_id}" in str(exc) |
43
|
|
|
|
44
|
|
|
# Now, destination interface hasn't been mocked yet |
45
|
1 |
|
mock_interface_a.metadata = {"proxy_port": 5} |
46
|
1 |
|
with pytest.raises(exceptions.ProxyPortDestNotFound) as exc: |
47
|
1 |
|
int_manager.get_proxy_port_or_raise(intf_id, evc_id) |
48
|
1 |
|
assert "isn't looped" in str(exc) |
49
|
|
|
|
50
|
1 |
|
mock_interface_b = get_interface_mock("s1-eth5", 5, mock_switch_a) |
51
|
1 |
|
mock_interface_b.metadata = {"looped": {"port_numbers": [5, 6]}} |
52
|
1 |
|
mock_interface_a.switch.get_interface_by_port_no = lambda x: mock_interface_b |
53
|
|
|
# Now all dependencies have been mocked and it should get the ProxyPort |
54
|
1 |
|
pp = int_manager.get_proxy_port_or_raise(intf_id, evc_id) |
55
|
1 |
|
assert pp.source == mock_interface_b |
56
|
|
|
|
57
|
1 |
|
def test_load_uni_src_proxy_port(self) -> None: |
58
|
|
|
"""Test test_load_uni_src_proxy_port.""" |
59
|
1 |
|
dpid_a = "00:00:00:00:00:00:00:01" |
60
|
1 |
|
mock_switch_a = get_switch_mock(dpid_a, 0x04) |
61
|
1 |
|
mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a) |
62
|
1 |
|
mock_interface_a.metadata = {"proxy_port": 3} |
63
|
1 |
|
mock_interface_z = get_interface_mock("s1-eth2", 2, mock_switch_a) |
64
|
1 |
|
mock_interface_z.metadata = {"proxy_port": 5} |
65
|
1 |
|
intf_id_a = f"{dpid_a}:1" |
66
|
1 |
|
intf_id_z = f"{dpid_a}:2" |
67
|
1 |
|
intf_id_a_1 = f"{dpid_a}:3" |
68
|
1 |
|
intf_id_z_1 = f"{dpid_a}:5" |
69
|
|
|
|
70
|
1 |
|
mock_interface_a_1 = get_interface_mock("s1-eth3", 3, mock_switch_a) |
71
|
1 |
|
mock_interface_a_1.metadata = {"looped": {"port_numbers": [3, 4]}} |
72
|
1 |
|
mock_interface_a_2 = get_interface_mock("s1-eth4", 4, mock_switch_a) |
73
|
1 |
|
mock_interface_z_1 = get_interface_mock("s1-eth5", 5, mock_switch_a) |
74
|
1 |
|
mock_interface_z_1.metadata = {"looped": {"port_numbers": [5, 6]}} |
75
|
1 |
|
mock_interface_z_2 = get_interface_mock("s1-eth6", 6, mock_switch_a) |
76
|
|
|
|
77
|
1 |
|
def get_interface_by_port_no(port_no): |
78
|
1 |
|
data = { |
79
|
|
|
1: mock_interface_a, |
80
|
|
|
2: mock_interface_z, |
81
|
|
|
3: mock_interface_a_1, |
82
|
|
|
4: mock_interface_a_2, |
83
|
|
|
5: mock_interface_z_1, |
84
|
|
|
6: mock_interface_z_2, |
85
|
|
|
} |
86
|
1 |
|
return data[port_no] |
87
|
|
|
|
88
|
1 |
|
def get_interface_by_id(intf_id): |
89
|
1 |
|
data = { |
90
|
|
|
intf_id_a: mock_interface_a, |
91
|
|
|
intf_id_z: mock_interface_z, |
92
|
|
|
} |
93
|
1 |
|
return data[intf_id] |
94
|
|
|
|
95
|
1 |
|
controller = get_controller_mock() |
96
|
1 |
|
mock_switch_a.get_interface_by_port_no = get_interface_by_port_no |
97
|
1 |
|
controller.get_interface_by_id = get_interface_by_id |
98
|
|
|
|
99
|
1 |
|
evcs = { |
100
|
|
|
"3766c105686749": { |
101
|
|
|
"metadata": {"telemetry": {"enabled": True}}, |
102
|
|
|
"uni_a": {"interface_id": intf_id_a}, |
103
|
|
|
"uni_z": {"interface_id": intf_id_z}, |
104
|
|
|
}, |
105
|
|
|
"3766c105686748": { |
106
|
|
|
"metadata": {"telemetry": {"enabled": True}}, |
107
|
|
|
"uni_a": {"interface_id": intf_id_a}, |
108
|
|
|
"uni_z": {"interface_id": intf_id_z}, |
109
|
|
|
}, |
110
|
|
|
"3766c105686747": { |
111
|
|
|
"metadata": {"telemetry": {"enabled": False}}, |
112
|
|
|
"uni_a": {"interface_id": intf_id_a}, |
113
|
|
|
"uni_z": {"interface_id": intf_id_z}, |
114
|
|
|
}, |
115
|
|
|
} |
116
|
1 |
|
int_manager = INTManager(controller) |
117
|
1 |
|
int_manager.load_uni_src_proxy_ports(evcs) |
118
|
1 |
|
assert len(int_manager.unis_src) == 2 |
119
|
1 |
|
assert int_manager.unis_src[intf_id_a] == intf_id_a_1 |
120
|
1 |
|
assert int_manager.unis_src[intf_id_z] == intf_id_z_1 |
121
|
|
|
|
122
|
1 |
|
assert len(int_manager.srcs_pp) == 2 |
123
|
1 |
|
assert int_manager.srcs_pp[intf_id_a_1].source == mock_interface_a_1 |
124
|
1 |
|
assert int_manager.srcs_pp[intf_id_a_1].destination == mock_interface_a_2 |
125
|
1 |
|
assert int_manager.srcs_pp[intf_id_z_1].source == mock_interface_z_1 |
126
|
1 |
|
assert int_manager.srcs_pp[intf_id_z_1].destination == mock_interface_z_2 |
127
|
|
|
|
128
|
1 |
|
assert int_manager.srcs_pp[intf_id_a_1].evc_ids == { |
129
|
|
|
"3766c105686749", |
130
|
|
|
"3766c105686748", |
131
|
|
|
} |
132
|
1 |
|
assert int_manager.srcs_pp[intf_id_z_1].evc_ids == { |
133
|
|
|
"3766c105686749", |
134
|
|
|
"3766c105686748", |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
async def test_handle_pp_link_down(self, monkeypatch): |
138
|
|
|
"""Test test_handle_pp_link_down.""" |
139
|
1 |
|
int_manager = INTManager(MagicMock()) |
140
|
1 |
|
api_mock, link_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock() |
141
|
1 |
|
link_mock.endpoint_a.id = "some_intf_id" |
142
|
1 |
|
evc_id = "3766c105686748" |
143
|
1 |
|
int_manager.srcs_pp[link_mock.endpoint_a.id] = pp_mock |
144
|
1 |
|
pp_mock.evc_ids = {evc_id} |
145
|
|
|
|
146
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
147
|
1 |
|
api_mock.get_evcs.return_value = {evc_id: {}} |
148
|
1 |
|
int_manager.remove_int_flows = AsyncMock() |
149
|
|
|
|
150
|
1 |
|
await int_manager.handle_pp_link_down(link_mock) |
151
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
152
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
153
|
1 |
|
assert api_mock.get_evcs.call_args[1] == { |
154
|
|
|
"metadata.telemetry.enabled": "true", |
155
|
|
|
"metadata.telemetry.status": "UP", |
156
|
|
|
} |
157
|
1 |
|
assert int_manager.remove_int_flows.call_count == 1 |
158
|
1 |
|
args = int_manager.remove_int_flows.call_args[0] |
159
|
1 |
|
assert evc_id in args[0] |
160
|
1 |
|
assert "telemetry" in args[1] |
161
|
1 |
|
telemetry = args[1]["telemetry"] |
162
|
1 |
|
assert telemetry["enabled"] |
163
|
1 |
|
assert telemetry["status"] == "DOWN" |
164
|
1 |
|
assert telemetry["status_reason"] == ["proxy_port_down"] |
165
|
1 |
|
assert "status_updated_at" in telemetry |
166
|
|
|
|
167
|
1 |
|
async def test_handle_pp_link_up(self, monkeypatch): |
168
|
|
|
"""Test handle_pp_link_up.""" |
169
|
1 |
|
int_manager = INTManager(MagicMock()) |
170
|
1 |
|
api_mock, link_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock() |
171
|
1 |
|
link_mock.endpoint_a.id = "3" |
172
|
1 |
|
pp_mock.status = EntityStatus.UP |
173
|
1 |
|
link_mock.status = EntityStatus.UP |
174
|
1 |
|
link_mock.status_reason = [] |
175
|
1 |
|
evc_id = "3766c105686748" |
176
|
1 |
|
uni_a_id, uni_z_id = "1", "2" |
177
|
1 |
|
src_a_id, src_z_id = "3", "5" |
178
|
1 |
|
int_manager.srcs_pp[src_a_id] = pp_mock |
179
|
1 |
|
int_manager.srcs_pp[src_z_id] = pp_mock |
180
|
1 |
|
int_manager.unis_src[uni_a_id] = src_a_id |
181
|
1 |
|
int_manager.unis_src[uni_z_id] = src_z_id |
182
|
1 |
|
pp_mock.evc_ids = {evc_id} |
183
|
|
|
|
184
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
185
|
1 |
|
api_mock.get_evcs.return_value = { |
186
|
|
|
evc_id: { |
187
|
|
|
"active": True, |
188
|
|
|
"archived": False, |
189
|
|
|
"uni_a": {"interface_id": uni_a_id}, |
190
|
|
|
"uni_z": {"interface_id": uni_z_id}, |
191
|
|
|
} |
192
|
|
|
} |
193
|
1 |
|
int_manager.install_int_flows = AsyncMock() |
194
|
1 |
|
int_manager._validate_map_enable_evcs = MagicMock() |
195
|
|
|
|
196
|
1 |
|
await int_manager.handle_pp_link_up(link_mock) |
197
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
198
|
1 |
|
assert api_mock.get_evcs.call_args[1] == { |
199
|
|
|
"metadata.telemetry.enabled": "true", |
200
|
|
|
"metadata.telemetry.status": "DOWN", |
201
|
|
|
} |
202
|
1 |
|
assert int_manager.install_int_flows.call_count == 1 |
203
|
1 |
|
args = int_manager.install_int_flows.call_args[0] |
204
|
1 |
|
assert "telemetry" in args[1] |
205
|
1 |
|
telemetry_dict = args[1]["telemetry"] |
206
|
1 |
|
expected_keys = ["enabled", "status", "status_reason", "status_updated_at"] |
207
|
1 |
|
assert sorted(list(telemetry_dict.keys())) == sorted(expected_keys) |
208
|
1 |
|
assert telemetry_dict["enabled"] |
209
|
1 |
|
assert telemetry_dict["status"] == "UP" |
210
|
1 |
|
assert not telemetry_dict["status_reason"] |
211
|
|
|
|
212
|
1 |
|
async def test_handle_pp_metadata_removed(self, monkeypatch): |
213
|
|
|
"""Test handle_pp_metadata_removed.""" |
214
|
1 |
|
int_manager = INTManager(MagicMock()) |
215
|
1 |
|
api_mock, intf_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock() |
216
|
1 |
|
intf_mock.id = "some_intf_id" |
217
|
1 |
|
source_id = "some_source_id" |
218
|
1 |
|
evc_id = "3766c105686748" |
219
|
1 |
|
int_manager.unis_src[intf_mock.id] = source_id |
220
|
1 |
|
int_manager.srcs_pp[source_id] = pp_mock |
221
|
1 |
|
pp_mock.evc_ids = {evc_id} |
222
|
|
|
|
223
|
1 |
|
assert "proxy_port" not in intf_mock.metadata |
224
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
225
|
1 |
|
api_mock.get_evcs.return_value = {evc_id: {}} |
226
|
1 |
|
int_manager.disable_int = AsyncMock() |
227
|
|
|
|
228
|
1 |
|
await int_manager.handle_pp_metadata_removed(intf_mock) |
229
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
230
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
231
|
1 |
|
assert api_mock.get_evcs.call_args[1] == { |
232
|
|
|
"metadata.telemetry.enabled": "true", |
233
|
|
|
"metadata.telemetry.status": "UP", |
234
|
|
|
} |
235
|
1 |
|
assert int_manager.disable_int.call_count == 1 |
236
|
1 |
|
args = int_manager.disable_int.call_args[0] |
237
|
1 |
|
assert evc_id in args[0] |
238
|
|
|
|
239
|
1 |
|
async def test_handle_pp_metadata_added(self, monkeypatch): |
240
|
|
|
"""Test handle_pp_metadata_added.""" |
241
|
1 |
|
int_manager = INTManager(MagicMock()) |
242
|
1 |
|
api_mock, intf_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock() |
243
|
1 |
|
intf_mock.id = "some_intf_id" |
244
|
1 |
|
source_id, source_port = "some_source_id", 2 |
245
|
1 |
|
intf_mock.metadata = {"proxy_port": source_port} |
246
|
1 |
|
evc_id = "3766c105686748" |
247
|
1 |
|
int_manager.unis_src[intf_mock.id] = source_id |
248
|
1 |
|
int_manager.srcs_pp[source_id] = pp_mock |
249
|
1 |
|
pp_mock.evc_ids = {evc_id} |
250
|
|
|
|
251
|
1 |
|
assert "proxy_port" in intf_mock.metadata |
252
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
253
|
1 |
|
api_mock.get_evcs.return_value = {evc_id: {}} |
254
|
1 |
|
int_manager.disable_int = AsyncMock() |
255
|
1 |
|
int_manager.enable_int = AsyncMock() |
256
|
|
|
|
257
|
1 |
|
await int_manager.handle_pp_metadata_added(intf_mock) |
258
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
259
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
260
|
1 |
|
assert api_mock.get_evcs.call_args[1] == {"metadata.telemetry.enabled": "true"} |
261
|
1 |
|
assert int_manager.disable_int.call_count == 1 |
262
|
1 |
|
assert int_manager.enable_int.call_count == 1 |
263
|
|
|
|
264
|
1 |
|
async def test_handle_pp_metadata_added_no_change(self, monkeypatch): |
265
|
|
|
"""Test handle_pp_metadata_added no change.""" |
266
|
1 |
|
int_manager = INTManager(MagicMock()) |
267
|
1 |
|
api_mock, intf_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock() |
268
|
1 |
|
intf_mock.id = "some_intf_id" |
269
|
1 |
|
source_id, source_port = "some_source_id", 2 |
270
|
1 |
|
source_intf = MagicMock() |
271
|
1 |
|
intf_mock.metadata = {"proxy_port": source_port} |
272
|
1 |
|
evc_id = "3766c105686748" |
273
|
1 |
|
int_manager.unis_src[intf_mock.id] = source_id |
274
|
1 |
|
int_manager.srcs_pp[source_id] = pp_mock |
275
|
1 |
|
pp_mock.evc_ids = {evc_id} |
276
|
|
|
|
277
|
|
|
# Simulating that the current and new proxy_port source are the same |
278
|
1 |
|
pp_mock.source = source_intf |
279
|
1 |
|
intf_mock.switch.get_interface_by_port_no.return_value = source_intf |
280
|
|
|
|
281
|
1 |
|
assert "proxy_port" in intf_mock.metadata |
282
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
283
|
1 |
|
api_mock.get_evcs.return_value = {evc_id: {}} |
284
|
1 |
|
int_manager.disable_int = AsyncMock() |
285
|
1 |
|
int_manager.enable_int = AsyncMock() |
286
|
|
|
|
287
|
1 |
|
await int_manager.handle_pp_metadata_added(intf_mock) |
288
|
1 |
|
assert not api_mock.get_evcs.call_count |
289
|
1 |
|
assert not int_manager.disable_int.call_count |
290
|
1 |
|
assert not int_manager.enable_int.call_count |
291
|
|
|
|
292
|
1 |
|
async def test_handle_pp_metadata_added_no_affected(self, monkeypatch): |
293
|
|
|
"""Test handle_pp_metadata_added no affected evcs.""" |
294
|
1 |
|
int_manager = INTManager(MagicMock()) |
295
|
1 |
|
api_mock, intf_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock() |
296
|
1 |
|
intf_mock.id = "some_intf_id" |
297
|
1 |
|
source_id, source_port = "some_source_id", 2 |
298
|
1 |
|
intf_mock.metadata = {"proxy_port": source_port} |
299
|
1 |
|
evc_id = "3766c105686748" |
300
|
1 |
|
int_manager.unis_src[intf_mock.id] = source_id |
301
|
1 |
|
int_manager.srcs_pp[source_id] = pp_mock |
302
|
1 |
|
pp_mock.evc_ids = {evc_id} |
303
|
|
|
|
304
|
1 |
|
assert "proxy_port" in intf_mock.metadata |
305
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
306
|
|
|
|
307
|
|
|
# Simulating returning no EVCs that were enabled and UP |
308
|
1 |
|
api_mock.get_evcs.return_value = {} |
309
|
1 |
|
int_manager.disable_int = AsyncMock() |
310
|
1 |
|
int_manager.enable_int = AsyncMock() |
311
|
|
|
|
312
|
1 |
|
await int_manager.handle_pp_metadata_added(intf_mock) |
313
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
314
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
315
|
1 |
|
assert api_mock.get_evcs.call_args[1] == { |
316
|
|
|
"metadata.telemetry.enabled": "true", |
317
|
|
|
} |
318
|
1 |
|
assert not int_manager.disable_int.call_count |
319
|
1 |
|
assert not int_manager.enable_int.call_count |
320
|
|
|
|
321
|
1 |
|
async def test_handle_pp_metadata_added_exc_port_shared(self, monkeypatch): |
322
|
|
|
"""Test handle_pp_metadata_added exception port shared.""" |
323
|
1 |
|
log_mock = MagicMock() |
324
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.log", log_mock) |
325
|
1 |
|
int_manager = INTManager(MagicMock()) |
326
|
1 |
|
api_mock, intf_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock() |
327
|
1 |
|
intf_mock.id = "some_intf_id" |
328
|
1 |
|
source_id, source_port = "some_source_id", 2 |
329
|
1 |
|
intf_mock.metadata = {"proxy_port": source_port} |
330
|
1 |
|
evc_id = "3766c105686748" |
331
|
1 |
|
int_manager.unis_src[intf_mock.id] = source_id |
332
|
1 |
|
int_manager.srcs_pp[source_id] = pp_mock |
333
|
1 |
|
pp_mock.evc_ids = {evc_id} |
334
|
|
|
|
335
|
1 |
|
assert "proxy_port" in intf_mock.metadata |
336
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
337
|
1 |
|
api_mock.get_evcs.return_value = {evc_id: {}} |
338
|
1 |
|
int_manager.disable_int = AsyncMock() |
339
|
1 |
|
int_manager.enable_int = AsyncMock() |
340
|
1 |
|
int_manager.enable_int.side_effect = ProxyPortShared(evc_id, "shared") |
341
|
|
|
|
342
|
1 |
|
await int_manager.handle_pp_metadata_added(intf_mock) |
343
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
344
|
1 |
|
assert api_mock.get_evcs.call_count == 1 |
345
|
1 |
|
assert api_mock.get_evcs.call_args[1] == {"metadata.telemetry.enabled": "true"} |
346
|
1 |
|
assert int_manager.disable_int.call_count == 1 |
347
|
1 |
|
assert int_manager.enable_int.call_count == 1 |
348
|
|
|
|
349
|
1 |
|
assert api_mock.add_evcs_metadata.call_count == 1 |
350
|
1 |
|
assert log_mock.error.call_count == 1 |
351
|
|
|
|
352
|
1 |
|
async def test_disable_int_metadata(self, monkeypatch) -> None: |
353
|
|
|
"""Test disable INT metadata args.""" |
354
|
1 |
|
controller = MagicMock() |
355
|
1 |
|
api_mock = AsyncMock() |
356
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
357
|
|
|
|
358
|
1 |
|
int_manager = INTManager(controller) |
359
|
1 |
|
int_manager._remove_int_flows_by_cookies = AsyncMock() |
360
|
1 |
|
await int_manager.disable_int({}, False) |
361
|
|
|
|
362
|
1 |
|
assert api_mock.add_evcs_metadata.call_count == 1 |
363
|
1 |
|
args = api_mock.add_evcs_metadata.call_args[0] |
364
|
1 |
|
assert args[0] == {} |
365
|
1 |
|
assert "telemetry" in args[1] |
366
|
1 |
|
telemetry_dict = args[1]["telemetry"] |
367
|
1 |
|
expected_keys = ["enabled", "status", "status_reason", "status_updated_at"] |
368
|
1 |
|
assert sorted(list(telemetry_dict.keys())) == sorted(expected_keys) |
369
|
|
|
|
370
|
1 |
|
assert not telemetry_dict["enabled"] |
371
|
1 |
|
assert telemetry_dict["status"] == "DOWN" |
372
|
1 |
|
assert telemetry_dict["status_reason"] == ["disabled"] |
373
|
|
|
|
374
|
1 |
|
assert args[2] is False |
375
|
|
|
|
376
|
1 |
|
async def test_enable_int_metadata(self, monkeypatch) -> None: |
377
|
|
|
"""Test enable INT metadata args.""" |
378
|
1 |
|
controller = MagicMock() |
379
|
1 |
|
api_mock = AsyncMock() |
380
|
1 |
|
stored_flows_mock = AsyncMock() |
381
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
382
|
1 |
|
monkeypatch.setattr( |
383
|
|
|
"napps.kytos.telemetry_int.utils.get_found_stored_flows", stored_flows_mock |
384
|
|
|
) |
385
|
|
|
|
386
|
1 |
|
int_manager = INTManager(controller) |
387
|
1 |
|
int_manager.remove_int_flows = AsyncMock() |
388
|
1 |
|
evcs = { |
389
|
|
|
"3766c105686749": { |
390
|
|
|
"active": True, |
391
|
|
|
"uni_a": MagicMock(), |
392
|
|
|
"uni_z": MagicMock(), |
393
|
|
|
} |
394
|
|
|
} |
395
|
1 |
|
int_manager._validate_map_enable_evcs = MagicMock() |
396
|
1 |
|
int_manager._validate_map_enable_evcs.return_value = evcs |
397
|
1 |
|
int_manager.flow_builder.build_int_flows = MagicMock() |
398
|
1 |
|
int_manager.flow_builder.build_int_flows.return_value = { |
399
|
|
|
0xAA3766C105686749: [MagicMock()] |
400
|
|
|
} |
401
|
1 |
|
int_manager._add_pps_evc_ids = MagicMock() |
402
|
1 |
|
int_manager._send_flows = AsyncMock() |
403
|
|
|
|
404
|
1 |
|
await int_manager.enable_int(evcs, False) |
405
|
|
|
|
406
|
1 |
|
assert stored_flows_mock.call_count == 1 |
407
|
1 |
|
assert api_mock.add_evcs_metadata.call_count == 3 |
408
|
1 |
|
args = api_mock.add_evcs_metadata.call_args[0] |
409
|
1 |
|
assert "telemetry" in args[1] |
410
|
1 |
|
telemetry_dict = args[1]["telemetry"] |
411
|
1 |
|
expected_keys = ["enabled", "status", "status_reason", "status_updated_at"] |
412
|
1 |
|
assert sorted(list(telemetry_dict.keys())) == sorted(expected_keys) |
413
|
1 |
|
assert int_manager._send_flows.call_count == 1 |
414
|
|
|
|
415
|
1 |
|
assert telemetry_dict["enabled"] is True |
416
|
1 |
|
assert telemetry_dict["status"] == "UP" |
417
|
1 |
|
assert telemetry_dict["status_reason"] == [] |
418
|
|
|
|
419
|
1 |
|
async def test_redeploy_int(self, monkeypatch) -> None: |
420
|
|
|
"""Test redeploy int.""" |
421
|
1 |
|
controller = MagicMock() |
422
|
1 |
|
api_mock = AsyncMock() |
423
|
1 |
|
stored_flows_mock = AsyncMock() |
424
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock) |
425
|
1 |
|
monkeypatch.setattr( |
426
|
|
|
"napps.kytos.telemetry_int.utils.get_found_stored_flows", stored_flows_mock |
427
|
|
|
) |
428
|
|
|
|
429
|
1 |
|
int_manager = INTManager(controller) |
430
|
1 |
|
int_manager._remove_int_flows_by_cookies = AsyncMock() |
431
|
1 |
|
int_manager._install_int_flows = AsyncMock() |
432
|
|
|
|
433
|
1 |
|
dpid_a = "00:00:00:00:00:00:00:01" |
434
|
1 |
|
intf_id_a = f"{dpid_a}:1" |
435
|
1 |
|
intf_id_z = f"{dpid_a}:2" |
436
|
1 |
|
evc_id = "3766c105686749" |
437
|
1 |
|
evcs = { |
438
|
|
|
evc_id: { |
439
|
|
|
"metadata": {"telemetry": {"enabled": True}}, |
440
|
|
|
"uni_a": {"interface_id": intf_id_a}, |
441
|
|
|
"uni_z": {"interface_id": intf_id_z}, |
442
|
|
|
} |
443
|
|
|
} |
444
|
1 |
|
int_manager._validate_map_enable_evcs = MagicMock() |
445
|
1 |
|
await int_manager.redeploy_int(evcs) |
446
|
|
|
|
447
|
1 |
|
assert stored_flows_mock.call_count == 1 |
448
|
1 |
|
assert int_manager._remove_int_flows_by_cookies.call_count == 1 |
449
|
1 |
|
assert api_mock.get_stored_flows.call_count == 1 |
450
|
1 |
|
assert int_manager._install_int_flows.call_count == 1 |
451
|
|
|
|
452
|
1 |
|
def test_validate_intra_evc_different_proxy_ports(self) -> None: |
453
|
|
|
"""Test _validate_intra_evc_different_proxy_ports.""" |
454
|
1 |
|
pp_a, pp_z, controller = MagicMock(), MagicMock(), MagicMock() |
455
|
1 |
|
evc = { |
456
|
|
|
"id": "some_id", |
457
|
|
|
"uni_a": {"proxy_port": pp_a, "interface_id": "00:00:00:00:00:00:00:01:1"}, |
458
|
|
|
"uni_z": {"proxy_port": pp_z, "interface_id": "00:00:00:00:00:00:00:01:2"}, |
459
|
|
|
} |
460
|
|
|
|
461
|
1 |
|
int_manager = INTManager(controller) |
462
|
1 |
|
int_manager._validate_intra_evc_different_proxy_ports(evc) |
463
|
|
|
|
464
|
1 |
|
source = MagicMock() |
465
|
1 |
|
pp_a.source, pp_z.source = source, source |
466
|
1 |
|
with pytest.raises(ProxyPortSameSourceIntraEVC): |
467
|
1 |
|
int_manager._validate_intra_evc_different_proxy_ports(evc) |
468
|
|
|
|
469
|
1 |
|
def test_validate_dedicated_proxy_port_evcs(self) -> None: |
470
|
|
|
"""Test _validate_intra_evc_different_proxy_ports.""" |
471
|
1 |
|
pp_a, pp_z, controller = MagicMock(), MagicMock(), MagicMock() |
472
|
1 |
|
evc = { |
473
|
|
|
"id": "some_id", |
474
|
|
|
"uni_a": {"proxy_port": pp_a, "interface_id": "00:00:00:00:00:00:00:01:1"}, |
475
|
|
|
"uni_z": {"proxy_port": pp_z, "interface_id": "00:00:00:00:00:00:00:01:2"}, |
476
|
|
|
} |
477
|
|
|
|
478
|
1 |
|
int_manager = INTManager(controller) |
479
|
1 |
|
int_manager._validate_dedicated_proxy_port_evcs({evc["id"]: evc}) |
480
|
|
|
|
481
|
1 |
|
source = MagicMock() |
482
|
1 |
|
pp_a.source, pp_z.source = source, source |
483
|
1 |
|
with pytest.raises(ProxyPortShared): |
484
|
1 |
|
int_manager._validate_dedicated_proxy_port_evcs({evc["id"]: evc}) |
485
|
|
|
|
486
|
1 |
|
def test_validate_dedicated_proxy_port_evcs_existing(self) -> None: |
487
|
|
|
"""Test _validate_intra_evc_different_proxy_ports existing.""" |
488
|
1 |
|
pp_a, pp_z, controller = MagicMock(), MagicMock(), MagicMock() |
489
|
1 |
|
evc = { |
490
|
|
|
"id": "some_id", |
491
|
|
|
"uni_a": {"proxy_port": pp_a, "interface_id": "00:00:00:00:00:00:00:01:1"}, |
492
|
|
|
"uni_z": {"proxy_port": pp_z, "interface_id": "00:00:00:00:00:00:00:01:2"}, |
493
|
|
|
} |
494
|
|
|
|
495
|
1 |
|
int_manager = INTManager(controller) |
496
|
1 |
|
int_manager.unis_src["00:00:00:00:00:00:00:01:3"] = pp_a.source.id |
497
|
1 |
|
with pytest.raises(ProxyPortShared): |
498
|
1 |
|
int_manager._validate_dedicated_proxy_port_evcs({evc["id"]: evc}) |
499
|
|
|
|
500
|
1 |
|
async def test__remove_int_flows_by_cookies( |
501
|
|
|
self, inter_evc_evpl_flows_data |
502
|
|
|
) -> None: |
503
|
|
|
"""test _remove_int_flows_by_cookies.""" |
504
|
1 |
|
controller = get_controller_mock() |
505
|
1 |
|
controller._buffers.app.aput = AsyncMock() |
506
|
1 |
|
int_manager = INTManager(controller) |
507
|
1 |
|
assert len(inter_evc_evpl_flows_data) == 3 |
508
|
1 |
|
res = await int_manager._remove_int_flows_by_cookies(inter_evc_evpl_flows_data) |
509
|
1 |
|
assert len(res) == 3 |
510
|
1 |
|
for flows in res.values(): |
511
|
1 |
|
for flow in flows: |
512
|
1 |
|
assert "cookie_mask" in flow |
513
|
1 |
|
assert flow["cookie_mask"] == int(0xFFFFFFFFFFFFFFFF) |
514
|
1 |
|
assert flow["table_id"] == 0xFF |
515
|
1 |
|
assert flow["owner"] == "telemetry_int" |
516
|
1 |
|
assert controller._buffers.app.aput.call_count == 3 |
517
|
|
|
|
518
|
1 |
|
async def test__remove_int_flows(self, inter_evc_evpl_flows_data) -> None: |
519
|
|
|
"""test _remove_int_flows.""" |
520
|
1 |
|
controller = get_controller_mock() |
521
|
1 |
|
controller._buffers.app.aput = AsyncMock() |
522
|
1 |
|
int_manager = INTManager(controller) |
523
|
1 |
|
assert len(inter_evc_evpl_flows_data) == 3 |
524
|
1 |
|
res = await int_manager._remove_int_flows(inter_evc_evpl_flows_data) |
525
|
1 |
|
assert len(res) == 3 |
526
|
1 |
|
assert controller._buffers.app.aput.call_count == 3 |
527
|
|
|
|
528
|
1 |
|
async def test__install_int_flows(self, inter_evc_evpl_flows_data, monkeypatch): |
529
|
|
|
"""test__install_int_flows.""" |
530
|
1 |
|
sleep_mock = AsyncMock() |
531
|
1 |
|
monkeypatch.setattr("asyncio.sleep", sleep_mock) |
532
|
1 |
|
controller = get_controller_mock() |
533
|
1 |
|
controller._buffers.app.aput = AsyncMock() |
534
|
1 |
|
int_manager = INTManager(controller) |
535
|
1 |
|
assert len(inter_evc_evpl_flows_data) == 3 |
536
|
1 |
|
res = await int_manager._install_int_flows(inter_evc_evpl_flows_data) |
537
|
1 |
|
assert len(res) == 3 |
538
|
1 |
|
assert controller._buffers.app.aput.call_count == 3 |
539
|
1 |
|
assert sleep_mock.call_count == 0 |
540
|
|
|
|
541
|
1 |
|
def test__add_pps_evc_ids(self): |
542
|
|
|
"""test_add_pps_evc_ids.""" |
543
|
1 |
|
dpid_a = "00:00:00:00:00:00:00:01" |
544
|
1 |
|
intf_id_a = f"{dpid_a}:1" |
545
|
1 |
|
intf_id_z = f"{dpid_a}:2" |
546
|
1 |
|
evc_id = "3766c105686749" |
547
|
1 |
|
evcs = { |
548
|
|
|
evc_id: { |
549
|
|
|
"metadata": {"telemetry": {"enabled": True}}, |
550
|
|
|
"uni_a": {"interface_id": intf_id_a}, |
551
|
|
|
"uni_z": {"interface_id": intf_id_z}, |
552
|
|
|
} |
553
|
|
|
} |
554
|
1 |
|
controller = get_controller_mock() |
555
|
1 |
|
int_manager = INTManager(controller) |
556
|
1 |
|
pp = MagicMock() |
557
|
1 |
|
mock = MagicMock() |
558
|
1 |
|
int_manager.get_proxy_port_or_raise = mock |
559
|
1 |
|
mock.return_value = pp |
560
|
1 |
|
int_manager._add_pps_evc_ids(evcs) |
561
|
1 |
|
assert int_manager.get_proxy_port_or_raise.call_count == 2 |
562
|
1 |
|
assert pp.evc_ids.add.call_count == 2 |
563
|
1 |
|
pp.evc_ids.add.assert_called_with(evc_id) |
564
|
|
|
|
565
|
1 |
|
def test__discard_pps_evc_ids(self): |
566
|
|
|
"""test_discard_pps_evc_ids.""" |
567
|
1 |
|
dpid_a = "00:00:00:00:00:00:00:01" |
568
|
1 |
|
intf_id_a = f"{dpid_a}:1" |
569
|
1 |
|
intf_id_z = f"{dpid_a}:2" |
570
|
1 |
|
evc_id = "3766c105686749" |
571
|
1 |
|
evcs = { |
572
|
|
|
evc_id: { |
573
|
|
|
"metadata": {"telemetry": {"enabled": True}}, |
574
|
|
|
"uni_a": {"interface_id": intf_id_a}, |
575
|
|
|
"uni_z": {"interface_id": intf_id_z}, |
576
|
|
|
} |
577
|
|
|
} |
578
|
1 |
|
controller = get_controller_mock() |
579
|
1 |
|
int_manager = INTManager(controller) |
580
|
1 |
|
pp = MagicMock() |
581
|
1 |
|
int_manager.unis_src[intf_id_a] = "a" |
582
|
1 |
|
int_manager.unis_src[intf_id_z] = "z" |
583
|
1 |
|
int_manager.srcs_pp[int_manager.unis_src[intf_id_a]] = pp |
584
|
1 |
|
int_manager.srcs_pp[int_manager.unis_src[intf_id_z]] = pp |
585
|
1 |
|
int_manager._discard_pps_evc_ids(evcs) |
586
|
1 |
|
assert pp.evc_ids.discard.call_count == 2 |
587
|
1 |
|
pp.evc_ids.discard.assert_called_with(evc_id) |
588
|
|
|
|
589
|
1 |
|
def test_validate_evc_stored_flows(self) -> None: |
590
|
|
|
"""Test validate evc stored flows.""" |
591
|
1 |
|
controller = MagicMock() |
592
|
1 |
|
int_manager = INTManager(controller) |
593
|
1 |
|
evcs = { |
594
|
|
|
"3766c105686749": { |
595
|
|
|
"active": True, |
596
|
|
|
"uni_a": MagicMock(), |
597
|
|
|
"uni_z": MagicMock(), |
598
|
|
|
} |
599
|
|
|
} |
600
|
1 |
|
stored_flows = {0xAA3766C105686749: [MagicMock()]} |
601
|
1 |
|
int_manager._validate_evcs_stored_flows(evcs, stored_flows) |
602
|
|
|
|
603
|
1 |
|
with pytest.raises(exceptions.FlowsNotFound): |
604
|
1 |
|
int_manager._validate_evcs_stored_flows(evcs, {0xAA3766C105686749: []}) |
605
|
|
|
|
606
|
1 |
|
with pytest.raises(exceptions.FlowsNotFound): |
607
|
1 |
|
int_manager._validate_evcs_stored_flows(evcs, {}) |
608
|
|
|
|
609
|
1 |
|
evcs["3766c105686749"]["active"] = False |
610
|
1 |
|
int_manager._validate_evcs_stored_flows(evcs, {}) |
611
|
|
|
|
612
|
1 |
|
async def test__send_flows(self) -> None: |
613
|
|
|
"""Test _send_flows.""" |
614
|
1 |
|
controller = get_controller_mock() |
615
|
1 |
|
controller._buffers.app.aput = AsyncMock() |
616
|
1 |
|
int_manager = INTManager(controller) |
617
|
1 |
|
switch_flows = {"dpid": []} |
618
|
1 |
|
await int_manager._send_flows(switch_flows, "install") |
619
|
1 |
|
controller._buffers.app.aput.assert_not_called() |
620
|
|
|
|
621
|
1 |
|
switch_flows = {"dpid": [MagicMock()]} |
622
|
1 |
|
await int_manager._send_flows(switch_flows, "install") |
623
|
|
|
controller._buffers.app.aput.assert_called() |
624
|
|
|
|