2022-07-18 14:30:46 +02:00
|
|
|
from Test import Test
|
|
|
|
from os.path import isdir, join, isfile
|
2022-07-19 12:32:54 +02:00
|
|
|
from os import chown, walk, getenv, listdir, mkdir, chmod
|
2022-07-18 14:30:46 +02:00
|
|
|
from shutil import copytree, rmtree
|
|
|
|
from traceback import format_exc
|
|
|
|
from subprocess import run
|
|
|
|
from time import sleep
|
|
|
|
from logger import log
|
|
|
|
|
|
|
|
class LinuxTest(Test) :
|
|
|
|
|
2022-07-18 17:05:05 +02:00
|
|
|
def __init__(self, name, timeout, tests, distro) :
|
2022-07-18 14:30:46 +02:00
|
|
|
super().__init__(name, "linux", timeout, tests)
|
|
|
|
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")
|
|
|
|
}
|
2022-07-18 17:05:05 +02:00
|
|
|
if not distro in ["ubuntu", "debian", "fedora", "centos"] :
|
|
|
|
raise(Exceptions("unknown distro " + distro))
|
|
|
|
self.__distro = distro
|
2022-07-18 14:30:46 +02:00
|
|
|
|
2022-07-18 22:00:14 +02:00
|
|
|
def init(distro) :
|
2022-07-18 14:30:46 +02:00
|
|
|
try :
|
|
|
|
if not Test.init() :
|
|
|
|
return False
|
2022-07-18 17:05:05 +02:00
|
|
|
# TODO : find the nginx uid/gid on Docker images
|
2022-07-18 14:30:46 +02:00
|
|
|
proc = run("sudo chown -R root:root /tmp/bw-data", shell=True)
|
|
|
|
if proc.returncode != 0 :
|
|
|
|
raise(Exception("chown failed (autoconf stack)"))
|
2022-07-18 22:16:33 +02:00
|
|
|
if isdir("/tmp/linux") :
|
2022-07-19 12:24:16 +02:00
|
|
|
rmtree("/tmp/linux")
|
2022-07-18 17:05:05 +02:00
|
|
|
mkdir("/tmp/linux")
|
|
|
|
chmod("/tmp/linux", 0o0777)
|
2022-07-19 15:13:40 +02:00
|
|
|
cmd = "docker run -p 80:80 -p 443:443 --rm --name linux-" + distro + " -d --tmpfs /tmp --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro bw-" + distro
|
2022-07-18 17:05:05 +02:00
|
|
|
proc = run(cmd, shell=True)
|
2022-07-18 14:30:46 +02:00
|
|
|
if proc.returncode != 0 :
|
2022-07-18 17:05:05 +02:00
|
|
|
raise(Exception("docker run failed (linux stack)"))
|
2022-07-18 22:00:14 +02:00
|
|
|
if distro in ["ubuntu", "debian"] :
|
2022-07-19 15:13:40 +02:00
|
|
|
cmd = "apt install -y /opt/\$(ls /opt | grep deb)"
|
2022-07-18 22:00:14 +02:00
|
|
|
elif distro in ["centos", "fedora"] :
|
2022-07-19 15:13:40 +02:00
|
|
|
cmd = "dnf install -y /opt/\$(ls /opt | grep rpm)"
|
|
|
|
proc = TestLinux.docker_exec(distro, cmd)
|
2022-07-18 14:30:46 +02:00
|
|
|
if proc.returncode != 0 :
|
2022-07-18 17:05:05 +02:00
|
|
|
raise(Exception("docker exec apt install failed (linux stack)"))
|
2022-07-19 15:13:40 +02:00
|
|
|
proc = LinuxTest.docker_exec(distro, "systemctl start bunkerweb")
|
2022-07-18 17:05:05 +02:00
|
|
|
if proc.returncode != 0 :
|
|
|
|
raise(Exception("docker exec systemctl start failed (linux stack)"))
|
2022-07-19 15:13:40 +02:00
|
|
|
cp_dirs = {
|
|
|
|
"/tmp/bw-data/letsencrypt": "/etc/letsencrypt",
|
|
|
|
"/tmp/bw-data/cache": "/opt/bunkerweb/cache"
|
|
|
|
}
|
|
|
|
for src, dst in cp_dirs.items() :
|
|
|
|
proc = LinuxTest.docker_cp(distro, src, dst)
|
|
|
|
if proc.returncode != 0 :
|
|
|
|
raise(Exception("docker cp failed for " + src + " (linux stack)"))
|
|
|
|
proc = LinuxTest.docker_exec(distro, "chown -R nginx:nginx " + dst + "/*")
|
|
|
|
if proc.returncode != 0 :
|
|
|
|
raise(Exception("docker exec failed for directory " + src + " (linux stack)"))
|
2022-07-18 14:30:46 +02:00
|
|
|
except :
|
2022-07-18 17:05:05 +02:00
|
|
|
log("LINUX", "❌", "exception while running LinuxTest.init()\n" + format_exc())
|
2022-07-18 14:30:46 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2022-07-18 22:00:14 +02:00
|
|
|
def end(distro) :
|
2022-07-18 14:30:46 +02:00
|
|
|
ret = True
|
|
|
|
try :
|
|
|
|
if not Test.end() :
|
|
|
|
return False
|
2022-07-18 22:00:14 +02:00
|
|
|
proc = run("docker kill linux-" + distro, shell=True)
|
2022-07-18 14:30:46 +02:00
|
|
|
if proc.returncode != 0 :
|
|
|
|
ret = False
|
|
|
|
except :
|
2022-07-18 17:05:05 +02:00
|
|
|
log("LINUX", "❌", "exception while running LinuxTest.end()\n" + format_exc())
|
2022-07-18 14:30:46 +02:00
|
|
|
return False
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def _setup_test(self) :
|
|
|
|
try :
|
|
|
|
super()._setup_test()
|
|
|
|
test = "/tmp/tests/" + self._name
|
|
|
|
example_data = "./examples/" + self._name + "/bw-data"
|
|
|
|
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)
|
|
|
|
Test.replace_in_files(test, "example.com", getenv("ROOT_DOMAIN"))
|
2022-07-18 17:05:05 +02:00
|
|
|
setup = test + "/setup-linux.sh"
|
2022-07-18 14:30:46 +02:00
|
|
|
if isfile(setup) :
|
2022-07-19 15:13:40 +02:00
|
|
|
proc = LinuxTest.docker_cp(self.__distro, "/tmp/" + self._name, "/opt/tests")
|
2022-07-18 14:30:46 +02:00
|
|
|
if proc.returncode != 0 :
|
2022-07-18 22:00:14 +02:00
|
|
|
raise(Exception("docker cp failed (linux stack)"))
|
2022-07-19 15:13:40 +02:00
|
|
|
proc = LinuxTest.docker_exec(self.__distro, "cd /opt/tests/" + self._name + " && ./setup-linux.sh")
|
2022-07-18 22:00:14 +02:00
|
|
|
if proc.returncode != 0 :
|
|
|
|
raise(Exception("docker exec setup failed (linux stack)"))
|
2022-07-18 14:30:46 +02:00
|
|
|
if isdir(example_data) :
|
|
|
|
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))
|
2022-07-19 14:08:47 +02:00
|
|
|
proc = LinuxTest.docker_exec(self.__distro, "systemctl restart bunkerweb")
|
2022-07-18 14:30:46 +02:00
|
|
|
if proc.returncode != 0 :
|
2022-07-18 17:05:05 +02:00
|
|
|
raise(Exception("docker exec systemctl restart failed (linux stack)"))
|
2022-07-18 14:30:46 +02:00
|
|
|
except :
|
2022-07-18 17:05:05 +02:00
|
|
|
log("LINUX", "❌", "exception while running LinuxTest._setup_test()\n" + format_exc())
|
2022-07-18 14:30:46 +02:00
|
|
|
self._cleanup_test()
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2022-07-18 17:05:05 +02:00
|
|
|
# def _cleanup_test(self) :
|
|
|
|
# try :
|
|
|
|
# super()._cleanup_test()
|
|
|
|
# except :
|
|
|
|
# log("AUTOCONF", "❌", "exception while running AutoconfTest._cleanup_test()\n" + format_exc())
|
|
|
|
# return False
|
|
|
|
# return True
|
2022-07-18 14:30:46 +02:00
|
|
|
|
2022-07-18 22:00:14 +02:00
|
|
|
def _debug_fail(self) :
|
2022-07-19 15:13:40 +02:00
|
|
|
LinuxTest.docker_exec(self.__distro, "cat /var/log/nginx/access.log ; cat /var/log/nginx/error.log ; journalctl -u bunkerweb --no-pager")
|
2022-07-18 22:00:14 +02:00
|
|
|
|
2022-07-19 14:08:47 +02:00
|
|
|
def docker_exec(distro, cmd_linux) :
|
2022-07-19 15:13:40 +02:00
|
|
|
return run("docker exec linux-" + distro + " /bin/bash -c \"" + cmd_linux + "\"", shell=True)
|
|
|
|
|
|
|
|
def docker_cp(distro, src, dst) :
|
|
|
|
return run("docker cp " + src + " linux-" + distro + ":" + dst)
|