Skip to content

Commit 849a253

Browse files
Capture stdout and stderr when executing docker compose (#18859)
* commit * linter * get compose logs * linter * linter * import SubprocessError * fix SubprocessError * remove -f * capture output * linter * docker compose output * remove import subprocesserror * removed capture * force ci * capture * propagate capture * propagate capture * self.capture * linter * test case for docker_run capture=True * restored README.md * add change log
1 parent 1da51b7 commit 849a253

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``capture`` parameter to ``docker_run`` to capture ``docker compose`` output.

datadog_checks_dev/datadog_checks/dev/docker.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def docker_run(
121121
wrappers=None,
122122
attempts=None,
123123
attempts_wait=1,
124+
capture=None,
124125
):
125126
"""
126127
A convenient context manager for safely setting up and tearing down Docker environments.
@@ -169,7 +170,10 @@ def docker_run(
169170
if not isinstance(compose_file, str):
170171
raise TypeError('The path to the compose file is not a string: {}'.format(repr(compose_file)))
171172

172-
set_up = ComposeFileUp(compose_file, build=build, service_name=service_name)
173+
composeFileArgs = {'compose_file': compose_file, 'build': build, 'service_name': service_name}
174+
if capture is not None:
175+
composeFileArgs['capture'] = capture
176+
set_up = ComposeFileUp(**composeFileArgs)
173177
if down is not None:
174178
tear_down = down
175179
else:
@@ -229,10 +233,11 @@ def docker_run(
229233

230234

231235
class ComposeFileUp(LazyFunction):
232-
def __init__(self, compose_file, build=False, service_name=None):
236+
def __init__(self, compose_file, build=False, service_name=None, capture=None):
233237
self.compose_file = compose_file
234238
self.build = build
235239
self.service_name = service_name
240+
self.capture = capture
236241
self.command = ['docker', 'compose', '-f', self.compose_file, 'up', '-d', '--force-recreate']
237242

238243
if self.build:
@@ -242,7 +247,10 @@ def __init__(self, compose_file, build=False, service_name=None):
242247
self.command.append(self.service_name)
243248

244249
def __call__(self):
245-
return run_command(self.command, check=True)
250+
args = {'check': True}
251+
if self.capture is not None:
252+
args['capture'] = self.capture
253+
return run_command(self.command, **args)
246254

247255

248256
class ComposeFileLogs(LazyFunction):

datadog_checks_dev/tests/test_docker.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,21 @@ def test_up(self):
3636

3737

3838
class TestDockerRun:
39-
def test_compose_file(self):
39+
@pytest.mark.parametrize(
40+
"capture",
41+
[
42+
None,
43+
True,
44+
],
45+
)
46+
def test_compose_file(self, capture):
4047
compose_file = os.path.join(DOCKER_DIR, 'test_default.yaml')
4148

4249
try:
43-
with docker_run(compose_file):
50+
args = {}
51+
if capture is not None:
52+
args['capture'] = capture
53+
with docker_run(compose_file, **args):
4454
assert compose_file_active(compose_file) is True
4555
assert compose_file_active(compose_file) is False
4656
finally:

sqlserver/tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,5 +328,7 @@ def high_cardinality_env_is_ready():
328328

329329
conditions += [CheckDockerLogs(compose_file, completion_message)]
330330

331-
with docker_run(compose_file=compose_file, conditions=conditions, mount_logs=True, build=True, attempts=3):
331+
with docker_run(
332+
compose_file=compose_file, conditions=conditions, mount_logs=True, build=True, attempts=3, capture=True
333+
):
332334
yield full_e2e_config, E2E_METADATA

0 commit comments

Comments
 (0)