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