Skip to content

Commit 6069bc5

Browse files
committed
TESTS: replace old test suite with the new one
get rid of Bats definitively
1 parent 250a01d commit 6069bc5

File tree

95 files changed

+146
-2059
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+146
-2059
lines changed

.travis.yml

+7-18
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ services:
44
- docker
55

66
env:
7-
global:
8-
- DOCKER_VERSION=1.13.1-0~ubuntu-trusty
7+
global:
8+
- DOCKER_VERSION=1.13.1-0~ubuntu-trusty
9+
matrix:
10+
- TEST_TARGET: test-debian
11+
- TEST_TARGET: test-alpine
912

1013
before_install:
1114
# list docker-engine versions
@@ -14,22 +17,8 @@ before_install:
1417
- sudo apt-get -o Dpkg::Options::="--force-confnew" install -y --force-yes docker-engine=${DOCKER_VERSION}
1518
- docker version
1619
- docker info
17-
# install bats
18-
- sudo add-apt-repository ppa:duggan/bats --yes
19-
- sudo apt-get update -qq
20-
- sudo apt-get install -qq bats
21-
# prepare docker images
20+
# prepare docker test requirements
2221
- make update-dependencies
2322

24-
matrix:
25-
include:
26-
- env: TEST_ID=test-debian
27-
- env: TEST_ID=test-alpine
28-
- env: TEST_ID=test2-debian
29-
- env: TEST_ID=test2-alpine
30-
allow_failures:
31-
- env: TEST_ID=test2-debian
32-
- env: TEST_ID=test2-alpine
33-
3423
script:
35-
- make $TEST_ID
24+
- make $TEST_TARGET

Makefile

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
.SILENT :
2-
.PHONY : test test2
2+
.PHONY : test-debian test-alpine test
3+
34

45
update-dependencies:
5-
docker pull jwilder/docker-gen:0.7.3
6-
docker pull nginx:1.11.9
7-
docker pull nginx:1.11.9-alpine
8-
docker pull python:3
9-
docker pull rancher/socat-docker:latest
10-
docker pull appropriate/curl:latest
11-
docker pull docker:1.10
6+
test/requirements/build.sh
127

13-
test-debian:
14-
docker build -t jwilder/nginx-proxy:bats .
15-
bats test
8+
test-debian: update-dependencies
9+
docker build -t jwilder/nginx-proxy:test .
10+
test/pytest.sh
1611

17-
test-alpine:
18-
docker build -f Dockerfile.alpine -t jwilder/nginx-proxy:bats .
19-
bats test
12+
test-alpine: update-dependencies
13+
docker build -f Dockerfile.alpine -t jwilder/nginx-proxy:test .
14+
test/pytest.sh
2015

2116
test: test-debian test-alpine
22-
23-
test2-debian:
24-
$(MAKE) -C test2 test-debian
25-
26-
test2-alpine:
27-
$(MAKE) -C test2 test-alpine

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,22 @@ Before submitting pull requests or issues, please check github to make sure an e
325325

326326
#### Running Tests Locally
327327

328-
To run tests, you'll need to install [bats 0.4.0](https://github.com/sstephenson/bats).
328+
To run tests, you need to prepare the docker image to test which must be tagged `jwilder/nginx-proxy:test`:
329+
330+
docker build -t jwilder/nginx-proxy:test . # build the Debian variant image
331+
332+
and call the [test/pytest.sh](test/pytest.sh) script.
333+
334+
Then build the Alpine variant of the image:
335+
336+
docker build -f Dockerfile.alpine -t jwilder/nginx-proxy:test . # build the Alpline variant image
337+
338+
and call the [test/pytest.sh](test/pytest.sh) script again.
339+
340+
341+
If your system has the `make` command, you can automate those tasks by calling:
329342

330343
make test
344+
345+
346+
You can learn more about how the test suite works and how to write new tests in the [test/README.md](test/README.md) file.

test/README.md

+107-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,107 @@
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test2/conftest.py test/conftest.py

File renamed without changes.

test/default-host.bats

-33
This file was deleted.

test/docker.bats

-123
This file was deleted.

0 commit comments

Comments
 (0)