bunkerized-nginx/tests/Test.py

135 lines
4.9 KiB
Python
Raw Normal View History

2022-07-11 12:17:33 +02:00
from abc import ABC, abstractmethod
from sys import stderr
from time import time, sleep
from requests import get
from traceback import format_exc
2022-07-13 21:23:58 +02:00
from shutil import copytree
2022-07-11 12:17:33 +02:00
from os.path import isdir, join
2022-07-20 16:26:53 +02:00
from os import mkdir, makedirs, walk, chmod, rename
2022-07-13 21:43:04 +02:00
from re import sub, search, MULTILINE
2022-07-13 17:27:21 +02:00
from datetime import datetime
2022-07-13 21:23:58 +02:00
from subprocess import run
from logger import log
2022-07-11 12:17:33 +02:00
class Test(ABC) :
def __init__(self, name, kind, timeout, tests) :
self._name = name
self.__kind = kind
self.__timeout = timeout
self.__tests = tests
2022-07-20 15:37:29 +02:00
log("TEST", "", "instiantiated with " + str(len(tests)) + " tests and timeout of " + str(timeout) + "s for " + self._name)
2022-07-11 12:17:33 +02:00
# Class method
# called once before running all the different tests for a given integration
def init() :
try :
if not isdir("/tmp/bw-data") :
mkdir("/tmp/bw-data")
2022-07-14 14:33:04 +02:00
run("sudo chmod 777 /tmp/bw-data", shell=True)
2022-07-11 12:17:33 +02:00
rm_dirs = ["configs", "plugins", "www"]
for rm_dir in rm_dirs :
if isdir(rm_dir) :
2022-07-13 21:26:44 +02:00
run("sudo rm -rf /tmp/bw-data/" + rm_dir, shell=True)
2022-07-11 16:23:50 +02:00
if not isdir("/tmp/tests") :
mkdir("/tmp/tests")
2022-07-11 12:17:33 +02:00
except :
log("TEST", "", "exception while running Test.init()\n" + format_exc())
2022-07-11 12:17:33 +02:00
return False
return True
2022-07-13 22:57:36 +02:00
# Class method
# called once all tests ended
def end() :
return True
2022-07-13 22:57:36 +02:00
2022-07-11 12:17:33 +02:00
# called before starting the tests
# must be override if specific actions needs to be done
def _setup_test(self) :
try :
2022-07-11 16:23:50 +02:00
rm_dirs = ["configs", "plugins", "www"]
for rm_dir in rm_dirs :
2022-07-13 21:23:58 +02:00
if isdir("/tmp/bw-data/" + rm_dir) :
2022-07-21 15:59:16 +02:00
run("sudo bash -c 'rm -rf /tmp/bw-data/" + rm_dir + "/*'", shell=True)
2022-07-11 12:17:33 +02:00
if isdir("/tmp/tests/" + self._name) :
2022-07-13 21:26:44 +02:00
run("sudo rm -rf /tmp/tests/" + self._name, shell=True)
2022-07-11 16:23:50 +02:00
copytree("./examples/" + self._name, "/tmp/tests/" + self._name)
2022-07-11 12:17:33 +02:00
except :
log("TEST", "", "exception while running Test._setup_test()\n" + format_exc())
2022-07-11 12:17:33 +02:00
return False
return True
# called after running the tests
def _cleanup_test(self) :
try :
2022-07-13 22:57:36 +02:00
run("sudo rm -rf /tmp/tests/" + self._name, shell=True)
2022-07-11 12:17:33 +02:00
except :
log("TEST", "", "exception while running Test._cleanup_test()\n" + format_exc())
2022-07-11 12:17:33 +02:00
return False
return True
# run all the tests
def run_tests(self) :
if not self._setup_test() :
return False
start = time()
while time() < start + self.__timeout :
all_ok = True
for test in self.__tests :
ok = self.__run_test(test)
2022-07-20 20:31:50 +02:00
sleep(1)
2022-07-11 12:17:33 +02:00
if not ok :
all_ok = False
break
if all_ok :
elapsed = str(int(time() - start))
log("TEST", "", "success (" + elapsed + "/" + str(self.__timeout) + "s)")
2022-07-11 12:17:33 +02:00
return self._cleanup_test()
log("TEST", "⚠️", "tests not ok, retrying in 1s ...")
2022-07-14 14:27:18 +02:00
self._debug_fail()
self._cleanup_test()
log("TEST", "", "failed (timeout = " + str(self.__timeout) + "s)")
2022-07-11 12:17:33 +02:00
return False
# run a single test
def __run_test(self, test) :
try :
2022-07-23 11:29:18 +02:00
ex_url = test["url"]
for ex_domain, test_domain in self._domains.items() :
if search(ex_domain, ex_url) :
ex_url = sub(ex_domain, test_domain, ex_url)
break
2022-07-11 12:17:33 +02:00
if test["type"] == "string" :
r = get(ex_url, timeout=5)
return test["string"].casefold() in r.text.casefold()
2022-07-23 11:29:18 +02:00
elif test["type"] == "status" :
r = get(ex_url, timeout=5)
return test["status"] == r.status_code
2022-07-11 12:17:33 +02:00
except :
#log("TEST", "❌", "exception while running test of type " + test["type"] + " on URL " + ex_url + "\n" + format_exc())
2022-07-13 21:43:04 +02:00
return False
raise(Exception("unknow test type " + test["type"]))
2022-07-11 12:17:33 +02:00
2022-07-14 14:27:18 +02:00
# called when tests fail : typical case is to show logs
def _debug_fail(self) :
pass
def replace_in_file(path, old, new) :
2022-07-11 12:17:33 +02:00
with open(path, "r") as f :
content = f.read()
2022-07-11 16:23:50 +02:00
content = sub(old, new, content, flags=MULTILINE)
2022-07-11 12:17:33 +02:00
with open(path, "w") as f :
f.write(content)
def replace_in_files(path, old, new) :
2022-07-11 16:23:50 +02:00
for root, dirs, files in walk(path) :
for name in files :
Test.replace_in_file(join(root, name), old, new)
2022-07-11 16:23:50 +02:00
def rename(path, old, new) :
2022-07-11 12:17:33 +02:00
for root, dirs, files in walk(path) :
for name in dirs + files :
full_path = join(root, name)
2022-07-20 16:26:53 +02:00
new_path = sub(old, new, full_path)
if full_path != new_path :
rename(full_path, new_path)