From 4d61e96e476d67fb7e81e1d7e13e743258ee1e4c Mon Sep 17 00:00:00 2001 From: bunkerity Date: Mon, 18 Jul 2022 17:05:05 +0200 Subject: [PATCH] tests - LinuxTest on the road --- tests/Dockerfile-centos | 1 - tests/Dockerfile-debian | 3 +- tests/Dockerfile-fedora | 3 +- tests/Dockerfile-ubuntu | 3 +- tests/LinuxTest.py | 104 +++++++++++++++++----------------------- 5 files changed, 47 insertions(+), 67 deletions(-) diff --git a/tests/Dockerfile-centos b/tests/Dockerfile-centos index b662cb55e..0a0e97825 100644 --- a/tests/Dockerfile-centos +++ b/tests/Dockerfile-centos @@ -19,7 +19,6 @@ RUN dnf install yum-utils epel-release -y && \ dnf install nginx-1.20.2 -y COPY ./packages/centos/*.rpm /opt -RUN dnf install -y /opt/*.rpm VOLUME /run /tmp diff --git a/tests/Dockerfile-debian b/tests/Dockerfile-debian index cad089b47..2c91c1958 100644 --- a/tests/Dockerfile-debian +++ b/tests/Dockerfile-debian @@ -32,8 +32,7 @@ RUN apt update && \ apt-get install -y --no-install-recommends nginx=${NGINX_VERSION}-1~bullseye COPY ./packages/debian/*.deb /opt -RUN apt install -y /opt/*.deb -VOLUME [ "/sys/fs/cgroup" ] +VOLUME ["/sys/fs/cgroup"] CMD ["/lib/systemd/systemd"] diff --git a/tests/Dockerfile-fedora b/tests/Dockerfile-fedora index 6e43ce8e3..2e74eb859 100644 --- a/tests/Dockerfile-fedora +++ b/tests/Dockerfile-fedora @@ -23,8 +23,7 @@ RUN dnf update -y && \ dnf install nginx-1.20.2 -y COPY ./packages/fedora/*.rpm /opt -RUN dnf install -y /opt/*.rpm -VOLUME [ "/sys/fs/cgroup" ] +VOLUME ["/sys/fs/cgroup"] CMD ["/usr/sbin/init"] \ No newline at end of file diff --git a/tests/Dockerfile-ubuntu b/tests/Dockerfile-ubuntu index 46f7244ae..6cbfe2b2f 100644 --- a/tests/Dockerfile-ubuntu +++ b/tests/Dockerfile-ubuntu @@ -32,8 +32,7 @@ RUN apt update && \ apt-get install -y --no-install-recommends nginx=${NGINX_VERSION}-1~jammy COPY ./packages/ubuntu/*.deb /opt -RUN apt install -y /opt/*.deb -VOLUME [ "/sys/fs/cgroup" ] +VOLUME ["/sys/fs/cgroup"] CMD ["/lib/systemd/systemd"] \ No newline at end of file diff --git a/tests/LinuxTest.py b/tests/LinuxTest.py index 50a93f6cf..e458c65f3 100644 --- a/tests/LinuxTest.py +++ b/tests/LinuxTest.py @@ -1,6 +1,6 @@ from Test import Test from os.path import isdir, join, isfile -from os import chown, walk, getenv, listdir +from os import chown, walk, getenv, listdir, mkdir from shutil import copytree, rmtree from traceback import format_exc from subprocess import run @@ -9,7 +9,7 @@ from logger import log class LinuxTest(Test) : - def __init__(self, name, timeout, tests) : + def __init__(self, name, timeout, tests, distro) : super().__init__(name, "linux", timeout, tests) self._domains = { r"www\.example\.com": getenv("TEST_DOMAIN1"), @@ -18,43 +18,39 @@ class LinuxTest(Test) : r"app2\.example\.com": getenv("TEST_DOMAIN1_2"), r"app3\.example\.com": getenv("TEST_DOMAIN1_3") } + if not distro in ["ubuntu", "debian", "fedora", "centos"] : + raise(Exceptions("unknown distro " + distro)) + self.__distro = distro def init() : try : if not Test.init() : return False - proc = run("", shell=True") + # TODO : find the nginx uid/gid on Docker images proc = run("sudo chown -R root:root /tmp/bw-data", shell=True) if proc.returncode != 0 : raise(Exception("chown failed (autoconf stack)")) - if isdir("/tmp/autoconf") : - rmtree("/tmp/autoconf") - copytree("./integrations/autoconf", "/tmp/autoconf") - compose = "/tmp/autoconf/docker-compose.yml" - Test.replace_in_file(compose, r"bunkerity/bunkerweb:.*$", "10.20.1.1:5000/bw-tests:latest") - Test.replace_in_file(compose, r"bunkerity/bunkerweb-autoconf:.*$", "10.20.1.1:5000/bw-autoconf-tests:latest") - Test.replace_in_file(compose, r"\./bw\-data:/", "/tmp/bw-data:/") - proc = run("docker-compose pull", cwd="/tmp/autoconf", shell=True) + if isdir("/tmp/linux") + rmdir("/tmp/linux") + mkdir("/tmp/linux") + chmod("/tmp/linux", 0o0777) + cmd = "docker run -v /tmp/bw-data/letsencrypt:/etc/letsencrypt -v /tmp/bw-data/cache:/opt/bunkerweb/cache -v /tmp/bw-data/configs:/opt/bunkerweb/configs -v /tmp/bw-data/www:/opt/bunkerweb/www -v /tmp/linux/variables.env:/opt/bunkerweb/variables.env -p 80:80 -p 443:443 --rm --name linux-" + self.__distro + " -d --tmpfs /tmp --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro bw-" + self.__distro + proc = run(cmd, shell=True) if proc.returncode != 0 : - raise(Exception("docker-compose pull failed (autoconf stack)")) - proc = run("docker-compose up -d", cwd="/tmp/autoconf", shell=True) + raise(Exception("docker run failed (linux stack)")) + cmd = "docker exec linux-" + self.__distro + " " + if self.__distro in ["ubuntu", "debian"] : + cmd += " apt install -y /opt/*.deb" + elif self.__distro in ["centos", "fedora"] : + cmd += " dnf install -y /opt/*.rpm" + proc = run(cmd, shell=True) if proc.returncode != 0 : - raise(Exception("docker-compose up failed (autoconf stack)")) - i = 0 - healthy = False - while i < 30 : - proc = run('docker inspect --format "{{json .State.Health }}" autoconf_mybunker_1', cwd="/tmp/autoconf", shell=True, capture_output=True) - if proc.returncode != 0 : - raise(Exception("docker-compose inspect failed (autoconf stack)")) - if "healthy" in proc.stdout.decode() : - healthy = True - break - sleep(1) - i += 1 - if not healthy : - raise(Exception("autoconf stack is not healthy")) + raise(Exception("docker exec apt install failed (linux stack)")) + proc = run("systemctl start bunkerweb", shell=True) + if proc.returncode != 0 : + raise(Exception("docker exec systemctl start failed (linux stack)")) except : - log("AUTOCONF", "❌", "exception while running AutoconfTest.init()\n" + format_exc()) + log("LINUX", "❌", "exception while running LinuxTest.init()\n" + format_exc()) return False return True @@ -63,12 +59,11 @@ class LinuxTest(Test) : try : if not Test.end() : return False - proc = run("docker-compose down -v", cwd="/tmp/autoconf", shell=True) + proc = run("docker kill linux-" + self.__distro, shell=True) if proc.returncode != 0 : ret = False - rmtree("/tmp/autoconf") except : - log("AUTOCONF", "❌", "exception while running AutoconfTest.end()\n" + format_exc()) + log("LINUX", "❌", "exception while running LinuxTest.end()\n" + format_exc()) return False return ret @@ -76,50 +71,39 @@ class LinuxTest(Test) : try : super()._setup_test() test = "/tmp/tests/" + self._name - compose = "/tmp/tests/" + self._name + "/autoconf.yml" example_data = "./examples/" + self._name + "/bw-data" - Test.replace_in_file(compose, r"bunkerity/bunkerweb:.*$", "10.20.1.1:5000/bw-tests:latest") - Test.replace_in_file(compose, r"\./bw\-data:/", "/tmp/bw-data:/") - Test.replace_in_file(compose, r"\- bw_data:/", "- /tmp/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")) - setup = test + "/setup-autoconf.sh" + setup = test + "/setup-linux.sh" if isfile(setup) : - proc = run("sudo ./setup-autoconf.sh", cwd=test, shell=True) + proc = run("sudo ./setup-linux.sh", cwd=test, shell=True) if proc.returncode != 0 : - raise(Exception("setup-autoconf failed")) + raise(Exception("setup-linux failed")) 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)) - proc = run("docker-compose -f autoconf.yml pull", shell=True, cwd=test) + proc = run("systemctl restart bunkerweb", shell=True) if proc.returncode != 0 : - raise(Exception("docker-compose pull failed")) - proc = run("docker-compose -f autoconf.yml up -d", shell=True, cwd=test) - if proc.returncode != 0 : - raise(Exception("docker-compose up failed")) + raise(Exception("docker exec systemctl restart failed (linux stack)")) except : - log("AUTOCONF", "❌", "exception while running AutoconfTest._setup_test()\n" + format_exc()) + log("LINUX", "❌", "exception while running LinuxTest._setup_test()\n" + format_exc()) self._cleanup_test() return False return True - def _cleanup_test(self) : - try : - test = "/tmp/tests/" + self._name - 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 : - log("AUTOCONF", "❌", "exception while running AutoconfTest._cleanup_test()\n" + format_exc()) - return False - return True + # def _cleanup_test(self) : + # try : + # super()._cleanup_test() + # except : + # log("AUTOCONF", "❌", "exception while running AutoconfTest._cleanup_test()\n" + format_exc()) + # return False + # return True - def _debug_fail(self) : - autoconf = "/tmp/autoconf" - proc = run("docker-compose logs", shell=True, cwd=autoconf) - test = "/tmp/tests/" + self._name - proc = run("docker-compose -f autoconf.yml logs", shell=True, cwd=test) \ No newline at end of file + # def _debug_fail(self) : + # cmd = "cat /var/log/nginx/access.log ;" + # proc = run("docker-compose logs", shell=True, cwd=autoconf) + # test = "/tmp/tests/" + self._name + # proc = run("docker-compose -f autoconf.yml logs", shell=True, cwd=test) \ No newline at end of file