2017-02-11 22:42:24 +01:00
|
|
|
import os
|
|
|
|
import docker
|
|
|
|
import logging
|
|
|
|
import pytest
|
2017-03-07 14:04:44 -05:00
|
|
|
import re
|
2021-03-19 14:08:54 +01:00
|
|
|
from distutils.version import LooseVersion
|
2017-02-17 01:09:53 +01:00
|
|
|
|
2017-02-11 22:42:24 +01:00
|
|
|
|
2021-03-18 22:48:13 +01:00
|
|
|
raw_version = docker.from_env().version()["Version"]
|
2017-03-06 23:36:16 +01:00
|
|
|
pytestmark = pytest.mark.skipif(
|
2021-03-19 14:08:54 +01:00
|
|
|
LooseVersion(raw_version) < LooseVersion("1.13"),
|
2021-03-19 12:12:24 +01:00
|
|
|
reason="Docker compose syntax v3 requires docker engine v1.13 or later (got {raw_version})"
|
2021-03-18 22:48:13 +01:00
|
|
|
)
|
2017-02-11 22:42:24 +01:00
|
|
|
|
|
|
|
|
2021-03-18 22:48:13 +01:00
|
|
|
@pytest.fixture(scope="module")
|
2017-02-11 22:42:24 +01:00
|
|
|
def nginx_tmpl():
|
2017-02-17 01:09:53 +01:00
|
|
|
"""
|
|
|
|
pytest fixture which extracts the the nginx config template from
|
2021-04-01 16:06:09 +02:00
|
|
|
the nginxproxy/nginx-proxy:test image
|
2017-02-17 01:09:53 +01:00
|
|
|
"""
|
|
|
|
script_dir = os.path.dirname(__file__)
|
2021-04-01 16:06:09 +02:00
|
|
|
logging.info("extracting nginx.tmpl from nginxproxy/nginx-proxy:test")
|
2017-02-17 01:09:53 +01:00
|
|
|
docker_client = docker.from_env()
|
2021-03-18 22:48:13 +01:00
|
|
|
print(
|
|
|
|
docker_client.containers.run(
|
|
|
|
image="nginxproxy/nginx-proxy:test",
|
|
|
|
remove=True,
|
|
|
|
volumes=["{current_dir}:{current_dir}".format(current_dir=script_dir)],
|
|
|
|
entrypoint="sh",
|
|
|
|
command='-xc "cp /app/nginx.tmpl {current_dir} && chmod 777 {current_dir}/nginx.tmpl"'.format(
|
|
|
|
current_dir=script_dir
|
|
|
|
),
|
|
|
|
stderr=True,
|
|
|
|
)
|
|
|
|
)
|
2017-02-17 01:09:53 +01:00
|
|
|
yield
|
|
|
|
logging.info("removing nginx.tmpl")
|
|
|
|
os.remove(os.path.join(script_dir, "nginx.tmpl"))
|
2017-02-11 22:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_unknown_virtual_host_is_503(nginx_tmpl, docker_compose, nginxproxy):
|
|
|
|
r = nginxproxy.get("http://unknown.nginx.container.docker/")
|
|
|
|
assert r.status_code == 503
|
|
|
|
|
|
|
|
|
|
|
|
def test_forwards_to_whoami(nginx_tmpl, docker_compose, nginxproxy):
|
|
|
|
r = nginxproxy.get("http://whoami.nginx.container.docker/")
|
|
|
|
assert r.status_code == 200
|
|
|
|
whoami_container = docker_compose.containers.get("whoami")
|
2021-03-19 12:12:24 +01:00
|
|
|
assert r.text == f"I'm {whoami_container.id[:12]}\n"
|
2017-03-06 23:36:16 +01:00
|
|
|
|
|
|
|
|
2021-03-18 22:48:13 +01:00
|
|
|
if __name__ == "__main__":
|
2017-03-06 23:36:16 +01:00
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|