bunkerweb/tests/Test.py

159 lines
5.4 KiB
Python
Raw Normal View History

from abc import ABC
2022-07-11 10:17:33 +00:00
from time import time, sleep
from requests import get
from traceback import format_exc
2022-07-13 19:23:58 +00:00
from shutil import copytree
2022-07-11 10:17:33 +00:00
from os.path import isdir, join
from os import getenv, mkdir, walk, rename
2022-07-13 19:43:04 +00:00
from re import sub, search, MULTILINE
2022-07-13 19:23:58 +00:00
from subprocess import run
from logger import setup_logger
2022-07-11 10:17:33 +00:00
class Test(ABC):
def __init__(self, name, kind, timeout, tests, no_copy_container=False, delay=0):
2022-07-11 10:17:33 +00:00
self._name = name
self.__kind = kind
self._timeout = timeout
2022-07-11 10:17:33 +00:00
self.__tests = tests
self._no_copy_container = no_copy_container
2022-07-27 12:44:46 +00:00
self.__delay = delay
self.__logger = setup_logger("Test", getenv("LOG_LEVEL", "INFO"))
self.__logger.info(
f"instantiated with {len(tests)} tests and timeout of {timeout}s for {self._name}",
)
2022-07-11 10:17:33 +00:00
# called once before running all the different tests for a given integration
@staticmethod
def init():
try:
if not isdir("/tmp/bw-data"):
2022-07-11 10:17:33 +00:00
mkdir("/tmp/bw-data")
2022-07-14 12:33:04 +00:00
run("sudo chmod 777 /tmp/bw-data", shell=True)
2022-07-11 10:17:33 +00:00
rm_dirs = ["configs", "plugins", "www"]
for rm_dir in rm_dirs:
if isdir(rm_dir):
run(f"sudo rm -rf /tmp/bw-data/{rm_dir}", shell=True)
if not isdir("/tmp/tests"):
2022-07-11 14:23:50 +00:00
mkdir("/tmp/tests")
except:
setup_logger("Test", getenv("LOG_LEVEL", "INFO")).error(
f"exception while running Test.init()\n{format_exc()}"
)
2022-07-11 10:17:33 +00:00
return False
return True
2022-07-13 20:57:36 +00:00
# called once all tests ended
@staticmethod
def end():
return True
2022-07-13 20:57:36 +00:00
2022-07-11 10:17:33 +00:00
# called before starting the tests
# must be override if specific actions needs to be done
def _setup_test(self):
try:
2022-07-11 14:23:50 +00:00
rm_dirs = ["configs", "plugins", "www"]
for rm_dir in rm_dirs:
if isdir(f"/tmp/bw-data/{rm_dir}"):
run(
f"sudo bash -c 'rm -rf /tmp/bw-data/{rm_dir}/*'",
shell=True,
)
if isdir(f"/tmp/tests/{self._name}"):
run(f"sudo rm -rf /tmp/tests/{self._name}", shell=True)
copytree(f"./examples/{self._name}", f"/tmp/tests/{self._name}")
except:
self.__logger.error(
f"exception while running Test._setup_test()\n{format_exc()}",
)
2022-07-11 10:17:33 +00:00
return False
return True
# called after running the tests
def _cleanup_test(self):
try:
run(f"sudo rm -rf /tmp/tests/{self._name}", shell=True)
except:
self.__logger.error(
f"exception while running Test._cleanup_test()\n{format_exc()}",
)
2022-07-11 10:17:33 +00:00
return False
return True
# run all the tests
def run_tests(self):
if not self._setup_test():
self._debug_fail()
2022-07-11 10:17:33 +00:00
return False
if self.__delay != 0:
self.__logger.info(f"delay is set, sleeping {self.__delay}s")
2022-07-27 12:44:46 +00:00
sleep(self.__delay)
2022-07-11 10:17:33 +00:00
start = time()
while time() < start + self._timeout:
2022-07-11 10:17:33 +00:00
all_ok = True
for test in self.__tests:
2022-07-11 10:17:33 +00:00
ok = self.__run_test(test)
2022-07-20 18:31:50 +00:00
sleep(1)
if not ok:
2022-07-11 10:17:33 +00:00
all_ok = False
break
if all_ok:
2022-07-11 10:17:33 +00:00
elapsed = str(int(time() - start))
self.__logger.info(
f"success ({elapsed}/{self._timeout}s)",
)
2022-07-11 10:17:33 +00:00
return self._cleanup_test()
self.__logger.warning("tests not ok, retrying in 1s ...")
2022-07-14 12:27:18 +00:00
self._debug_fail()
self._cleanup_test()
self.__logger.error(f"failed (timeout = {self._timeout}s)")
2022-07-11 10:17:33 +00:00
return False
# run a single test
def __run_test(self, test):
try:
2022-07-23 09:29:18 +00:00
ex_url = test["url"]
for ex_domain, test_domain in self._domains.items():
if search(ex_domain, ex_url):
2022-07-23 09:29:18 +00:00
ex_url = sub(ex_domain, test_domain, ex_url)
break
if test["type"] == "string":
r = get(ex_url, timeout=10)
return test["string"].casefold() in r.text.casefold()
elif test["type"] == "status":
r = get(ex_url, timeout=10)
2022-07-23 09:29:18 +00:00
return test["status"] == r.status_code
except:
2022-07-13 19:43:04 +00:00
return False
raise Exception(f"Unknown test type {test['type']}")
2022-07-11 10:17:33 +00:00
2022-07-14 12:27:18 +00:00
# called when tests fail : typical case is to show logs
def _debug_fail(self):
2022-07-14 12:27:18 +00:00
pass
@staticmethod
def replace_in_file(path, old, new):
try:
with open(path, "r") as f:
content = f.read()
content = sub(old, new, content, flags=MULTILINE)
with open(path, "w") as f:
f.write(content)
except:
setup_logger("Test", getenv("LOG_LEVEL", "INFO")).warning(
f"Can't replace file {path}"
)
2022-07-11 10:17:33 +00:00
def replace_in_files(path, old, new):
for root, dirs, files in walk(path):
for name in files:
Test.replace_in_file(join(root, name), old, new)
2022-07-11 14:23:50 +00:00
def rename(path, old, new):
for root, dirs, files in walk(path):
for name in dirs + files:
2022-07-11 10:17:33 +00:00
full_path = join(root, name)
2022-07-20 14:26:53 +00:00
new_path = sub(old, new, full_path)
if full_path != new_path:
rename(full_path, new_path)