|
1
|
|
|
"""Test Main methods.""" |
|
2
|
|
|
from unittest import TestCase |
|
3
|
|
|
from unittest.mock import 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
|
|
|
def test_update_topology_success_case(self): |
|
21
|
|
|
"""Test update topology method to success case.""" |
|
22
|
|
|
topology = get_topology_mock() |
|
23
|
|
|
event = KytosEvent(name='kytos.topology.updated', |
|
24
|
|
|
content={'topology': topology}) |
|
25
|
|
|
self.napp.update_topology(event) |
|
26
|
|
|
|
|
27
|
|
|
self.assertEqual(self.napp._topology, topology) |
|
28
|
|
|
|
|
29
|
|
|
def test_update_topology_failure_case(self): |
|
30
|
|
|
"""Test update topology method to failure case.""" |
|
31
|
|
|
event = KytosEvent(name='kytos.topology.updated') |
|
32
|
|
|
self.napp.update_topology(event) |
|
33
|
|
|
|
|
34
|
|
|
self.assertIsNone(self.napp._topology) |
|
35
|
|
|
|
|
36
|
|
|
@patch('napps.kytos.pathfinder.graph.KytosGraph.shortest_paths') |
|
37
|
|
|
def test_shortest_path(self, mock_shortest_paths): |
|
38
|
|
|
"""Test shortest path.""" |
|
39
|
|
|
self.napp._topology = get_topology_mock() |
|
40
|
|
|
path = ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:1"] |
|
41
|
|
|
mock_shortest_paths.return_value = [path] |
|
42
|
|
|
|
|
43
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
|
44
|
|
|
url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2" |
|
45
|
|
|
data = {"source": "00:00:00:00:00:00:00:01:1", |
|
46
|
|
|
"destination": "00:00:00:00:00:00:00:02:1", |
|
47
|
|
|
"desired_links": ["1"], |
|
48
|
|
|
"undesired_links": None} |
|
49
|
|
|
response = api.open(url, method='POST', json=data) |
|
50
|
|
|
|
|
51
|
|
|
expected_response = {'paths': [{'hops': path}]} |
|
52
|
|
|
self.assertEqual(response.json, expected_response) |
|
53
|
|
|
self.assertEqual(response.status_code, 200) |
|
54
|
|
|
|
|
55
|
|
|
def test_filter_paths(self): |
|
56
|
|
|
"""Test filter paths.""" |
|
57
|
|
|
self.napp._topology = get_topology_mock() |
|
58
|
|
|
paths = [{"hops": ["00:00:00:00:00:00:00:01:1", |
|
59
|
|
|
"00:00:00:00:00:00:00:02:1"]}] |
|
60
|
|
|
desired, undesired = ["1"], None |
|
61
|
|
|
|
|
62
|
|
|
filtered_paths = self.napp._filter_paths(paths, desired, undesired) |
|
63
|
|
|
self.assertEqual(filtered_paths, paths) |
|
64
|
|
|
|
|
65
|
|
|
paths = [{"hops": ["00:00:00:00:00:00:00:01:2", |
|
66
|
|
|
"00:00:00:00:00:00:00:03:1"]}] |
|
67
|
|
|
desired, undesired = None, ["2"] |
|
68
|
|
|
filtered_paths = self.napp._filter_paths(paths, desired, undesired) |
|
69
|
|
|
self.assertEqual(filtered_paths, []) |
|
70
|
|
|
|