This also changes the structure of the object store, though the basic idea is the same. The object store contains a directory of objects, which are content addressable filesystem trees. Currently we only ever use their content-hash internally, but the idea for this is basically Lars Karlitski and Kay Sievers' `treesum()`. We may exopse this in the future. Moreover, it contains a directory of refs, which are symlinks named by the stage id they correspond to (as before), pointing to an object generated from that stage-id. The ObjectStore exposes three method: `has_tree()`: This checks if the content store contains the given tree. If so, we can rely on the tree remaining there. `get_tree()`: This is meant to be used with a `with` block and yields the path to a read-only instance of the tree with the given id. If the tree_id is passed in as None, an empty directory is given instead. `new_tree()`: This is meant to be used with a `with` block and yields the path to a directory in which the tree by the given id should be created. If a base_id is passed in, the tree is initialized with the tree with the given id. Only when the block is exited successfully is the tree written to the content store, referenced by the id in question. Use this in Pipeline.run() to avoid regenerating trees unneccessarily. In order to trigger a regeneration, the content store must currently be manually flushed. Update the travis test to run the noop pipeline twice, verifying that the stage is only run the first time. Signed-off-by: Tom Gundersen <teg@jklm.no>
100 lines
3 KiB
Python
100 lines
3 KiB
Python
import contextlib
|
|
import logging
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
|
|
EXPECTED_TIME_TO_BOOT = 60 # seconds
|
|
RESET = "\033[0m"
|
|
BOLD = "\033[1m"
|
|
RED = "\033[31m"
|
|
OBJECTS = os.environ.get("OBJECTS", tempfile.mkdtemp(prefix="osbuild-"))
|
|
OUTPUT_DIR = os.environ.get("OUTPUT_DIR", tempfile.mkdtemp(prefix="osbuild-"))
|
|
OSBUILD = os.environ.get("OSBUILD", "osbuild").split(' ')
|
|
IMAGE_PATH = os.environ.get("IMAGE_PATH", OUTPUT_DIR + "/base.qcow2")
|
|
|
|
|
|
logging.basicConfig(level=logging.getLevelName(os.environ.get("TESTS_LOGLEVEL", "INFO")))
|
|
|
|
|
|
def run_osbuild(pipeline: str, check=True):
|
|
cmd = OSBUILD + ["--store", OBJECTS, "-o", OUTPUT_DIR, pipeline]
|
|
logging.info(f"Running osbuild: {cmd}")
|
|
osbuild = subprocess.run(cmd, capture_output=True)
|
|
if osbuild.returncode != 0:
|
|
logging.error(f"{RED}osbuild failed!{RESET}")
|
|
print(f"{BOLD}STDERR{RESET}")
|
|
print(osbuild.stderr.decode())
|
|
print(f"{BOLD}STDOUT{RESET}")
|
|
print(osbuild.stdout.decode())
|
|
if check:
|
|
sys.exit(1)
|
|
|
|
return osbuild.returncode
|
|
|
|
|
|
def rel_path(fname: str) -> str:
|
|
return os.path.join(os.path.dirname(__file__), fname)
|
|
|
|
|
|
def build_web_server_image():
|
|
run_osbuild(rel_path("4-all.json"))
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def boot_image(path: str):
|
|
acceleration = ["-accel", "kvm:hvf:tcg"]
|
|
network = ["-net", "nic,model=rtl8139", "-net", "user,hostfwd=tcp::8888-:8888"]
|
|
cmd = ["qemu-system-x86_64", "-nographic", "-m", "1024", "-snapshot"] + acceleration + [path] + network
|
|
logging.info(f"Booting image: {cmd}")
|
|
vm = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
try:
|
|
time.sleep(EXPECTED_TIME_TO_BOOT)
|
|
yield None
|
|
finally:
|
|
vm.kill()
|
|
|
|
|
|
def test_web_server():
|
|
cmd = ["curl", "-s", "http://127.0.0.1:8888/index"]
|
|
logging.info(f"Running curl: {cmd}")
|
|
curl = subprocess.run(cmd, capture_output=True)
|
|
logging.info(f"Curl returned: code={curl.returncode}, stdout={curl.stdout.decode()}, stderr={curl.stderr.decode()}")
|
|
assert curl.returncode == 0
|
|
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:
|
|
evaluate_test(test)
|
|
|
|
build_timezone_image()
|
|
evaluate_test(test_timezone)
|
|
|