1
|
|
|
"""Test Main methods.""" |
2
|
|
|
|
3
|
1 |
|
import pytest |
4
|
|
|
|
5
|
1 |
|
from unittest.mock import AsyncMock, MagicMock, patch |
6
|
1 |
|
from napps.kytos.telemetry_int.main import Main |
7
|
1 |
|
from napps.kytos.telemetry_int import utils |
8
|
1 |
|
from kytos.lib.helpers import get_controller_mock, get_test_client |
9
|
1 |
|
from kytos.core.events import KytosEvent |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
class TestMain: |
13
|
|
|
"""Tests for the Main class.""" |
14
|
|
|
|
15
|
1 |
|
def setup_method(self): |
16
|
|
|
"""Setup.""" |
17
|
1 |
|
patch("kytos.core.helpers.run_on_thread", lambda x: x).start() |
18
|
|
|
# pylint: disable=import-outside-toplevel |
19
|
1 |
|
controller = get_controller_mock() |
20
|
1 |
|
self.napp = Main(controller) |
21
|
1 |
|
self.api_client = get_test_client(controller, self.napp) |
22
|
1 |
|
self.base_endpoint = "kytos/telemetry_int/v1" |
23
|
|
|
|
24
|
1 |
View Code Duplication |
async def test_enable_telemetry(self, monkeypatch) -> None: |
|
|
|
|
25
|
|
|
"""Test enable telemetry.""" |
26
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
27
|
1 |
|
flow.cookie = 0xA800000000000001 |
28
|
1 |
|
monkeypatch.setattr( |
29
|
|
|
"napps.kytos.telemetry_int.main.api", |
30
|
|
|
api_mock, |
31
|
|
|
) |
32
|
|
|
|
33
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
34
|
1 |
|
api_mock.get_evcs.return_value = { |
35
|
|
|
evc_id: {"metadata": {"telemetry": {"enabled": False}}} |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
self.napp.int_manager = AsyncMock() |
39
|
|
|
|
40
|
1 |
|
endpoint = f"{self.base_endpoint}/evc/enable" |
41
|
1 |
|
response = await self.api_client.post(endpoint, json={"evc_ids": [evc_id]}) |
42
|
1 |
|
assert self.napp.int_manager.enable_int.call_count == 1 |
43
|
1 |
|
assert self.napp.int_manager._remove_int_flows.call_count == 1 |
44
|
1 |
|
assert response.status_code == 201 |
45
|
1 |
|
assert response.json() == [evc_id] |
46
|
|
|
|
47
|
1 |
View Code Duplication |
async def test_redeploy_telemetry(self, monkeypatch) -> None: |
|
|
|
|
48
|
|
|
"""Test redeploy telemetry.""" |
49
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
50
|
1 |
|
flow.cookie = 0xA800000000000001 |
51
|
1 |
|
monkeypatch.setattr( |
52
|
|
|
"napps.kytos.telemetry_int.main.api", |
53
|
|
|
api_mock, |
54
|
|
|
) |
55
|
|
|
|
56
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
57
|
1 |
|
api_mock.get_evcs.return_value = { |
58
|
|
|
evc_id: {"metadata": {"telemetry": {"enabled": False}}} |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
self.napp.int_manager = AsyncMock() |
62
|
|
|
|
63
|
1 |
|
endpoint = f"{self.base_endpoint}/evc/redeploy" |
64
|
1 |
|
response = await self.api_client.patch(endpoint, json={"evc_ids": [evc_id]}) |
65
|
1 |
|
assert self.napp.int_manager.redeploy_int.call_count == 1 |
66
|
1 |
|
assert response.status_code == 201 |
67
|
1 |
|
assert response.json() == [evc_id] |
68
|
|
|
|
69
|
1 |
|
@pytest.mark.parametrize("route", ["/evc/enable", "/evc/disable"]) |
70
|
1 |
|
async def test_en_dis_openapi_validation(self, route: str) -> None: |
71
|
|
|
"""Test OpenAPI enable/disable basic validation.""" |
72
|
1 |
|
endpoint = f"{self.base_endpoint}{route}" |
73
|
|
|
# wrong evc_ids payload data type |
74
|
1 |
|
response = await self.api_client.post(endpoint, json={"evc_ids": 1}) |
75
|
1 |
|
assert response.status_code == 400 |
76
|
1 |
|
assert "evc_ids" in response.json()["description"] |
77
|
|
|
|
78
|
1 |
View Code Duplication |
async def test_disable_telemetry(self, monkeypatch) -> None: |
|
|
|
|
79
|
|
|
"""Test disable telemetry.""" |
80
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
81
|
1 |
|
flow.cookie = 0xA800000000000001 |
82
|
1 |
|
monkeypatch.setattr( |
83
|
|
|
"napps.kytos.telemetry_int.main.api", |
84
|
|
|
api_mock, |
85
|
|
|
) |
86
|
|
|
|
87
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
88
|
1 |
|
api_mock.get_evcs.return_value = { |
89
|
|
|
evc_id: {"metadata": {"telemetry": {"enabled": True}}} |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
self.napp.int_manager = AsyncMock() |
93
|
|
|
|
94
|
1 |
|
endpoint = f"{self.base_endpoint}/evc/disable" |
95
|
1 |
|
response = await self.api_client.post(endpoint, json={"evc_ids": [evc_id]}) |
96
|
1 |
|
assert self.napp.int_manager.disable_int.call_count == 1 |
97
|
1 |
|
assert response.status_code == 200 |
98
|
1 |
|
assert response.json() == [evc_id] |
99
|
|
|
|
100
|
1 |
|
async def test_get_enabled_evcs(self, monkeypatch) -> None: |
101
|
|
|
"""Test get enabled evcs.""" |
102
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
103
|
1 |
|
flow.cookie = 0xA800000000000001 |
104
|
1 |
|
monkeypatch.setattr( |
105
|
|
|
"napps.kytos.telemetry_int.main.api", |
106
|
|
|
api_mock, |
107
|
|
|
) |
108
|
|
|
|
109
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
110
|
1 |
|
api_mock.get_evcs.return_value = { |
111
|
|
|
evc_id: {"metadata": {"telemetry": {"enabled": True}}}, |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
endpoint = f"{self.base_endpoint}/evc" |
115
|
1 |
|
response = await self.api_client.get(endpoint) |
116
|
1 |
|
assert api_mock.get_evcs.call_args[1] == {"metadata.telemetry.enabled": "true"} |
117
|
1 |
|
assert response.status_code == 200 |
118
|
1 |
|
data = response.json() |
119
|
1 |
|
assert len(data) == 1 |
120
|
1 |
|
assert evc_id in data |
121
|
|
|
|
122
|
1 |
|
async def test_get_evc_compare(self, monkeypatch) -> None: |
123
|
|
|
"""Test get evc compre ok case.""" |
124
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
125
|
1 |
|
flow.cookie = 0xA800000000000001 |
126
|
1 |
|
evc_id = "1" |
127
|
1 |
|
monkeypatch.setattr( |
128
|
|
|
"napps.kytos.telemetry_int.main.api", |
129
|
|
|
api_mock, |
130
|
|
|
) |
131
|
|
|
|
132
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
133
|
1 |
|
api_mock.get_evcs.return_value = { |
134
|
|
|
evc_id: { |
135
|
|
|
"id": evc_id, |
136
|
|
|
"name": "evc", |
137
|
|
|
"metadata": {"telemetry": {"enabled": True}}, |
138
|
|
|
}, |
139
|
|
|
} |
140
|
1 |
|
api_mock.get_stored_flows.side_effect = [ |
141
|
|
|
{flow.cookie: [{"id": "some_1", "match": {"in_port": 1}}]}, |
142
|
|
|
{flow.cookie: [{"id": "some_2", "match": {"in_port": 1}}]}, |
143
|
|
|
] |
144
|
|
|
|
145
|
1 |
|
endpoint = f"{self.base_endpoint}/evc/compare" |
146
|
1 |
|
response = await self.api_client.get(endpoint) |
147
|
1 |
|
assert response.status_code == 200 |
148
|
1 |
|
data = response.json() |
149
|
1 |
|
assert len(data) == 0 |
150
|
|
|
|
151
|
1 |
|
async def test_get_evc_compare_wrong_metadata(self, monkeypatch) -> None: |
152
|
|
|
"""Test get evc compre wrong_metadata_has_int_flows case.""" |
153
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
154
|
1 |
|
flow.cookie = 0xA800000000000001 |
155
|
1 |
|
evc_id = "1" |
156
|
1 |
|
monkeypatch.setattr( |
157
|
|
|
"napps.kytos.telemetry_int.main.api", |
158
|
|
|
api_mock, |
159
|
|
|
) |
160
|
|
|
|
161
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
162
|
1 |
|
api_mock.get_evcs.return_value = { |
163
|
|
|
evc_id: {"id": evc_id, "name": "evc", "metadata": {}}, |
164
|
|
|
} |
165
|
1 |
|
api_mock.get_stored_flows.side_effect = [ |
166
|
|
|
{flow.cookie: [{"id": "some_1", "match": {"in_port": 1}}]}, |
167
|
|
|
{flow.cookie: [{"id": "some_2", "match": {"in_port": 1}}]}, |
168
|
|
|
] |
169
|
|
|
|
170
|
1 |
|
endpoint = f"{self.base_endpoint}/evc/compare" |
171
|
1 |
|
response = await self.api_client.get(endpoint) |
172
|
1 |
|
assert response.status_code == 200 |
173
|
1 |
|
data = response.json() |
174
|
1 |
|
assert len(data) == 1 |
175
|
1 |
|
assert data[0]["id"] == evc_id |
176
|
1 |
|
assert data[0]["compare_reason"] == ["wrong_metadata_has_int_flows"] |
177
|
1 |
|
assert data[0]["name"] == "evc" |
178
|
|
|
|
179
|
1 |
|
async def test_get_evc_compare_missing_some_int_flows(self, monkeypatch) -> None: |
180
|
|
|
"""Test get evc compre missing_some_int_flows case.""" |
181
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
182
|
1 |
|
flow.cookie = 0xA800000000000001 |
183
|
1 |
|
evc_id = "1" |
184
|
1 |
|
monkeypatch.setattr( |
185
|
|
|
"napps.kytos.telemetry_int.main.api", |
186
|
|
|
api_mock, |
187
|
|
|
) |
188
|
|
|
|
189
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
190
|
1 |
|
api_mock.get_evcs.return_value = { |
191
|
|
|
evc_id: { |
192
|
|
|
"id": evc_id, |
193
|
|
|
"name": "evc", |
194
|
|
|
"metadata": {"telemetry": {"enabled": True}}, |
195
|
|
|
}, |
196
|
|
|
} |
197
|
1 |
|
api_mock.get_stored_flows.side_effect = [ |
198
|
|
|
{flow.cookie: [{"id": "some_1", "match": {"in_port": 1}}]}, |
199
|
|
|
{ |
200
|
|
|
flow.cookie: [ |
201
|
|
|
{"id": "some_2", "match": {"in_port": 1}}, |
202
|
|
|
{"id": "some_3", "match": {"in_port": 1}}, |
203
|
|
|
] |
204
|
|
|
}, |
205
|
|
|
] |
206
|
|
|
|
207
|
1 |
|
endpoint = f"{self.base_endpoint}/evc/compare" |
208
|
1 |
|
response = await self.api_client.get(endpoint) |
209
|
1 |
|
assert response.status_code == 200 |
210
|
1 |
|
data = response.json() |
211
|
1 |
|
assert len(data) == 1 |
212
|
1 |
|
assert data[0]["id"] == evc_id |
213
|
1 |
|
assert data[0]["compare_reason"] == ["missing_some_int_flows"] |
214
|
1 |
|
assert data[0]["name"] == "evc" |
215
|
|
|
|
216
|
1 |
|
async def test_on_table_enabled(self) -> None: |
217
|
|
|
"""Test on_table_enabled.""" |
218
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3} |
219
|
1 |
|
await self.napp.on_table_enabled( |
220
|
|
|
KytosEvent(content={"telemetry_int": {"evpl": 22, "epl": 33}}) |
221
|
|
|
) |
222
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 22, "epl": 33} |
223
|
1 |
|
assert self.napp.controller.buffers.app.aput.call_count == 1 |
224
|
|
|
|
225
|
1 |
|
async def test_on_table_enabled_no_group(self) -> None: |
226
|
|
|
"""Test on_table_enabled no group.""" |
227
|
1 |
|
await self.napp.on_table_enabled( |
228
|
|
|
KytosEvent(content={"mef_eline": {"evpl": 22, "epl": 33}}) |
229
|
|
|
) |
230
|
1 |
|
assert not self.napp.controller.buffers.app.aput.call_count |
231
|
|
|
|
232
|
1 |
|
async def test_on_evc_deleted(self) -> None: |
233
|
|
|
"""Test on_evc_deleted.""" |
234
|
1 |
|
content = {"metadata": {"telemetry": {"enabled": True}}, "evc_id": "some_id"} |
235
|
1 |
|
self.napp.int_manager.disable_int = AsyncMock() |
236
|
1 |
|
await self.napp.on_evc_deleted(KytosEvent(content=content)) |
237
|
1 |
|
assert self.napp.int_manager.disable_int.call_count == 1 |
238
|
|
|
|
239
|
1 |
|
async def test_on_evc_undeployed(self) -> None: |
240
|
|
|
"""Test on_evc_undeployed.""" |
241
|
1 |
|
content = { |
242
|
|
|
"enabled": False, |
243
|
|
|
"metadata": {"telemetry": {"enabled": False}}, |
244
|
|
|
"evc_id": "some_id", |
245
|
|
|
} |
246
|
1 |
|
self.napp.int_manager.remove_int_flows = AsyncMock() |
247
|
1 |
|
await self.napp.on_evc_undeployed(KytosEvent(content=content)) |
248
|
1 |
|
assert self.napp.int_manager.remove_int_flows.call_count == 0 |
249
|
|
|
|
250
|
1 |
|
content["metadata"]["telemetry"]["enabled"] = True |
251
|
1 |
|
await self.napp.on_evc_undeployed(KytosEvent(content=content)) |
252
|
1 |
|
assert self.napp.int_manager.remove_int_flows.call_count == 1 |
253
|
|
|
|
254
|
1 |
|
async def test_on_evc_redeployed_link(self) -> None: |
255
|
|
|
"""Test on redeployed_link_down|redeployed_link_up.""" |
256
|
1 |
|
content = { |
257
|
|
|
"enabled": True, |
258
|
|
|
"metadata": {"telemetry": {"enabled": False}}, |
259
|
|
|
"evc_id": "some_id", |
260
|
|
|
} |
261
|
1 |
|
self.napp.int_manager.redeploy_int = AsyncMock() |
262
|
1 |
|
await self.napp.on_evc_redeployed_link(KytosEvent(content=content)) |
263
|
1 |
|
assert self.napp.int_manager.redeploy_int.call_count == 0 |
264
|
|
|
|
265
|
1 |
|
content["metadata"]["telemetry"]["enabled"] = True |
266
|
1 |
|
await self.napp.on_evc_redeployed_link(KytosEvent(content=content)) |
267
|
1 |
|
assert self.napp.int_manager.redeploy_int.call_count == 1 |
268
|
|
|
|
269
|
1 |
|
async def test_on_evc_error_redeployed_link_down(self) -> None: |
270
|
|
|
"""Test error_redeployed_link_down.""" |
271
|
1 |
|
content = { |
272
|
|
|
"enabled": True, |
273
|
|
|
"metadata": {"telemetry": {"enabled": False}}, |
274
|
|
|
"evc_id": "some_id", |
275
|
|
|
} |
276
|
1 |
|
self.napp.int_manager.remove_int_flows = AsyncMock() |
277
|
1 |
|
await self.napp.on_evc_error_redeployed_link_down(KytosEvent(content=content)) |
278
|
1 |
|
assert self.napp.int_manager.remove_int_flows.call_count == 0 |
279
|
|
|
|
280
|
1 |
|
content["metadata"]["telemetry"]["enabled"] = True |
281
|
1 |
|
await self.napp.on_evc_error_redeployed_link_down(KytosEvent(content=content)) |
282
|
1 |
|
assert self.napp.int_manager.remove_int_flows.call_count == 1 |
283
|
|
|
|
284
|
1 |
|
async def test_on_link_down(self) -> None: |
285
|
|
|
"""Test on link_down.""" |
286
|
1 |
|
self.napp.int_manager.handle_pp_link_down = AsyncMock() |
287
|
1 |
|
await self.napp.on_link_down(KytosEvent(content={"link": MagicMock()})) |
288
|
1 |
|
assert self.napp.int_manager.handle_pp_link_down.call_count == 1 |
289
|
|
|
|
290
|
1 |
|
async def test_on_link_up(self) -> None: |
291
|
|
|
"""Test on link_up.""" |
292
|
1 |
|
self.napp.int_manager.handle_pp_link_up = AsyncMock() |
293
|
1 |
|
await self.napp.on_link_up(KytosEvent(content={"link": MagicMock()})) |
294
|
1 |
|
assert self.napp.int_manager.handle_pp_link_up.call_count == 1 |
295
|
|
|
|
296
|
1 |
|
async def test_on_table_enabled_error(self, monkeypatch) -> None: |
297
|
|
|
"""Test on_table_enabled error case.""" |
298
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3} |
299
|
1 |
|
log_mock = MagicMock() |
300
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.main.log", log_mock) |
301
|
1 |
|
await self.napp.on_table_enabled( |
302
|
|
|
KytosEvent(content={"telemetry_int": {"invalid": 1}}) |
303
|
|
|
) |
304
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3} |
305
|
1 |
|
assert log_mock.error.call_count == 1 |
306
|
1 |
|
assert not self.napp.controller.buffers.app.aput.call_count |
307
|
|
|
|
308
|
1 |
|
async def test_on_flow_mod_error(self, monkeypatch) -> None: |
309
|
|
|
"""Test on_flow_mod_error.""" |
310
|
1 |
|
api_mock_main, api_mock_int, flow = AsyncMock(), AsyncMock(), MagicMock() |
311
|
1 |
|
flow.cookie = 0xA800000000000001 |
312
|
1 |
|
monkeypatch.setattr( |
313
|
|
|
"napps.kytos.telemetry_int.main.api", |
314
|
|
|
api_mock_main, |
315
|
|
|
) |
316
|
1 |
|
monkeypatch.setattr( |
317
|
|
|
"napps.kytos.telemetry_int.managers.int.api", |
318
|
|
|
api_mock_int, |
319
|
|
|
) |
320
|
1 |
|
cookie = utils.get_id_from_cookie(flow.cookie) |
321
|
1 |
|
api_mock_main.get_evc.return_value = { |
322
|
|
|
cookie: {"metadata": {"telemetry": {"enabled": True}}} |
323
|
|
|
} |
324
|
1 |
|
api_mock_int.get_stored_flows.return_value = {cookie: [MagicMock()]} |
325
|
1 |
|
self.napp.int_manager._remove_int_flows = AsyncMock() |
326
|
|
|
|
327
|
1 |
|
event = KytosEvent(content={"flow": flow, "error_command": "add"}) |
328
|
1 |
|
await self.napp.on_flow_mod_error(event) |
329
|
|
|
|
330
|
1 |
|
assert api_mock_main.get_evc.call_count == 1 |
331
|
1 |
|
assert api_mock_int.get_stored_flows.call_count == 1 |
332
|
1 |
|
assert api_mock_int.add_evcs_metadata.call_count == 1 |
333
|
1 |
|
assert self.napp.int_manager._remove_int_flows.call_count == 1 |
334
|
|
|
|
335
|
1 |
|
async def test_on_mef_eline_evcs_loaded(self): |
336
|
|
|
"""Test on_mef_eline_evcs_loaded.""" |
337
|
1 |
|
evcs = {"1": {}, "2": {}} |
338
|
1 |
|
event = KytosEvent(content=evcs) |
339
|
1 |
|
self.napp.int_manager = MagicMock() |
340
|
1 |
|
await self.napp.on_mef_eline_evcs_loaded(event) |
341
|
1 |
|
self.napp.int_manager.load_uni_src_proxy_ports.assert_called_with(evcs) |
342
|
|
|
|
343
|
1 |
|
async def test_on_intf_metadata_remove(self): |
344
|
|
|
"""Test on_intf_metadata_removed.""" |
345
|
1 |
|
intf = MagicMock() |
346
|
1 |
|
event = KytosEvent(content={"interface": intf}) |
347
|
1 |
|
self.napp.int_manager = MagicMock() |
348
|
1 |
|
await self.napp.on_intf_metadata_removed(event) |
349
|
1 |
|
self.napp.int_manager.handle_pp_metadata_removed.assert_called_with(intf) |
350
|
|
|
|
351
|
1 |
|
async def test_on_intf_metadata_added(self): |
352
|
|
|
"""Test on_intf_metadata_added.""" |
353
|
1 |
|
intf = MagicMock() |
354
|
1 |
|
event = KytosEvent(content={"interface": intf}) |
355
|
1 |
|
self.napp.int_manager = MagicMock() |
356
|
1 |
|
await self.napp.on_intf_metadata_added(event) |
357
|
|
|
self.napp.int_manager.handle_pp_metadata_added.assert_called_with(intf) |
358
|
|
|
|