mirror of
https://github.com/thib8956/nginx-proxy
synced 2024-11-22 11:56:31 +00:00
TESTS: refactor dhparam tests
This commit is contained in:
parent
98b5828f83
commit
abdd5883a1
@ -1,74 +1,93 @@
|
|||||||
import pytest
|
|
||||||
import os
|
|
||||||
import docker
|
|
||||||
import time
|
|
||||||
import subprocess
|
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
import backoff
|
||||||
|
import docker
|
||||||
|
import pytest
|
||||||
|
|
||||||
docker_client = docker.from_env()
|
docker_client = docker.from_env()
|
||||||
|
|
||||||
def wait_for_nginxproxy_to_be_ready():
|
|
||||||
|
###############################################################################
|
||||||
|
#
|
||||||
|
# Tests helpers
|
||||||
|
#
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
@backoff.on_exception(backoff.constant, AssertionError, interval=2, max_tries=15, jitter=None)
|
||||||
|
def assert_log_contains(expected_log_line):
|
||||||
"""
|
"""
|
||||||
If one (and only one) container started from image jwilder/nginx-proxy:test is found,
|
Check that the nginx-proxy container log contains a given string.
|
||||||
wait for its log to contain substring "Watching docker events"
|
The backoff decorator will retry the check 15 times with a 2 seconds delay.
|
||||||
|
|
||||||
|
:param expected_log_line: string to search for
|
||||||
|
:return: None
|
||||||
|
:raises: AssertError if the expected string is not found in the log
|
||||||
"""
|
"""
|
||||||
containers = docker_client.containers.list(filters={"ancestor": "jwilder/nginx-proxy:test"})
|
sut_container = docker_client.containers.get("nginxproxy")
|
||||||
if len(containers) != 1:
|
|
||||||
return
|
|
||||||
container = containers[0]
|
|
||||||
for line in container.logs(stream=True):
|
|
||||||
if "Watching docker events" in line:
|
|
||||||
break
|
|
||||||
|
|
||||||
def test_dhparam_is_not_generated_if_present(docker_compose, nginxproxy):
|
|
||||||
wait_for_nginxproxy_to_be_ready()
|
|
||||||
|
|
||||||
containers = docker_client.containers.list(filters={"ancestor": "jwilder/nginx-proxy:test"})
|
|
||||||
if len(containers) != 1:
|
|
||||||
assert 0
|
|
||||||
return
|
|
||||||
|
|
||||||
sut_container = containers[0]
|
|
||||||
|
|
||||||
docker_logs = sut_container.logs(stdout=True, stderr=True, stream=False, follow=False)
|
docker_logs = sut_container.logs(stdout=True, stderr=True, stream=False, follow=False)
|
||||||
|
assert expected_log_line in docker_logs
|
||||||
|
|
||||||
assert "Custom dhparam.pem file found, generation skipped" in docker_logs
|
|
||||||
|
def require_openssl(required_version):
|
||||||
|
"""
|
||||||
|
This function checks that the required version of OpenSSL is present, and skips the test if not.
|
||||||
|
Use it as a test function decorator:
|
||||||
|
|
||||||
|
@require_openssl("2.3.4")
|
||||||
|
def test_something():
|
||||||
|
...
|
||||||
|
|
||||||
|
:param required_version: minimal required version as a string: "1.2.3"
|
||||||
|
"""
|
||||||
|
|
||||||
|
def versiontuple(v):
|
||||||
|
clean_v = re.sub("[^\d\.]", "", v)
|
||||||
|
return tuple(map(int, (clean_v.split("."))))
|
||||||
|
|
||||||
|
try:
|
||||||
|
command_output = subprocess.check_output(["openssl", "version"])
|
||||||
|
except OSError:
|
||||||
|
return pytest.mark.skip("openssl command is not available in test environment")
|
||||||
|
else:
|
||||||
|
if not command_output:
|
||||||
|
raise Exception("Could not get openssl version")
|
||||||
|
openssl_version = command_output.split()[1]
|
||||||
|
return pytest.mark.skipif(
|
||||||
|
versiontuple(openssl_version) < versiontuple(required_version),
|
||||||
|
reason="openssl v%s is less than required version %s" % (openssl_version, required_version))
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
#
|
||||||
|
# Tests
|
||||||
|
#
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
def test_dhparam_is_not_generated_if_present(docker_compose):
|
||||||
|
sut_container = docker_client.containers.get("nginxproxy")
|
||||||
|
assert sut_container.status == "running"
|
||||||
|
|
||||||
|
assert_log_contains("Custom dhparam.pem file found, generation skipped")
|
||||||
|
|
||||||
# Make sure the dhparam in use is not the default, pre-generated one
|
# Make sure the dhparam in use is not the default, pre-generated one
|
||||||
default_checksum = sut_container.exec_run("md5sum /app/dhparam.pem.default").split()
|
default_checksum = sut_container.exec_run("md5sum /app/dhparam.pem.default").split()
|
||||||
current_checksum = sut_container.exec_run("md5sum /etc/nginx/dhparam/dhparam.pem").split()
|
current_checksum = sut_container.exec_run("md5sum /etc/nginx/dhparam/dhparam.pem").split()
|
||||||
assert default_checksum[0] != current_checksum[0]
|
assert default_checksum[0] != current_checksum[0]
|
||||||
|
|
||||||
|
|
||||||
def test_web5_https_works(docker_compose, nginxproxy):
|
def test_web5_https_works(docker_compose, nginxproxy):
|
||||||
r = nginxproxy.get("https://web5.nginx-proxy.tld/port", allow_redirects=False)
|
r = nginxproxy.get("https://web5.nginx-proxy.tld/port", allow_redirects=False)
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
assert "answer from port 85\n" in r.text
|
assert "answer from port 85\n" in r.text
|
||||||
|
|
||||||
def versiontuple(v):
|
|
||||||
clean_v = re.sub("[^\d\.]", "", v)
|
|
||||||
return tuple(map(int, (clean_v.split("."))))
|
|
||||||
|
|
||||||
|
@require_openssl("1.0.2")
|
||||||
# This code checks that the required version of OpenSSL is present, and skips the test if not
|
def test_web5_dhparam_is_used(docker_compose):
|
||||||
openssl_version_required = "1.0.2"
|
sut_container = docker_client.containers.get("nginxproxy")
|
||||||
openssl_version = "0.0.0"
|
assert sut_container.status == "running"
|
||||||
|
|
||||||
try:
|
|
||||||
openssl_version = subprocess.check_output(["openssl", "version"]).split()[1]
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@pytest.mark.skipif(versiontuple(openssl_version) < versiontuple(openssl_version_required),
|
|
||||||
reason="openssl command is not available in test environment or is less than version %s" % openssl_version_required)
|
|
||||||
|
|
||||||
def test_web5_dhparam_is_used(docker_compose, nginxproxy):
|
|
||||||
containers = docker_client.containers.list(filters={"ancestor": "jwilder/nginx-proxy:test"})
|
|
||||||
if len(containers) != 1:
|
|
||||||
assert 0
|
|
||||||
return
|
|
||||||
|
|
||||||
sut_container = containers[0]
|
|
||||||
|
|
||||||
host = "%s:443" % sut_container.attrs["NetworkSettings"]["IPAddress"]
|
host = "%s:443" % sut_container.attrs["NetworkSettings"]["IPAddress"]
|
||||||
r = subprocess.check_output("echo '' | openssl s_client -verify 0 -connect %s -cipher 'EDH' | grep 'Server Temp Key'" % host, shell=True)
|
r = subprocess.check_output(
|
||||||
assert "Server Temp Key: DH, 2048 bits" in r
|
"echo '' | openssl s_client -verify 0 -connect %s -cipher 'EDH' | grep 'Server Temp Key'" % host, shell=True)
|
||||||
|
assert "Server Temp Key: DH, 2048 bits\n" == r
|
||||||
|
@ -5,11 +5,11 @@ web5:
|
|||||||
environment:
|
environment:
|
||||||
WEB_PORTS: "85"
|
WEB_PORTS: "85"
|
||||||
VIRTUAL_HOST: "web5.nginx-proxy.tld"
|
VIRTUAL_HOST: "web5.nginx-proxy.tld"
|
||||||
HTTPS_METHOD: nohttp
|
|
||||||
|
|
||||||
|
|
||||||
sut:
|
sut:
|
||||||
image: jwilder/nginx-proxy:test
|
image: jwilder/nginx-proxy:test
|
||||||
|
container_name: nginxproxy
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/tmp/docker.sock:ro
|
- /var/run/docker.sock:/tmp/docker.sock:ro
|
||||||
- ../lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro
|
- ../lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro
|
||||||
|
@ -1,52 +1,42 @@
|
|||||||
import pytest
|
import backoff
|
||||||
import os
|
|
||||||
import docker
|
import docker
|
||||||
import time
|
|
||||||
|
|
||||||
docker_client = docker.from_env()
|
docker_client = docker.from_env()
|
||||||
|
|
||||||
def wait_for_nginxproxy_to_be_ready():
|
|
||||||
|
###############################################################################
|
||||||
|
#
|
||||||
|
# Tests helpers
|
||||||
|
#
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
@backoff.on_exception(backoff.constant, AssertionError, interval=2, max_tries=15, jitter=None)
|
||||||
|
def assert_log_contains(expected_log_line):
|
||||||
"""
|
"""
|
||||||
If one (and only one) container started from image jwilder/nginx-proxy:test is found,
|
Check that the nginx-proxy container log contains a given string.
|
||||||
wait for its log to contain substring "Watching docker events"
|
The backoff decorator will retry the check 15 times with a 2 seconds delay.
|
||||||
|
|
||||||
|
:param expected_log_line: string to search for
|
||||||
|
:return: None
|
||||||
|
:raises: AssertError if the expected string is not found in the log
|
||||||
"""
|
"""
|
||||||
containers = docker_client.containers.list(filters={"ancestor": "jwilder/nginx-proxy:test"})
|
sut_container = docker_client.containers.get("nginxproxy")
|
||||||
if len(containers) != 1:
|
|
||||||
return
|
|
||||||
container = containers[0]
|
|
||||||
for line in container.logs(stream=True):
|
|
||||||
if "Watching docker events" in line:
|
|
||||||
break
|
|
||||||
|
|
||||||
def test_dhparam_is_generated_if_missing(docker_compose, nginxproxy):
|
|
||||||
wait_for_nginxproxy_to_be_ready()
|
|
||||||
|
|
||||||
containers = docker_client.containers.list(filters={"ancestor": "jwilder/nginx-proxy:test"})
|
|
||||||
if len(containers) != 1:
|
|
||||||
assert 0
|
|
||||||
return
|
|
||||||
|
|
||||||
sut_container = containers[0]
|
|
||||||
|
|
||||||
docker_logs = sut_container.logs(stdout=True, stderr=True, stream=False, follow=False)
|
docker_logs = sut_container.logs(stdout=True, stderr=True, stream=False, follow=False)
|
||||||
|
assert expected_log_line in docker_logs
|
||||||
|
|
||||||
assert "Generating DH parameters" in docker_logs
|
|
||||||
|
|
||||||
expected_line = "dhparam generation complete, reloading nginx"
|
###############################################################################
|
||||||
max_wait = 30
|
#
|
||||||
sleep_interval = 2
|
# Tests
|
||||||
current_wait = 0
|
#
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
while current_wait < max_wait:
|
def test_dhparam_is_generated_if_missing(docker_compose):
|
||||||
docker_logs = sut_container.logs(stdout=True, stderr=True, stream=False, follow=False)
|
sut_container = docker_client.containers.get("nginxproxy")
|
||||||
if expected_line in docker_logs:
|
assert sut_container.status == "running"
|
||||||
break
|
|
||||||
|
|
||||||
time.sleep(sleep_interval)
|
assert_log_contains("Generating DH parameters")
|
||||||
current_wait += sleep_interval
|
assert_log_contains("dhparam generation complete, reloading nginx")
|
||||||
|
|
||||||
# Re-check the logs to get better assert output on failure
|
|
||||||
assert expected_line in docker_logs
|
|
||||||
|
|
||||||
# Make sure the dhparam in use is not the default, pre-generated one
|
# Make sure the dhparam in use is not the default, pre-generated one
|
||||||
default_checksum = sut_container.exec_run("md5sum /app/dhparam.pem.default").split()
|
default_checksum = sut_container.exec_run("md5sum /app/dhparam.pem.default").split()
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
sut:
|
sut:
|
||||||
image: jwilder/nginx-proxy:test
|
image: jwilder/nginx-proxy:test
|
||||||
|
container_name: nginxproxy
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/tmp/docker.sock:ro
|
- /var/run/docker.sock:/tmp/docker.sock:ro
|
||||||
- ./certs:/etc/nginx/certs:ro
|
- ./certs:/etc/nginx/certs:ro
|
||||||
|
Loading…
Reference in New Issue
Block a user