1
|
|
|
"""Module to test the Path class.""" |
2
|
|
|
import sys |
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import patch, Mock |
5
|
|
|
|
6
|
|
|
from kytos.core.common import EntityStatus |
7
|
|
|
|
8
|
|
|
# pylint: disable=wrong-import-position |
9
|
|
|
sys.path.insert(0, '/var/lib/kytos/napps/..') |
10
|
|
|
# pylint: enable=wrong-import-position |
11
|
|
|
from napps.kytos.mef_eline.models import Path # NOQA pycodestyle |
12
|
|
|
from tests.helpers import MockResponse, get_link_mocked # NOQA pycodestyle |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class TestPath(TestCase): |
16
|
|
|
"""Class to test path methods.""" |
17
|
|
|
|
18
|
|
|
def test_is_affected_by_link_1(self): |
19
|
|
|
"""Test method is affected by link.""" |
20
|
|
|
path = Path() |
21
|
|
|
self.assertIs(path.is_affected_by_link(), False) |
22
|
|
|
|
23
|
|
|
def test_link_affected_by_interface_1(self): |
24
|
|
|
"""Test method to get the link using an interface.""" |
25
|
|
|
link1 = Mock() |
26
|
|
|
link1.endpoint_a = 'a' |
27
|
|
|
link1.endpoint_b = 'b' |
28
|
|
|
link2 = Mock() |
29
|
|
|
link2.endpoint_a = 'c' |
30
|
|
|
link2.endpoint_b = 'd' |
31
|
|
|
path = Path([link1, link2]) |
32
|
|
|
self.assertIsNone(path.link_affected_by_interface()) |
33
|
|
|
|
34
|
|
|
def test_link_affected_by_interface_2(self): |
35
|
|
|
"""Test method to get the link using an interface.""" |
36
|
|
|
link1 = Mock() |
37
|
|
|
link1.endpoint_a = 'a' |
38
|
|
|
link1.endpoint_b = 'b' |
39
|
|
|
link2 = Mock() |
40
|
|
|
link2.endpoint_a = 'c' |
41
|
|
|
link2.endpoint_b = 'd' |
42
|
|
|
path = Path([link1, link2]) |
43
|
|
|
self.assertEqual(path.link_affected_by_interface('a'), link1) |
44
|
|
|
|
45
|
|
|
def test_status_case_1(self): |
46
|
|
|
"""Test if empty link is DISABLED.""" |
47
|
|
|
current_path = Path() |
48
|
|
|
self.assertEqual(current_path.status, EntityStatus.DISABLED) |
49
|
|
|
|
50
|
|
|
# This method will be used by the mock to replace requests.get |
51
|
|
|
def _mocked_requests_get_status_case_2(self): |
52
|
|
|
# pylint: disable=no-self-use |
53
|
|
|
return MockResponse({'links': {'abc': {'active': False, |
54
|
|
|
'enabled': True}, |
55
|
|
|
'def': {'active': True, |
56
|
|
|
'enabled': True}}}, 200) |
57
|
|
|
|
58
|
|
|
@patch('requests.get', side_effect=_mocked_requests_get_status_case_2) |
59
|
|
|
def test_status_case_2(self, requests_mocked): |
60
|
|
|
# pylint: disable=unused-argument |
61
|
|
|
"""Test if link status is DOWN.""" |
62
|
|
|
link1 = get_link_mocked() |
63
|
|
|
link2 = get_link_mocked() |
64
|
|
|
link1.id = 'def' |
65
|
|
|
link2.id = 'abc' |
66
|
|
|
links = [link1, link2] |
67
|
|
|
current_path = Path(links) |
68
|
|
|
self.assertEqual(current_path.status, EntityStatus.DOWN) |
69
|
|
|
|
70
|
|
|
def test_status_case_3(self): |
71
|
|
|
"""Test if link status is DISABLED.""" |
72
|
|
|
links = [] |
73
|
|
|
current_path = Path(links) |
74
|
|
|
self.assertEqual(current_path.status, EntityStatus.DISABLED) |
75
|
|
|
|
76
|
|
|
# This method will be used by the mock to replace requests.get |
77
|
|
|
def _mocked_requests_get_status_case_4(self): |
78
|
|
|
# pylint: disable=no-self-use |
79
|
|
|
return MockResponse({'links': {'abc': {'active': True, |
80
|
|
|
'enabled': True}, |
81
|
|
|
'def': {'active': True, |
82
|
|
|
'enabled': True}}}, 200) |
83
|
|
|
|
84
|
|
|
@patch('requests.get', side_effect=_mocked_requests_get_status_case_4) |
85
|
|
|
def test_status_case_4(self, requests_mocked): |
86
|
|
|
# pylint: disable=unused-argument |
87
|
|
|
"""Test if link status is UP.""" |
88
|
|
|
link1 = get_link_mocked() |
89
|
|
|
link2 = get_link_mocked() |
90
|
|
|
link1.id = 'def' |
91
|
|
|
link2.id = 'abc' |
92
|
|
|
links = [link1, link2] |
93
|
|
|
current_path = Path(links) |
94
|
|
|
self.assertEqual(current_path.status, EntityStatus.UP) |
95
|
|
|
|
96
|
|
|
# This method will be used by the mock to replace requests.get |
97
|
|
|
def _mocked_requests_get_status_case_5(self): |
98
|
|
|
# pylint: disable=no-self-use |
99
|
|
|
return MockResponse({'links': {'abc': {'active': True, |
100
|
|
|
'enabled': True}, |
101
|
|
|
'def': {'active': False, |
102
|
|
|
'enabled': False}}}, 200) |
103
|
|
|
|
104
|
|
|
@patch('requests.get', side_effect=_mocked_requests_get_status_case_5) |
105
|
|
|
def test_status_case_5(self, requests_mocked): |
106
|
|
|
# pylint: disable=unused-argument |
107
|
|
|
"""Test if link status is UP.""" |
108
|
|
|
link1 = get_link_mocked() |
109
|
|
|
link2 = get_link_mocked() |
110
|
|
|
link1.id = 'def' |
111
|
|
|
link2.id = 'abc' |
112
|
|
|
links = [link1, link2] |
113
|
|
|
current_path = Path(links) |
114
|
|
|
self.assertEqual(current_path.status, EntityStatus.DISABLED) |
115
|
|
|
|
116
|
|
|
# This method will be used by the mock to replace requests.get |
117
|
|
|
def _mocked_requests_get_status_case_6(self): |
118
|
|
|
# pylint: disable=no-self-use |
119
|
|
|
return MockResponse({'links': {'abc': {'active': False, |
120
|
|
|
'enabled': False}, |
121
|
|
|
'def': {'active': False, |
122
|
|
|
'enabled': True}}}, 200) |
123
|
|
|
|
124
|
|
|
@patch('requests.get', side_effect=_mocked_requests_get_status_case_6) |
125
|
|
|
def test_status_case_6(self, requests_mocked): |
126
|
|
|
# pylint: disable=unused-argument |
127
|
|
|
"""Test if link status is UP.""" |
128
|
|
|
link1 = get_link_mocked() |
129
|
|
|
link2 = get_link_mocked() |
130
|
|
|
link1.id = 'def' |
131
|
|
|
link2.id = 'abc' |
132
|
|
|
links = [link1, link2] |
133
|
|
|
current_path = Path(links) |
134
|
|
|
self.assertEqual(current_path.status, EntityStatus.DISABLED) |
135
|
|
|
|
136
|
|
|
def test_compare_same_paths(self): |
137
|
|
|
"""Test compare paths with same links.""" |
138
|
|
|
links = [ |
139
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
140
|
|
|
metadata={"s_vlan": 5}), |
141
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
142
|
|
|
metadata={"s_vlan": 6}) |
143
|
|
|
] |
144
|
|
|
|
145
|
|
|
path_1 = Path(links) |
146
|
|
|
path_2 = Path(links) |
147
|
|
|
self.assertEqual(path_1, path_2) |
148
|
|
|
|
149
|
|
|
def test_compare_different_paths(self): |
150
|
|
|
"""Test compare paths with different links.""" |
151
|
|
|
links_1 = [ |
152
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
153
|
|
|
metadata={"s_vlan": 5}), |
154
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
155
|
|
|
metadata={"s_vlan": 6}) |
156
|
|
|
] |
157
|
|
|
links_2 = [ |
158
|
|
|
get_link_mocked(endpoint_a_port=12, endpoint_b_port=11, |
159
|
|
|
metadata={"s_vlan": 5}), |
160
|
|
|
get_link_mocked(endpoint_a_port=14, endpoint_b_port=16, |
161
|
|
|
metadata={"s_vlan": 11}) |
162
|
|
|
] |
163
|
|
|
|
164
|
|
|
path_1 = Path(links_1) |
165
|
|
|
path_2 = Path(links_2) |
166
|
|
|
self.assertNotEqual(path_1, path_2) |
167
|
|
|
|
168
|
|
|
def test_as_dict(self): |
169
|
|
|
"""Test path as dict.""" |
170
|
|
|
links = [ |
171
|
|
|
get_link_mocked(link_dict={"id": 3}), |
172
|
|
|
get_link_mocked(link_dict={"id": 2}) |
173
|
|
|
] |
174
|
|
|
|
175
|
|
|
current_path = Path(links) |
176
|
|
|
expected_dict = [{"id": 3}, {"id": 2}] |
177
|
|
|
self.assertEqual(expected_dict, current_path.as_dict()) |
178
|
|
|
|