1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2025-02-24 17:58:16 +00:00

chore(ci): ⬆️ porting python 2.7 code to python 3.9

This commit is contained in:
Kevin Marilleau 2021-03-18 22:48:13 +01:00 committed by Nicolas Duchon
parent fe79dd4cd3
commit 950a28f5ea
No known key found for this signature in database
GPG Key ID: EA3151C66A4D79E7
10 changed files with 63 additions and 51 deletions

View File

@ -1,4 +1,3 @@
from __future__ import print_function
import contextlib import contextlib
import logging import logging
import os import os
@ -142,7 +141,7 @@ def container_ip(container):
return net_info["bridge"]["IPAddress"] return net_info["bridge"]["IPAddress"]
# not default bridge network, fallback on first network defined # not default bridge network, fallback on first network defined
network_name = net_info.keys()[0] network_name = list(net_info.keys())[0]
return net_info[network_name]["IPAddress"] return net_info[network_name]["IPAddress"]
@ -155,7 +154,7 @@ def container_ipv6(container):
return net_info["bridge"]["GlobalIPv6Address"] return net_info["bridge"]["GlobalIPv6Address"]
# not default bridge network, fallback on first network defined # not default bridge network, fallback on first network defined
network_name = net_info.keys()[0] network_name = list(net_info.keys())[0]
return net_info[network_name]["GlobalIPv6Address"] return net_info[network_name]["GlobalIPv6Address"]
@ -188,7 +187,7 @@ def docker_container_dns_resolver(domain_name):
log = logging.getLogger('DNS') log = logging.getLogger('DNS')
log.debug("docker_container_dns_resolver(%r)" % domain_name) log.debug("docker_container_dns_resolver(%r)" % domain_name)
match = re.search('(^|.+\.)(?P<container>[^.]+)\.container\.docker$', domain_name) match = re.search(r'(^|.+\.)(?P<container>[^.]+)\.container\.docker$', domain_name)
if not match: if not match:
log.debug("%r does not match" % domain_name) log.debug("%r does not match" % domain_name)
return return
@ -253,9 +252,12 @@ def get_nginx_conf_from_container(container):
return the nginx /etc/nginx/conf.d/default.conf file content from a container return the nginx /etc/nginx/conf.d/default.conf file content from a container
""" """
import tarfile import tarfile
from cStringIO import StringIO from io import BytesIO
strm, stat = container.get_archive('/etc/nginx/conf.d/default.conf')
with tarfile.open(fileobj=StringIO(strm.read())) as tf: strm_generator, stat = container.get_archive('/etc/nginx/conf.d/default.conf')
strm_fileobj = BytesIO(b"".join(strm_generator))
with tarfile.open(fileobj=strm_fileobj) as tf:
conffile = tf.extractfile('default.conf') conffile = tf.extractfile('default.conf')
return conffile.read() return conffile.read()
@ -264,7 +266,7 @@ def docker_compose_up(compose_file='docker-compose.yml'):
logging.info('docker-compose -f %s up -d' % compose_file) logging.info('docker-compose -f %s up -d' % compose_file)
try: try:
subprocess.check_output(shlex.split('docker-compose -f %s up -d' % compose_file), stderr=subprocess.STDOUT) subprocess.check_output(shlex.split('docker-compose -f %s up -d' % compose_file), stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError as e:
pytest.fail("Error while runninng 'docker-compose -f %s up -d':\n%s" % (compose_file, e.output), pytrace=False) pytest.fail("Error while runninng 'docker-compose -f %s up -d':\n%s" % (compose_file, e.output), pytrace=False)
@ -272,7 +274,7 @@ def docker_compose_down(compose_file='docker-compose.yml'):
logging.info('docker-compose -f %s down' % compose_file) logging.info('docker-compose -f %s down' % compose_file)
try: try:
subprocess.check_output(shlex.split('docker-compose -f %s down' % compose_file), stderr=subprocess.STDOUT) subprocess.check_output(shlex.split('docker-compose -f %s down' % compose_file), stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError as e:
pytest.fail("Error while runninng 'docker-compose -f %s down':\n%s" % (compose_file, e.output), pytrace=False) pytest.fail("Error while runninng 'docker-compose -f %s down':\n%s" % (compose_file, e.output), pytrace=False)
@ -286,7 +288,7 @@ def wait_for_nginxproxy_to_be_ready():
return return
container = containers[0] container = containers[0]
for line in container.logs(stream=True): for line in container.logs(stream=True):
if "Watching docker events" in line: if b"Watching docker events" in line:
logging.debug("nginx-proxy ready") logging.debug("nginx-proxy ready")
break break
@ -337,7 +339,7 @@ def connect_to_network(network):
return return
# figure out our container networks # figure out our container networks
my_networks = my_container.attrs["NetworkSettings"]["Networks"].keys() my_networks = list(my_container.attrs["NetworkSettings"]["Networks"].keys())
# make sure our container is connected to the nginx-proxy's network # make sure our container is connected to the nginx-proxy's network
if network not in my_networks: if network not in my_networks:
@ -360,7 +362,7 @@ def disconnect_from_network(network=None):
return return
# figure out our container networks # figure out our container networks
my_networks_names = my_container.attrs["NetworkSettings"]["Networks"].keys() my_networks_names = list(my_container.attrs["NetworkSettings"]["Networks"].keys())
# disconnect our container from the given network # disconnect our container from the given network
if network.name in my_networks_names: if network.name in my_networks_names:
@ -378,7 +380,7 @@ def connect_to_all_networks():
return [] return []
else: else:
# find the list of docker networks # find the list of docker networks
networks = filter(lambda network: len(network.containers) > 0 and network.name != 'bridge', docker_client.networks.list()) networks = [network for network in docker_client.networks.list() if len(network.containers) > 0 and network.name != 'bridge']
return [connect_to_network(network) for network in networks] return [connect_to_network(network) for network in networks]
@ -388,7 +390,7 @@ def connect_to_all_networks():
# #
############################################################################### ###############################################################################
@pytest.yield_fixture(scope="module") @pytest.fixture(scope="module")
def docker_compose(request): def docker_compose(request):
""" """
pytest fixture providing containers described in a docker compose file. After the tests, remove the created containers pytest fixture providing containers described in a docker compose file. After the tests, remove the created containers
@ -412,7 +414,7 @@ def docker_compose(request):
restore_urllib_dns_resolver(original_dns_resolver) restore_urllib_dns_resolver(original_dns_resolver)
@pytest.yield_fixture() @pytest.fixture()
def nginxproxy(): def nginxproxy():
""" """
Provides the `nginxproxy` object that can be used in the same way the requests module is: Provides the `nginxproxy` object that can be used in the same way the requests module is:

View File

@ -1,4 +1,4 @@
FROM python:2.7-alpine FROM python:3.9-alpine
# Note: we're using alpine because it has openssl 1.0.2, which we need for testing # Note: we're using alpine because it has openssl 1.0.2, which we need for testing
RUN apk add --update bash openssl curl && rm -rf /var/cache/apk/* RUN apk add --update bash openssl curl && rm -rf /var/cache/apk/*

View File

@ -12,7 +12,7 @@ script_dir = os.path.dirname(__file__)
pytestmark = pytest.mark.xfail() # TODO delete this marker once those issues are fixed pytestmark = pytest.mark.xfail() # TODO delete this marker once those issues are fixed
@pytest.yield_fixture(scope="module", autouse=True) @pytest.fixture(scope="module", autouse=True)
def certs(): def certs():
""" """
pytest fixture that provides cert and key files into the tmp_certs directory pytest fixture that provides cert and key files into the tmp_certs directory
@ -43,7 +43,7 @@ def test_http_web_is_301(docker_compose, nginxproxy):
def test_https_web_is_200(docker_compose, nginxproxy): def test_https_web_is_200(docker_compose, nginxproxy):
r = nginxproxy.get("https://web.nginx-proxy/port") r = nginxproxy.get("https://web.nginx-proxy/port")
assert r.status_code == 200 assert r.status_code == 200
assert 'answer from port 81\n' in r.text assert "answer from port 81\n" in r.text
@pytest.mark.incremental @pytest.mark.incremental

View File

@ -19,4 +19,4 @@ def test_custom_conf_does_not_apply_to_web2(docker_compose, nginxproxy):
assert "X-test" not in r.headers assert "X-test" not in r.headers
def test_custom_block_is_present_in_nginx_generated_conf(docker_compose, nginxproxy): def test_custom_block_is_present_in_nginx_generated_conf(docker_compose, nginxproxy):
assert "include /etc/nginx/vhost.d/web1.nginx-proxy.local_location;" in nginxproxy.get_conf() assert b"include /etc/nginx/vhost.d/web1.nginx-proxy.local_location;" in nginxproxy.get_conf()

View File

@ -4,7 +4,7 @@ import logging
import pytest import pytest
@pytest.yield_fixture(scope="module") @pytest.fixture(scope="module")
def nginx_tmpl(): def nginx_tmpl():
""" """
pytest fixture which extracts the the nginx config template from pytest fixture which extracts the the nginx config template from
@ -13,14 +13,18 @@ def nginx_tmpl():
script_dir = os.path.dirname(__file__) script_dir = os.path.dirname(__file__)
logging.info("extracting nginx.tmpl from nginxproxy/nginx-proxy:test") logging.info("extracting nginx.tmpl from nginxproxy/nginx-proxy:test")
docker_client = docker.from_env() docker_client = docker.from_env()
print(docker_client.containers.run( print(
image='nginxproxy/nginx-proxy:test', docker_client.containers.run(
image="nginxproxy/nginx-proxy:test",
remove=True, remove=True,
volumes=['{current_dir}:{current_dir}'.format(current_dir=script_dir)], volumes=["{current_dir}:{current_dir}".format(current_dir=script_dir)],
entrypoint='sh', entrypoint="sh",
command='-xc "cp /app/nginx.tmpl {current_dir} && chmod 777 {current_dir}/nginx.tmpl"'.format( command='-xc "cp /app/nginx.tmpl {current_dir} && chmod 777 {current_dir}/nginx.tmpl"'.format(
current_dir=script_dir), current_dir=script_dir
stderr=True)) ),
stderr=True,
)
)
yield yield
logging.info("removing nginx.tmpl") logging.info("removing nginx.tmpl")
os.remove(os.path.join(script_dir, "nginx.tmpl")) os.remove(os.path.join(script_dir, "nginx.tmpl"))

View File

@ -18,16 +18,18 @@ def versiontuple(v):
>>> versiontuple("17.03.0-ce") < (1, 13) >>> versiontuple("17.03.0-ce") < (1, 13)
False False
""" """
return tuple(map(int, (v.split('-')[0].split(".")))) return tuple(map(int, (v.split("-")[0].split("."))))
raw_version = docker.from_env().version()['Version'] raw_version = docker.from_env().version()["Version"]
pytestmark = pytest.mark.skipif( pytestmark = pytest.mark.skipif(
versiontuple(raw_version) < (1, 13), versiontuple(raw_version) < (1, 13),
reason="Docker compose syntax v3 requires docker engine v1.13 or later (got %s)" % raw_version) reason="Docker compose syntax v3 requires docker engine v1.13 or later (got %s)"
% raw_version,
)
@pytest.yield_fixture(scope="module") @pytest.fixture(scope="module")
def nginx_tmpl(): def nginx_tmpl():
""" """
pytest fixture which extracts the the nginx config template from pytest fixture which extracts the the nginx config template from
@ -36,14 +38,18 @@ def nginx_tmpl():
script_dir = os.path.dirname(__file__) script_dir = os.path.dirname(__file__)
logging.info("extracting nginx.tmpl from nginxproxy/nginx-proxy:test") logging.info("extracting nginx.tmpl from nginxproxy/nginx-proxy:test")
docker_client = docker.from_env() docker_client = docker.from_env()
print(docker_client.containers.run( print(
image='nginxproxy/nginx-proxy:test', docker_client.containers.run(
image="nginxproxy/nginx-proxy:test",
remove=True, remove=True,
volumes=['{current_dir}:{current_dir}'.format(current_dir=script_dir)], volumes=["{current_dir}:{current_dir}".format(current_dir=script_dir)],
entrypoint='sh', entrypoint="sh",
command='-xc "cp /app/nginx.tmpl {current_dir} && chmod 777 {current_dir}/nginx.tmpl"'.format( command='-xc "cp /app/nginx.tmpl {current_dir} && chmod 777 {current_dir}/nginx.tmpl"'.format(
current_dir=script_dir), current_dir=script_dir
stderr=True)) ),
stderr=True,
)
)
yield yield
logging.info("removing nginx.tmpl") logging.info("removing nginx.tmpl")
os.remove(os.path.join(script_dir, "nginx.tmpl")) os.remove(os.path.join(script_dir, "nginx.tmpl"))
@ -61,6 +67,6 @@ def test_forwards_to_whoami(nginx_tmpl, docker_compose, nginxproxy):
assert r.text == "I'm %s\n" % whoami_container.id[:12] assert r.text == "I'm %s\n" % whoami_container.id[:12]
if __name__ == '__main__': if __name__ == "__main__":
import doctest import doctest
doctest.testmod() doctest.testmod()

View File

@ -7,7 +7,7 @@ import pytest
from docker.errors import NotFound from docker.errors import NotFound
@pytest.yield_fixture() @pytest.fixture()
def web1(docker_compose): def web1(docker_compose):
""" """
pytest fixture creating a web container with `VIRTUAL_HOST=web1.nginx-proxy` listening on port 81. pytest fixture creating a web container with `VIRTUAL_HOST=web1.nginx-proxy` listening on port 81.

View File

@ -26,7 +26,7 @@ def assert_log_contains(expected_log_line):
""" """
sut_container = docker_client.containers.get("nginxproxy") sut_container = docker_client.containers.get("nginxproxy")
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 bytes(expected_log_line, encoding="utf8") in docker_logs
def require_openssl(required_version): def require_openssl(required_version):
@ -42,7 +42,7 @@ def require_openssl(required_version):
""" """
def versiontuple(v): def versiontuple(v):
clean_v = re.sub("[^\d\.]", "", v) clean_v = re.sub(r"[^\d\.]", "", v)
return tuple(map(int, (clean_v.split(".")))) return tuple(map(int, (clean_v.split("."))))
try: try:
@ -52,7 +52,7 @@ def require_openssl(required_version):
else: else:
if not command_output: if not command_output:
raise Exception("Could not get openssl version") raise Exception("Could not get openssl version")
openssl_version = command_output.split()[1] openssl_version = str(command_output.split()[1])
return pytest.mark.skipif( return pytest.mark.skipif(
versiontuple(openssl_version) < versiontuple(required_version), versiontuple(openssl_version) < versiontuple(required_version),
reason="openssl v%s is less than required version %s" % (openssl_version, required_version)) reason="openssl v%s is less than required version %s" % (openssl_version, required_version))
@ -71,8 +71,8 @@ def test_dhparam_is_not_generated_if_present(docker_compose):
assert_log_contains("Custom dhparam.pem file found, generation skipped") 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").output.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").output.split()
assert default_checksum[0] != current_checksum[0] assert default_checksum[0] != current_checksum[0]
@ -89,5 +89,5 @@ def test_web5_dhparam_is_used(docker_compose):
host = "%s:443" % sut_container.attrs["NetworkSettings"]["IPAddress"] host = "%s:443" % sut_container.attrs["NetworkSettings"]["IPAddress"]
r = subprocess.check_output( r = subprocess.check_output(
"echo '' | openssl s_client -connect %s -cipher 'EDH' | grep 'Server Temp Key'" % host, shell=True) f"echo '' | openssl s_client -connect {host} -cipher 'EDH' | grep 'Server Temp Key'", shell=True)
assert "Server Temp Key: X25519, 253 bits\n" == r assert b"Server Temp Key: X25519, 253 bits\n" == r

View File

@ -22,7 +22,7 @@ def assert_log_contains(expected_log_line):
""" """
sut_container = docker_client.containers.get("nginxproxy") sut_container = docker_client.containers.get("nginxproxy")
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 bytes(expected_log_line, encoding="utf8") in docker_logs
############################################################################### ###############################################################################

View File

@ -1,5 +1,5 @@
import pytest import pytest
from backports.ssl_match_hostname import CertificateError from ssl import CertificateError
from requests.exceptions import SSLError from requests.exceptions import SSLError