1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2024-11-24 21:06:31 +00:00
nginx-proxy/test
Thomas LEVEIL 99a569b2e6 fix #755: Tests fail with new Docker version scheme
Docker introduced a [new version scheme](https://blog.docker.com/2017/03/docker-enterprise-edition/). As the result, the latest docker version is now `17.03.0-ce`.
2017-03-06 23:36:16 +01:00
..
certs TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
requirements TESTS: separated containers: fix indentation and remove dependency over jwilder/whoami image 2017-02-17 01:11:11 +01:00
test_custom TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_dockergen fix #755: Tests fail with new Docker version scheme 2017-03-06 23:36:16 +01:00
test_headers TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_multiple-ports TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_ssl TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
conftest.py TESTS: improve error reporting for failed to run docker-compose commands 2017-02-17 00:54:14 +01:00
pytest.ini TESTS: ignore the requirements and certs directory when collecting tests 2017-02-17 01:24:48 +01:00
pytest.sh TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
README.md TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_composev2.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_composev2.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_default-host.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_default-host.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_DOCKER_HOST_unix_socket.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_DOCKER_HOST_unix_socket.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_events.py TESTS: add a test to verify nginx conf is update on container creation/deletion 2017-02-21 03:20:38 +01:00
test_events.yml TESTS: add a test to verify nginx conf is update on container creation/deletion 2017-02-21 03:20:38 +01:00
test_ipv6.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_ipv6.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_multiple-hosts.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_multiple-hosts.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_multiple-networks.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_multiple-networks.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_nominal.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_nominal.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_wildcard_host.py TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00
test_wildcard_host.yml TESTS: replace old test suite with the new one 2017-02-17 00:29:30 +01:00

Nginx proxy test suite

Install requirements

You need python 2.7 and pip installed. Then run the commands:

requirements/build.sh
pip install -r requirements/python-requirements.txt

If you can't install those requirements on your computer, you can alternatively use the pytest.sh script which will run the tests from a Docker container which has those requirements.

Prepare the nginx-proxy test image

docker build -t jwilder/nginx-proxy:test ..

or if you want to test the alpine flavor:

docker build -t jwilder/nginx-proxy:test -f Dockerfile.alpine ..

make sure to tag that test image exactly jwilder/nginx-proxy:test or the test suite won't work.

Run the test suite

pytest

need more verbosity ?

pytest -s

Run one single test module

pytest test_nominal.py

Write a test module

This test suite uses pytest. The conftest.py file will be automatically loaded by pytest and will provide you with two useful pytest fixtures:

  • docker_compose
  • nginxproxy

docker_compose fixture

When using the docker_compose fixture in a test, pytest will try to find a yml file named after your test module filename. For instance, if your test module is test_example.py, then the docker_compose fixture will try to load a test_example.yml docker compose file.

Once the docker compose file found, the fixture will remove all containers, run docker-compose up, and finally your test will be executed.

The fixture will run the docker-compose command with the -f option to load the given compose file. So you can test your docker compose file syntax by running it yourself with:

docker-compose -f test_example.yml up -d

In the case you are running pytest from within a docker container, the docker_compose fixture will make sure the container running pytest is attached to all docker networks. That way, your test will be able to reach any of them.

In your tests, you can use the docker_compose variable to query and command the docker daemon as it provides you with a client from the docker python module.

Also this fixture alters the way the python interpreter resolves domain names to IP addresses in the following ways:

Any domain name containing the substring nginx-proxy will resolve to the IP address of the container that was created from the jwilder/nginx-proxy:test image. So all the following domain names will resolve to the nginx-proxy container in tests:

  • nginx-proxy
  • nginx-proxy.com
  • www.nginx-proxy.com
  • www.nginx-proxy.test
  • www.nginx-proxy
  • whatever.nginx-proxyooooooo
  • ...

Any domain name ending with XXX.container.docker will resolve to the IP address of the XXX container.

  • web1.container.docker will resolve to the IP address of the web1 container
  • f00.web1.container.docker will resolve to the IP address of the web1 container
  • anything.whatever.web2.container.docker will resolve to the IP address of the web2 container

Otherwise, domain names are resoved as usual using your system DNS resolver.

nginxproxy fixture

The nginxproxy fixture will provide you with a replacement for the python requests module. This replacement will just repeat up to 30 times a requests if it receives the HTTP error 404 or 502. This error occurs when you try to send queries to nginx-proxy too early after the container creation.

Also this requests replacement is preconfigured to use the Certificate Authority root certificate certs/ca-root.crt to validate https connections.

Furthermore, the nginxproxy methods accept an additional keyword parameter: ipv6 which forces requests made against containers to use the containers IPv6 address when set to True. If IPv6 is not supported by the system or docker, that particular test will be skipped.

def test_forwards_to_web1_ipv6(docker_compose, nginxproxy):
    r = nginxproxy.get("http://web1.nginx-proxy.tld/port", ipv6=True)
    assert r.status_code == 200   
    assert r.text == "answer from port 81\n"

The web docker image

When you ran the requirements/build.sh script earlier, you built a web docker image which is convenient for running a small web server in a container. This image can produce containers that listens on multiple ports at the same time.

Testing TLS

If you need to create server certificates, use the certs/create_server_certificate.sh script. Pytest will be able to validate any certificate issued from this script.