Skip to content

Commit

Permalink
Handle failing SSH commands gracefully (#162)
Browse files Browse the repository at this point in the history
Log output and exit code instead of throwing an CalledProcessError.

Signed-off-by: Georg Pfuetzenreuter <[email protected]>
  • Loading branch information
tacerus authored May 20, 2023
1 parent 55a1f0b commit f640646
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
TOX_PARALLEL_NO_SPINNER: 1
TOXENV: ${{ matrix.tox_env }}
FORCE_COLOR: 1
PYTEST_REQPASS: 24
PYTEST_REQPASS: 25

steps:
- name: Check vagrant presence
Expand Down
11 changes: 8 additions & 3 deletions src/vagrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,11 +1101,16 @@ def _run_vagrant_command(self, args) -> str:
# Make subprocess command
command = self._make_vagrant_command(args)
with self.err_cm() as err_fh:
return compat.decode(
subprocess.check_output(
try:
output = subprocess.check_output(
command, cwd=self.root, env=self.env, stderr=err_fh
)
)
except subprocess.CalledProcessError as err:
output = err.output
log.error(
"Command %s returned with exit code %i", command, err.returncode
)
return compat.decode(output)

def _stream_vagrant_command(self, args) -> Iterator[str]:
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,18 @@ def test_ssh_command(vm_dir):
assert output.strip() == "hello"


def test_bad_ssh_command(vm_dir, caplog):
"""
Test executing a failing command
"""
v = vagrant.Vagrant(vm_dir, quiet_stderr=False)
v.up()
v.ssh(command="logger -s oopsie; false")
assert "returned with exit code 1" in caplog.text
# https://github.com/pytest-dev/pytest/issues/5997
# and capsys.readouterr().err == 'vagrant: oopsie'


@pytest.mark.parametrize("vm_dir", (MULTIVM_VAGRANTFILE,), indirect=True)
def test_ssh_command_multivm(vm_dir):
"""
Expand Down

0 comments on commit f640646

Please sign in to comment.