1
|
|
|
"""Module to test the EVCBase class.""" |
2
|
|
|
import sys |
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import patch |
5
|
|
|
|
6
|
|
|
# pylint: disable=wrong-import-position |
7
|
|
|
sys.path.insert(0, '/var/lib/kytos/napps/..') |
8
|
|
|
# pylint: enable=wrong-import-position |
9
|
|
|
from napps.kytos.mef_eline.models import EVC # NOQA pycodestyle |
10
|
|
|
from napps.kytos.mef_eline.scheduler import \ |
11
|
|
|
CircuitSchedule # NOQA pycodestyle |
12
|
|
|
from tests.helpers import get_uni_mocked, \ |
13
|
|
|
get_controller_mock # NOQA pycodestyle |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class TestEVC(TestCase): # pylint: disable=too-many-public-methods |
17
|
|
|
"""Tests to verify EVC class.""" |
18
|
|
|
|
19
|
|
|
def test_attributes_empty(self): |
20
|
|
|
"""Test if the EVC raises an error with name is required.""" |
21
|
|
|
attributes = {"controller": get_controller_mock()} |
22
|
|
|
error_message = "name is required." |
23
|
|
|
with self.assertRaises(ValueError) as handle_error: |
24
|
|
|
EVC(**attributes) |
25
|
|
|
self.assertEqual(str(handle_error.exception), error_message) |
26
|
|
|
|
27
|
|
|
def test_without_uni_a(self): |
28
|
|
|
"""Test if the EVC raises and error with UNI A is required.""" |
29
|
|
|
attributes = {"controller": get_controller_mock(), |
30
|
|
|
"name": "circuit_name"} |
31
|
|
|
error_message = "uni_a is required." |
32
|
|
|
with self.assertRaises(ValueError) as handle_error: |
33
|
|
|
EVC(**attributes) |
34
|
|
|
self.assertEqual(str(handle_error.exception), error_message) |
35
|
|
|
|
36
|
|
|
def test_with_invalid_uni_a(self): |
37
|
|
|
"""Test if the EVC raises and error with invalid UNI A.""" |
38
|
|
|
attributes = { |
39
|
|
|
"controller": get_controller_mock(), |
40
|
|
|
"name": "circuit_name", |
41
|
|
|
"uni_a": get_uni_mocked(tag_value=82) |
42
|
|
|
} |
43
|
|
|
error_message = "VLAN tag 82 is not available in uni_a" |
44
|
|
|
with self.assertRaises(ValueError) as handle_error: |
45
|
|
|
EVC(**attributes) |
46
|
|
|
self.assertEqual(str(handle_error.exception), error_message) |
47
|
|
|
|
48
|
|
|
def test_without_uni_z(self): |
49
|
|
|
"""Test if the EVC raises and error with UNI Z is required.""" |
50
|
|
|
attributes = { |
51
|
|
|
"controller": get_controller_mock(), |
52
|
|
|
"name": "circuit_name", |
53
|
|
|
"uni_a": get_uni_mocked(is_valid=True) |
54
|
|
|
} |
55
|
|
|
error_message = "uni_z is required." |
56
|
|
|
with self.assertRaises(ValueError) as handle_error: |
57
|
|
|
EVC(**attributes) |
58
|
|
|
self.assertEqual(str(handle_error.exception), error_message) |
59
|
|
|
|
60
|
|
|
def test_with_invalid_uni_z(self): |
61
|
|
|
"""Test if the EVC raises and error with UNI Z is required.""" |
62
|
|
|
attributes = { |
63
|
|
|
"controller": get_controller_mock(), |
64
|
|
|
"name": "circuit_name", |
65
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
66
|
|
|
"uni_z": get_uni_mocked(tag_value=83) |
67
|
|
|
} |
68
|
|
|
error_message = "VLAN tag 83 is not available in uni_z" |
69
|
|
|
with self.assertRaises(ValueError) as handle_error: |
70
|
|
|
EVC(**attributes) |
71
|
|
|
self.assertEqual(str(handle_error.exception), error_message) |
72
|
|
|
|
73
|
|
|
def test_update_read_only(self): |
74
|
|
|
"""Test if raises an error when trying to update read only attr.""" |
75
|
|
|
attributes = { |
76
|
|
|
"controller": get_controller_mock(), |
77
|
|
|
"name": "circuit_name", |
78
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
79
|
|
|
"uni_z": get_uni_mocked(is_valid=True) |
80
|
|
|
} |
81
|
|
|
update_attr = [ |
82
|
|
|
("archived", True), |
83
|
|
|
("_id", True), |
84
|
|
|
("active", True), |
85
|
|
|
("current_path", []), |
86
|
|
|
("creation_time", "date") |
87
|
|
|
] |
88
|
|
|
|
89
|
|
|
for name, value in update_attr: |
90
|
|
|
with self.subTest(name=name, value=value): |
91
|
|
|
update_dict = {name: value} |
92
|
|
|
error_message = f"{name} can't be updated." |
93
|
|
|
with self.assertRaises(ValueError) as handle_error: |
94
|
|
|
evc = EVC(**attributes) |
95
|
|
|
evc.update(**update_dict) |
96
|
|
|
self.assertEqual(str(handle_error.exception), error_message) |
97
|
|
|
|
98
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.sync') |
99
|
|
|
def test_update_disable(self, _sync_mock): |
100
|
|
|
"""Test if evc is disabled.""" |
101
|
|
|
attributes = { |
102
|
|
|
"controller": get_controller_mock(), |
103
|
|
|
"name": "circuit_name", |
104
|
|
|
"enable": True, |
105
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
106
|
|
|
"uni_z": get_uni_mocked(is_valid=True) |
107
|
|
|
} |
108
|
|
|
update_dict = { |
109
|
|
|
"enable": False |
110
|
|
|
} |
111
|
|
|
evc = EVC(**attributes) |
112
|
|
|
evc.update(**update_dict) |
113
|
|
|
self.assertIs(evc.is_enabled(), False) |
114
|
|
|
|
115
|
|
|
def test_circuit_representation(self): |
116
|
|
|
"""Test the method __repr__.""" |
117
|
|
|
attributes = { |
118
|
|
|
"controller": get_controller_mock(), |
119
|
|
|
"name": "circuit_name", |
120
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
121
|
|
|
"uni_z": get_uni_mocked(is_valid=True) |
122
|
|
|
} |
123
|
|
|
evc = EVC(**attributes) |
124
|
|
|
expected_value = f'EVC({evc.id}, {evc.name})' |
125
|
|
|
self.assertEqual(str(evc), expected_value) |
126
|
|
|
|
127
|
|
|
def test_comparison_method(self): |
128
|
|
|
"""Test the method __eq__.""" |
129
|
|
|
attributes = { |
130
|
|
|
"controller": get_controller_mock(), |
131
|
|
|
"name": "circuit_name", |
132
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
133
|
|
|
"uni_z": get_uni_mocked(is_valid=True) |
134
|
|
|
} |
135
|
|
|
evc1 = EVC(**attributes) |
136
|
|
|
evc2 = EVC(**attributes) |
137
|
|
|
|
138
|
|
|
attributes = { |
139
|
|
|
"controller": get_controller_mock(), |
140
|
|
|
"name": "circuit_name_2", |
141
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
142
|
|
|
"uni_z": get_uni_mocked(is_valid=True) |
143
|
|
|
} |
144
|
|
|
evc3 = EVC(**attributes) |
145
|
|
|
evc4 = EVC(**attributes) |
146
|
|
|
|
147
|
|
|
self.assertEqual(evc1 == evc2, True) |
148
|
|
|
self.assertEqual(evc1 == evc3, False) |
149
|
|
|
self.assertEqual(evc2 == evc3, False) |
150
|
|
|
self.assertEqual(evc3 == evc4, True) |
151
|
|
|
|
152
|
|
|
def test_as_dict(self): |
153
|
|
|
"""Test the method as_dict.""" |
154
|
|
|
attributes = { |
155
|
|
|
"controller": get_controller_mock(), |
156
|
|
|
"id": "custom_id", |
157
|
|
|
"name": "custom_name", |
158
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
159
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
160
|
|
|
"start_date": '2018-08-21T18:44:54', |
161
|
|
|
"end_date": '2018-08-21T18:44:55', |
162
|
|
|
'primary_links': [], |
163
|
|
|
'request_time': '2018-08-21T19:10:41', |
164
|
|
|
'creation_time': '2018-08-21T18:44:54', |
165
|
|
|
'owner': "my_name", |
166
|
|
|
'circuit_scheduler': [ |
167
|
|
|
CircuitSchedule.from_dict({"id": 234243247, "action": "create", |
168
|
|
|
"frequency": "1 * * * *"}), |
169
|
|
|
CircuitSchedule.from_dict({"id": 234243239, "action": "create", |
170
|
|
|
"interval": {"hours": 2}}) |
171
|
|
|
], |
172
|
|
|
'enabled': True, |
173
|
|
|
'priority': 2 |
174
|
|
|
} |
175
|
|
|
evc = EVC(**attributes) |
176
|
|
|
|
177
|
|
|
expected_dict = { |
178
|
|
|
'id': 'custom_id', |
179
|
|
|
'name': 'custom_name', |
180
|
|
|
'uni_a': attributes['uni_a'].as_dict(), |
181
|
|
|
'uni_z': attributes['uni_z'].as_dict(), |
182
|
|
|
'start_date': '2018-08-21T18:44:54', |
183
|
|
|
'end_date': '2018-08-21T18:44:55', |
184
|
|
|
'bandwidth': 0, |
185
|
|
|
'primary_links': [], |
186
|
|
|
'backup_links': [], |
187
|
|
|
'current_path': [], |
188
|
|
|
'primary_path': [], |
189
|
|
|
'backup_path': [], |
190
|
|
|
'dynamic_backup_path': False, |
191
|
|
|
'request_time': '2018-08-21T19:10:41', |
192
|
|
|
'creation_time': '2018-08-21T18:44:54', |
193
|
|
|
'circuit_scheduler': [ |
194
|
|
|
{ |
195
|
|
|
"id": 234243247, |
196
|
|
|
"action": "create", |
197
|
|
|
"frequency": "1 * * * *" |
198
|
|
|
}, |
199
|
|
|
{ |
200
|
|
|
"id": 234243239, |
201
|
|
|
"action": "create", |
202
|
|
|
"interval": { |
203
|
|
|
"hours": 2 |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
], |
207
|
|
|
'active': False, |
208
|
|
|
'enabled': True, |
209
|
|
|
'priority': 2 |
210
|
|
|
} |
211
|
|
|
actual_dict = evc.as_dict() |
212
|
|
|
for name, value in expected_dict.items(): |
213
|
|
|
actual = actual_dict.get(name) |
214
|
|
|
self.assertEqual(value, actual) |
215
|
|
|
|