Skip to content

Commit

Permalink
Feature/rcs/command error (#112)
Browse files Browse the repository at this point in the history
* run_async_command docstring text

* run_async_command encodings debug logging
  • Loading branch information
blueskyjunkie authored Dec 2, 2021
1 parent eb93e0d commit 9a3a46d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
25 changes: 24 additions & 1 deletion foodx_devops_tools/utilities/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import asyncio
import dataclasses
import locale
import logging
import pathlib
import shutil
Expand Down Expand Up @@ -88,6 +89,10 @@ async def run_async_command(
"""
Run an external command asynchronously.
Command logging is disabled by default to help prevent accidental
credential leakage to logs when commands may necessarily contain
sensitive data.
Args:
command: Command to be executed.
enable_logging: Enable command and argument logging (default disabled)
Expand All @@ -99,15 +104,33 @@ async def run_async_command(
# WARNING: logging is _disabled_ by default to prevent "default"
# leakage of secrets into logs via command arguments
log.debug("command to run, {0}".format(str(command)))

this_encoding = sys.getfilesystemencoding()
log.debug("sys.getdefaultencoding(), {0}".format(sys.getdefaultencoding()))
log.debug(
"locale.getpreferredencoding(), {0}".format(
locale.getpreferredencoding()
)
)
log.debug(
"sys.getfilesystemencoding(), {0}".format(sys.getfilesystemencoding())
)
this_process = await asyncio.create_subprocess_exec(
*command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)

stdout, stderr = await this_process.communicate()
log.debug(
"run_async_command stdout, {0}".format(stdout.decode(this_encoding))
)
log.debug(
"run_async_command stderr, {0}".format(stderr.decode(this_encoding))
)
result = CapturedStreams(
out=stdout.decode("utf-8"), error=stderr.decode("utf-8")
out=stdout.decode(this_encoding), error=stderr.decode(this_encoding)
)
if this_process.returncode != 0:
log.debug(f"command std out, {result.out}")
raise CommandError(
"External command run did not exit cleanly, {0}".format(
result.error
Expand Down
23 changes: 23 additions & 0 deletions tests/ci/unit_tests/utilities/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
run_async_command,
run_command,
)
from foodx_devops_tools.utilities.exceptions import CommandError


class TestRunCommand:
Expand Down Expand Up @@ -56,3 +57,25 @@ async def test_simple(self, mocker):
stderr=asyncio.subprocess.PIPE
)
assert result == expected_output

@pytest.mark.asyncio
async def test_error(self, mocker):
expected_output = CapturedStreams(out="", error="some error")
async_mock = AsyncMock()
async_mock.return_value.returncode = 1
async_mock.return_value.communicate.return_value = (
expected_output.out.encode(),
expected_output.error.encode(),
)
mocker.patch(
"foodx_devops_tools.utilities.command.asyncio"
".create_subprocess_exec",
side_effect=async_mock,
)
command = ["something", "--option"]

with pytest.raises(
CommandError,
match=r"^External command run did " r"not exit cleanly",
):
await run_async_command(command)

0 comments on commit 9a3a46d

Please sign in to comment.