diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index b19ba2198..b919258ff 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -377,8 +377,10 @@ jobs: # run: ./tests/main.py "autoconf" # - name: Run Swarm tests # run: ./tests/main.py "swarm" - - name: Run Kubernetes tests - run: ./tests/main.py "kubernetes" + # - name: Run Kubernetes tests + # run: ./tests/main.py "kubernetes" + - name: Generate Linux packages and build test images + run: ./tests/linux.sh ${{ env.BUILD_MODE }} - name: Temp stop tests run: exit 1 - name: Run autoconf tests diff --git a/tests/Dockerfile-centos b/tests/Dockerfile-centos index ae32a5fb3..f75baa342 100644 --- a/tests/Dockerfile-centos +++ b/tests/Dockerfile-centos @@ -14,11 +14,14 @@ rm -f /lib/systemd/system/basic.target.wants/*;\ rm -f /lib/systemd/system/anaconda.target.wants/*; COPY linux/nginx.repo /etc/yum.repos.d/nginx.repo -#COPY linux/nginx.repo /etc/yum.repos.d/nginx.repo RUN dnf install yum-utils epel-release -y && \ dnf install nginx-1.20.2 -y +ARG target=dev +COPY /opt/packages/${target}/centos/*.rpm /opt +RUN dnf install -y /opt/*.rpm + VOLUME /run /tmp CMD /usr/sbin/init \ No newline at end of file diff --git a/tests/Dockerfile-debian b/tests/Dockerfile-debian index d411859bd..e6ed9c581 100644 --- a/tests/Dockerfile-debian +++ b/tests/Dockerfile-debian @@ -31,6 +31,10 @@ RUN apt update && \ apt-get update && \ apt-get install -y --no-install-recommends nginx=${NGINX_VERSION}-1~bullseye +ARG target=dev +COPY /opt/packages/${target}/debian/*.deb /opt +RUN apt install -y /opt/*.deb + VOLUME [ "/sys/fs/cgroup" ] CMD ["/lib/systemd/systemd"] diff --git a/tests/Dockerfile-fedora b/tests/Dockerfile-fedora index e07d02694..d4f920191 100644 --- a/tests/Dockerfile-fedora +++ b/tests/Dockerfile-fedora @@ -22,6 +22,10 @@ RUN dnf update -y && \ dnf install -y curl gnupg2 ca-certificates redhat-lsb-core python3-pip && \ dnf install nginx-1.20.2 -y +ARG target=dev +COPY /opt/packages/${target}/fedora/*.rpm /opt +RUN dnf install -y /opt/*.rpm + 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 b5a0dc026..ddf2f8d3f 100644 --- a/tests/Dockerfile-ubuntu +++ b/tests/Dockerfile-ubuntu @@ -31,6 +31,10 @@ RUN apt update && \ apt-get update && \ apt-get install -y --no-install-recommends nginx=${NGINX_VERSION}-1~jammy +ARG target=dev +COPY /opt/packages/${target}/ubuntu/*.deb /opt +RUN apt install -y /opt/*.deb + VOLUME [ "/sys/fs/cgroup" ] CMD ["/lib/systemd/systemd"] \ No newline at end of file diff --git a/tests/LinuxTest.py b/tests/LinuxTest.py new file mode 100644 index 000000000..50a93f6cf --- /dev/null +++ b/tests/LinuxTest.py @@ -0,0 +1,125 @@ +from Test import Test +from os.path import isdir, join, isfile +from os import chown, walk, getenv, listdir +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) : + + def __init__(self, name, timeout, tests) : + 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") + } + + def init() : + try : + if not Test.init() : + return False + proc = run("", shell=True") + 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 proc.returncode != 0 : + raise(Exception("docker-compose pull failed (autoconf stack)")) + proc = run("docker-compose up -d", cwd="/tmp/autoconf", 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")) + except : + log("AUTOCONF", "❌", "exception while running AutoconfTest.init()\n" + format_exc()) + return False + return True + + def end() : + ret = True + try : + if not Test.end() : + return False + proc = run("docker-compose down -v", cwd="/tmp/autoconf", shell=True) + if proc.returncode != 0 : + ret = False + rmtree("/tmp/autoconf") + except : + log("AUTOCONF", "❌", "exception while running AutoconfTest.end()\n" + format_exc()) + return False + return ret + + def _setup_test(self) : + 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" + if isfile(setup) : + proc = run("sudo ./setup-autoconf.sh", cwd=test, shell=True) + if proc.returncode != 0 : + raise(Exception("setup-autoconf 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) + 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")) + except : + log("AUTOCONF", "❌", "exception while running AutoconfTest._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 _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 diff --git a/tests/linux.sh b/tests/linux.sh index 4bf9f596b..aadaf3ae7 100755 --- a/tests/linux.sh +++ b/tests/linux.sh @@ -19,6 +19,11 @@ function gen_package() { do_and_check_cmd cp "/tmp/packages/${linux}/bunkerweb.$type" "/opt/packages/${mode}/${linux}/${name}.${type}" } +function build_image() { + mode="$1" + linux="$2" + do_and_check_cmd docker build -t "bw-${linux}" --build-arg "target=${mode}" -f "./Dockerfile-${linux}" . +} echo "Linux tests" @@ -38,4 +43,10 @@ gen_package "$1" "debian" gen_package "$1" "centos" gen_package "$1" "fedora" +# Build test images +build_image "$1" "ubuntu" +build_image "$1" "debian" +build_image "$1" "centos" +build_image "$1" "fedora" + exit 0