From 593c6de385b5a21736e3da69873ae01150a46f22 Mon Sep 17 00:00:00 2001 From: Martin Sehnoutka Date: Fri, 26 Jul 2019 13:00:29 +0200 Subject: [PATCH] add timezone stage --- stages/org.osbuild.timezone | 30 ++++++++++++++++++++++++++++++ test/run-tests.py | 33 +++++++++++++++++++++++++++------ test/timezone-test.json | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 6 deletions(-) create mode 100755 stages/org.osbuild.timezone create mode 100644 test/timezone-test.json diff --git a/stages/org.osbuild.timezone b/stages/org.osbuild.timezone new file mode 100755 index 00000000..bc5234c4 --- /dev/null +++ b/stages/org.osbuild.timezone @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +import json +import subprocess +import sys +import os + + +def main(tree, options): + zone = options["zone"] + + # We need to remove the /etc/localtime symlink first, because it is created while we install RPM packages. + # systemd-firstboot expects that if /etc/localtime exists it is a user-defined value and does not change it, but + # the assumption is wrong, because it contains a default value from RPM package. + # This is (hopefully) a temporary workaround. + try: + os.remove(f"{tree}/etc/localtime") + # ^ This will fail once systemd RPM package stops shipping the file + except FileNotFoundError: + print("systemd does not ship /etc/localtime anymore. Remove the workaround") + + subprocess.run(["systemd-firstboot", f"--root={tree}", f"--timezone={zone}"], check=True) + + return 0 + + +if __name__ == '__main__': + args = json.load(sys.stdin) + r = main(args["tree"], args["options"]) + sys.exit(r) diff --git a/test/run-tests.py b/test/run-tests.py index ee5ed8f2..e352aedd 100644 --- a/test/run-tests.py +++ b/test/run-tests.py @@ -66,14 +66,35 @@ def test_web_server(): assert curl.stdout.decode("utf-8").strip() == "hello, world!" +def build_timezone_image(): + run_osbuild(rel_path("timezone-test.json")) + + +def test_timezone(): + extract_dir = tempfile.mkdtemp(prefix="osbuild-") + subprocess.run(["tar", "xf", OUTPUT_DIR + "/timezone-output.tar.xz"], cwd=extract_dir, check=True) + ls = subprocess.run(["ls", "-l", "etc/localtime"], cwd=extract_dir, check=True, stdout=subprocess.PIPE) + ls_output = ls.stdout.decode("utf-8") + assert "Europe/Prague" in ls_output + + +def evaluate_test(test): + try: + test() + print(f"{RESET}{BOLD}{test.__name__}: Success{RESET}") + except AssertionError as e: + print(f"{RESET}{BOLD}{test.__name__}: {RESET}{RED}Fail{RESET}") + print(e) + + if __name__ == '__main__': logging.info("Running tests") + build_web_server_image() tests = [test_web_server] with boot_image(IMAGE_PATH): for test in tests: - try: - test() - print(f"{RESET}{BOLD}{test.__name__}: Success{RESET}") - except AssertionError as e: - print(f"{RESET}{BOLD}{test.__name__}: {RESET}{RED}Fail{RESET}") - print(e) + evaluate_test(test) + + build_timezone_image() + evaluate_test(test_timezone) + diff --git a/test/timezone-test.json b/test/timezone-test.json new file mode 100644 index 00000000..cfa36a1b --- /dev/null +++ b/test/timezone-test.json @@ -0,0 +1,32 @@ +{ + "name": "Example Image", + "stages": [ + { + "name": "org.osbuild.dnf", + "options": { + "releasever": "30", + "repos": { + "fedora": { + "name": "Fedora", + "metalink": "https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch", + "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch" + } + }, + "packages": ["@Core"] + } + }, + { + "name": "org.osbuild.timezone", + "options": { + "zone": "Europe/Prague" + } + } + ], + "assembler": { + "name": "org.osbuild.tar", + "options": { + "filename": "timezone-output.tar.xz", + "compression": "xz" + } + } +}