From 746ddbc6e17d9ab8238cc4a8e3a348ccb44878b5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 8 Sep 2020 12:23:48 +0300 Subject: test: add a subplot to verify the roles work --- subplot/subplot.py | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 subplot/subplot.py (limited to 'subplot/subplot.py') diff --git a/subplot/subplot.py b/subplot/subplot.py new file mode 100644 index 0000000..4434f24 --- /dev/null +++ b/subplot/subplot.py @@ -0,0 +1,120 @@ +import logging +import os +import yaml + + +def fixme(ctx, **kwargs): + assert 0 + + +def create_vm(ctx): + QemuSystem = globals()["QemuSystem"] + srcdir = globals()["srcdir"] + + name = "debian-ansible-test" + base_image = "/home/liw/tmp/debian-10-openstack-amd64.qcow2" + GiB = 1024 ** 3 + disk_size = 10 * GiB + pubkey = open(os.path.join(srcdir, "ssh", "id.pub")).read().strip() + memory = 1 * GiB + cpus = 2 + username = "debian" + + logging.info("starting a VM using qemu-system") + logging.info(f" name : {name}") + logging.info(f" image : {base_image}") + logging.info(f" disk : {disk_size}") + logging.info(f" pubkey : {pubkey}") + logging.info(f" memory : {memory}") + logging.info(f" cpus : {cpus}") + logging.info(f" username: {username}") + + qemu = QemuSystem(name, base_image, disk_size, pubkey) + qemu.set_memory(memory) + qemu.set_vcpus(cpus) + qemu.set_username(username) + qemu.start() + + logging.debug("waiting for SSH to be ready") + if qemu.wait_for_ssh(): + logging.debug("SSH is ready") + else: + logging.error("SSH did not get ready") + assert 0 + logging.info("a qemu-system VM is up and running and accessible over SSH") + ctx["qemu"] = qemu + + +def destroy_vm(ctx): + logging.debug(f"destroying qemu running") + qemu = ctx["qemu"] + qemu.stop() + + +def run_true_on_host(ctx): + qemu = ctx["qemu"] + qemu.ssh(["/bin/true"]) + + +def use_role_in_playbook(ctx, role=None): + empty_playbook = { + "hosts": "test-host", + "remote_user": "debian", # FIXME: don't hardcode this + "become": True, + "roles": [], + } + playbook = ctx.get("playbook", dict(empty_playbook)) + playbook["roles"].append(role) + ctx["playbook"] = playbook + + +def set_vars_file(ctx, filename=None): + get_file = globals()["get_file"] + data = get_file(filename) + with open("vars.yml", "wb") as f: + f.write(data) + + +def run_playbook(ctx): + runcmd = globals()["runcmd"] + exit_code_zero = globals()["exit_code_zero"] + assert_ne = globals()["assert_ne"] + srcdir = globals()["srcdir"] + + with open("hosts", "w") as f: + f.write("test-host\n") + + if not os.path.exists("vars.yml"): + with open("vars.yml", "w") as f: + yaml.safe_dump([], stream=f) + + playbook = [ctx["playbook"]] + assert_ne(playbook, None) + with open("playbook.yml", "w") as f: + yaml.safe_dump(playbook, stream=f) + + ssh_opts = [ + "-ouserknownhostsfile=/dev/null", + "-ostricthostkeychecking=accept-new", + "-i", + os.path.join(srcdir, "ssh", "id"), + "-p2222", + ] + + env = dict(os.environ) + env["ANSIBLE_SSH_ARGS"] = " ".join(ssh_opts) + env["ANSIBLE_LOG"] = "ansible.log" + env["ANSIBLE_ROLES_PATH"] = os.path.join(srcdir, "roles") + + argv = [ + "ansible-playbook", + "-i", + "hosts", + f"-e@vars.yml", + "-eansible_ssh_host=localhost", + "-eansible_ssh_port=2222", + "playbook.yml", + ] + + runcmd(ctx, argv, env=env) + exit_code_zero(ctx) -- cgit v1.2.1