From 257f1df1d5fb1cbe611d11940af5062024ae2082 Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Tue, 21 May 2024 22:00:38 +0200 Subject: [PATCH] test: case: user_scripts: Add test Verify that: - Scripts are executed when the service is enabled - Scripts are not enabled when the service is disabled --- test/case/infix_services/user_scripts.py | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 test/case/infix_services/user_scripts.py diff --git a/test/case/infix_services/user_scripts.py b/test/case/infix_services/user_scripts.py new file mode 100755 index 00000000..efa1f7c5 --- /dev/null +++ b/test/case/infix_services/user_scripts.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# + +import random +import string + +import infamy + +def attach(env): + return (env.attach("target", "mgmt"), env.attach("target", "mgmt", "ssh")) + +with infamy.Test() as test: + with test.step("Initialize"): + env = infamy.Env(infamy.std_topology("1x1")) + target, tgtssh = attach(env) + + cookie = "".join((random.choices(string.ascii_lowercase, k=16))) + script = f"/cfg/user-scripts.d/infamy-user-script-test.sh" + output = f"/tmp/{cookie}.cookie" + + with test.step(f"Install {script}"): + tgtssh.runsh(f""" + set -e + + mkdir -p $(dirname {script}) + printf '#!/bin/sh\necho -n {cookie} >{output}\n' >{script} + chmod +x {script} + """) + + with test.step("Enable user scripts and reboot"): + target.put_config_dict("infix-services", { + "user-scripts": { + "enabled": True + } + }) + target.copy("running", "startup") + target.reboot() + infamy.util.wait_boot(target) or test.fail() + target, tgtssh = attach(env) + + with test.step(f"Verify that {script} has run"): + cat = tgtssh.runsh(f"cat {output}").stdout + assert cat == cookie, f"Read back {repr(cat)}, expected {repr(cookie)}" + + with test.step("Disable user scripts and reboot"): + target.put_config_dict("infix-services", { + "user-scripts": { + "enabled": False + } + }) + target.copy("running", "startup") + target.reboot() + infamy.util.wait_boot(target) or test.fail() + target, tgtssh = attach(env) + + with test.step(f"Verify that {script} has not run"): + exists = tgtssh.run(["test", "-f", f"{output}"]).returncode == 0 + assert not exists, f"Unexpectedly found {output}" + + with test.step(f"Remove {script}"): + tgtssh.runsh(f"rm {script}") + + test.succeed()