From f4847fb4b450ea8ff532f3f8212312ca58266450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Thu, 2 Jan 2025 16:29:49 +0100 Subject: [PATCH] Test/imgtestlib: add write_build_info() and use it everywhere MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new helper function `write_build_info()` to imgtestlib, to ensure consistent way to write the build `info.json` and de-duplicate code. Modify all remaining places which read / write `info.json` to use functions from imgtestlib. Signed-off-by: Tomáš Hozza --- test/scripts/boot-image | 4 +--- test/scripts/build-image | 4 +--- test/scripts/imgtestlib.py | 9 +++++++++ test/scripts/upload-results | 7 ++----- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/test/scripts/boot-image b/test/scripts/boot-image index 73859f0cf7..9347b6a08a 100755 --- a/test/scripts/boot-image +++ b/test/scripts/boot-image @@ -186,9 +186,7 @@ def main(): # amend build info with boot success # search_path is the root of the build path (build/build_name) build_info["boot-success"] = True - info_file_path = os.path.join(search_path, "info.json") - with open(info_file_path, "w", encoding="utf-8") as info_fp: - json.dump(build_info, info_fp, indent=2) + testlib.write_build_info(search_path, build_info) if bib_image_id: # write a separate file with the bib image ID as filename to mark the boot success with that image bib_id_file = os.path.join(search_path, f"bib-{bib_image_id}") diff --git a/test/scripts/build-image b/test/scripts/build-image index 4bbb8bcfd2..1175cc76cd 100755 --- a/test/scripts/build-image +++ b/test/scripts/build-image @@ -62,9 +62,7 @@ def main(): "osbuild-commit": osbuild_commit, "commit": os.environ.get("CI_COMMIT_SHA", "N/A") } - info_file_path = os.path.join(build_dir, "info.json") - with open(info_file_path, "w", encoding="utf-8") as info_fp: - json.dump(build_info, info_fp, indent=2) + testlib.write_build_info(build_dir, build_info) if __name__ == "__main__": diff --git a/test/scripts/imgtestlib.py b/test/scripts/imgtestlib.py index 0614419d2f..2f69267d8c 100644 --- a/test/scripts/imgtestlib.py +++ b/test/scripts/imgtestlib.py @@ -546,3 +546,12 @@ def read_build_info(build_path: str) -> Dict: info_file_path = os.path.join(build_path, "info.json") with open(info_file_path, encoding="utf-8") as info_fp: return json.load(info_fp) + + +def write_build_info(build_path: str, data: Dict): + """ + Write the data to the info.json file in the build directory. + """ + info_file_path = os.path.join(build_path, "info.json") + with open(info_file_path, "w", encoding="utf-8") as info_fp: + json.dump(data, info_fp, indent=2) diff --git a/test/scripts/upload-results b/test/scripts/upload-results index ae326bc9da..a3be07eaa5 100755 --- a/test/scripts/upload-results +++ b/test/scripts/upload-results @@ -33,13 +33,10 @@ def main(): # add the PR number (gitlab branch name) to the info.json if available if pr_number := os.environ.get("CI_COMMIT_BRANCH"): - info_path = os.path.join(build_dir, "info.json") - with open(info_path, "r", encoding="utf-8") as info_fp: - build_info = json.load(info_fp) + build_info = testlib.read_build_info(build_dir) # strip the PR prefix build_info["pr"] = pr_number.removeprefix("PR-") - with open(info_path, "w", encoding="utf-8") as info_fp: - json.dump(build_info, info_fp, indent=2) + testlib.write_build_info(build_dir, build_info) s3url = testlib.gen_build_info_s3(distro, arch, manifest_id)