From 35c1d16ff85cc9c17e0f1d09cd1f2c96b9a790a4 Mon Sep 17 00:00:00 2001 From: Nicolas Duchon Date: Wed, 8 May 2024 01:30:39 +0200 Subject: [PATCH] test: nginx.conf customizations --- Dockerfile.alpine | 2 +- test/conftest.py | 34 +++++++++++++++++++++--- test/test_nginx-conf/test_nginx-conf.py | 8 ++++++ test/test_nginx-conf/test_nginx-conf.yml | 7 +++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 test/test_nginx-conf/test_nginx-conf.py create mode 100644 test/test_nginx-conf/test_nginx-conf.yml diff --git a/Dockerfile.alpine b/Dockerfile.alpine index e6eccdc..ab78e6d 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -17,7 +17,7 @@ ENV NGINX_PROXY_VERSION=${NGINX_PROXY_VERSION} \ RUN apk add --no-cache --virtual .run-deps bash openssl # Configure Nginx -RUN sed -i 's/worker_connections.*;$/worker_connections 10240;/' /etc/nginx/nginx.conf \ +RUN sed -i 's/worker_connections.*;$/worker_connections 10240;/' /etc/nginx/nginx.conf \ && sed -i -e '/^\}$/{s//\}\nworker_rlimit_nofile 20480;/;:a' -e '$!N;$!ba' -e '}' /etc/nginx/nginx.conf \ && mkdir -p '/etc/nginx/dhparam' \ && mkdir -p '/etc/nginx/certs' diff --git a/test/conftest.py b/test/conftest.py index a4fcff5..78eb010 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -84,10 +84,17 @@ class requests_for_docker(object): def get_conf(self): """ - Return the nginx config file + Return the generated nginx config file """ nginx_proxy_containers = self.get_nginx_proxy_containers() return get_nginx_conf_from_container(nginx_proxy_containers[0]) + + def get_toplevel_conf(self): + """ + Return the top level nginx config file + """ + nginx_proxy_containers = self.get_nginx_proxy_containers() + return get_nginx_toplevel_conf_from_container(nginx_proxy_containers[0]) def get_ip(self) -> str: """ @@ -288,13 +295,32 @@ def remove_all_containers(): def get_nginx_conf_from_container(container): """ - return the full nginx config from a container + return the nginx /etc/nginx/conf.d/default.conf file content from a container """ + import tarfile from io import BytesIO - _, strm_generator = container.exec_run("nginx -T", stream = True) + strm_generator, stat = container.get_archive('/etc/nginx/conf.d/default.conf') strm_fileobj = BytesIO(b"".join(strm_generator)) - return strm_fileobj.read() + + with tarfile.open(fileobj=strm_fileobj) as tf: + conffile = tf.extractfile('default.conf') + return conffile.read() + + +def get_nginx_toplevel_conf_from_container(container): + """ + return the nginx /etc/nginx/nginx.conf file content from a container + """ + import tarfile + from io import BytesIO + + strm_generator, stat = container.get_archive('/etc/nginx/nginx.conf') + strm_fileobj = BytesIO(b"".join(strm_generator)) + + with tarfile.open(fileobj=strm_fileobj) as tf: + conffile = tf.extractfile('nginx.conf') + return conffile.read() def docker_compose_up(compose_file='docker-compose.yml'): diff --git a/test/test_nginx-conf/test_nginx-conf.py b/test/test_nginx-conf/test_nginx-conf.py new file mode 100644 index 0000000..64ce121 --- /dev/null +++ b/test/test_nginx-conf/test_nginx-conf.py @@ -0,0 +1,8 @@ +import pytest +import re + + +def test_nginx_toplevel_conf_contains_customizations(docker_compose, nginxproxy): + conf = nginxproxy.get_toplevel_conf().decode('ASCII') + assert re.search(r"^ +worker_connections 10240;$", conf) + assert re.search(r"^worker_rlimit_nofile 20480;$", conf) diff --git a/test/test_nginx-conf/test_nginx-conf.yml b/test/test_nginx-conf/test_nginx-conf.yml new file mode 100644 index 0000000..0c42921 --- /dev/null +++ b/test/test_nginx-conf/test_nginx-conf.yml @@ -0,0 +1,7 @@ +version: "2" + +services: + sut: + image: nginxproxy/nginx-proxy:test + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro