Skip to content

Commit 8a2390a

Browse files
authored
Merge pull request #272 from tkdchen/include-subprocess-stderr-in-logs
Include subprocess stderr in logs
2 parents f45b90b + 04d8179 commit 8a2390a

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

source-container-build/app/source_build.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from collections import defaultdict
1919
from dataclasses import dataclass, field
2020
from pathlib import Path
21-
from subprocess import run
21+
from subprocess import CalledProcessError, run
2222
from tarfile import TarInfo
2323
from typing import Any, TypedDict, NotRequired, Literal, Final, Dict
2424
from urllib.parse import urlparse
@@ -1085,7 +1085,12 @@ def main() -> int:
10851085
build_result = build(build_args)
10861086
except Exception as e:
10871087
build_result = {"status": "failure", "message": str(e)}
1088-
logger.exception("failed to build source image")
1088+
if isinstance(e, CalledProcessError):
1089+
logger.exception(
1090+
"command execution failure, status: %d, stderr: %s", e.returncode, e.stderr
1091+
)
1092+
else:
1093+
logger.exception("failed to build source image")
10891094

10901095
logger.info("build result %s", json.dumps(build_result))
10911096
if build_args.result_file:

source-container-build/app/test_source_build.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,15 @@ def _remove_the_invalid_git_repo():
716716
"--registry-allowlist",
717717
REGISTRY_ALLOWLIST,
718718
]
719-
with patch("sys.argv", cli_cmd):
720-
rc = source_build.main()
719+
720+
with self.assertLogs() as logs:
721+
with patch("sys.argv", cli_cmd):
722+
rc = source_build.main()
721723

722724
self.assertEqual(1, rc)
725+
self.assertRegex(
726+
"\n".join(logs.output), r"command execution failure, status: \d+, stderr: .+"
727+
)
723728

724729
with open(self.result_file, "r") as f:
725730
build_result: BuildResult = json.loads(f.read())

source-container-build/app/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ env_list = flake8,black,test
33

44
[testenv:test]
55
deps = -r requirements-dev.txt
6-
commands = python3 -m pytest --cov=source_build --cov-report=term {posargs:.}
6+
commands = python3 -m pytest --cov=source_build --cov-report=term --cov-report=html {posargs:.}
77

88
[testenv:flake8]
99
deps = flake8

0 commit comments

Comments
 (0)