1
|
|
|
"""Test Main methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from kytos.core.events import KytosEvent |
6
|
|
|
from kytos.lib.helpers import get_controller_mock, get_test_client |
7
|
|
|
|
8
|
|
|
from napps.kytos.pathfinder.main import Main |
9
|
|
|
from tests.helpers import get_topology_mock |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
# pylint: disable=protected-access |
13
|
|
|
class TestMain(TestCase): |
14
|
|
|
"""Tests for the Main class.""" |
15
|
|
|
|
16
|
|
|
def setUp(self): |
17
|
|
|
"""Execute steps before each tests.""" |
18
|
|
|
self.napp = Main(get_controller_mock()) |
19
|
|
|
|
20
|
|
|
@patch('napps.kytos.pathfinder.graph.KytosGraph.update_topology') |
21
|
|
|
def test_update_topology(self, mock_update_topology): |
22
|
|
|
"""Test update topology.""" |
23
|
|
|
event = KytosEvent(name='kytos.topology.updated') |
24
|
|
|
self.napp.update_topology(event) |
25
|
|
|
self.assertIsNone(self.napp._topology) |
26
|
|
|
|
27
|
|
|
topology = get_topology_mock() |
28
|
|
|
event = KytosEvent(name='kytos.topology.updated', |
29
|
|
|
content={'topology': topology}) |
30
|
|
|
self.napp.update_topology(event) |
31
|
|
|
self.assertEqual(self.napp._topology, topology) |
32
|
|
|
mock_update_topology.assert_called_with(topology) |
33
|
|
|
|
34
|
|
|
@patch('napps.kytos.pathfinder.main.Main._filter_paths') |
35
|
|
|
@patch('napps.kytos.pathfinder.graph.KytosGraph.shortest_paths') |
36
|
|
|
def test_shortest_path(self, *args): |
37
|
|
|
"""Test shortest path.""" |
38
|
|
|
(mock_shortest_paths, mock_filter_paths) = args |
39
|
|
|
|
40
|
|
|
path = ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:01", |
41
|
|
|
"00:00:00:00:00:00:00:01:2"] |
42
|
|
|
mock_shortest_paths.return_value = path |
43
|
|
|
mock_filter_paths.return_value = {} |
44
|
|
|
|
45
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
46
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2" |
47
|
|
|
data = {"source": "00:00:00:00:00:00:00:01:1", |
48
|
|
|
"destination": "00:00:00:00:00:00:00:01:2", |
49
|
|
|
"desired_links": ["1"], |
50
|
|
|
"undesired_links": ["2"], |
51
|
|
|
"parameter": "custom_weight"} |
52
|
|
|
response = api.open(url, method='POST', json=data) |
53
|
|
|
|
54
|
|
|
hops = [{'hops': addr} for addr in path] |
55
|
|
|
mock_filter_paths.assert_called_with(hops, data["desired_links"], |
56
|
|
|
data["undesired_links"]) |
57
|
|
|
self.assertEqual(response.status_code, 200) |
58
|
|
|
|
59
|
|
|
def test_filter_paths(self): |
60
|
|
|
"""Test filter paths.""" |
61
|
|
|
self.napp._topology = get_topology_mock() |
62
|
|
|
paths = [{"hops": ["00:00:00:00:00:00:00:01:1", |
63
|
|
|
"00:00:00:00:00:00:00:02:1"]}] |
64
|
|
|
desired, undesired = ["1"], None |
65
|
|
|
|
66
|
|
|
filtered_paths = self.napp._filter_paths(paths, desired, undesired) |
67
|
|
|
self.assertEqual(filtered_paths, paths) |
68
|
|
|
|
69
|
|
|
paths = [{"hops": ["00:00:00:00:00:00:00:01:2", |
70
|
|
|
"00:00:00:00:00:00:00:03:1"]}] |
71
|
|
|
desired, undesired = None, ["2"] |
72
|
|
|
filtered_paths = self.napp._filter_paths(paths, desired, undesired) |
73
|
|
|
self.assertEqual(filtered_paths, []) |
74
|
|
|
|