|
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 response.status_code == 201 |
|
43
|
1 |
|
assert response.json() == [evc_id] |
|
44
|
|
|
|
|
45
|
1 |
|
@pytest.mark.parametrize("route", ["/evc/enable", "/evc/disable"]) |
|
46
|
1 |
|
async def test_en_dis_openapi_validation(self, route: str) -> None: |
|
47
|
|
|
"""Test OpenAPI enable/disable basic validation.""" |
|
48
|
1 |
|
endpoint = f"{self.base_endpoint}{route}" |
|
49
|
|
|
# wrong evc_ids payload data type |
|
50
|
1 |
|
response = await self.api_client.post(endpoint, json={"evc_ids": 1}) |
|
51
|
1 |
|
assert response.status_code == 400 |
|
52
|
1 |
|
assert "evc_ids" in response.json()["description"] |
|
53
|
|
|
|
|
54
|
1 |
View Code Duplication |
async def test_disable_telemetry(self, monkeypatch) -> None: |
|
|
|
|
|
|
55
|
|
|
"""Test disable telemetry.""" |
|
56
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
|
57
|
1 |
|
flow.cookie = 0xA800000000000001 |
|
58
|
1 |
|
monkeypatch.setattr( |
|
59
|
|
|
"napps.kytos.telemetry_int.main.api", |
|
60
|
|
|
api_mock, |
|
61
|
|
|
) |
|
62
|
|
|
|
|
63
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
|
64
|
1 |
|
api_mock.get_evcs.return_value = { |
|
65
|
|
|
evc_id: {"metadata": {"telemetry": {"enabled": True}}} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
self.napp.int_manager = AsyncMock() |
|
69
|
|
|
|
|
70
|
1 |
|
endpoint = f"{self.base_endpoint}/evc/disable" |
|
71
|
1 |
|
response = await self.api_client.post(endpoint, json={"evc_ids": [evc_id]}) |
|
72
|
1 |
|
assert self.napp.int_manager.disable_int.call_count == 1 |
|
73
|
1 |
|
assert response.status_code == 200 |
|
74
|
1 |
|
assert response.json() == [evc_id] |
|
75
|
|
|
|
|
76
|
1 |
|
async def test_get_enabled_evcs(self, monkeypatch) -> None: |
|
77
|
|
|
"""Test get enabled evcs.""" |
|
78
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
|
79
|
1 |
|
flow.cookie = 0xA800000000000001 |
|
80
|
1 |
|
monkeypatch.setattr( |
|
81
|
|
|
"napps.kytos.telemetry_int.main.api", |
|
82
|
|
|
api_mock, |
|
83
|
|
|
) |
|
84
|
|
|
|
|
85
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
|
86
|
1 |
|
api_mock.get_evcs.return_value = { |
|
87
|
|
|
evc_id: {"metadata": {"telemetry": {"enabled": True}}}, |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
endpoint = f"{self.base_endpoint}/evc" |
|
91
|
1 |
|
response = await self.api_client.get(endpoint) |
|
92
|
1 |
|
assert api_mock.get_evcs.call_args[1] == {"metadata.telemetry.enabled": "true"} |
|
93
|
1 |
|
assert response.status_code == 200 |
|
94
|
1 |
|
data = response.json() |
|
95
|
1 |
|
assert len(data) == 1 |
|
96
|
1 |
|
assert evc_id in data |
|
97
|
|
|
|
|
98
|
1 |
|
async def test_on_table_enabled(self) -> None: |
|
99
|
|
|
"""Test on_table_enabled.""" |
|
100
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3} |
|
101
|
1 |
|
await self.napp.on_table_enabled( |
|
102
|
|
|
KytosEvent(content={"telemetry_int": {"evpl": 22, "epl": 33}}) |
|
103
|
|
|
) |
|
104
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 22, "epl": 33} |
|
105
|
1 |
|
assert self.napp.controller.buffers.app.aput.call_count == 1 |
|
106
|
|
|
|
|
107
|
1 |
|
async def test_on_table_enabled_error(self, monkeypatch) -> None: |
|
108
|
|
|
"""Test on_table_enabled error case.""" |
|
109
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3} |
|
110
|
1 |
|
log_mock = MagicMock() |
|
111
|
1 |
|
monkeypatch.setattr("napps.kytos.telemetry_int.main.log", log_mock) |
|
112
|
1 |
|
await self.napp.on_table_enabled( |
|
113
|
|
|
KytosEvent(content={"telemetry_int": {"invalid": 1}}) |
|
114
|
|
|
) |
|
115
|
1 |
|
assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3} |
|
116
|
1 |
|
assert log_mock.error.call_count == 1 |
|
117
|
1 |
|
assert not self.napp.controller.buffers.app.aput.call_count |
|
118
|
|
|
|
|
119
|
1 |
|
async def test_on_flow_mod_error(self, monkeypatch) -> None: |
|
120
|
|
|
"""Test on_flow_mod_error.""" |
|
121
|
1 |
|
api_mock, flow = AsyncMock(), MagicMock() |
|
122
|
1 |
|
flow.cookie = 0xA800000000000001 |
|
123
|
1 |
|
monkeypatch.setattr( |
|
124
|
|
|
"napps.kytos.telemetry_int.main.api", |
|
125
|
|
|
api_mock, |
|
126
|
|
|
) |
|
127
|
1 |
|
api_mock.get_evc.return_value = { |
|
128
|
|
|
utils.get_id_from_cookie(flow.cookie): { |
|
129
|
|
|
"metadata": {"telemetry": {"enabled": True}} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
1 |
|
self.napp.int_manager.remove_int_flows = AsyncMock() |
|
133
|
|
|
|
|
134
|
1 |
|
event = KytosEvent(content={"flow": flow, "error_command": "add"}) |
|
135
|
1 |
|
await self.napp.on_flow_mod_error(event) |
|
136
|
|
|
|
|
137
|
1 |
|
assert api_mock.get_evc.call_count == 1 |
|
138
|
1 |
|
assert api_mock.get_stored_flows.call_count == 1 |
|
139
|
1 |
|
assert api_mock.add_evcs_metadata.call_count == 1 |
|
140
|
|
|
assert self.napp.int_manager.remove_int_flows.call_count == 1 |
|
141
|
|
|
|