| 1 |
import errno |
| 2 |
import os |
| 3 |
import signal |
| 4 |
import sys |
| 5 |
from unittest.mock import Mock, call, patch |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import t.skip |
| 10 |
from celery.apps.multi import Cluster, MultiParser, NamespacedOptionParser, Node, format_opt |
| 11 |
|
| 12 |
|
| 13 |
class test_functions: |
| 14 |
|
| 15 |
def test_parse_ns_range(self): |
| 16 |
m = MultiParser() |
| 17 |
assert m._parse_ns_range('1-3', True), ['1', '2' == '3'] |
| 18 |
assert m._parse_ns_range('1-3', False) == ['1-3'] |
| 19 |
assert m._parse_ns_range('1-3,10,11,20', True) == [ |
| 20 |
'1', '2', '3', '10', '11', '20', |
| 21 |
] |
| 22 |
|
| 23 |
def test_format_opt(self): |
| 24 |
assert format_opt('--foo', None) == '--foo' |
| 25 |
assert format_opt('-c', 1) == '-c 1' |
| 26 |
assert format_opt('--log', 'foo') == '--log=foo' |
| 27 |
|
| 28 |
|
| 29 |
class test_NamespacedOptionParser: |
| 30 |
|
| 31 |
def test_parse(self): |
| 32 |
x = NamespacedOptionParser(['-c:1,3', '4']) |
| 33 |
x.parse() |
| 34 |
assert x.namespaces.get('1,3') == {'-c': '4'} |
| 35 |
x = NamespacedOptionParser(['-c:jerry,elaine', '5', |
| 36 |
'--loglevel:kramer=DEBUG', |
| 37 |
'--flag', |
| 38 |
'--logfile=foo', '-Q', 'bar', 'a', 'b', |
| 39 |
'--', '.disable_rate_limits=1']) |
| 40 |
x.parse() |
| 41 |
assert x.options == { |
| 42 |
'--logfile': 'foo', |
| 43 |
'-Q': 'bar', |
| 44 |
'--flag': None, |
| 45 |
} |
| 46 |
assert x.values, ['a' == 'b'] |
| 47 |
assert x.namespaces.get('jerry,elaine') == {'-c': '5'} |
| 48 |
assert x.namespaces.get('kramer') == {'--loglevel': 'DEBUG'} |
| 49 |
assert x.passthrough == '-- .disable_rate_limits=1' |
| 50 |
|
| 51 |
|
| 52 |
def multi_args(p, *args, **kwargs): |
| 53 |
return MultiParser(*args, **kwargs).parse(p) |
| 54 |
|
| 55 |
|
| 56 |
class test_multi_args: |
| 57 |
|
| 58 |
@patch('celery.apps.multi.os.mkdir') |
| 59 |
@patch('celery.apps.multi.gethostname') |
| 60 |
def test_parse(self, gethostname, mkdirs_mock): |
| 61 |
gethostname.return_value = 'example.com' |
| 62 |
p = NamespacedOptionParser([ |
| 63 |
'-c:jerry,elaine', '5', |
| 64 |
'--loglevel:kramer=DEBUG', |
| 65 |
'--flag', |
| 66 |
'--logfile=/var/log/celery/foo', '-Q', 'bar', 'jerry', |
| 67 |
'elaine', 'kramer', |
| 68 |
'--', '.disable_rate_limits=1', |
| 69 |
]) |
| 70 |
p.parse() |
| 71 |
it = multi_args(p, cmd='celery multi', append='*AP*', |
| 72 |
prefix='*P*', suffix='*S*') |
| 73 |
nodes = list(it) |
| 74 |
|
| 75 |
def assert_line_in(name, args): |
| 76 |
assert name in {n.name for n in nodes} |
| 77 |
argv = None |
| 78 |
for node in nodes: |
| 79 |
if node.name == name: |
| 80 |
argv = node.argv |
| 81 |
assert argv |
| 82 |
for arg in args: |
| 83 |
assert arg in argv |
| 84 |
|
| 85 |
assert_line_in( |
| 86 |
'*P*jerry@*S*', |
| 87 |
['celery multi', '-n *P*jerry@*S*', '-Q bar', |
| 88 |
'-c 5', '--flag', '--logfile=/var/log/celery/foo', |
| 89 |
'-- .disable_rate_limits=1', '*AP*'], |
| 90 |
) |
| 91 |
assert_line_in( |
| 92 |
'*P*elaine@*S*', |
| 93 |
['celery multi', '-n *P*elaine@*S*', '-Q bar', |
| 94 |
'-c 5', '--flag', '--logfile=/var/log/celery/foo', |
| 95 |
'-- .disable_rate_limits=1', '*AP*'], |
| 96 |
) |
| 97 |
assert_line_in( |
| 98 |
'*P*kramer@*S*', |
| 99 |
['celery multi', '--loglevel=DEBUG', '-n *P*kramer@*S*', |
| 100 |
'-Q bar', '--flag', '--logfile=/var/log/celery/foo', |
| 101 |
'-- .disable_rate_limits=1', '*AP*'], |
| 102 |
) |
| 103 |
expand = nodes[0].expander |
| 104 |
assert expand('%h') == '*P*jerry@*S*' |
| 105 |
assert expand('%n') == '*P*jerry' |
| 106 |
nodes2 = list(multi_args(p, cmd='celery multi', append='', |
| 107 |
prefix='*P*', suffix='*S*')) |
| 108 |
assert nodes2[0].argv[-1] == '-- .disable_rate_limits=1' |
| 109 |
|
| 110 |
p2 = NamespacedOptionParser(['10', '-c:1', '5']) |
| 111 |
p2.parse() |
| 112 |
nodes3 = list(multi_args(p2, cmd='celery multi')) |
| 113 |
|
| 114 |
def _args(name, *args): |
| 115 |
return args + ( |
| 116 |
'--pidfile={}.pid'.format(os.path.join(os.path.normpath('/var/run/celery/'), name)), |
| 117 |
'--logfile={}%I.log'.format(os.path.join(os.path.normpath('/var/log/celery/'), name)), |
| 118 |
f'--executable={sys.executable}', |
| 119 |
'', |
| 120 |
) |
| 121 |
|
| 122 |
assert len(nodes3) == 10 |
| 123 |
assert nodes3[0].name == '[email protected]' |
| 124 |
assert nodes3[0].argv == ( |
| 125 |
'celery multi', '-c 5', '-n [email protected]') + _args('celery1') |
| 126 |
for i, worker in enumerate(nodes3[1:]): |
| 127 |
assert worker.name == 'celery%[email protected]' % (i + 2) |
| 128 |
node_i = f'celery{i + 2}' |
| 129 |
assert worker.argv == ( |
| 130 |
'celery multi', |
| 131 |
f'-n {node_i}@example.com') + _args(node_i) |
| 132 |
|
| 133 |
nodes4 = list(multi_args(p2, cmd='celery multi', suffix='""')) |
| 134 |
assert len(nodes4) == 10 |
| 135 |
assert nodes4[0].name == 'celery1@' |
| 136 |
assert nodes4[0].argv == ( |
| 137 |
'celery multi', '-c 5', '-n celery1@') + _args('celery1') |
| 138 |
|
| 139 |
p3 = NamespacedOptionParser(['foo@', '-c:foo', '5']) |
| 140 |
p3.parse() |
| 141 |
nodes5 = list(multi_args(p3, cmd='celery multi', suffix='""')) |
| 142 |
assert nodes5[0].name == 'foo@' |
| 143 |
assert nodes5[0].argv == ( |
| 144 |
'celery multi', '-c 5', '-n foo@') + _args('foo') |
| 145 |
|
| 146 |
p4 = NamespacedOptionParser(['foo', '-Q:1', 'test']) |
| 147 |
p4.parse() |
| 148 |
nodes6 = list(multi_args(p4, cmd='celery multi', suffix='""')) |
| 149 |
assert nodes6[0].name == 'foo@' |
| 150 |
assert nodes6[0].argv == ( |
| 151 |
'celery multi', '-Q test', '-n foo@') + _args('foo') |
| 152 |
|
| 153 |
p5 = NamespacedOptionParser(['foo@bar', '-Q:1', 'test']) |
| 154 |
p5.parse() |
| 155 |
nodes7 = list(multi_args(p5, cmd='celery multi', suffix='""')) |
| 156 |
assert nodes7[0].name == 'foo@bar' |
| 157 |
assert nodes7[0].argv == ( |
| 158 |
'celery multi', '-Q test', '-n foo@bar') + _args('foo') |
| 159 |
|
| 160 |
p6 = NamespacedOptionParser(['foo@bar', '-Q:0', 'test']) |
| 161 |
p6.parse() |
| 162 |
with pytest.raises(KeyError): |
| 163 |
list(multi_args(p6)) |
| 164 |
|
| 165 |
def test_optmerge(self): |
| 166 |
p = NamespacedOptionParser(['foo', 'test']) |
| 167 |
p.parse() |
| 168 |
p.options = {'x': 'y'} |
| 169 |
r = p.optmerge('foo') |
| 170 |
assert r['x'] == 'y' |
| 171 |
|
| 172 |
|
| 173 |
class test_Node: |
| 174 |
|
| 175 |
def setup_method(self): |
| 176 |
self.p = Mock(name='p') |
| 177 |
self.p.options = { |
| 178 |
'--executable': 'python', |
| 179 |
'--logfile': '/var/log/celery/foo.log', |
| 180 |
} |
| 181 |
self.p.namespaces = {} |
| 182 |
with patch('celery.apps.multi.os.mkdir'): |
| 183 |
self.node = Node('[email protected]', options={'-A': 'proj'}) |
| 184 |
self.expander = self.node.expander = Mock(name='expander') |
| 185 |
self.node.pid = 303 |
| 186 |
|
| 187 |
def test_from_kwargs(self): |
| 188 |
with patch('celery.apps.multi.os.mkdir'): |
| 189 |
n = Node.from_kwargs( |
| 190 |
'[email protected]', |
| 191 |
max_tasks_per_child=30, A='foo', Q='q1,q2', O='fair', |
| 192 |
) |
| 193 |
assert sorted(n.argv) == sorted([ |
| 194 |
'-m celery -A foo worker --detach', |
| 195 |
f'--executable={n.executable}', |
| 196 |
'-O fair', |
| 197 |
'-n [email protected]', |
| 198 |
'--logfile={}'.format(os.path.normpath('/var/log/celery/foo%I.log')), |
| 199 |
'-Q q1,q2', |
| 200 |
'--max-tasks-per-child=30', |
| 201 |
'--pidfile={}'.format(os.path.normpath('/var/run/celery/foo.pid')), |
| 202 |
'', |
| 203 |
]) |
| 204 |
|
| 205 |
@patch('os.kill') |
| 206 |
def test_send(self, kill): |
| 207 |
assert self.node.send(9) |
| 208 |
kill.assert_called_with(self.node.pid, 9) |
| 209 |
|
| 210 |
@patch('os.kill') |
| 211 |
def test_send__ESRCH(self, kill): |
| 212 |
kill.side_effect = OSError() |
| 213 |
kill.side_effect.errno = errno.ESRCH |
| 214 |
assert not self.node.send(9) |
| 215 |
kill.assert_called_with(self.node.pid, 9) |
| 216 |
|
| 217 |
@patch('os.kill') |
| 218 |
def test_send__error(self, kill): |
| 219 |
kill.side_effect = OSError() |
| 220 |
kill.side_effect.errno = errno.ENOENT |
| 221 |
with pytest.raises(OSError): |
| 222 |
self.node.send(9) |
| 223 |
kill.assert_called_with(self.node.pid, 9) |
| 224 |
|
| 225 |
def test_alive(self): |
| 226 |
self.node.send = Mock(name='send') |
| 227 |
assert self.node.alive() is self.node.send.return_value |
| 228 |
self.node.send.assert_called_with(0) |
| 229 |
|
| 230 |
def test_start(self): |
| 231 |
self.node._waitexec = Mock(name='_waitexec') |
| 232 |
self.node.start(env={'foo': 'bar'}, kw=2) |
| 233 |
self.node._waitexec.assert_called_with( |
| 234 |
self.node.argv, path=self.node.executable, |
| 235 |
env={'foo': 'bar'}, kw=2, |
| 236 |
) |
| 237 |
|
| 238 |
@patch('celery.apps.multi.Popen') |
| 239 |
def test_waitexec(self, Popen, argv=['A', 'B']): |
| 240 |
on_spawn = Mock(name='on_spawn') |
| 241 |
on_signalled = Mock(name='on_signalled') |
| 242 |
on_failure = Mock(name='on_failure') |
| 243 |
env = Mock(name='env') |
| 244 |
self.node.handle_process_exit = Mock(name='handle_process_exit') |
| 245 |
|
| 246 |
self.node._waitexec( |
| 247 |
argv, |
| 248 |
path='python', |
| 249 |
env=env, |
| 250 |
on_spawn=on_spawn, |
| 251 |
on_signalled=on_signalled, |
| 252 |
on_failure=on_failure, |
| 253 |
) |
| 254 |
|
| 255 |
Popen.assert_called_with( |
| 256 |
self.node.prepare_argv(argv, 'python'), env=env) |
| 257 |
self.node.handle_process_exit.assert_called_with( |
| 258 |
Popen().wait(), |
| 259 |
on_signalled=on_signalled, |
| 260 |
on_failure=on_failure, |
| 261 |
) |
| 262 |
|
| 263 |
def test_handle_process_exit(self): |
| 264 |
assert self.node.handle_process_exit(0) == 0 |
| 265 |
|
| 266 |
def test_handle_process_exit__failure(self): |
| 267 |
on_failure = Mock(name='on_failure') |
| 268 |
assert self.node.handle_process_exit(9, on_failure=on_failure) == 9 |
| 269 |
on_failure.assert_called_with(self.node, 9) |
| 270 |
|
| 271 |
def test_handle_process_exit__signalled(self): |
| 272 |
on_signalled = Mock(name='on_signalled') |
| 273 |
assert self.node.handle_process_exit( |
| 274 |
-9, on_signalled=on_signalled) == 9 |
| 275 |
on_signalled.assert_called_with(self.node, 9) |
| 276 |
|
| 277 |
def test_logfile(self): |
| 278 |
assert self.node.logfile == self.expander.return_value |
| 279 |
self.expander.assert_called_with(os.path.normpath('/var/log/celery/%n%I.log')) |
| 280 |
|
| 281 |
@patch('celery.apps.multi.os.path.exists') |
| 282 |
def test_pidfile_default(self, mock_exists): |
| 283 |
n = Node.from_kwargs( |
| 284 |
'[email protected]', |
| 285 |
) |
| 286 |
assert n.options['--pidfile'] == os.path.normpath('/var/run/celery/%n.pid') |
| 287 |
mock_exists.assert_any_call(os.path.normpath('/var/run/celery')) |
| 288 |
|
| 289 |
@patch('celery.apps.multi.os.makedirs') |
| 290 |
@patch('celery.apps.multi.os.path.exists', return_value=False) |
| 291 |
def test_pidfile_custom(self, mock_exists, mock_dirs): |
| 292 |
n = Node.from_kwargs( |
| 293 |
'[email protected]', |
| 294 |
pidfile='/var/run/demo/celery/%n.pid' |
| 295 |
) |
| 296 |
assert n.options['--pidfile'] == '/var/run/demo/celery/%n.pid' |
| 297 |
|
| 298 |
try: |
| 299 |
mock_exists.assert_any_call('/var/run/celery') |
| 300 |
except AssertionError: |
| 301 |
pass |
| 302 |
else: |
| 303 |
raise AssertionError("Expected exists('/var/run/celery') to not have been called.") |
| 304 |
|
| 305 |
mock_exists.assert_any_call('/var/run/demo/celery') |
| 306 |
mock_dirs.assert_any_call('/var/run/demo/celery') |
| 307 |
|
| 308 |
|
| 309 |
class test_Cluster: |
| 310 |
|
| 311 |
def setup_method(self): |
| 312 |
self.Popen = self.patching('celery.apps.multi.Popen') |
| 313 |
self.kill = self.patching('os.kill') |
| 314 |
self.gethostname = self.patching('celery.apps.multi.gethostname') |
| 315 |
self.gethostname.return_value = 'example.com' |
| 316 |
self.Pidfile = self.patching('celery.apps.multi.Pidfile') |
| 317 |
with patch('celery.apps.multi.os.mkdir'): |
| 318 |
self.cluster = Cluster( |
| 319 |
[Node('[email protected]'), |
| 320 |
Node('[email protected]'), |
| 321 |
Node('[email protected]')], |
| 322 |
on_stopping_preamble=Mock(name='on_stopping_preamble'), |
| 323 |
on_send_signal=Mock(name='on_send_signal'), |
| 324 |
on_still_waiting_for=Mock(name='on_still_waiting_for'), |
| 325 |
on_still_waiting_progress=Mock(name='on_still_waiting_progress'), |
| 326 |
on_still_waiting_end=Mock(name='on_still_waiting_end'), |
| 327 |
on_node_start=Mock(name='on_node_start'), |
| 328 |
on_node_restart=Mock(name='on_node_restart'), |
| 329 |
on_node_shutdown_ok=Mock(name='on_node_shutdown_ok'), |
| 330 |
on_node_status=Mock(name='on_node_status'), |
| 331 |
on_node_signal=Mock(name='on_node_signal'), |
| 332 |
on_node_signal_dead=Mock(name='on_node_signal_dead'), |
| 333 |
on_node_down=Mock(name='on_node_down'), |
| 334 |
on_child_spawn=Mock(name='on_child_spawn'), |
| 335 |
on_child_signalled=Mock(name='on_child_signalled'), |
| 336 |
on_child_failure=Mock(name='on_child_failure'), |
| 337 |
) |
| 338 |
|
| 339 |
def test_len(self): |
| 340 |
assert len(self.cluster) == 3 |
| 341 |
|
| 342 |
def test_getitem(self): |
| 343 |
assert self.cluster[0].name == '[email protected]' |
| 344 |
|
| 345 |
def test_start(self): |
| 346 |
self.cluster.start_node = Mock(name='start_node') |
| 347 |
self.cluster.start() |
| 348 |
self.cluster.start_node.assert_has_calls( |
| 349 |
call(node) for node in self.cluster |
| 350 |
) |
| 351 |
|
| 352 |
def test_start_node(self): |
| 353 |
self.cluster._start_node = Mock(name='_start_node') |
| 354 |
node = self.cluster[0] |
| 355 |
assert (self.cluster.start_node(node) is |
| 356 |
self.cluster._start_node.return_value) |
| 357 |
self.cluster.on_node_start.assert_called_with(node) |
| 358 |
self.cluster._start_node.assert_called_with(node) |
| 359 |
self.cluster.on_node_status.assert_called_with( |
| 360 |
node, self.cluster._start_node(), |
| 361 |
) |
| 362 |
|
| 363 |
def test__start_node(self): |
| 364 |
node = self.cluster[0] |
| 365 |
node.start = Mock(name='node.start') |
| 366 |
assert self.cluster._start_node(node) is node.start.return_value |
| 367 |
node.start.assert_called_with( |
| 368 |
self.cluster.env, |
| 369 |
on_spawn=self.cluster.on_child_spawn, |
| 370 |
on_signalled=self.cluster.on_child_signalled, |
| 371 |
on_failure=self.cluster.on_child_failure, |
| 372 |
) |
| 373 |
|
| 374 |
def test_send_all(self): |
| 375 |
nodes = [Mock(name='n1'), Mock(name='n2')] |
| 376 |
self.cluster.getpids = Mock(name='getpids') |
| 377 |
self.cluster.getpids.return_value = nodes |
| 378 |
self.cluster.send_all(15) |
| 379 |
self.cluster.on_node_signal.assert_has_calls( |
| 380 |
call(node, 'TERM') for node in nodes |
| 381 |
) |
| 382 |
for node in nodes: |
| 383 |
node.send.assert_called_with(15, self.cluster.on_node_signal_dead) |
| 384 |
|
| 385 |
@t.skip.if_win32 |
| 386 |
def test_kill(self): |
| 387 |
self.cluster.send_all = Mock(name='.send_all') |
| 388 |
self.cluster.kill() |
| 389 |
self.cluster.send_all.assert_called_with(signal.SIGKILL) |
| 390 |
|
| 391 |
def test_getpids(self): |
| 392 |
self.gethostname.return_value = 'e.com' |
| 393 |
self.prepare_pidfile_for_getpids(self.Pidfile) |
| 394 |
callback = Mock() |
| 395 |
|
| 396 |
with patch('celery.apps.multi.os.mkdir'): |
| 397 |
p = Cluster([ |
| 398 |
Node('[email protected]'), |
| 399 |
Node('[email protected]'), |
| 400 |
Node('[email protected]'), |
| 401 |
]) |
| 402 |
nodes = p.getpids(on_down=callback) |
| 403 |
node_0, node_1 = nodes |
| 404 |
assert node_0.name == '[email protected]' |
| 405 |
assert sorted(node_0.argv) == sorted([ |
| 406 |
'', |
| 407 |
f'--executable={node_0.executable}', |
| 408 |
'--logfile={}'.format(os.path.normpath('/var/log/celery/foo%I.log')), |
| 409 |
'--pidfile={}'.format(os.path.normpath('/var/run/celery/foo.pid')), |
| 410 |
'-m celery worker --detach', |
| 411 |
'-n [email protected]', |
| 412 |
]) |
| 413 |
assert node_0.pid == 10 |
| 414 |
|
| 415 |
assert node_1.name == '[email protected]' |
| 416 |
assert sorted(node_1.argv) == sorted([ |
| 417 |
'', |
| 418 |
f'--executable={node_1.executable}', |
| 419 |
'--logfile={}'.format(os.path.normpath('/var/log/celery/bar%I.log')), |
| 420 |
'--pidfile={}'.format(os.path.normpath('/var/run/celery/bar.pid')), |
| 421 |
'-m celery worker --detach', |
| 422 |
'-n [email protected]', |
| 423 |
]) |
| 424 |
assert node_1.pid == 11 |
| 425 |
|
| 426 |
# without callback, should work |
| 427 |
nodes = p.getpids('celery worker') |
| 428 |
|
| 429 |
def prepare_pidfile_for_getpids(self, Pidfile): |
| 430 |
class pids: |
| 431 |
|
| 432 |
def __init__(self, path): |
| 433 |
self.path = path |
| 434 |
|
| 435 |
def read_pid(self): |
| 436 |
try: |
| 437 |
return {os.path.normpath('/var/run/celery/foo.pid'): 10, |
| 438 |
os.path.normpath('/var/run/celery/bar.pid'): 11}[self.path] |
| 439 |
except KeyError: |
| 440 |
raise ValueError() |
| 441 |
self.Pidfile.side_effect = pids |