tests - replace internal _log with logger.log

This commit is contained in:
florian 2022-07-14 13:42:06 +02:00
parent eb59a9377d
commit fdb8ca3cad
4 changed files with 106 additions and 30 deletions

View File

@ -0,0 +1,81 @@
version: '3'
services:
# APPLICATIONS
app1:
image: node
working_dir: /home/node/app
networks:
bw-services:
aliases:
- app1
volumes:
- ./js-app:/home/node/app
environment:
- NODE_ENV=production
command: bash -c "npm install express && node index.js"
labels:
- bunkerweb.USE_REVERSE_PROXY=yes
- bunkerweb.REVERSE_PROXY_URL=/
- bunkerweb.REVERSE_PROXY_HOST=http://app1:3000
- bunkerweb.REVERSE_PROXY_AUTH_REQUEST=/authelia
- bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SIGNIN_URL=https://auth.example.com/?rd=$$scheme%3A%2F%2F$$host$$request_uri
- bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SET=$$user $$upstream_http_remote_user;$$groups $$upstream_http_remote_groups;$$name $$upstream_http_remote_name;$$email $$upstream_http_remote_email
- bunkerweb.REVERSE_PROXY_HEADERS=Remote-User $$user;Remote-Groups $$groups;Remote-Name $$name;Remote-Email $$email
- bunkerweb.REVERSE_PROXY_URL_999=/authelia
- bunkerweb.REVERSE_PROXY_HOST_999=http://authelia:9091/api/verify
- bunkerweb.REVERSE_PROXY_HEADERS_999=X-Original-URL
app2:
image: tutum/hello-world
networks:
bw-services:
aliases:
- app2
labels:
- bunkerweb.SERVER_NAME=app2.example.com
- bunkerweb.USE_REVERSE_PROXY=yes
- bunkerweb.REVERSE_PROXY_URL=/
- bunkerweb.REVERSE_PROXY_HOST=http://app2
- bunkerweb.REVERSE_PROXY_AUTH_REQUEST=/authelia
- bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SIGNIN_URL=https://auth.example.com/?rd=$$scheme%3A%2F%2F$$host$$request_uri
- bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SET=$$user $$upstream_http_remote_user;$$groups $$upstream_http_remote_groups;$$name $$upstream_http_remote_name;$$email $$upstream_http_remote_email
- bunkerweb.REVERSE_PROXY_HEADERS=Remote-User $$user;Remote-Groups $$groups;Remote-Name $$name;Remote-Email $$email
- bunkerweb.REVERSE_PROXY_URL_999=/authelia
- bunkerweb.REVERSE_PROXY_HOST_999=http://authelia:9091/api/verify
- bunkerweb.REVERSE_PROXY_HEADERS_999=X-Original-URL
# AUTHELIA
authelia:
image: authelia/authelia
container_name: authelia
volumes:
- ./authelia:/config
restart: unless-stopped
healthcheck:
disable: true
environment:
- TZ=Europe/Paris
labels:
- bunkerweb.SERVER_NAME=auth.example.com
- bunkerweb.USE_REVERSE_PROXY=yes
- bunkerweb.REVERSE_PROXY_URL=/
- bunkerweb.REVERSE_PROXY_HOST=http://authelia:9091
- bunkerweb.REVERSE_PROXY_INTERCEPT_ERRORS=no
redis:
image: redis:alpine
container_name: redis
volumes:
- ./redis:/data
expose:
- 6379
restart: unless-stopped
environment:
- TZ=Europe/Paris
networks:
bw-services:
external:
name: bw-services

View File

@ -5,6 +5,7 @@ from shutil import copytree
from traceback import format_exc
from subprocess import run
from time import sleep
from logger import log
class AutoconfTest(Test) :
@ -52,7 +53,7 @@ class AutoconfTest(Test) :
if not healthy :
raise(Exception("autoconf stack is not healthy"))
except :
self._log("exception while running AutoconfTest.init()\n" + format_exc(), error=True)
log("AUTOCONF", "", "exception while running AutoconfTest.init()\n" + format_exc())
return False
return True
@ -66,7 +67,7 @@ class AutoconfTest(Test) :
ret = False
rmtree("/tmp/autoconf")
except :
self._log("exception while running AutoconfTest.end()\n" + format_exc(), error=True)
log("AUTOCONF", "", "exception while running AutoconfTest.end()\n" + format_exc())
return False
return ret
@ -74,7 +75,7 @@ class AutoconfTest(Test) :
try :
super()._setup_test()
test = "/tmp/tests/" + self._name
compose = "/tmp/tests/" + self._name + "/docker-compose.yml"
compose = "/tmp/tests/" + self._name + "/service.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:/")
@ -91,14 +92,14 @@ class AutoconfTest(Test) :
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)
proc = run("docker-compose -f autoconf.yml 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)
proc = run("docker-compose -f autoconf.yml 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)
log("AUTOCONF", "", "exception while running AutoconfTest._setup_test()\n" + format_exc())
self._cleanup_test()
return False
self._cleanup_test()
@ -107,12 +108,12 @@ class AutoconfTest(Test) :
def _cleanup_test(self) :
try :
test = "/tmp/tests/" + self._name
proc = run("docker-compose down -v", shell=True, cwd=test)
proc = run("docker-compose -f autoconf.yml 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)
log("AUTOCONF", "", "exception while running AutoconfTest._cleanup_test()\n" + format_exc())
return False
return True

View File

@ -4,6 +4,7 @@ from os import chown, walk, getenv, listdir
from shutil import copytree
from traceback import format_exc
from subprocess import run
from logger import log
class DockerTest(Test) :
@ -21,11 +22,11 @@ class DockerTest(Test) :
try :
if not Test.init() :
return False
for root, dirs, files in walk("/tmp/bw-data") :
for name in dirs + files :
chown(join(root, name), 101, 101)
proc = run("sudo chown -R 101:101 /tmp/bw-data", shell=True)
if proc.returncode != 0 :
raise(Exception("chown failed (autoconf stack)"))
except :
self._log("exception while running DockerTest.init()\n" + format_exc(), error=True)
log("DOCKER", "", "exception while running DockerTest.init()\n" + format_exc())
return False
return True
@ -57,7 +58,7 @@ class DockerTest(Test) :
if proc.returncode != 0 :
raise(Exception("docker-compose up failed"))
except :
self._log("exception while running DockerTest._setup_test()\n" + format_exc(), error=True)
log("DOCKER", "", "exception while running DockerTest._setup_test()\n" + format_exc())
self._cleanup_test()
return False
self._cleanup_test()
@ -71,7 +72,7 @@ class DockerTest(Test) :
raise(Exception("docker-compose down failed"))
super()._cleanup_test()
except :
self._log("exception while running DockerTest._setup_test()\n" + format_exc(), error=True)
log("DOCKER", "", "exception while running DockerTest._cleanup_test()\n" + format_exc())
return False
return True

View File

@ -9,6 +9,7 @@ from os import mkdir, makedirs, walk, chmod
from re import sub, search, MULTILINE
from datetime import datetime
from subprocess import run
from logger import log
class Test(ABC) :
@ -17,15 +18,7 @@ class Test(ABC) :
self.__kind = kind
self.__timeout = timeout
self.__tests = tests
self._log("instiantiated with " + str(len(tests)) + " tests and timeout of " + str(timeout) + "s")
def _log(self, msg, error=False) :
when = datetime.today().strftime("[%Y-%m-%d %H:%M:%S]")
what = self._name + " - " + self.__kind + " - " + msg
if error :
print(when + " " + what, flush=True, file=stderr)
else :
print(when + " " + what, flush=True)
log("TEST", "", "instiantiated with " + str(len(tests)) + " tests and timeout of " + str(timeout) + "s")
# Class method
# called once before running all the different tests for a given integration
@ -41,7 +34,7 @@ class Test(ABC) :
if not isdir("/tmp/tests") :
mkdir("/tmp/tests")
except :
print("exception while running Test.init()\n" + format_exc(), flush=True, file=stderr)
log("TEST", "", "exception while running Test.init()\n" + format_exc())
return False
return True
@ -62,7 +55,7 @@ class Test(ABC) :
run("sudo rm -rf /tmp/tests/" + self._name, shell=True)
copytree("./examples/" + self._name, "/tmp/tests/" + self._name)
except :
self._log("exception while running Test._setup_test()\n" + format_exc(), error=True)
log("TEST", "", "exception while running Test._setup_test()\n" + format_exc())
return False
return True
@ -71,7 +64,7 @@ class Test(ABC) :
try :
run("sudo rm -rf /tmp/tests/" + self._name, shell=True)
except :
self._log("exception while running Test._cleanup_test()\n" + format_exc(), error=True)
log("TEST", "", "exception while running Test._cleanup_test()\n" + format_exc())
return False
return True
@ -89,11 +82,11 @@ class Test(ABC) :
break
if all_ok :
elapsed = str(int(time() - start))
self._log("success (" + elapsed + "/" + str(self.__timeout) + "s)")
log("TEST", "", "success (" + elapsed + "/" + str(self.__timeout) + "s)")
return self._cleanup_test()
self._log("tests not ok, retrying in 1s ...", error=True)
log("TEST", "⚠️", "tests not ok, retrying in 1s ...")
sleep(1)
self._log("failed (timeout = " + str(self.__timeout) + "s)", error=True)
log("TEST", "", "failed (timeout = " + str(self.__timeout) + "s)")
return False
# run a single test
@ -107,7 +100,7 @@ class Test(ABC) :
r = get(ex_url, timeout=5)
return test["string"].casefold() in r.text.casefold()
except :
self._log("exception while running test of type " + test["type"] + " on URL " + test["url"] + "\n" + format_exc(), error=True)
log("TEST", "", "exception while running test of type " + test["type"] + " on URL " + test["url"] + "\n" + format_exc())
return False
raise(Exception("unknow test type " + test["type"]))