1
|
|
|
"""Tests for the main madule.""" |
2
|
|
|
|
3
|
1 |
|
from unittest.mock import patch, MagicMock |
4
|
1 |
|
from datetime import datetime, timedelta |
5
|
|
|
|
6
|
1 |
|
import pytz |
7
|
|
|
|
8
|
1 |
|
from kytos.lib.helpers import get_controller_mock, get_test_client |
9
|
1 |
|
from napps.kytos.maintenance.main import Main |
10
|
1 |
|
from napps.kytos.maintenance.models import MaintenanceWindow as MW |
11
|
1 |
|
from napps.kytos.maintenance.models import MaintenanceWindows |
12
|
|
|
|
13
|
1 |
|
TIME_FMT = "%Y-%m-%dT%H:%M:%S%z" |
14
|
|
|
|
15
|
|
|
|
16
|
1 |
|
class TestMain: |
17
|
|
|
"""Test the Main class of this NApp.""" |
18
|
|
|
|
19
|
|
|
# pylint: disable=too-many-public-methods |
20
|
|
|
|
21
|
1 |
|
def setup_method(self): |
22
|
|
|
"""Initialize before tests are executed.""" |
23
|
1 |
|
self.controller = get_controller_mock() |
24
|
1 |
|
self.scheduler = MagicMock() |
25
|
1 |
|
new_sched = 'napps.kytos.maintenance.models.Scheduler.new_scheduler' |
26
|
1 |
|
with patch(new_sched) as new_scheduler: |
27
|
1 |
|
new_scheduler.return_value = self.scheduler |
28
|
1 |
|
self.napp = Main(self.controller) |
29
|
1 |
|
self.api = get_test_client(self.controller, self.napp) |
30
|
1 |
|
self.maxDiff = None |
31
|
1 |
|
self.base_endpoint = "kytos/maintenance/v1" |
32
|
|
|
|
33
|
1 |
|
async def test_create_mw_case_1(self, event_loop): |
34
|
|
|
"""Test a successful case of the REST to create.""" |
35
|
1 |
|
self.napp.controller.loop = event_loop |
36
|
1 |
|
url = f'{self.base_endpoint}' |
37
|
1 |
|
start = datetime.now(pytz.utc) + timedelta(days=1) |
38
|
1 |
|
end = start + timedelta(hours=2) |
39
|
1 |
|
payload = { |
40
|
|
|
"start": start.strftime(TIME_FMT), |
41
|
|
|
"end": end.strftime(TIME_FMT), |
42
|
|
|
"switches": [ |
43
|
|
|
"00:00:00:00:00:00:02", |
44
|
|
|
], |
45
|
|
|
'interfaces': [ |
46
|
|
|
"00:00:00:00:00:00:00:03:3", |
47
|
|
|
], |
48
|
|
|
} |
49
|
|
|
response = await self.api.post(url, json=payload) |
50
|
1 |
|
assert response.status_code == 201 |
51
|
1 |
|
current_data = response.json() |
52
|
1 |
|
args, kwargs = self.scheduler.add.call_args |
53
|
1 |
|
window: MW = args[0] |
54
|
|
|
|
55
|
|
|
assert window.start == start.replace(microsecond=0) |
56
|
|
|
assert window.end == end.replace(microsecond=0) |
57
|
|
|
assert window.switches == ['00:00:00:00:00:00:02'] |
58
|
|
|
assert window.interfaces == ['00:00:00:00:00:00:00:03:3'] |
59
|
|
|
assert window.links == [] |
60
|
|
|
|
61
|
|
|
assert current_data == {'mw_id': window.id} |
62
|
|
|
|
63
|
1 |
|
async def test_create_mw_case_2(self, event_loop): |
64
|
|
|
"""Test a fail case of the REST to create a maintenance window.""" |
65
|
1 |
|
self.napp.controller.loop = event_loop |
66
|
|
|
url = f'{self.base_endpoint}' |
67
|
1 |
|
payload = { |
68
|
1 |
|
"switches": [ |
69
|
1 |
|
"00:00:00:00:00:00:02", |
70
|
|
|
], |
71
|
|
|
'interfaces': [ |
72
|
|
|
"00:00:00:00:00:00:00:03:3", |
73
|
|
|
], |
74
|
|
|
} |
75
|
|
|
response = await self.api.post(url, json=payload) |
76
|
|
|
assert response.status_code == 400 |
77
|
1 |
|
self.scheduler.add.assert_not_called() |
78
|
1 |
|
|
79
|
1 |
View Code Duplication |
async def test_create_mw_case_3(self, event_loop): |
|
|
|
|
80
|
|
|
"""Test a fail case of the REST to create a maintenance window.""" |
81
|
1 |
|
self.napp.controller.loop = event_loop |
82
|
|
|
url = f'{self.base_endpoint}' |
83
|
1 |
|
start = datetime.now(pytz.utc) - timedelta(days=1) |
84
|
1 |
|
end = start + timedelta(hours=2) |
85
|
1 |
|
payload = { |
86
|
1 |
|
"start": start.strftime(TIME_FMT), |
87
|
1 |
|
"end": end.strftime(TIME_FMT), |
88
|
|
|
"switches": [ |
89
|
|
|
"00:00:00:00:00:00:02", |
90
|
|
|
], |
91
|
|
|
'interfaces': [ |
92
|
|
|
"00:00:00:00:00:00:00:03:3", |
93
|
|
|
], |
94
|
|
|
} |
95
|
|
|
response = await self.api.post(url, json=payload) |
96
|
|
|
current_data = response.json() |
97
|
1 |
|
assert response.status_code == 400 |
98
|
1 |
|
assert current_data['description'] == \ |
99
|
1 |
|
'Start in the past not allowed' |
100
|
1 |
|
self.scheduler.add.assert_not_called() |
101
|
|
|
|
102
|
1 |
View Code Duplication |
async def test_create_mw_case_4(self, event_loop): |
|
|
|
|
103
|
|
|
"""Test a fail case of the REST to create a maintenance window.""" |
104
|
1 |
|
self.napp.controller.loop = event_loop |
105
|
|
|
url = f'{self.base_endpoint}' |
106
|
1 |
|
start = datetime.now(pytz.utc) + timedelta(days=1) |
107
|
1 |
|
end = start - timedelta(hours=2) |
108
|
1 |
|
payload = { |
109
|
1 |
|
"start": start.strftime(TIME_FMT), |
110
|
1 |
|
"end": end.strftime(TIME_FMT), |
111
|
|
|
"switches": [ |
112
|
|
|
"00:00:00:00:00:00:02", |
113
|
|
|
], |
114
|
|
|
'interfaces': [ |
115
|
|
|
"00:00:00:00:00:00:00:03:3", |
116
|
|
|
], |
117
|
|
|
} |
118
|
|
|
response = await self.api.post(url, json=payload) |
119
|
|
|
current_data = response.json() |
120
|
1 |
|
|
121
|
1 |
|
assert response.status_code == 400 |
122
|
|
|
assert current_data['description'] == \ |
123
|
1 |
|
'End before start not allowed' |
124
|
1 |
|
self.scheduler.add.assert_not_called() |
125
|
|
|
|
126
|
1 |
View Code Duplication |
async def test_create_mw_case_5(self, event_loop): |
|
|
|
|
127
|
|
|
"""Test a fail case of the REST to create a maintenance window.""" |
128
|
1 |
|
self.napp.controller.loop = event_loop |
129
|
|
|
url = f'{self.base_endpoint}' |
130
|
1 |
|
start = datetime.now(pytz.utc) + timedelta(days=1) |
131
|
1 |
|
end = start + timedelta(hours=2) |
132
|
1 |
|
payload = { |
133
|
1 |
|
'id': '1234', |
134
|
1 |
|
"start": start.strftime(TIME_FMT), |
135
|
1 |
|
"end": end.strftime(TIME_FMT), |
136
|
1 |
|
"switches": [ |
137
|
|
|
"00:00:00:00:00:00:02", |
138
|
1 |
|
], |
139
|
|
|
'interfaces': [ |
140
|
1 |
|
"00:00:00:00:00:00:00:03:3", |
141
|
1 |
|
], |
142
|
1 |
|
} |
143
|
1 |
|
response = await self.api.post(url, json=payload) |
144
|
1 |
|
current_data = response.json() |
145
|
1 |
|
|
146
|
|
|
assert response.status_code == 400 |
147
|
|
|
assert current_data['description'] == \ |
148
|
|
|
'Setting a maintenance id is not allowed' |
149
|
|
|
self.scheduler.add.assert_not_called() |
150
|
|
|
|
151
|
|
View Code Duplication |
async def test_create_mw_case_6(self, event_loop): |
|
|
|
|
152
|
|
|
"""Test a fail case of the REST to create a maintenance window.""" |
153
|
|
|
self.napp.controller.loop = event_loop |
154
|
|
|
url = f'{self.base_endpoint}' |
155
|
|
|
start = datetime.now(pytz.utc) + timedelta(days=1) |
156
|
|
|
end = start + timedelta(hours=2) |
157
|
|
|
payload = { |
158
|
|
|
'status': 'fun', |
159
|
|
|
"start": start.strftime(TIME_FMT), |
160
|
|
|
"end": end.strftime(TIME_FMT), |
161
|
|
|
"switches": [ |
162
|
|
|
"00:00:00:00:00:00:02", |
163
|
|
|
], |
164
|
|
|
'interfaces': [ |
165
|
|
|
"00:00:00:00:00:00:00:03:3", |
166
|
|
|
], |
167
|
|
|
} |
168
|
|
|
response = await self.api.post(url, json=payload) |
169
|
|
|
current_data = response.json() |
170
|
|
|
|
171
|
|
|
assert response.status_code == 400 |
172
|
|
|
assert current_data['description'] == \ |
173
|
|
|
'Setting a maintenance status is not allowed' |
174
|
|
|
self.scheduler.add.assert_not_called() |
175
|
|
|
|
176
|
1 |
|
async def test_get_mw_case_1(self): |
177
|
|
|
"""Test get all maintenance windows, empty list.""" |
178
|
|
|
self.scheduler.list_maintenances.return_value = MaintenanceWindows.construct(__root__ = []) |
179
|
|
|
url = f'{self.base_endpoint}' |
180
|
|
|
response = await self.api.get(url) |
181
|
|
|
current_data = response.json() |
182
|
|
|
assert response.status_code == 200 |
183
|
|
|
assert current_data == [] |
184
|
|
|
self.scheduler.list_maintenances.assert_called_once() |
185
|
|
|
|
186
|
|
|
async def test_get_mw_case_2(self): |
187
|
|
|
"""Test get all maintenance windows.""" |
188
|
|
|
now = datetime.now(pytz.utc) |
189
|
|
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
190
|
|
|
end1 = start1 + timedelta(hours=6) |
191
|
|
|
start2 = datetime.now(pytz.utc) + timedelta(hours=5) |
192
|
|
|
end2 = start2 + timedelta(hours=1, minutes=30) |
193
|
|
|
self.scheduler.list_maintenances.return_value = MaintenanceWindows.construct( |
194
|
|
|
__root__ = [ |
195
|
|
|
MW.construct( |
196
|
|
|
id = '1234', |
197
|
|
|
start = start1.replace(microsecond=0), |
198
|
|
|
end = end1.replace(microsecond=0), |
199
|
|
|
switches = [ |
200
|
|
|
'00:00:00:00:00:00:12:23' |
201
|
|
|
], |
202
|
|
|
description = '', |
203
|
|
|
links = [], |
204
|
|
|
interfaces = [], |
205
|
|
|
status = 'pending', |
206
|
|
|
updated_at = now.replace(microsecond=0), |
207
|
1 |
|
inserted_at = now.replace(microsecond=0), |
208
|
1 |
|
), |
209
|
1 |
|
MW.construct( |
210
|
1 |
|
id = '4567', |
211
|
1 |
|
start = start2.replace(microsecond=0), |
212
|
1 |
|
end = end2.replace(microsecond=0), |
213
|
|
|
switches = [ |
214
|
1 |
|
'12:34:56:78:90:ab:cd:ef' |
215
|
|
|
], |
216
|
1 |
|
description = '', |
217
|
1 |
|
links = [], |
218
|
1 |
|
interfaces = [], |
219
|
1 |
|
status = 'pending', |
220
|
1 |
|
updated_at = now.replace(microsecond=0), |
221
|
1 |
|
inserted_at = now.replace(microsecond=0), |
222
|
|
|
), |
223
|
1 |
|
]) |
224
|
|
|
mw_dict = [ |
225
|
1 |
|
{ |
226
|
|
|
'id': '1234', |
227
|
1 |
|
'start': start1.strftime(TIME_FMT), |
228
|
1 |
|
'end': end1.strftime(TIME_FMT), |
229
|
1 |
|
'switches': [ |
230
|
1 |
|
'00:00:00:00:00:00:12:23' |
231
|
|
|
], |
232
|
|
|
'description': '', |
233
|
|
|
'links': [], |
234
|
|
|
'interfaces': [], |
235
|
|
|
'status': 'pending', |
236
|
|
|
'updated_at': now.strftime(TIME_FMT), |
237
|
|
|
'inserted_at': now.strftime(TIME_FMT), |
238
|
|
|
}, |
239
|
|
|
{ |
240
|
1 |
|
'id': '4567', |
241
|
|
|
'start': start2.strftime(TIME_FMT), |
242
|
|
|
'end': end2.strftime(TIME_FMT), |
243
|
|
|
'switches': [ |
244
|
|
|
'12:34:56:78:90:ab:cd:ef' |
245
|
|
|
], |
246
|
|
|
'description': '', |
247
|
|
|
'links': [], |
248
|
|
|
'interfaces': [], |
249
|
|
|
'status': 'pending', |
250
|
|
|
'updated_at': now.strftime(TIME_FMT), |
251
|
|
|
'inserted_at': now.strftime(TIME_FMT), |
252
|
|
|
} |
253
|
|
|
] |
254
|
1 |
|
|
255
|
1 |
|
url = f'{self.base_endpoint}' |
256
|
1 |
|
response = await self.api.get(url) |
257
|
1 |
|
current_data = response.json() |
258
|
1 |
|
assert response.status_code == 200 |
259
|
1 |
|
assert current_data == mw_dict |
260
|
|
|
self.scheduler.list_maintenances.assert_called_once() |
261
|
1 |
|
|
262
|
|
|
async def test_get_mw_case_3(self): |
263
|
1 |
|
"""Test get non-existent id.""" |
264
|
1 |
|
self.scheduler.get_maintenance.return_value = None |
265
|
1 |
|
url = f'{self.base_endpoint}/2345' |
266
|
1 |
|
response = await self.api.get(url) |
267
|
1 |
|
current_data = response.json() |
268
|
1 |
|
assert response.status_code == 404 |
269
|
|
|
assert current_data['description'] == \ |
270
|
1 |
|
'Maintenance with id 2345 not found' |
271
|
1 |
|
self.scheduler.get_maintenance.assert_called_once_with('2345') |
272
|
|
|
|
273
|
1 |
|
async def test_get_mw_case_4(self): |
274
|
|
|
"""Test get existent id.""" |
275
|
1 |
|
now = datetime.now(pytz.utc) |
276
|
1 |
|
start2 = datetime.now(pytz.utc) + timedelta(hours=5) |
277
|
1 |
|
end2 = start2 + timedelta(hours=1, minutes=30) |
278
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
279
|
|
|
id = '4567', |
280
|
|
|
start = start2.replace(microsecond=0), |
281
|
|
|
end = end2.replace(microsecond=0), |
282
|
|
|
switches = [ |
283
|
|
|
'12:34:56:78:90:ab:cd:ef' |
284
|
|
|
], |
285
|
1 |
|
updated_at = now.replace(microsecond=0), |
286
|
1 |
|
inserted_at = now.replace(microsecond=0), |
287
|
1 |
|
) |
288
|
1 |
|
mw_dict = { |
289
|
1 |
|
'id': '4567', |
290
|
|
|
'start': start2.strftime(TIME_FMT), |
291
|
1 |
|
'end': end2.strftime(TIME_FMT), |
292
|
1 |
|
'switches': [ |
293
|
|
|
'12:34:56:78:90:ab:cd:ef' |
294
|
1 |
|
], |
295
|
|
|
'description': '', |
296
|
1 |
|
'links': [], |
297
|
1 |
|
'interfaces': [], |
298
|
1 |
|
'status': 'pending', |
299
|
|
|
'updated_at': now.strftime(TIME_FMT), |
300
|
|
|
'inserted_at': now.strftime(TIME_FMT), |
301
|
|
|
} |
302
|
|
|
url = f'{self.base_endpoint}/4567' |
303
|
|
|
response = await self.api.get(url) |
304
|
|
|
current_data = response.json() |
305
|
|
|
assert response.status_code == 200 |
306
|
|
|
assert current_data == mw_dict |
307
|
1 |
|
self.scheduler.get_maintenance.assert_called_once_with('4567') |
308
|
1 |
|
|
309
|
1 |
|
async def test_remove_mw_case_1(self): |
310
|
1 |
|
"""Test remove non-existent id.""" |
311
|
1 |
|
self.scheduler.get_maintenance.return_value = None |
312
|
|
|
url = f'{self.base_endpoint}/2345' |
313
|
1 |
|
response = await self.api.delete(url) |
314
|
1 |
|
current_data = response.json() |
315
|
|
|
assert response.status_code == 404 |
316
|
1 |
|
assert current_data['description'] == \ |
317
|
|
|
'Maintenance with id 2345 not found' |
318
|
1 |
|
self.scheduler.get_maintenance.assert_called_once_with('2345') |
319
|
1 |
|
self.scheduler.remove.assert_not_called() |
320
|
1 |
|
|
321
|
1 |
|
async def test_remove_mw_case_2(self): |
322
|
|
|
"""Test remove existent id.""" |
323
|
|
|
start1 = datetime.now(pytz.utc) + timedelta(hours=1) |
324
|
1 |
|
end1 = start1 + timedelta(hours=6) |
325
|
1 |
|
self.scheduler.get_maintenance.return_value = MW.construct( |
326
|
1 |
|
id = '1234', |
327
|
1 |
|
start = start1.replace(microsecond=0), |
328
|
1 |
|
end = end1.replace(microsecond=0), |
329
|
|
|
switches = [ |
330
|
1 |
|
'00:00:00:00:00:00:12:23' |
331
|
|
|
], |
332
|
1 |
|
) |
333
|
|
|
url = f'{self.base_endpoint}/1234' |
334
|
1 |
|
response = await self.api.delete(url) |
335
|
1 |
|
current_data = response.json() |
336
|
1 |
|
assert response.status_code == 200 |
337
|
|
|
assert current_data == {'response': 'Maintenance with id 1234 ' \ |
338
|
|
|
'successfully removed'} |
339
|
1 |
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
340
|
1 |
|
self.scheduler.remove.assert_called_once_with('1234') |
341
|
1 |
|
|
342
|
1 |
View Code Duplication |
async def test_remove_mw_case_3(self): |
|
|
|
|
343
|
1 |
|
"""Test remove existent id.""" |
344
|
|
|
start1 = datetime.now(pytz.utc) - timedelta(days=1) |
345
|
1 |
|
end1 = start1 + timedelta(hours=6) |
346
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
347
|
1 |
|
id = '1234', |
348
|
1 |
|
start = start1.replace(microsecond=0), |
349
|
1 |
|
end = end1.replace(microsecond=0), |
350
|
1 |
|
switches = [ |
351
|
|
|
'00:00:00:00:00:00:12:23' |
352
|
|
|
], |
353
|
|
|
status = 'running', |
354
|
|
|
) |
355
|
|
|
url = f'{self.base_endpoint}/1234' |
356
|
|
|
response = await self.api.delete(url) |
357
|
|
|
current_data = response.json() |
358
|
1 |
|
assert response.status_code == 400 |
359
|
1 |
|
assert current_data['description'] == \ |
360
|
|
|
'Deleting a running maintenance is not allowed' |
361
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
362
|
1 |
|
self.scheduler.remove.assert_not_called() |
363
|
1 |
|
|
364
|
1 |
|
async def test_update_mw_case_1(self, event_loop): |
365
|
1 |
|
"""Test update non-existent id.""" |
366
|
|
|
self.napp.controller.loop = event_loop |
367
|
1 |
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
368
|
1 |
|
self.scheduler.get_maintenance.return_value = None |
369
|
1 |
|
payload = { |
370
|
|
|
"start": start1.strftime(TIME_FMT), |
371
|
|
|
} |
372
|
|
|
url = f'{self.base_endpoint}/2345' |
373
|
|
|
response = await self.api.patch(url, json=payload) |
374
|
|
|
current_data = response.json() |
375
|
|
|
assert response.status_code == 404 |
376
|
|
|
assert current_data['description'] == \ |
377
|
|
|
'Maintenance with id 2345 not found' |
378
|
|
|
self.scheduler.update.assert_not_called() |
379
|
|
|
|
380
|
1 |
|
async def test_update_mw_case_2(self, event_loop): |
381
|
|
|
"""Test update no data.""" |
382
|
1 |
|
self.napp.controller.loop = event_loop |
383
|
1 |
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
384
|
1 |
|
payload = { |
385
|
1 |
|
"start": start1.strftime(TIME_FMT), |
386
|
|
|
} |
387
|
|
|
url = f'{self.base_endpoint}/1234' |
388
|
|
|
response = await self.api.patch(url, json=payload) |
389
|
|
|
current_data = response.json() |
390
|
|
|
assert response.status_code == 400 |
391
|
|
|
assert "field required" in current_data['description'] |
392
|
|
|
|
393
|
1 |
|
async def test_update_mw_case_3(self, event_loop): |
394
|
1 |
|
"""Test successful update.""" |
395
|
|
|
self.napp.controller.loop = event_loop |
396
|
|
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
397
|
1 |
|
end1 = start1 + timedelta(hours=6) |
398
|
1 |
|
self.scheduler.get_maintenance.return_value = MW.construct( |
399
|
1 |
|
id = '1234', |
400
|
1 |
|
start = start1.replace(microsecond=0), |
401
|
1 |
|
end = end1.replace(microsecond=0), |
402
|
|
|
switches = [ |
403
|
1 |
|
'00:00:00:00:00:00:12:23' |
404
|
1 |
|
], |
405
|
|
|
) |
406
|
1 |
|
start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3) |
407
|
|
|
payload = { |
408
|
1 |
|
"start": start_new.strftime(TIME_FMT), |
409
|
1 |
|
} |
410
|
1 |
|
url = f'{self.base_endpoint}/1234' |
411
|
1 |
|
response = await self.api.patch(url, json=payload) |
412
|
|
|
current_data = response.json() |
413
|
|
|
assert current_data == \ |
414
|
|
|
{'response': 'Maintenance 1234 updated'} |
415
|
|
|
assert response.status_code == 200 |
416
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
417
|
|
|
self.scheduler.update.assert_called_once_with( |
418
|
|
|
MW.construct( |
419
|
1 |
|
id = '1234', |
420
|
1 |
|
start = start_new.replace(microsecond=0), |
421
|
1 |
|
end = end1.replace(microsecond=0), |
422
|
|
|
switches = [ |
423
|
|
|
'00:00:00:00:00:00:12:23' |
424
|
|
|
], |
425
|
1 |
|
) |
426
|
1 |
|
) |
427
|
1 |
|
|
428
|
1 |
|
async def test_update_mw_case_4(self, event_loop): |
429
|
1 |
|
"""Test successful update.""" |
430
|
|
|
self.napp.controller.loop = event_loop |
431
|
1 |
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
432
|
1 |
|
end1 = start1 + timedelta(hours=6) |
433
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
434
|
1 |
|
id = '1234', |
435
|
|
|
start = start1.replace(microsecond=0), |
436
|
1 |
|
end = end1.replace(microsecond=0), |
437
|
1 |
|
switches = [ |
438
|
1 |
|
'00:00:00:00:00:00:12:23' |
439
|
1 |
|
], |
440
|
|
|
) |
441
|
|
|
start_new = datetime.now(pytz.utc) - timedelta(days=1, hours=3) |
442
|
|
|
payload = { |
443
|
|
|
"start": start_new.strftime(TIME_FMT), |
444
|
|
|
} |
445
|
|
|
url = f'{self.base_endpoint}/1234' |
446
|
|
|
response = await self.api.patch(url, json=payload) |
447
|
1 |
|
current_data = response.json() |
448
|
1 |
|
assert response.status_code == 400 |
449
|
|
|
assert current_data['description'] == \ |
450
|
|
|
'Start in the past not allowed' |
451
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
452
|
|
|
self.scheduler.update.assert_not_called() |
453
|
|
|
|
454
|
|
|
async def test_update_mw_case_5(self, event_loop): |
455
|
1 |
|
"""Test successful update.""" |
456
|
1 |
|
self.napp.controller.loop = event_loop |
457
|
1 |
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
458
|
1 |
|
end1 = start1 + timedelta(hours=6) |
459
|
1 |
|
self.scheduler.get_maintenance.return_value = MW.construct( |
460
|
|
|
id = '1234', |
461
|
1 |
|
start = start1.replace(microsecond=0), |
462
|
1 |
|
end = end1.replace(microsecond=0), |
463
|
|
|
switches = [ |
464
|
1 |
|
'00:00:00:00:00:00:12:23' |
465
|
|
|
], |
466
|
1 |
|
) |
467
|
1 |
|
start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3) |
468
|
1 |
|
end_new = start_new - timedelta(hours=5) |
469
|
1 |
|
payload = { |
470
|
|
|
"start": start_new.strftime(TIME_FMT), |
471
|
|
|
"end": end_new.strftime(TIME_FMT) |
472
|
|
|
} |
473
|
|
|
url = f'{self.base_endpoint}/1234' |
474
|
|
|
response = await self.api.patch(url, json=payload) |
475
|
|
|
current_data = response.json() |
476
|
|
|
assert response.status_code == 400 |
477
|
1 |
|
assert current_data['description'] == \ |
478
|
|
|
'End before start not allowed' |
479
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
480
|
1 |
|
self.scheduler.update.assert_not_called() |
481
|
1 |
|
|
482
|
1 |
|
async def test_update_mw_case_6(self, event_loop): |
483
|
1 |
|
"""Test successful update.""" |
484
|
1 |
|
self.napp.controller.loop = event_loop |
485
|
|
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
486
|
1 |
|
end1 = start1 + timedelta(hours=6) |
487
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
488
|
1 |
|
id = '1234', |
489
|
|
|
start = start1.replace(microsecond=0), |
490
|
1 |
|
end = end1.replace(microsecond=0), |
491
|
1 |
|
switches = [ |
492
|
1 |
|
'00:00:00:00:00:00:12:23' |
493
|
1 |
|
], |
494
|
1 |
|
) |
495
|
1 |
|
start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3) |
496
|
|
|
payload = { |
497
|
|
|
"start": start_new.strftime(TIME_FMT), |
498
|
1 |
|
"switches": [], |
499
|
|
|
'interfaces': [], |
500
|
1 |
|
'links': [], |
501
|
1 |
|
} |
502
|
1 |
|
|
503
|
|
|
url = f'{self.base_endpoint}/1234' |
504
|
|
|
response = await self.api.patch(url, json=payload) |
505
|
|
|
current_data = response.json() |
506
|
|
|
assert response.status_code == 400 |
507
|
|
|
assert current_data['description'] == \ |
508
|
|
|
'At least one item must be provided' |
509
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
510
|
|
|
self.scheduler.update.assert_not_called() |
511
|
1 |
|
|
512
|
1 |
View Code Duplication |
async def test_update_mw_case_7(self, event_loop): |
|
|
|
|
513
|
1 |
|
"""Test successful update.""" |
514
|
1 |
|
self.napp.controller.loop = event_loop |
515
|
1 |
|
start1 = datetime.now(pytz.utc) + timedelta(days=1) |
516
|
1 |
|
end1 = start1 + timedelta(hours=6) |
517
|
1 |
|
self.scheduler.get_maintenance.return_value = MW.construct( |
518
|
|
|
id = '1234', |
519
|
|
|
start = start1.replace(microsecond=0), |
520
|
1 |
|
end = end1.replace(microsecond=0), |
521
|
|
|
switches = [ |
522
|
1 |
|
'00:00:00:00:00:00:12:23' |
523
|
1 |
|
], |
524
|
1 |
|
) |
525
|
|
|
payload = { |
526
|
|
|
'status': 'running', |
527
|
|
|
} |
528
|
|
|
url = f'{self.base_endpoint}/1234' |
529
|
|
|
response = await self.api.patch(url, json=payload) |
530
|
|
|
current_data = response.json() |
531
|
|
|
assert response.status_code == 400 |
532
|
|
|
assert current_data['description'] == \ |
533
|
1 |
|
'Updating a maintenance status is not allowed' |
534
|
1 |
|
self.scheduler.update.assert_not_called() |
535
|
1 |
|
|
536
|
1 |
|
async def test_end_mw_case_1(self): |
537
|
1 |
|
"""Test method that finishes the maintenance now.""" |
538
|
1 |
|
self.scheduler.get_maintenance.return_value = None |
539
|
1 |
|
url = f'{self.base_endpoint}/2345/end' |
540
|
|
|
response = await self.api.patch(url) |
541
|
|
|
current_data = response.json() |
542
|
1 |
|
assert response.status_code == 404 |
543
|
|
|
assert current_data['description'] == \ |
544
|
1 |
|
'Maintenance with id 2345 not found' |
545
|
1 |
|
|
546
|
1 |
View Code Duplication |
async def test_end_mw_case_2(self): |
|
|
|
|
547
|
|
|
"""Test method that finishes the maintenance now.""" |
548
|
|
|
start1 = datetime.now(pytz.utc) - timedelta(hours=1) |
549
|
|
|
end1 = start1 + timedelta(hours=6) |
550
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
551
|
|
|
id = '1234', |
552
|
|
|
start = start1.replace(microsecond=0), |
553
|
|
|
end = end1.replace(microsecond=0), |
554
|
|
|
switches = [ |
555
|
1 |
|
'00:00:00:00:00:00:12:23' |
556
|
1 |
|
], |
557
|
1 |
|
status = 'running', |
558
|
1 |
|
) |
559
|
1 |
|
url = f'{self.base_endpoint}/1234/end' |
560
|
1 |
|
response = await self.api.patch(url) |
561
|
1 |
|
current_data = response.json() |
562
|
|
|
self.scheduler.get.asssert_called_once_with('1234') |
563
|
|
|
self.scheduler.end_maintenance_early.assert_called_once_with('1234') |
564
|
1 |
|
assert response.status_code == 200 |
565
|
|
|
assert current_data == \ |
566
|
1 |
|
{'response': 'Maintenance window 1234 finished'} |
567
|
1 |
|
|
568
|
1 |
View Code Duplication |
async def test_end_mw_case_3(self): |
|
|
|
|
569
|
1 |
|
"""Test method that finishes the maintenance now.""" |
570
|
|
|
start1 = datetime.now(pytz.utc) + timedelta(hours=1) |
571
|
|
|
end1 = start1 + timedelta(hours=6) |
572
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
573
|
|
|
id = '1234', |
574
|
|
|
start = start1.replace(microsecond=0), |
575
|
|
|
end = end1.replace(microsecond=0), |
576
|
|
|
switches = [ |
577
|
|
|
'00:00:00:00:00:00:12:23' |
578
|
1 |
|
], |
579
|
1 |
|
status = 'pending', |
580
|
|
|
) |
581
|
|
|
url = f'{self.base_endpoint}/1234/end' |
582
|
1 |
|
response = await self.api.patch(url) |
583
|
1 |
|
current_data = response.json() |
584
|
1 |
|
self.scheduler.get.asssert_called_once_with('1234') |
585
|
1 |
|
self.scheduler.end_maintenance_early.assert_not_called() |
586
|
|
|
assert response.status_code == 400 |
587
|
|
|
assert current_data['description'] == \ |
588
|
|
|
'Maintenance window 1234 has not yet started' |
589
|
|
|
|
590
|
|
View Code Duplication |
async def test_end_mw_case_4(self): |
|
|
|
|
591
|
|
|
"""Test method that finishes the maintenance now.""" |
592
|
|
|
start1 = datetime.now(pytz.utc) - timedelta(hours=5) |
593
|
|
|
end1 = start1 + timedelta(hours=4) |
594
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
595
|
1 |
|
id = '1234', |
596
|
|
|
start = start1.replace(microsecond=0), |
597
|
1 |
|
end = end1.replace(microsecond=0), |
598
|
1 |
|
switches = [ |
599
|
1 |
|
'00:00:00:00:00:00:12:23' |
600
|
1 |
|
], |
601
|
1 |
|
status = 'finished', |
602
|
1 |
|
) |
603
|
1 |
|
url = f'{self.base_endpoint}/1234/end' |
604
|
|
|
response = await self.api.patch(url) |
605
|
1 |
|
current_data = response.json() |
606
|
|
|
self.scheduler.get.asssert_called_once_with('1234') |
607
|
1 |
|
self.scheduler.end_maintenance_early.assert_not_called() |
608
|
1 |
|
assert response.status_code == 400 |
609
|
1 |
|
assert current_data['description'] == \ |
610
|
|
|
'Maintenance window 1234 has already finished' |
611
|
|
|
|
612
|
1 |
|
async def test_extend_case_1(self, event_loop): |
613
|
1 |
|
"""Test successful extension.""" |
614
|
1 |
|
self.napp.controller.loop = event_loop |
615
|
1 |
|
start1 = datetime.now(pytz.utc) - timedelta(hours=3) |
616
|
|
|
end1 = start1 + timedelta(hours=4) |
617
|
1 |
|
self.scheduler.get_maintenance.return_value = MW.construct( |
618
|
|
|
id = '1234', |
619
|
1 |
|
start = start1.replace(microsecond=0), |
620
|
|
|
end = end1.replace(microsecond=0), |
621
|
1 |
|
switches = [ |
622
|
1 |
|
'00:00:00:00:00:00:12:23' |
623
|
1 |
|
], |
624
|
|
|
status = 'running', |
625
|
|
|
) |
626
|
1 |
|
url = f'{self.base_endpoint}/1234/extend' |
627
|
1 |
|
payload = { |
628
|
1 |
|
'minutes': 45 |
629
|
1 |
|
} |
630
|
|
|
response = await self.api.patch(url, json=payload) |
631
|
1 |
|
assert response.status_code == 200 |
632
|
|
|
self.scheduler.get_maintenance.called_with('1234') |
633
|
1 |
|
self.scheduler.update.assert_called_with(MW.construct( |
634
|
|
|
id = '1234', |
635
|
1 |
|
start = start1.replace(microsecond=0), |
636
|
1 |
|
end = end1.replace(microsecond=0) + timedelta(minutes=45), |
637
|
1 |
|
switches = [ |
638
|
1 |
|
'00:00:00:00:00:00:12:23' |
639
|
|
|
], |
640
|
|
|
status = 'running', |
641
|
|
|
)) |
642
|
|
|
|
643
|
|
|
async def test_extend_case_2(self, event_loop): |
644
|
|
|
"""Test no payload error.""" |
645
|
|
|
self.napp.controller.loop = event_loop |
646
|
|
|
url = f'{self.base_endpoint}/1234/extend' |
647
|
1 |
|
response = await self.api.patch(url) |
648
|
1 |
|
assert response.status_code == 400 |
649
|
|
|
current_data = response.json() |
650
|
|
|
assert 'Invalid json' in current_data['description'] |
651
|
1 |
|
self.scheduler.update.assert_not_called() |
652
|
1 |
|
|
653
|
1 |
|
async def test_extend_case_3(self, event_loop): |
654
|
1 |
|
"""Test payload without minutes.""" |
655
|
|
|
self.napp.controller.loop = event_loop |
656
|
1 |
|
url = f'{self.base_endpoint}/1234/extend' |
657
|
1 |
|
payload = { |
658
|
|
|
'seconds': 240 |
659
|
1 |
|
} |
660
|
|
|
response = await self.api.patch(url, json=payload) |
661
|
1 |
|
assert response.status_code == 400 |
662
|
1 |
|
current_data = response.json() |
663
|
1 |
|
assert current_data['description'] == \ |
664
|
1 |
|
'Minutes of extension must be sent' |
665
|
|
|
self.scheduler.update.assert_not_called() |
666
|
|
|
|
667
|
|
|
async def test_extend_case_4(self, event_loop): |
668
|
|
|
"""Test no integer extension minutes.""" |
669
|
|
|
self.napp.controller.loop = event_loop |
670
|
|
|
url = f'{self.base_endpoint}/1234/extend' |
671
|
|
|
payload = { |
672
|
|
|
'minutes': '240' |
673
|
1 |
|
} |
674
|
1 |
|
response = await self.api.patch(url, json=payload) |
675
|
|
|
assert response.status_code == 400 |
676
|
|
|
current_data = response.json() |
677
|
1 |
|
assert current_data['description'] == \ |
678
|
1 |
|
'Minutes of extension must be integer' |
679
|
1 |
|
self.scheduler.update.assert_not_called() |
680
|
1 |
|
|
681
|
|
View Code Duplication |
async def test_extend_case_5(self, event_loop): |
|
|
|
|
682
|
1 |
|
"""Test maintenance did not start.""" |
683
|
1 |
|
self.napp.controller.loop = event_loop |
684
|
|
|
start1 = datetime.now(pytz.utc) + timedelta(hours=3) |
685
|
1 |
|
end1 = start1 + timedelta(hours=4) |
686
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
687
|
1 |
|
id = '1234', |
688
|
1 |
|
start = start1.replace(microsecond=0), |
689
|
1 |
|
end = end1.replace(microsecond=0), |
690
|
1 |
|
switches = [ |
691
|
|
|
'00:00:00:00:00:00:12:23' |
692
|
|
|
], |
693
|
1 |
|
status = 'pending', |
694
|
1 |
|
) |
695
|
1 |
|
url = f'{self.base_endpoint}/1234/extend' |
696
|
1 |
|
payload = { |
697
|
|
|
'minutes': 240 |
698
|
1 |
|
} |
699
|
1 |
|
response = await self.api.patch(url, json=payload) |
700
|
|
|
assert response.status_code == 400 |
701
|
|
|
current_data = response.json() |
702
|
|
|
assert current_data['description'] == \ |
703
|
|
|
'Maintenance window 1234 has not yet started' |
704
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
705
|
|
|
self.scheduler.update.assert_not_called() |
706
|
|
|
|
707
|
|
View Code Duplication |
async def test_extend_case_6(self, event_loop): |
|
|
|
|
708
|
|
|
"""Test maintenance already finished.""" |
709
|
|
|
self.napp.controller.loop = event_loop |
710
|
|
|
start1 = datetime.now(pytz.utc) - timedelta(hours=3) |
711
|
|
|
end1 = start1 + timedelta(hours=2) |
712
|
|
|
self.scheduler.get_maintenance.return_value = MW.construct( |
713
|
|
|
id = '1234', |
714
|
|
|
start = start1.replace(microsecond=0), |
715
|
|
|
end = end1.replace(microsecond=0), |
716
|
|
|
switches = [ |
717
|
|
|
'00:00:00:00:00:00:12:23' |
718
|
|
|
], |
719
|
|
|
status = 'finished', |
720
|
|
|
) |
721
|
|
|
url = f'{self.base_endpoint}/1234/extend' |
722
|
|
|
payload = { |
723
|
|
|
'minutes': 240 |
724
|
|
|
} |
725
|
|
|
response = await self.api.patch(url, json=payload) |
726
|
|
|
assert response.status_code == 400 |
727
|
|
|
current_data = response.json() |
728
|
|
|
assert current_data['description'] == \ |
729
|
|
|
'Maintenance window 1234 has already finished' |
730
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1234') |
731
|
|
|
self.scheduler.update.assert_not_called() |
732
|
|
|
|
733
|
|
|
async def test_extend_case_7(self, event_loop): |
734
|
|
|
"""Test no maintenace found.""" |
735
|
|
|
self.napp.controller.loop = event_loop |
736
|
|
|
self.scheduler.get_maintenance.return_value = None |
737
|
|
|
url = f'{self.base_endpoint}/1235/extend' |
738
|
|
|
payload = { |
739
|
|
|
'minutes': 240 |
740
|
|
|
} |
741
|
|
|
response = await self.api.patch(url, json=payload) |
742
|
|
|
assert response.status_code == 404 |
743
|
|
|
current_data = response.json() |
744
|
|
|
assert current_data['description'] == \ |
745
|
|
|
'Maintenance with id 1235 not found' |
746
|
|
|
self.scheduler.get_maintenance.assert_called_once_with('1235') |
747
|
|
|
self.scheduler.update.assert_not_called() |
748
|
|
|
|