-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_frontend.py
More file actions
609 lines (507 loc) · 21.6 KB
/
Copy pathtest_frontend.py
File metadata and controls
609 lines (507 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
from __future__ import annotations
from unittest import mock
import pytest
import workflows.frontend
from workflows.services.common_service import CommonService
### Helper classes used in tests ##############################################
class ServiceCrashingOnInit(CommonService):
"""A service that raises an unhandled exception."""
@staticmethod
def initializing():
"""Raise AssertionError.
This should set the error state, kill the service and cause the frontend
to leave its main loop."""
assert False # pragma: no cover
class MockPipe:
"""An object that behaves like a pipe."""
def __init__(self, contents):
"""Load up contents into pipe."""
self.contents = contents
@staticmethod
def poll(time=None):
"""Check if pipe is empty. There is always something there, either
some content or an EOFError."""
return True
def recv(self):
"""Return first item off the list or raise exception.
Call callback function if defined and pipe is emptied."""
if not self.contents:
raise EOFError("Pipe is empty")
return_value = self.contents.pop(0)
if isinstance(return_value, Exception):
raise return_value
return return_value
@staticmethod
def close():
"""This pipe can't be written to anyway. Ignore call."""
def assert_empty(self):
"""Pipe must not contain any valid messages. Exceptions may remain."""
assert all(isinstance(m, Exception) for m in self.contents)
def assert_single_call_only(target):
def wrapper(*args, **kwargs):
if hasattr(wrapper, "called"):
raise Exception("Only a single call to object %s is allowed" % str(target))
setattr(wrapper, "called", True)
return target(*args, **kwargs)
return wrapper
###############################################################################
@mock.patch("workflows.frontend.multiprocessing")
@mock.patch("workflows.frontend.workflows.transport")
def test_frontend_connects_to_transport_layer(mock_transport, mock_mp):
"""Frontend should call connect method on transport layer module and subscribe to a unique command queue."""
workflows.frontend.Frontend()
mock_transport.lookup.assert_called_once_with(None)
mock_transport.lookup.return_value.assert_called_once_with()
mock_transport.lookup.return_value.return_value.connect.assert_called_once_with()
workflows.frontend.Frontend(transport="some_transport")
mock_transport.lookup.assert_called_with("some_transport")
mock_trn = mock.Mock()
workflows.frontend.Frontend(mock_trn)
mock_trn.assert_called_once_with()
mock_trn.return_value.connect.assert_called_once_with()
@mock.patch("workflows.frontend.workflows.transport")
def test_frontend_subscribes_to_command_channel(mock_transport):
"""Frontend should call connect method on transport layer module and subscribe to a unique command queue."""
transport = mock_transport.lookup.return_value.return_value
fe = workflows.frontend.Frontend()
mock_transport.lookup.assert_called_once_with(None)
transport.connect.assert_called_once()
transport.subscribe.assert_not_called()
def frontend_with_message(message):
transport.reset_mock()
fe = workflows.frontend.Frontend(
transport_command_channel=mock.sentinel.command
)
transport.subscribe_broadcast.assert_called_once_with(
mock.sentinel.command, mock.ANY
)
transport.subscribe_broadcast.call_args[0][1]({}, message)
return fe
assert frontend_with_message({}).shutdown is False
assert frontend_with_message({"command": "shutdown"}).shutdown is False
assert (
frontend_with_message(
{"command": "shutdown", "host": fe.get_host_id()}
).shutdown
is True
)
assert (
frontend_with_message(
{"command": "shutdown", "host": mock.sentinel.different_host}
).shutdown
is False
)
assert (
frontend_with_message(
{"command": "shutdown", "service": fe.get_status()["serviceclass"]}
).shutdown
is True
)
assert (
frontend_with_message(
{"command": "shutdown", "service": mock.sentinel.different_service}
).shutdown
is False
)
@mock.patch("workflows.frontend.multiprocessing")
@mock.patch("workflows.frontend.workflows.transport")
def test_start_service_in_frontend(mock_transport, mock_mp):
"""Check that the service is being run and connected to the frontend via the correct pipes."""
mock_service = mock.Mock()
pipes = [mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock()]
mock_mp.Pipe.side_effect = [(pipes[0], pipes[1]), (pipes[2], pipes[3]), None]
# initialize frontend
environment = {mock.sentinel.foo: mock.sentinel.bar}
fe = workflows.frontend.Frontend(environment=environment)
# start service
fe.switch_service(mock_service)
# check service was started properly
mock_service.assert_called_once_with(environment=environment)
mock_service.return_value.connect.assert_called_once_with(
commands=pipes[0], frontend=pipes[3]
)
mock_mp.Process.assert_called_once_with(
target=mock_service.return_value.start, args=(), kwargs=mock.ANY
)
mock_mp.Process.return_value.start.assert_called_once()
# Fun with python multiprocessing:
# Because the pipe file descriptors are copied they must be closed in the process not using them
pipes[0].close.assert_called_once_with()
pipes[3].close.assert_called_once_with()
@mock.patch("workflows.frontend.workflows.transport")
def test_get_frontend_status(mock_transport):
"""Check that the get_status-method works and contains the correct host-id."""
fe = workflows.frontend.Frontend()
status = fe.get_status()
assert status["host"] == fe.get_host_id()
@pytest.mark.timeout(3)
def test_frontend_can_handle_unhandled_service_initialization_exceptions():
"""When a service crashes on initialization an exception should be thrown."""
transport = mock.Mock()
fe = workflows.frontend.Frontend(
transport=transport, service=assert_single_call_only(ServiceCrashingOnInit)
)
transport = transport.return_value
transport.connect.assert_called_once()
with pytest.raises(workflows.Error):
fe.run()
status_list = [
args[0].get("status")
for args, kwargs in transport.broadcast_status.call_args_list
if args
]
assert status_list == [
CommonService.SERVICE_STATUS_NEW,
CommonService.SERVICE_STATUS_STARTING,
CommonService.SERVICE_STATUS_ERROR,
CommonService.SERVICE_STATUS_END,
CommonService.SERVICE_STATUS_TEARDOWN,
]
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_can_handle_service_initialization_segfaults(mock_mp):
"""When a service crashes on initialization an exception should be thrown."""
transport = mock.Mock()
service = mock.Mock()
service_process = mock.Mock()
dummy_pipe = mock.Mock()
dummy_pipe.recv.side_effect = EOFError() # Dead on arrival
mock_mp.Pipe.return_value = (dummy_pipe, dummy_pipe)
mock_mp.Process.return_value = service_process
fe = workflows.frontend.Frontend(transport=transport, service=service)
transport = transport.return_value
transport.connect.assert_called_once()
mock_mp.Process.assert_called_once_with(
target=service.return_value.start, args=(), kwargs=mock.ANY
)
with pytest.raises(workflows.Error):
fe.run()
service_process.start.assert_called()
service_process.join.assert_called()
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_terminates_on_transport_disconnection(mock_mp):
"""When the transport connection is lost permanently, the frontend should stop."""
transport = mock.Mock()
service = mock.Mock()
service_process = mock.Mock()
dummy_pipe = mock.Mock()
dummy_pipe.poll.side_effect = [
False,
Exception("Frontend did not terminate on transport disconnect"),
]
mock_mp.Pipe.return_value = (dummy_pipe, dummy_pipe)
mock_mp.Process.return_value = service_process
service_process.is_alive.return_value = True
fe = workflows.frontend.Frontend(transport=transport, service=service)
transport = transport.return_value
transport.connect.assert_called_once()
transport.is_connected.return_value = False
mock_mp.Process.assert_called_once_with(
target=service.return_value.start, args=(), kwargs=mock.ANY
)
with pytest.raises(workflows.Error):
fe.run()
service_process.terminate.assert_called()
service_process.join.assert_called()
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_parses_status_updates(mock_mp):
"""The frontend should forward status updates to the advertiser thread when appropriate."""
transport = mock.Mock()
service_process = mock.Mock()
mock_mp.Process.return_value = service_process
dummy_pipe = MockPipe(
[
{"band": "status_update", "statuscode": CommonService.SERVICE_STATUS_NEW},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_STARTING,
},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_PROCESSING,
},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_SHUTDOWN,
},
{"band": "status_update", "statuscode": CommonService.SERVICE_STATUS_END},
]
)
mock_mp.Pipe.return_value = (dummy_pipe, dummy_pipe)
fe = workflows.frontend.Frontend(transport=transport, service=mock.Mock())
# intercept status code updates
status_list = []
original_status_update_fn = fe.update_status
def intercept(*args, **kwargs):
if "status_code" in kwargs:
status_list.append(kwargs["status_code"])
return original_status_update_fn(*args, **kwargs)
fe.update_status = intercept
fe.run()
dummy_pipe.assert_empty()
assert status_list == [
CommonService.SERVICE_STATUS_NEW,
CommonService.SERVICE_STATUS_STARTING,
CommonService.SERVICE_STATUS_PROCESSING,
CommonService.SERVICE_STATUS_SHUTDOWN,
CommonService.SERVICE_STATUS_END, # Following updates caused by frontend
CommonService.SERVICE_STATUS_END,
CommonService.SERVICE_STATUS_TEARDOWN,
]
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_parses_termination_requests(mock_mp):
"""The frontend should observe termination requests from the server."""
transport = mock.Mock()
service_process = mock.Mock()
mock_mp.Process.return_value = service_process
dummy_pipe = MockPipe(
[
{"band": "status_update", "statuscode": CommonService.SERVICE_STATUS_NEW},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_STARTING,
},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_PROCESSING,
},
{"band": "request_termination"},
RuntimeError("Service termination request not observed"),
]
)
mock_mp.Pipe.return_value = (dummy_pipe, dummy_pipe)
fe = workflows.frontend.Frontend(transport=transport, service=mock.Mock())
# intercept status code updates
status_list = []
original_status_update_fn = fe.update_status
def intercept(*args, **kwargs):
if "status_code" in kwargs:
status_list.append(kwargs["status_code"])
return original_status_update_fn(*args, **kwargs)
fe.update_status = intercept
fe.run()
dummy_pipe.assert_empty()
assert status_list == [
CommonService.SERVICE_STATUS_NEW,
CommonService.SERVICE_STATUS_STARTING,
CommonService.SERVICE_STATUS_PROCESSING,
CommonService.SERVICE_STATUS_END,
CommonService.SERVICE_STATUS_TEARDOWN,
]
@mock.patch("workflows.frontend.time")
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_sends_status_updates(mock_mp, mock_time):
"""The frontend should send status updates on update_status() calls.
Some sensible rate limiting must be applied."""
transport = mock.Mock()
mock_time.time.return_value = 10
fe = workflows.frontend.Frontend(transport=transport)
transport = transport.return_value
transport.broadcast_status.assert_called_once()
assert (
transport.broadcast_status.call_args[0][0]["status"]
== CommonService.SERVICE_STATUS_NONE
)
transport.broadcast_status.reset_mock()
fe.update_status(status_code=CommonService.SERVICE_STATUS_STARTING)
transport.broadcast_status.assert_called_once()
assert (
transport.broadcast_status.call_args[0][0]["status"]
== CommonService.SERVICE_STATUS_STARTING
)
transport.broadcast_status.reset_mock()
# subsequent update call that does not change anything should be ignored
fe.update_status()
transport.broadcast_status.assert_not_called()
# time passes. Update call should cause broadcast.
mock_time.time.return_value = 20
fe.update_status()
transport.broadcast_status.assert_called_once()
assert (
transport.broadcast_status.call_args[0][0]["status"]
== CommonService.SERVICE_STATUS_STARTING
)
transport.broadcast_status.reset_mock()
# not much time passes. Update call should still cause broadcast, because status has not been seen before
mock_time.time.return_value = 20.1
fe.update_status(status_code=CommonService.SERVICE_STATUS_PROCESSING)
transport.broadcast_status.assert_called_once()
assert (
transport.broadcast_status.call_args[0][0]["status"]
== CommonService.SERVICE_STATUS_PROCESSING
)
transport.broadcast_status.reset_mock()
# not much time passes. Update call should not broadcast, because this is a recent IDLE status
mock_time.time.return_value = 20.2
fe.update_status(status_code=CommonService.SERVICE_STATUS_IDLE)
transport.broadcast_status.assert_not_called()
# not much time passes. Update call should not broadcast, because the announced status has not changed
mock_time.time.return_value = 20.3
fe.update_status(status_code=CommonService.SERVICE_STATUS_PROCESSING)
transport.broadcast_status.assert_not_called()
transport.broadcast_status.reset_mock()
# not much time passes. Update call should still not broadcast, because this is again a recent IDLE status
mock_time.time.return_value = 20.4
fe.update_status(status_code=CommonService.SERVICE_STATUS_IDLE)
transport.broadcast_status.assert_not_called()
# not much time passes. Update call should still not broadcast, because this IDLE is still too recent
mock_time.time.return_value = 20.8
fe.update_status(status_code=CommonService.SERVICE_STATUS_IDLE)
transport.broadcast_status.assert_not_called()
# however as time passes the update call should cause a late broadcast
mock_time.time.return_value = 21
fe.update_status()
transport.broadcast_status.assert_called_once()
assert (
transport.broadcast_status.call_args[0][0]["status"]
== CommonService.SERVICE_STATUS_IDLE
)
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_does_not_restart_nonrestartable_service_on_segfault(mock_mp):
"""When the frontend is constructed with restart_service=False failing services must not be restarted."""
service_factory = mock.Mock()
service_process = mock.Mock()
dummy_pipe = mock.Mock()
dummy_pipe.recv.side_effect = EOFError() # Dead on arrival
mock_mp.Pipe.return_value = (dummy_pipe, dummy_pipe)
mock_mp.Process.return_value = service_process
service_instances = [mock.Mock()]
service_factory.side_effect = service_instances + [
Exception("More than one service object instantiated")
]
fe = workflows.frontend.Frontend(
transport=mock.Mock(), service=service_factory, restart_service=False
)
with pytest.raises(workflows.Error):
fe.run()
service_factory.assert_called_once()
service_process.start.assert_called_once()
service_process.join.assert_called_once()
mock_mp.Process.assert_called_once_with(
target=service_instances[0].start, args=(), kwargs=mock.ANY
)
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_does_not_restart_nonrestartable_service_on_error(mock_mp):
"""When the frontend is constructed with restart_service=False failing services must not be restarted."""
service_instances = [mock.Mock()]
service_factory = mock.Mock()
service_factory.side_effect = service_instances + [
Exception("More than one service object instantiated")
]
service_process = mock.Mock()
mock_mp.Process.return_value = service_process
dummy_pipe = MockPipe(
[
{"band": "status_update", "statuscode": CommonService.SERVICE_STATUS_NEW},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_STARTING,
},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_PROCESSING,
},
{"band": "status_update", "statuscode": CommonService.SERVICE_STATUS_ERROR},
]
)
mock_mp.Pipe.return_value = (dummy_pipe, dummy_pipe)
fe = workflows.frontend.Frontend(
transport=mock.Mock(), service=service_factory, restart_service=False
)
with pytest.raises(workflows.Error):
fe.run()
service_factory.assert_called_once()
service_process.start.assert_called_once()
service_process.join.assert_called_once()
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_does_restart_restartable_service_on_segfault(mock_mp):
"""When the frontend is constructed with restart_service=True failing services must be restarted."""
transport_factory = mock.Mock()
transport1 = mock.Mock()
transport2 = mock.Mock()
transport3 = mock.Mock()
transport1.return_value.connect.return_value = True
transport2.return_value.connect.return_value = True
transport3.return_value.connect.return_value = True
transport_factory.side_effect = [transport1, transport2, transport3, None]
service_factory = mock.Mock()
service_process = mock.Mock()
dummy_pipe = mock.Mock()
dummy_pipe.recv.side_effect = EOFError() # Dead on arrival
mock_mp.Pipe.return_value = (dummy_pipe, dummy_pipe)
mock_mp.Process.return_value = service_process
sentinel_exception = Exception("break loop")
service_instances = [mock.Mock(), mock.Mock()]
service_factory.side_effect = service_instances + [sentinel_exception]
fe = workflows.frontend.Frontend(
transport=transport_factory, service=service_factory, restart_service=True
)
with pytest.raises(Exception) as e:
fe.run()
assert e.value == sentinel_exception
assert service_factory.call_count == 3
assert service_process.start.call_count == 2
assert service_process.join.call_count == 2
mock_mp.Process.assert_has_calls(
[
mock.call(args=(), kwargs=mock.ANY, target=service_instances[0].start),
mock.call(args=(), kwargs=mock.ANY, target=service_instances[1].start),
],
any_order=True,
)
assert service_instances[0].transport == transport2
assert service_instances[1].transport == transport3
assert transport1.connect.call_count == 1
assert transport2.connect.call_count == 0
assert transport3.connect.call_count == 0
@mock.patch("workflows.frontend.multiprocessing")
def test_frontend_does_restart_restartable_service_on_error(mock_mp):
"""When the frontend is constructed with restart_service=True failing services must be restarted."""
sentinel_exception = Exception("break loop")
service_instances = [mock.Mock(), mock.Mock()]
service_factory = mock.Mock()
service_factory.side_effect = service_instances + [sentinel_exception]
service_process = mock.Mock()
mock_mp.Process.return_value = service_process
def pipe_creator(*args, **kwargs):
"""Pipe creator creates pipes."""
dummy_pipe = MockPipe(
[
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_NEW,
},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_STARTING,
},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_PROCESSING,
},
{
"band": "status_update",
"statuscode": CommonService.SERVICE_STATUS_ERROR,
},
]
)
return (dummy_pipe, dummy_pipe)
mock_mp.Pipe.side_effect = pipe_creator
fe = workflows.frontend.Frontend(
transport=mock.Mock(), service=service_factory, restart_service=True
)
try:
fe.run()
assert False, "Exception should have been raised"
except Exception as e:
if e != sentinel_exception:
raise
assert service_factory.call_count == 3
assert service_process.start.call_count == 2
assert service_process.join.call_count == 2
mock_mp.Process.assert_has_calls(
[
mock.call(args=(), kwargs=mock.ANY, target=service_instances[0].start),
mock.call(args=(), kwargs=mock.ANY, target=service_instances[1].start),
],
any_order=True,
)