tests - init refactoring for autoconf

This commit is contained in:
florian 2022-07-13 22:57:36 +02:00
parent 2e0542dbb0
commit eb59a9377d
5 changed files with 150 additions and 13 deletions

View file

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

118
tests/AutoconfTest.py Normal file
View file

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

View file

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

View file

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

View file

@ -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 + " !")