Debian Codegraph @

t/unit/bin/test_control.py

raw
1 import os
2 import re
3 from unittest.mock import patch
4
5 import pytest
6 from click.testing import CliRunner
7 from kombu.exceptions import OperationalError
8
9 from celery.bin.celery import celery
10 from celery.platforms import EX_UNAVAILABLE
11
12 _GLOBAL_OPTIONS = ['-A', 't.unit.bin.proj.app_with_custom_cmds', '--broker', 'memory://']
13 _INSPECT_OPTIONS = ['--timeout', '0'] # Avoid waiting for the zero workers to reply
14
15
16 @pytest.fixture(autouse=True)
17 def clean_os_environ():
18 # Celery modifies os.environ when given the CLI option --broker memory://
19 # This interferes with other tests, so we need to reset os.environ
20 with patch.dict(os.environ, clear=True):
21 yield
22
23
24 @pytest.mark.parametrize(
25 ('celery_cmd', 'custom_cmd'),
26 [
27 ('inspect', ('custom_inspect_cmd', '123')),
28 ('control', ('custom_control_cmd', '123', '456')),
29 ],
30 )
31 def test_custom_remote_command(celery_cmd, custom_cmd, isolated_cli_runner: CliRunner):
32 res = isolated_cli_runner.invoke(
33 celery,
34 [*_GLOBAL_OPTIONS, celery_cmd, *_INSPECT_OPTIONS, *custom_cmd],
35 catch_exceptions=False,
36 )
37 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
38 assert res.output.strip() == 'Error: No nodes replied within time constraint'
39
40
41 @pytest.mark.parametrize(
42 ('celery_cmd', 'remote_cmd'),
43 [
44 # Test nonexistent commands
45 ('inspect', 'this_command_does_not_exist'),
46 ('control', 'this_command_does_not_exist'),
47 # Test commands that exist, but are of the wrong type
48 ('inspect', 'custom_control_cmd'),
49 ('control', 'custom_inspect_cmd'),
50 ],
51 )
52 def test_unrecognized_remote_command(celery_cmd, remote_cmd, isolated_cli_runner: CliRunner):
53 res = isolated_cli_runner.invoke(
54 celery,
55 [*_GLOBAL_OPTIONS, celery_cmd, *_INSPECT_OPTIONS, remote_cmd],
56 catch_exceptions=False,
57 )
58 assert res.exit_code == 2, (res, res.output)
59 assert f'Error: Command {remote_cmd} not recognized. Available {celery_cmd} commands: ' in res.output
60
61
62 _expected_inspect_regex = (
63 '\n custom_inspect_cmd x\\s+Ask the workers to reply with x\\.\n'
64 )
65 _expected_control_regex = (
66 '\n custom_control_cmd a b\\s+Ask the workers to reply with a and b\\.\n'
67 )
68
69
70 @pytest.mark.parametrize(
71 ('celery_cmd', 'expected_regex'),
72 [
73 ('inspect', re.compile(_expected_inspect_regex, re.MULTILINE)),
74 ('control', re.compile(_expected_control_regex, re.MULTILINE)),
75 ],
76 )
77 def test_listing_remote_commands(celery_cmd, expected_regex, isolated_cli_runner: CliRunner):
78 res = isolated_cli_runner.invoke(
79 celery,
80 [*_GLOBAL_OPTIONS, celery_cmd, '--list'],
81 )
82 assert res.exit_code == 0, (res, res.stdout)
83 assert expected_regex.search(res.stdout)
84
85
86 def test_status_shows_friendly_error_when_broker_unreachable(cli_runner: CliRunner):
87 with patch('celery.app.control.Inspect.ping',
88 side_effect=OperationalError('[Errno 61] Connection refused')):
89 res = cli_runner.invoke(
90 celery,
91 [*_GLOBAL_OPTIONS, 'status'],
92 catch_exceptions=False,
93 )
94 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
95 assert 'Error: Could not connect to the message broker.' in res.output
96 assert 'Reason: [Errno 61] Connection refused' in res.output
97 assert 'Traceback' not in res.output
98
99
100 def test_status_unexpected_error_is_summarized(cli_runner: CliRunner):
101 with patch('celery.app.control.Inspect.ping',
102 side_effect=RuntimeError('boom')):
103 res = cli_runner.invoke(
104 celery,
105 [*_GLOBAL_OPTIONS, 'status'],
106 catch_exceptions=False,
107 )
108 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
109 assert 'Error: Unable to run the `status` command. Reason: boom' in res.output
110 assert 'Traceback' not in res.output
111
112
113 def test_graph_workers_shows_friendly_error_when_broker_unreachable(
114 cli_runner: CliRunner,
115 ):
116 with patch('celery.app.control.Inspect.stats',
117 side_effect=OperationalError('connection failed')):
118 res = cli_runner.invoke(
119 celery,
120 [*_GLOBAL_OPTIONS, 'graph', 'workers'],
121 catch_exceptions=False,
122 )
123 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
124 assert 'Error: Could not connect to the message broker.' in res.output
125 assert 'Reason: connection failed' in res.output
126
127
128 def test_events_dump_shows_friendly_error_when_broker_unreachable(
129 cli_runner: CliRunner,
130 ):
131 with patch('celery.bin.events._run_evdump',
132 side_effect=OperationalError('connection failed')):
133 res = cli_runner.invoke(
134 celery,
135 [*_GLOBAL_OPTIONS, 'events', '--dump'],
136 catch_exceptions=False,
137 )
138 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
139 assert 'Error: Could not connect to the message broker.' in res.output
140 assert 'Reason: connection failed' in res.output
141
142
143 def test_handle_remote_command_error_reraises_click_exception():
144 """base.py: bare ``raise`` inside the ClickException branch must be covered."""
145 import click
146
147 from celery.bin.base import handle_remote_command_error
148
149 original = click.ClickException('original click error')
150 with pytest.raises(click.ClickException) as exc_info:
151 try:
152 raise original
153 except Exception as exc:
154 handle_remote_command_error('any', exc)
155 assert exc_info.value is original
156
157
158 def test_inspect_shows_friendly_error_when_broker_unreachable(cli_runner: CliRunner):
159 with patch('celery.app.control.Inspect._request',
160 side_effect=OperationalError('connection refused')):
161 res = cli_runner.invoke(
162 celery,
163 [*_GLOBAL_OPTIONS, 'inspect', *_INSPECT_OPTIONS, 'custom_inspect_cmd', '1'],
164 catch_exceptions=False,
165 )
166 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
167 assert 'Error: Could not connect to the message broker.' in res.output
168 assert 'Reason: connection refused' in res.output
169 assert 'Traceback' not in res.output
170
171
172 def test_inspect_unexpected_error_is_summarized(cli_runner: CliRunner):
173 with patch('celery.app.control.Inspect._request',
174 side_effect=RuntimeError('inspect boom')):
175 res = cli_runner.invoke(
176 celery,
177 [*_GLOBAL_OPTIONS, 'inspect', *_INSPECT_OPTIONS, 'custom_inspect_cmd', '1'],
178 catch_exceptions=False,
179 )
180 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
181 assert 'Error: Unable to run the `inspect custom_inspect_cmd` command. Reason: inspect boom' in res.output
182 assert 'Traceback' not in res.output
183
184
185 def test_control_shows_friendly_error_when_broker_unreachable(cli_runner: CliRunner):
186 with patch('celery.app.control.Control.broadcast',
187 side_effect=OperationalError('connection refused')):
188 res = cli_runner.invoke(
189 celery,
190 [*_GLOBAL_OPTIONS, 'control', *_INSPECT_OPTIONS, 'custom_control_cmd', '1', '2'],
191 catch_exceptions=False,
192 )
193 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
194 assert 'Error: Could not connect to the message broker.' in res.output
195 assert 'Reason: connection refused' in res.output
196 assert 'Traceback' not in res.output
197
198
199 def test_control_unexpected_error_is_summarized(cli_runner: CliRunner):
200 with patch('celery.app.control.Control.broadcast',
201 side_effect=RuntimeError('control boom')):
202 res = cli_runner.invoke(
203 celery,
204 [*_GLOBAL_OPTIONS, 'control', *_INSPECT_OPTIONS, 'custom_control_cmd', '1', '2'],
205 catch_exceptions=False,
206 )
207 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
208 assert 'Error: Unable to run the `control custom_control_cmd` command. Reason: control boom' in res.output
209 assert 'Traceback' not in res.output
210
211
212 def test_events_camera_shows_friendly_error_when_broker_unreachable(cli_runner: CliRunner):
213 with patch('celery.bin.events._run_evcam',
214 side_effect=OperationalError('connection failed')):
215 res = cli_runner.invoke(
216 celery,
217 [*_GLOBAL_OPTIONS, 'events', '--camera', 'myapp.MyCameraClass'],
218 catch_exceptions=False,
219 )
220 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
221 assert 'Error: Could not connect to the message broker.' in res.output
222 assert 'Reason: connection failed' in res.output
223 assert 'Traceback' not in res.output
224
225
226 def test_events_evtop_shows_friendly_error_when_broker_unreachable(cli_runner: CliRunner):
227 with patch('celery.bin.events._run_evtop',
228 side_effect=OperationalError('connection failed')):
229 res = cli_runner.invoke(
230 celery,
231 [*_GLOBAL_OPTIONS, 'events'],
232 catch_exceptions=False,
233 )
234 assert res.exit_code == EX_UNAVAILABLE, (res, res.output)
235 assert 'Error: Could not connect to the message broker.' in res.output
236 assert 'Reason: connection failed' in res.output
237 assert 'Traceback' not in res.output