From 77204544959ac58f3f67bf22e81d3404fa00e01d Mon Sep 17 00:00:00 2001 From: Fabs Date: Tue, 18 Dec 2018 18:28:39 +0100 Subject: [PATCH] feature: add 'exec' returning a process and deprecates 'exec_command' --- dcos_test_utils/dcos_cli.py | 41 +++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + tests/test_dcos_cli.py | 38 ++++++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/dcos_test_utils/dcos_cli.py b/dcos_test_utils/dcos_cli.py index 2cbeff9..c2a550f 100644 --- a/dcos_test_utils/dcos_cli.py +++ b/dcos_test_utils/dcos_cli.py @@ -16,6 +16,8 @@ import requests +import deprecation + log = logging.getLogger(__name__) DCOS_CLI_URL = os.getenv('DCOS_CLI_URL', 'https://downloads.dcos.io/binaries/cli/linux/x86-64/dcos-1.12/dcos') @@ -92,6 +94,45 @@ def clear_cli_dir(): if os.path.exists(path): shutil.rmtree(path) + def exec(self, cmd: str, stdin=None, check=True) -> subprocess.CompletedProcess: + """Execute CLI command and processes result. + + This method expects that process won't block. + + :param cmd: Program and arguments + :type cmd: str + :param stdin: File to use for stdin + :param check: Does it check for raised errors + :type stdin: File + :returns: A tuple with stdout and stderr + :rtype: subprocess.CompletedProcess + """ + + log.info('CMD: {!r}'.format(cmd)) + + try: + process = subprocess.run( + cmd, + stdin=stdin, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=self.env, + check=check) + except subprocess.CalledProcessError as e: + if e.stderr: + stderr = e.stderr.decode('utf-8') + log.info('STDOUT: {}'.format(stdout)) + log.error('STDERR: {}'.format(stderr)) + raise + + stdout, stderr = process.stdout.decode('utf-8'), process.stderr.decode('utf-8') + + log.info('STDOUT: {}'.format(stdout)) + log.info('STDERR: {}'.format(stderr)) + + return process + + @deprecation.deprecated(details="Deprecated in favor of the `exec` function. DCOS-44823") def exec_command(self, cmd: str, stdin=None) -> tuple: """Execute CLI command and processes result. diff --git a/requirements.txt b/requirements.txt index 224c3d3..ec3abf1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,6 @@ pytest requests retrying tox +deprecation sphinx diff --git a/tests/test_dcos_cli.py b/tests/test_dcos_cli.py index f6440b9..662fc14 100644 --- a/tests/test_dcos_cli.py +++ b/tests/test_dcos_cli.py @@ -1,9 +1,11 @@ -import pytest import subprocess +import deprecation +import pytest from dcos_test_utils import dcos_cli +@deprecation.deprecated(details="Deprecated in favor of the `exec` function. DCOS-44823") def test_exec_command(caplog): cli = dcos_cli.DcosCli('') stdout, stderr = cli.exec_command( @@ -15,10 +17,42 @@ def test_exec_command(caplog): assert any(rec.message.startswith('STDOUT:') for rec in caplog.records) assert any(rec.message.startswith('STDERR:') for rec in caplog.records) - +@deprecation.deprecated(details="Deprecated in favor of the `exec` function. DCOS-44823") def test_exec_command_fail(caplog): cli = dcos_cli.DcosCli('') with pytest.raises(subprocess.CalledProcessError): cli.exec_command(['/bin/sh', '-c', 'does-not-exist']) assert any(rec.message.startswith('CMD:') for rec in caplog.records) assert any(rec.message.startswith('STDERR:') for rec in caplog.records) + +def test_exec(caplog): + cli = dcos_cli.DcosCli('') + process = cli.exec( + ['/bin/sh', '-c', 'echo "hello, world!"'] + ) + assert process.stdout == b'hello, world!\n' + assert process.stderr == b'' + assert process.returncode == 0 + assert any(rec.message.startswith('CMD:') for rec in caplog.records) + assert any(rec.message.startswith('STDOUT:') for rec in caplog.records) + assert any(rec.message.startswith('STDERR:') for rec in caplog.records) + +def test_exec_fail(caplog): + cli = dcos_cli.DcosCli('') + with pytest.raises(subprocess.CalledProcessError): + cli.exec(['/bin/sh', '-c', 'does-not-exist']) + assert any(rec.message.startswith('CMD:') for rec in caplog.records) + assert any(rec.message.startswith('STDERR:') for rec in caplog.records) + +def test_exec_fail_without_check(caplog): + cli = dcos_cli.DcosCli('') + process = cli.exec( + ['/bin/sh', '-c', 'does-not-exist'], + check=False + ) + assert process.stdout == b'' + assert process.stderr == b'/bin/sh: does-not-exist: command not found\n' + assert process.returncode == 127 + assert any(rec.message.startswith('CMD:') for rec in caplog.records) + assert any(rec.message.startswith('STDOUT:') for rec in caplog.records) + assert any(rec.message.startswith('STDERR:') for rec in caplog.records)