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