test-case-generators: report errors from child processes

The current implementation does not report any errors because it uses
the subprocess.check_output function from Python std library. The
function is convenient to use but it hides all the errors from us which
makes it very inconvenient when the test generation often fails (like
developing support for alternative architecture). This patch introduces
a wrapper around the subprocess.run function which dumps the
subprocess's stdout to stderr in case of failure and forwards it to the
caller in case of success. This way the generation code is still very
straight forward, but the errors are reported properly.
This commit is contained in:
Martin Sehnoutka 2020-04-06 16:52:31 +02:00 committed by Tom Gundersen
parent 0eb3bfe89a
commit 24b25bd06c
2 changed files with 22 additions and 5 deletions

View file

@ -26,9 +26,18 @@ It then outputs a json test case as standard output.
'''
def get_subprocess_stdout(*args, **kwargs):
sp = subprocess.run(*args, **kwargs, stdout=subprocess.PIPE)
if sp.returncode != 0:
sys.stderr.write(sp.stdout)
sys.exit(1)
return sp.stdout
def run_osbuild(manifest, store):
osbuild_cmd = ["osbuild", "--store", store, "--json", "-"]
result = json.loads(subprocess.check_output(osbuild_cmd, encoding="utf-8", input=json.dumps(manifest)))
result = json.loads(get_subprocess_stdout(osbuild_cmd, encoding="utf-8", input=json.dumps(manifest)))
return result.get("output_id")
@ -37,10 +46,10 @@ def main(test_case, store):
compose_request = json.dumps(test_case["compose-request"])
pipeline_command = ["go", "run", "./cmd/osbuild-pipeline", "-"]
test_case["manifest"] = json.loads(subprocess.check_output(pipeline_command, input=compose_request, encoding="utf-8"))
test_case["manifest"] = json.loads(get_subprocess_stdout(pipeline_command, input=compose_request, encoding="utf-8"))
pipeline_command = ["go", "run", "./cmd/osbuild-pipeline", "-rpmmd", "-"]
test_case["rpmmd"] = json.loads(subprocess.check_output(pipeline_command, input=compose_request, encoding="utf-8"))
test_case["rpmmd"] = json.loads(get_subprocess_stdout(pipeline_command, input=compose_request, encoding="utf-8"))
if boot_type != "nspawn-extract":
output_id = run_osbuild(test_case["manifest"], store)
@ -53,7 +62,7 @@ def main(test_case, store):
with open(fn, "w") as f:
subprocess.run(["xz", "--decompress", "--stdout", image_file], stdout=f)
image_file = fn
test_case["image-info"] = json.loads(subprocess.check_output(["tools/image-info", image_file]))
test_case["image-info"] = json.loads(get_subprocess_stdout(["tools/image-info", image_file], encoding="utf-8"))
return test_case

View file

@ -6,6 +6,14 @@ import json
import os
import sys
def get_subprocess_stdout(*args, **kwargs):
sp = subprocess.run(*args, **kwargs, stdout=subprocess.PIPE)
if sp.returncode != 0:
sys.stderr.write(sp.stdout)
sys.exit(1)
return sp.stdout
def main(distro, arch, store, output):
with open("tools/test-case-generators/format-request-map.json") as format_request_json:
format_request_dict = json.load(format_request_json)
@ -15,7 +23,7 @@ def main(distro, arch, store, output):
test_case_request["compose-request"]["distro"] = distro
test_case_request["compose-request"]["arch"] = arch
test_case_request["compose-request"]["repositories"] = repos_dict[distro][arch]
test_case = json.loads(subprocess.check_output(["tools/test-case-generators/generate-test-case", store], input=json.dumps(test_case_request), encoding="utf-8"))
test_case = json.loads(get_subprocess_stdout(["tools/test-case-generators/generate-test-case", store], input=json.dumps(test_case_request), encoding="utf-8"))
name = distro.replace("-", "_") + "-" + arch + "-" + output_format.replace("-", "_") + "-boot.json"
file_name = output + "/" + name
with open(file_name, 'w') as case_file: