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