|
1 |
| -Test suite |
2 |
| -========== |
3 |
| - |
4 |
| -This test suite is implemented on top of the [Bats](https://github.com/sstephenson/bats/blob/master/README.md) test framework. |
5 |
| - |
6 |
| -It is intended to verify the correct behavior of the Docker image `jwilder/nginx-proxy:bats`. |
7 |
| - |
8 |
| -Running the test suite |
9 |
| ----------------------- |
10 |
| - |
11 |
| -Make sure you have Bats installed, then run: |
12 |
| - |
13 |
| - docker build -t jwilder/nginx-proxy:bats . |
14 |
| - bats test/ |
| 1 | +Nginx proxy test suite |
| 2 | +====================== |
| 3 | + |
| 4 | +Install requirements |
| 5 | +-------------------- |
| 6 | + |
| 7 | +You need [python 2.7](https://www.python.org/) and [pip](https://pip.pypa.io/en/stable/installing/) installed. Then run the commands: |
| 8 | + |
| 9 | + requirements/build.sh |
| 10 | + pip install -r requirements/python-requirements.txt |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | + |
| 15 | +Prepare the nginx-proxy test image |
| 16 | +---------------------------------- |
| 17 | + |
| 18 | + docker build -t jwilder/nginx-proxy:test .. |
| 19 | + |
| 20 | +or if you want to test the alpine flavor: |
| 21 | + |
| 22 | + docker build -t jwilder/nginx-proxy:test -f Dockerfile.alpine .. |
| 23 | + |
| 24 | +make sure to tag that test image exactly `jwilder/nginx-proxy:test` or the test suite won't work. |
| 25 | + |
| 26 | + |
| 27 | +Run the test suite |
| 28 | +------------------ |
| 29 | + |
| 30 | + pytest |
| 31 | + |
| 32 | +need more verbosity ? |
| 33 | + |
| 34 | + pytest -s |
| 35 | + |
| 36 | + |
| 37 | +Run one single test module |
| 38 | +-------------------------- |
| 39 | + |
| 40 | + pytest test_nominal.py |
| 41 | + |
| 42 | + |
| 43 | +Write a test module |
| 44 | +------------------- |
| 45 | + |
| 46 | +This test suite uses [pytest](http://doc.pytest.org/en/latest/). The [conftest.py](conftest.py) file will be automatically loaded by pytest and will provide you with two useful pytest [fixtures](http://doc.pytest.org/en/latest/fixture.html#fixture): |
| 47 | + |
| 48 | +- docker_compose |
| 49 | +- nginxproxy |
| 50 | + |
| 51 | + |
| 52 | +### docker_compose fixture |
| 53 | + |
| 54 | +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](https://docs.docker.com/compose/compose-file/). |
| 55 | + |
| 56 | +Once the docker compose file found, the fixture will remove all containers, run `docker-compose up`, and finally your test will be executed. |
| 57 | + |
| 58 | +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: |
| 59 | + |
| 60 | + docker-compose -f test_example.yml up -d |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +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](https://docker-py.readthedocs.io/en/2.0.2/client.html#client-reference). |
| 65 | + |
| 66 | +Also this fixture alters the way the python interpreter resolves domain names to IP addresses in the following ways: |
| 67 | + |
| 68 | +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: |
| 69 | +- `nginx-proxy` |
| 70 | +- `nginx-proxy.com` |
| 71 | +- `www.nginx-proxy.com` |
| 72 | +- `www.nginx-proxy.test` |
| 73 | +- `www.nginx-proxy` |
| 74 | +- `whatever.nginx-proxyooooooo` |
| 75 | +- ... |
| 76 | + |
| 77 | +Any domain name ending with `XXX.container.docker` will resolve to the IP address of the XXX container. |
| 78 | +- `web1.container.docker` will resolve to the IP address of the `web1` container |
| 79 | +- `f00.web1.container.docker` will resolve to the IP address of the `web1` container |
| 80 | +- `anything.whatever.web2.container.docker` will resolve to the IP address of the `web2` container |
| 81 | + |
| 82 | +Otherwise, domain names are resoved as usual using your system DNS resolver. |
| 83 | + |
| 84 | + |
| 85 | +### nginxproxy fixture |
| 86 | + |
| 87 | +The `nginxproxy` fixture will provide you with a replacement for the python [requests](https://pypi.python.org/pypi/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. |
| 88 | + |
| 89 | +Also this requests replacement is preconfigured to use the Certificate Authority root certificate [certs/ca-root.crt](certs/) to validate https connections. |
| 90 | + |
| 91 | +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. |
| 92 | + |
| 93 | + def test_forwards_to_web1_ipv6(docker_compose, nginxproxy): |
| 94 | + r = nginxproxy.get("http://web1.nginx-proxy.tld/port", ipv6=True) |
| 95 | + assert r.status_code == 200 |
| 96 | + assert r.text == "answer from port 81\n" |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +### The web docker image |
| 101 | + |
| 102 | +When you ran the `requirements/build.sh` script earlier, you built a [`web`](requirements/README.md) 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. |
| 103 | + |
| 104 | + |
| 105 | +### Testing TLS |
| 106 | + |
| 107 | +If you need to create server certificates, use the [`certs/create_server_certificate.sh`](certs/) script. Pytest will be able to validate any certificate issued from this script. |
0 commit comments