From 30b1ea257b5b9ad496c0ecfbbc37d75d65b49f1d Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 8 Feb 2018 14:37:59 -0800 Subject: [PATCH] Add a test mode to /compose This will allow testing without having a full system setup with anaconda, if ?test=1 is passed to the POST /compose command it will wait 10 seconds instead of running Anaconda, and then raise an error to generate a failed build. Passing ?test=2 will also wait 10 seconds instead of running Anaconda, but will finish successfully. --- src/pylorax/api/compose.py | 6 +++++- src/pylorax/api/queue.py | 20 ++++++++++++++++++-- src/pylorax/api/v0.py | 9 ++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/pylorax/api/compose.py b/src/pylorax/api/compose.py index d0a58cbd..2cc10e54 100644 --- a/src/pylorax/api/compose.py +++ b/src/pylorax/api/compose.py @@ -76,7 +76,7 @@ def repo_to_ks(r, url="url"): return cmd -def start_build(cfg, yumlock, gitlock, branch, recipe_name, compose_type): +def start_build(cfg, yumlock, gitlock, branch, recipe_name, compose_type, test_mode=0): """ Start the build :param cfg: Configuration object @@ -190,6 +190,10 @@ def start_build(cfg, yumlock, gitlock, branch, recipe_name, compose_type): # Set the initial status open(joinpaths(results_dir, "STATUS"), "w").write("WAITING") + # Set the test mode, if requested + if test_mode > 0: + open(joinpaths(results_dir, "TEST"), "w").write("%s" % test_mode) + log.info("Adding %s with recipe %s output type %s to compose queue", build_id, recipe["name"], compose_type) os.symlink(results_dir, joinpaths(lib_dir, "queue/new/", build_id)) diff --git a/src/pylorax/api/queue.py b/src/pylorax/api/queue.py index d70c86d0..53be67d2 100644 --- a/src/pylorax/api/queue.py +++ b/src/pylorax/api/queue.py @@ -133,6 +133,9 @@ def make_compose(cfg, results_dir): raise RuntimeError("Missing config.toml for %s" % results_dir) cfg_dict = toml.loads(open(cfg_path, "r").read()) + # Make sure that image_name contains no path components + cfg_dict["image_name"] = os.path.basename(cfg_dict["image_name"]) + install_cfg = DataHolder(**cfg_dict) # Some kludges for the 99-copy-logs %post, failure in it will crash the build @@ -145,8 +148,21 @@ def make_compose(cfg, results_dir): log.debug("repo_url = %s, cfg = %s", repo_url, install_cfg) try: - novirt_install(install_cfg, joinpaths(results_dir, install_cfg.image_name), None, repo_url, - callback_func=cancel_build) + test_path = joinpaths(results_dir, "TEST") + if os.path.exists(test_path): + # Pretend to run the compose + time.sleep(10) + try: + test_mode = int(open(test_path, "r").read()) + except Exception: + test_mode = 1 + if test_mode == 1: + raise RuntimeError("TESTING FAILED compose") + else: + open(joinpaths(results_dir, install_cfg.image_name), "w").write("TEST IMAGE") + else: + novirt_install(install_cfg, joinpaths(results_dir, install_cfg.image_name), None, repo_url, + callback_func=cancel_build) finally: # Make sure that everything under the results directory is owned by the user user = pwd.getpwuid(cfg.uid).pw_name diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index ce19683e..debaad02 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -1330,6 +1330,13 @@ def v0_api(api): compose_type - The type of output to create, from /compose/types branch - Optional, defaults to master, selects the git branch to use for the recipe. """ + # Passing ?test=1 will generate a fake FAILED compose. + # Passing ?test=2 will generate a fake FINISHED compose. + try: + test_mode = int(request.args.get("test", "0")) + except ValueError: + test_mode = 0 + compose = request.get_json(cache=False) errors = [] @@ -1356,7 +1363,7 @@ def v0_api(api): try: build_id = start_build(api.config["COMPOSER_CFG"], api.config["YUMLOCK"], api.config["GITLOCK"], - branch, recipe_name, compose_type) + branch, recipe_name, compose_type, test_mode) except Exception as e: return jsonify(status=False, error={"msg":str(e)}), 400