From eb59a9377d167f486356174cc391053c37096ff3 Mon Sep 17 00:00:00 2001 From: florian Date: Wed, 13 Jul 2022 22:57:36 +0200 Subject: [PATCH] tests - init refactoring for autoconf --- .github/workflows/dev.yml | 2 + tests/AutoconfTest.py | 118 ++++++++++++++++++++++++++++++++++++++ tests/DockerTest.py | 7 ++- tests/Test.py | 8 ++- tests/main.py | 28 ++++++--- 5 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 tests/AutoconfTest.py diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index bf7856cad..ae294fdc4 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -373,6 +373,8 @@ jobs: # Run tests - name: Run Docker tests run: ./tests/main.py "docker" + - name: Run Autoconf tests + run: ./tests/main.py "autoconf" - name: Temp stop tests run: exit 1 - name: Run autoconf tests diff --git a/tests/AutoconfTest.py b/tests/AutoconfTest.py new file mode 100644 index 000000000..b3c67f8c3 --- /dev/null +++ b/tests/AutoconfTest.py @@ -0,0 +1,118 @@ +from Test import Test +from os.path import isdir, join, isfile +from os import chown, walk, getenv, listdir +from shutil import copytree +from traceback import format_exc +from subprocess import run +from time import sleep + +class AutoconfTest(Test) : + + def __init__(self, name, timeout, tests) : + super().__init__(name, "autoconf", timeout, tests) + self._domains = { + r"www\.example\.com": getenv("TEST_DOMAIN1"), + r"auth\.example\.com": getenv("TEST_DOMAIN1"), + r"app1\.example\.com": getenv("TEST_DOMAIN1_1"), + r"app2\.example\.com": getenv("TEST_DOMAIN1_2"), + r"app3\.example\.com": getenv("TEST_DOMAIN1_3") + } + + def init() : + try : + if not Test.init() : + return False + proc = run("sudo chown -R root:root /tmp/bw-data", shell=True) + if proc.returncode != 0 : + raise(Exception("chown failed (autoconf stack)")) + if isdir("/tmp/autoconf") : + rmtree("/tmp/autoconf") + copytree("./integrations/autoconf", "/tmp/autoconf") + compose = "/tmp/autoconf/docker-compose.yml" + self._replace_in_file(compose, r"bunkerity/bunkerweb:.*$", "10.20.1.1:5000/bw-tests:latest") + self._replace_in_file(compose, r"bunkerity/bunkerweb-autoconf:.*$", "10.20.1.1:5000/bw-autoconf-tests:latest") + self._replace_in_file(compose, r"\./bw\-data:/", "/tmp/bw-data:/") + proc = run("docker-compose pull", cwd="/tmp/autoconf", shell=True) + if proc.returncode != 0 : + raise(Exception("docker-compose pull failed (autoconf stack)")) + proc = run("docker-compose up -d", cwd="/tmp/autoconf", shell=True) + if proc.returncode != 0 : + raise(Exception("docker-compose up failed (autoconf stack)")) + i = 0 + healthy = False + while i < 30 : + proc = run('docker inspect --format "{{json .State.Health }}" autoconf_mybunker_1', cwd="/tmp/autoconf", shell=True, capture_output=True) + if proc.returncode != 0 : + raise(Exception("docker-compose inspect failed (autoconf stack)")) + if "healthy" in proc.stdout : + healthy = True + break + sleep(1) + i += 1 + if not healthy : + raise(Exception("autoconf stack is not healthy")) + except : + self._log("exception while running AutoconfTest.init()\n" + format_exc(), error=True) + return False + return True + + def end() : + ret = True + try : + if not Test.end() : + return False + proc = run("docker-compose down -v", cwd="/tmp/autoconf", shell=True) + if proc.returncode != 0 : + ret = False + rmtree("/tmp/autoconf") + except : + self._log("exception while running AutoconfTest.end()\n" + format_exc(), error=True) + return False + return ret + + def _setup_test(self) : + try : + super()._setup_test() + test = "/tmp/tests/" + self._name + compose = "/tmp/tests/" + self._name + "/docker-compose.yml" + example_data = "./examples/" + self._name + "/bw-data" + self._replace_in_file(compose, r"bunkerity/bunkerweb:.*$", "10.20.1.1:5000/bw-tests:latest") + self._replace_in_file(compose, r"\./bw\-data:/", "/tmp/bw-data:/") + self._replace_in_file(compose, r"\- bw_data:/", "- /tmp/bw-data:/") + for ex_domain, test_domain in self._domains.items() : + self._replace_in_files(test, ex_domain, test_domain) + self._rename(test, ex_domain, test_domain) + setup = test + "/setup-autoconf.sh" + if isfile(setup) : + proc = run("sudo ./setup-autoconf.sh", cwd=test, shell=True) + if proc.returncode != 0 : + raise(Exception("setup-autoconf failed")) + if isdir(example_data) : + for cp_dir in listdir(example_data) : + if isdir(join(example_data, cp_dir)) : + copytree(join(example_data, cp_dir), join("/tmp/bw-data", cp_dir)) + proc = run("docker-compose pull", shell=True, cwd=test) + if proc.returncode != 0 : + raise(Exception("docker-compose pull failed")) + proc = run("docker-compose up -d", shell=True, cwd=test) + if proc.returncode != 0 : + raise(Exception("docker-compose up failed")) + except : + self._log("exception while running AutoconfTest._setup_test()\n" + format_exc(), error=True) + self._cleanup_test() + return False + self._cleanup_test() + return True + + def _cleanup_test(self) : + try : + test = "/tmp/tests/" + self._name + proc = run("docker-compose down -v", shell=True, cwd=test) + if proc.returncode != 0 : + raise(Exception("docker-compose down failed")) + super()._cleanup_test() + except : + self._log("exception while running AutoconfTest._setup_test()\n" + format_exc(), error=True) + return False + return True + \ No newline at end of file diff --git a/tests/DockerTest.py b/tests/DockerTest.py index c12031a02..d7a3d9e92 100644 --- a/tests/DockerTest.py +++ b/tests/DockerTest.py @@ -43,7 +43,9 @@ class DockerTest(Test) : self._rename(test, ex_domain, test_domain) setup = test + "/setup-docker.sh" if isfile(setup) : - run("./docker-setup.sh", cwd=test, shell=True, check=True) + proc = run("sudo ./setup-docker.sh", cwd=test, shell=True) + if proc.returncode != 0 : + raise(Exception("setup-docker failed")) if isdir(example_data) : for cp_dir in listdir(example_data) : if isdir(join(example_data, cp_dir)) : @@ -56,9 +58,10 @@ class DockerTest(Test) : raise(Exception("docker-compose up failed")) except : self._log("exception while running DockerTest._setup_test()\n" + format_exc(), error=True) + self._cleanup_test() return False + self._cleanup_test() return True - def _cleanup_test(self) : try : diff --git a/tests/Test.py b/tests/Test.py index 82f3746a6..711d77d93 100644 --- a/tests/Test.py +++ b/tests/Test.py @@ -45,6 +45,11 @@ class Test(ABC) : return False return True + # Class method + # called once all tests ended + def end() : + pass + # called before starting the tests # must be override if specific actions needs to be done def _setup_test(self) : @@ -64,7 +69,7 @@ class Test(ABC) : # called after running the tests def _cleanup_test(self) : try : - rmtree("/tmp/tests/" + self._name) + run("sudo rm -rf /tmp/tests/" + self._name, shell=True) except : self._log("exception while running Test._cleanup_test()\n" + format_exc(), error=True) return False @@ -89,7 +94,6 @@ class Test(ABC) : self._log("tests not ok, retrying in 1s ...", error=True) sleep(1) self._log("failed (timeout = " + str(self.__timeout) + "s)", error=True) - self._cleanup_test() return False # run a single test diff --git a/tests/main.py b/tests/main.py index bf940a630..a1e1fb43a 100755 --- a/tests/main.py +++ b/tests/main.py @@ -18,12 +18,17 @@ if len(argv) != 2 : exit(1) test_type = argv[1] -if not test_type in ["linux", "docker", "swarm", "kubernetes", "ansible"] : +if not test_type in ["linux", "docker", "autoconf", "swarm", "kubernetes", "ansible"] : log("TESTS", "❌", "Wrong type argument " + test_type) exit(1) log("TESTS", "ℹ️", "Starting tests for " + test_type + " ...") -if not Test.init() : +ret = False +end_fun = None +if test_type == "docker" : + ret = DockerTest.init() + end_fun = DockerTest.end +if not ret : log("TESTS", "❌", "Test.init() failed") exit(1) @@ -35,15 +40,20 @@ for example in glob("./examples/*") : if not test_type in tests["kinds"] : log("TESTS", "ℹ️", "Skipping tests for " + tests["name"] + " (not in kinds)") continue - for test in tests["tests"] : - test_obj = None - if test_type == "docker" : - test_obj = DockerTest(tests["name"], tests["timeout"], tests["tests"]) - if not test_obj.run_tests() : - log("TESTS", "❌", "Tests failed for " + tests["name"]) - _exit(1) + test_obj = None + if test_type == "docker" : + test_obj = DockerTest(tests["name"], tests["timeout"], tests["tests"]) + if not test_obj.run_tests() : + log("TESTS", "❌", "Tests failed for " + tests["name"]) + end_fun() + _exit(1) except : log("TESTS", "❌", "Exception while executing test for example " + example + " : " + format_exc()) + end_fun() exit(1) +if not end_fun() : + log("TESTS", "❌", "Test.end() failed") + exit(1) + log("TESTS", "ℹ️", "All tests finished for " + test_type + " !") \ No newline at end of file