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.
This commit is contained in:
Brian C. Lane 2018-02-08 14:37:59 -08:00
parent 2e1e8c20c7
commit 8da6214e10
3 changed files with 31 additions and 4 deletions

View File

@ -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))

View File

@ -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

View File

@ -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