Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bin/docker-to-squash.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ def shell_command(command, print_stdout, print_stderr, raise_on_error, timeout_s
except Exception as ex:
if process and process.poll() is None:
process.kill()
raise Exception("Popen failure, " + str(ex))
if raise_on_error:
raise Exception("Popen failure, " + str(ex)) from ex
logging.debug("command %s failed: %s", command, ex)
finally:
# Note that finally clause executes even when there is no exception, hence the "return" statement
if timer:
timer.cancel()
return out, err, (process.returncode if process else -1)

# Don't return from the finally clause, it would discard the exception raised above.
return out, err, (process.returncode if process else -1)


def process_timeout(process):
Expand Down
13 changes: 9 additions & 4 deletions bin/test_docker_to_squash.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ def test_shell_command(self):
out, err, rc = dtsq.shell_command(cmd, True, True, True, timeout_sec=10)
self.assertEqual(0, rc, f"Failed cmd={cmd}\nrc={rc}\nout={out}\nerr={err}")

# expect failure
cmd = ["badcmd", "-l"]
out, err, rc = dtsq.shell_command(cmd, True, True, True, timeout_sec=10)
self.assertNotEqual(0, rc, f"Expected to fail cmd={cmd}\nrc={rc}\nout={out}\nerr={err}")
# expect failure, without raise_on_error the return code is returned
for cmd in (["badcmd", "-l"], ["false"]):
out, err, rc = dtsq.shell_command(cmd, True, True, False, timeout_sec=10)
self.assertNotEqual(0, rc, f"Expected to fail cmd={cmd}\nrc={rc}\nout={out}\nerr={err}")

# expect failure, with raise_on_error an exception is raised
for cmd in (["badcmd", "-l"], ["false"]):
with self.assertRaises(Exception, msg=f"Expected to raise cmd={cmd}"):
dtsq.shell_command(cmd, True, True, True, timeout_sec=10)

# TODO:
# def test_read_image_tag_to_hash(self):
Expand Down