bunkerized-nginx/tests/AutoconfTest.py

124 lines
5.7 KiB
Python
Raw Normal View History

2022-07-13 22:57:36 +02:00
from Test import Test
from os.path import isdir, join, isfile
from os import chown, walk, getenv, listdir
2022-07-14 15:16:33 +02:00
from shutil import copytree, rmtree
2022-07-13 22:57:36 +02:00
from traceback import format_exc
from subprocess import run
from time import sleep
from logger import log
2022-07-13 22:57:36 +02:00
class AutoconfTest(Test) :
2022-07-27 14:44:46 +02:00
def __init__(self, name, timeout, tests, no_copy_container=False, delay=0) :
super().__init__(name, "autoconf", timeout, tests, no_copy_container=no_copy_container, delay=delay)
2022-07-13 22:57:36 +02:00
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"
Test.replace_in_file(compose, r"bunkerity/bunkerweb:.*$", "10.20.1.1:5000/bw-tests:latest")
Test.replace_in_file(compose, r"bunkerity/bunkerweb-autoconf:.*$", "10.20.1.1:5000/bw-autoconf-tests:latest")
Test.replace_in_file(compose, r"\./bw\-data:/", "/tmp/bw-data:/")
2022-07-13 22:57:36 +02:00
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)"))
2022-07-14 15:32:18 +02:00
if "healthy" in proc.stdout.decode() :
2022-07-13 22:57:36 +02:00
healthy = True
break
sleep(1)
i += 1
if not healthy :
raise(Exception("autoconf stack is not healthy"))
except :
log("AUTOCONF", "", "exception while running AutoconfTest.init()\n" + format_exc())
2022-07-13 22:57:36 +02:00
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 :
log("AUTOCONF", "", "exception while running AutoconfTest.end()\n" + format_exc())
2022-07-13 22:57:36 +02:00
return False
return ret
def _setup_test(self) :
try :
super()._setup_test()
test = "/tmp/tests/" + self._name
compose = "/tmp/tests/" + self._name + "/autoconf.yml"
example_data = "/tmp/tests/" + self._name + "/bw-data"
Test.replace_in_file(compose, r"bunkerity/bunkerweb:.*$", "10.20.1.1:5000/bw-tests:latest")
Test.replace_in_file(compose, r"\./bw\-data:/", "/tmp/bw-data:/")
Test.replace_in_file(compose, r"\- bw_data:/", "- /tmp/bw-data:/")
2022-07-13 22:57:36 +02:00
for ex_domain, test_domain in self._domains.items() :
Test.replace_in_files(test, ex_domain, test_domain)
Test.rename(test, ex_domain, test_domain)
2022-07-15 09:46:50 +02:00
Test.replace_in_files(test, "example.com", getenv("ROOT_DOMAIN"))
2022-07-13 22:57:36 +02:00
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) and not self._no_copy_container :
2022-07-20 16:32:20 +02:00
proc = run("sudo bash -c 'cp -rp " + example_data + "/* /tmp/bw-data'", shell=True)
2022-07-20 16:01:14 +02:00
if proc.returncode != 0 :
raise(Exception("cp bw-data failed"))
proc = run("docker-compose -f autoconf.yml pull", shell=True, cwd=test)
2022-07-13 22:57:36 +02:00
if proc.returncode != 0 :
raise(Exception("docker-compose pull failed"))
proc = run("docker-compose -f autoconf.yml up -d", shell=True, cwd=test)
2022-07-13 22:57:36 +02:00
if proc.returncode != 0 :
raise(Exception("docker-compose up failed"))
except :
log("AUTOCONF", "", "exception while running AutoconfTest._setup_test()\n" + format_exc())
2022-07-13 22:57:36 +02:00
self._cleanup_test()
return False
return True
def _cleanup_test(self) :
try :
test = "/tmp/tests/" + self._name
proc = run("docker-compose -f autoconf.yml down -v", shell=True, cwd=test)
2022-07-13 22:57:36 +02:00
if proc.returncode != 0 :
raise(Exception("docker-compose down failed"))
super()._cleanup_test()
except :
log("AUTOCONF", "", "exception while running AutoconfTest._cleanup_test()\n" + format_exc())
2022-07-13 22:57:36 +02:00
return False
return True
2022-07-14 14:27:18 +02:00
def _debug_fail(self) :
autoconf = "/tmp/autoconf"
proc = run("docker-compose logs", shell=True, cwd=autoconf)
test = "/tmp/tests/" + self._name
proc = run("docker-compose -f autoconf.yml logs", shell=True, cwd=test)