2022-07-15 13:20:14 +02:00
|
|
|
from Test import Test
|
2023-03-02 18:04:02 +01:00
|
|
|
from os.path import isdir, join, isfile
|
|
|
|
from os import chown, walk, getenv, listdir, mkdir
|
2022-07-15 13:20:14 +02:00
|
|
|
from shutil import copytree, rmtree, copy
|
|
|
|
from traceback import format_exc
|
|
|
|
from subprocess import run
|
|
|
|
from time import sleep
|
2023-03-02 18:04:02 +01:00
|
|
|
from logger import log
|
2023-03-29 22:03:24 +02:00
|
|
|
from yaml import safe_load_all, dump_all
|
2022-07-15 13:20:14 +02:00
|
|
|
|
2023-04-14 17:37:59 +02:00
|
|
|
|
2023-03-09 10:04:59 +01:00
|
|
|
class KubernetesTest(Test):
|
|
|
|
def __init__(self, name, timeout, tests, delay=0):
|
2022-07-27 14:44:46 +02:00
|
|
|
super().__init__(name, "kubernetes", timeout, tests, delay=delay)
|
2022-07-15 13:20:14 +02:00
|
|
|
self._domains = {
|
2023-04-14 17:37:59 +02:00
|
|
|
r"www\.example\.com": f"{Test.random_string(6)}.{getenv('TEST_DOMAIN1_2')}",
|
|
|
|
r"auth\.example\.com": f"{Test.random_string(1)}.{getenv('TEST_DOMAIN1_2')}",
|
|
|
|
r"app1\.example\.com": f"{Test.random_string(6)}.{getenv('TEST_DOMAIN1')}",
|
|
|
|
r"app2\.example\.com": f"{Test.random_string(6)}.{getenv('TEST_DOMAIN2')}",
|
|
|
|
r"app3\.example\.com": f"{Test.random_string(6)}.{getenv('TEST_DOMAIN3')}",
|
2022-07-15 13:20:14 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 10:04:59 +01:00
|
|
|
def init():
|
|
|
|
try:
|
|
|
|
if not Test.init():
|
2022-07-15 13:20:14 +02:00
|
|
|
return False
|
2023-02-16 15:15:19 +01:00
|
|
|
mkdir("/tmp/kubernetes")
|
2023-03-02 18:04:02 +01:00
|
|
|
copy("./misc/integrations/k8s.mariadb.yml", "/tmp/kubernetes/bunkerweb.yml")
|
2022-07-15 13:20:14 +02:00
|
|
|
deploy = "/tmp/kubernetes/bunkerweb.yml"
|
2023-03-29 22:03:24 +02:00
|
|
|
yamls = []
|
2023-04-14 17:37:59 +02:00
|
|
|
with open(deploy, "r") as f:
|
2023-03-29 22:03:24 +02:00
|
|
|
data = safe_load_all(f.read())
|
2023-04-03 12:15:54 +02:00
|
|
|
append_env = {
|
|
|
|
"AUTO_LETS_ENCRYPT": "yes",
|
|
|
|
"USE_LETS_ENCRYPT_STAGING": "yes",
|
|
|
|
"USE_REAL_IP": "yes",
|
|
|
|
"USE_PROXY_PROTOCOL": "yes",
|
2023-04-03 13:39:56 +02:00
|
|
|
"REAL_IP_FROM": "100.64.0.0/16",
|
2023-04-14 17:37:59 +02:00
|
|
|
"REAL_IP_HEADER": "proxy_protocol",
|
2023-04-03 12:15:54 +02:00
|
|
|
}
|
2023-04-14 17:37:59 +02:00
|
|
|
replace_env = {"API_WHITELIST_IP": "127.0.0.1/8 100.64.0.0/10"}
|
|
|
|
for yaml in data:
|
|
|
|
if yaml["metadata"]["name"] == "bunkerweb":
|
|
|
|
for k, v in append_env.items():
|
|
|
|
yaml["spec"]["template"]["spec"]["containers"][0]["env"].append(
|
|
|
|
{"name": k, "value": v}
|
|
|
|
)
|
|
|
|
for ele in yaml["spec"]["template"]["spec"]["containers"][0]["env"]:
|
|
|
|
if ele["name"] in replace_env:
|
2023-04-03 12:15:54 +02:00
|
|
|
ele["value"] = replace_env[ele["name"]]
|
2023-04-14 17:37:59 +02:00
|
|
|
if yaml["metadata"]["name"] in [
|
|
|
|
"bunkerweb",
|
|
|
|
"bunkerweb-controller",
|
|
|
|
"bunkerweb-scheduler",
|
|
|
|
]:
|
|
|
|
yaml["spec"]["template"]["spec"]["imagePullSecrets"] = [
|
|
|
|
{"name": "secret-registry"}
|
|
|
|
]
|
2023-03-29 22:03:24 +02:00
|
|
|
yamls.append(yaml)
|
2023-04-14 17:37:59 +02:00
|
|
|
with open(deploy, "w") as f:
|
2023-03-29 22:03:24 +02:00
|
|
|
f.write(dump_all(yamls))
|
2023-03-09 10:04:59 +01:00
|
|
|
Test.replace_in_file(
|
|
|
|
deploy,
|
|
|
|
r"bunkerity/bunkerweb:.*$",
|
2023-08-31 11:32:40 +02:00
|
|
|
f"ghcr.io/bunkerity/bunkerweb-tests:{getenv('IMAGE_TAG')}",
|
2023-03-09 10:04:59 +01:00
|
|
|
)
|
|
|
|
Test.replace_in_file(
|
|
|
|
deploy,
|
|
|
|
r"bunkerity/bunkerweb-autoconf:.*$",
|
2023-08-31 11:32:40 +02:00
|
|
|
f"ghcr.io/bunkerity/autoconf-tests:{getenv('IMAGE_TAG')}",
|
2023-03-09 10:04:59 +01:00
|
|
|
)
|
|
|
|
Test.replace_in_file(
|
|
|
|
deploy,
|
|
|
|
r"bunkerity/bunkerweb-scheduler:.*$",
|
2023-08-31 11:32:40 +02:00
|
|
|
f"ghcr.io/bunkerity/scheduler-tests:{getenv('IMAGE_TAG')}",
|
2023-03-09 10:04:59 +01:00
|
|
|
)
|
|
|
|
proc = run(
|
|
|
|
"kubectl apply -f bunkerweb.yml", cwd="/tmp/kubernetes", shell=True
|
|
|
|
)
|
|
|
|
if proc.returncode != 0:
|
|
|
|
raise (Exception("kubectl apply bunkerweb failed (k8s stack)"))
|
2022-07-29 11:30:12 +02:00
|
|
|
healthy = False
|
2022-07-15 13:20:14 +02:00
|
|
|
i = 0
|
2023-03-23 16:00:54 +01:00
|
|
|
while i < 120:
|
2023-03-09 10:04:59 +01:00
|
|
|
proc = run(
|
|
|
|
"kubectl get pods | grep bunkerweb | grep -v Running",
|
|
|
|
shell=True,
|
|
|
|
capture_output=True,
|
|
|
|
)
|
|
|
|
if "" == proc.stdout.decode():
|
2022-07-15 13:20:14 +02:00
|
|
|
healthy = True
|
|
|
|
break
|
|
|
|
sleep(1)
|
|
|
|
i += 1
|
2023-03-09 10:04:59 +01:00
|
|
|
if not healthy:
|
|
|
|
run(
|
|
|
|
"kubectl describe daemonset/bunkerweb",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
"kubectl logs daemonset/bunkerweb",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
"kubectl describe deployment/bunkerweb-controller",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
"kubectl logs deployment/bunkerweb-controller",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
"kubectl describe deployment/bunkerweb-scheduler",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
"kubectl logs deployment/bunkerweb-scheduler",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
2023-03-22 16:24:48 +01:00
|
|
|
run(
|
|
|
|
"kubectl describe deployment/bunkerweb-db",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
2023-03-09 10:04:59 +01:00
|
|
|
run(
|
|
|
|
"kubectl logs deployment/bunkerweb-db",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
"kubectl logs deployment/bunkerweb-redis",
|
|
|
|
cwd="/tmp/kubernetes",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
raise (Exception("k8s stack is not healthy"))
|
2022-07-15 13:20:14 +02:00
|
|
|
sleep(60)
|
2023-03-09 10:04:59 +01:00
|
|
|
except:
|
|
|
|
log(
|
|
|
|
"KUBERNETES",
|
|
|
|
"❌",
|
2023-04-14 17:37:59 +02:00
|
|
|
f"exception while running KubernetesTest.init()\n{format_exc()}",
|
2023-03-09 10:04:59 +01:00
|
|
|
)
|
2022-07-15 13:20:14 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-03-09 10:04:59 +01:00
|
|
|
def end():
|
2022-07-15 13:20:14 +02:00
|
|
|
ret = True
|
2023-03-09 10:04:59 +01:00
|
|
|
try:
|
|
|
|
if not Test.end():
|
2022-07-15 13:20:14 +02:00
|
|
|
return False
|
2023-03-09 10:04:59 +01:00
|
|
|
proc = run(
|
|
|
|
"kubectl delete -f bunkerweb.yml", cwd="/tmp/kubernetes", shell=True
|
|
|
|
)
|
|
|
|
if proc.returncode != 0:
|
2022-07-15 13:20:14 +02:00
|
|
|
ret = False
|
|
|
|
rmtree("/tmp/kubernetes")
|
2023-03-09 10:04:59 +01:00
|
|
|
except:
|
|
|
|
log(
|
|
|
|
"KUBERNETES",
|
|
|
|
"❌",
|
2023-04-14 17:37:59 +02:00
|
|
|
f"exception while running KubernetesTest.end()\n{format_exc()}",
|
2023-03-09 10:04:59 +01:00
|
|
|
)
|
2022-07-15 13:20:14 +02:00
|
|
|
return False
|
|
|
|
return ret
|
|
|
|
|
2023-03-09 10:04:59 +01:00
|
|
|
def _setup_test(self):
|
|
|
|
try:
|
2022-07-15 13:20:14 +02:00
|
|
|
super()._setup_test()
|
2023-04-14 17:37:59 +02:00
|
|
|
test = f"/tmp/tests/{self._name}"
|
|
|
|
deploy = f"/tmp/tests/{self._name}/kubernetes.yml"
|
|
|
|
example_data = f"./examples/{self._name}/bw-data"
|
2023-03-09 10:04:59 +01:00
|
|
|
for ex_domain, test_domain in self._domains.items():
|
2022-07-15 13:20:14 +02:00
|
|
|
Test.replace_in_files(test, ex_domain, test_domain)
|
|
|
|
Test.rename(test, ex_domain, test_domain)
|
|
|
|
Test.replace_in_files(test, "example.com", getenv("ROOT_DOMAIN"))
|
2023-04-14 17:37:59 +02:00
|
|
|
setup = f"{test}/setup-kubernetes.sh"
|
2023-03-09 10:04:59 +01:00
|
|
|
if isfile(setup):
|
2023-03-02 18:04:02 +01:00
|
|
|
proc = run("./setup-kubernetes.sh", cwd=test, shell=True)
|
2023-03-09 10:04:59 +01:00
|
|
|
if proc.returncode != 0:
|
|
|
|
raise (Exception("setup-kubernetes failed"))
|
2023-02-16 15:15:19 +01:00
|
|
|
proc = run("kubectl apply -f kubernetes.yml", shell=True, cwd=test)
|
2023-03-09 10:04:59 +01:00
|
|
|
if proc.returncode != 0:
|
|
|
|
raise (Exception("kubectl apply failed"))
|
|
|
|
except:
|
|
|
|
log(
|
|
|
|
"KUBERNETES",
|
|
|
|
"❌",
|
2023-04-14 17:37:59 +02:00
|
|
|
f"exception while running KubernetesTest._setup_test()\n{format_exc()}",
|
2023-03-09 10:04:59 +01:00
|
|
|
)
|
2022-07-15 13:20:14 +02:00
|
|
|
self._cleanup_test()
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-03-09 10:04:59 +01:00
|
|
|
def _cleanup_test(self):
|
|
|
|
try:
|
2023-04-14 17:37:59 +02:00
|
|
|
test = f"/tmp/tests/{self._name}"
|
|
|
|
cleanup = f"{test}/cleanup-kubernetes.sh"
|
2023-03-09 10:04:59 +01:00
|
|
|
if isfile(cleanup):
|
2023-03-02 18:04:02 +01:00
|
|
|
proc = run("./cleanup-kubernetes.sh", cwd=test, shell=True)
|
2023-03-09 10:04:59 +01:00
|
|
|
if proc.returncode != 0:
|
|
|
|
raise (Exception("cleanup-kubernetes failed"))
|
2023-02-16 15:15:19 +01:00
|
|
|
proc = run("kubectl delete -f kubernetes.yml", shell=True, cwd=test)
|
2023-03-09 10:04:59 +01:00
|
|
|
if proc.returncode != 0:
|
|
|
|
raise (Exception("kubectl delete failed"))
|
2022-07-15 13:20:14 +02:00
|
|
|
super()._cleanup_test()
|
2023-03-09 10:04:59 +01:00
|
|
|
except:
|
|
|
|
log(
|
|
|
|
"KUBERNETES",
|
|
|
|
"❌",
|
2023-04-14 17:37:59 +02:00
|
|
|
f"exception while running KubernetesTest._cleanup_test()\n{format_exc()}",
|
2023-03-09 10:04:59 +01:00
|
|
|
)
|
2022-07-15 13:20:14 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-03-09 10:04:59 +01:00
|
|
|
def _debug_fail(self):
|
|
|
|
proc = run(
|
|
|
|
'kubectl get pods --no-headers -o custom-columns=":metadata.name"',
|
|
|
|
shell=True,
|
|
|
|
capture_output=True,
|
|
|
|
)
|
|
|
|
for pod in proc.stdout.decode().splitlines():
|
2023-04-14 17:37:59 +02:00
|
|
|
run(f"kubectl logs {pod}", shell=True)
|