From 787fa28799392cca7b77ba9cae1c10e203bbc47c Mon Sep 17 00:00:00 2001 From: Thomas LEVEIL Date: Tue, 24 Jan 2017 22:42:13 +0100 Subject: [PATCH] TESTS: add script to run the test suite from a docker container --- test2/Makefile | 2 +- test2/README.md | 4 ++- test2/conftest.py | 5 ++- test2/nginx-proxy-tester.sh | 36 +++++++++++++++++++ .../Dockerfile-nginx-proxy-tester | 5 +++ test2/requirements/README.md | 19 ++++++++++ test2/requirements/build.sh | 2 +- .../python-requirements.txt} | 0 8 files changed, 69 insertions(+), 4 deletions(-) create mode 100755 test2/nginx-proxy-tester.sh create mode 100644 test2/requirements/Dockerfile-nginx-proxy-tester rename test2/{requirements.txt => requirements/python-requirements.txt} (100%) diff --git a/test2/Makefile b/test2/Makefile index f2ff33a..7ed0f74 100644 --- a/test2/Makefile +++ b/test2/Makefile @@ -4,7 +4,7 @@ update-dependencies: requirements/build.sh - pip install -U -r requirements.txt + pip install -U -r requirements/python-requirements.txt test-debian: update-dependencies docker build -t jwilder/nginx-proxy:test .. diff --git a/test2/README.md b/test2/README.md index 66bd237..10bcb9d 100644 --- a/test2/README.md +++ b/test2/README.md @@ -7,7 +7,9 @@ Install requirements You need [python 2.7](https://www.python.org/) and [pip](https://pip.pypa.io/en/stable/installing/) installed. Then run the commands: requirements/build.sh - pip install -r requirements.txt + pip install -r requirements/python-requirements.txt + +If you can't install those requirements on your computer, you can alternatively use the _nginx-proxy-tester.sh_ script which will run the tests from a Docker container which has those requirements. Prepare the nginx-proxy test image diff --git a/test2/conftest.py b/test2/conftest.py index 40d6295..00a5b6f 100644 --- a/test2/conftest.py +++ b/test2/conftest.py @@ -18,7 +18,7 @@ logging.getLogger('patched DNS').setLevel(logging.INFO) CA_ROOT_CERTIFICATE = os.path.join(os.path.dirname(__file__), 'certs/ca-root.crt') - +I_AM_RUNNING_INSIDE_A_DOCKER_CONTAINER = os.path.isfile("/.dockerenv") ############################################################################### # @@ -26,6 +26,7 @@ CA_ROOT_CERTIFICATE = os.path.join(os.path.dirname(__file__), 'certs/ca-root.crt # ############################################################################### + class requests_retry_on_error_502(object): """ Proxy for calling methods of the requests module. @@ -113,6 +114,8 @@ def restore_urllib_dns_resolver(getaddrinfo_func): def remove_all_containers(): docker_client = docker.from_env() for info in docker_client.containers(all=True): + if I_AM_RUNNING_INSIDE_A_DOCKER_CONTAINER and info['Id'].startswith(socket.gethostname()): + continue # pytest is running within a Docker container, so we do not want to remove that particular container docker_client.remove_container(info["Id"], v=True, force=True) diff --git a/test2/nginx-proxy-tester.sh b/test2/nginx-proxy-tester.sh new file mode 100755 index 0000000..90f06d3 --- /dev/null +++ b/test2/nginx-proxy-tester.sh @@ -0,0 +1,36 @@ +#!/bin/bash +############################################################################### +# # +# This script is meant to run the test suite from a Docker container. # +# # +# This is usefull when you want to run the test suite from Mac or # +# Docker Toolbox. # +# # +############################################################################### + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +ARGS="$@" + +# check requirements +if [[ "$(docker images -q nginx-proxy-tester 2>/dev/null)" == "" ]]; then + echo "> Building nginx-proxy-tester image..." + docker build -t nginx-proxy-tester -f $DIR/requirements/Dockerfile-nginx-proxy-tester $DIR/requirements +fi + +# delete python cache +[[ -d "${DIR}/__pycache__" ]] && rm "${DIR}/__pycache__" -rf + +# run the nginx-proxy-tester container setting the correct value for the working dir in order for +# docker-compose to work properly when run from within that container. +docker run --rm -it \ + -v ${DIR}:/${DIR} \ + -v ${DIR}/__pycache__/ \ + -w ${DIR} \ + -v /var/run/docker.sock:/var/run/docker.sock \ + nginx-proxy-tester "${ARGS}" +PYTEST_EXIT_CODE=$? + +# delete python cache +[[ -d "${DIR}/__pycache__" ]] && rm "${DIR}/__pycache__" -rf + +exit ${PYTEST_EXIT_CODE} diff --git a/test2/requirements/Dockerfile-nginx-proxy-tester b/test2/requirements/Dockerfile-nginx-proxy-tester new file mode 100644 index 0000000..b403ed7 --- /dev/null +++ b/test2/requirements/Dockerfile-nginx-proxy-tester @@ -0,0 +1,5 @@ +FROM python:2.7 +COPY python-requirements.txt /requirements.txt +RUN pip install -r /requirements.txt +WORKDIR /test +ENTRYPOINT ["pytest"] diff --git a/test2/requirements/README.md b/test2/requirements/README.md index 79195d6..2dd1523 100644 --- a/test2/requirements/README.md +++ b/test2/requirements/README.md @@ -5,6 +5,17 @@ This directory contains ressources to build Docker images tests depend on ./build.sh +# python-requirements.txt + +If you want to run the test suite from your computer, you need python and a few python modules. +The _python-requirements.txt_ file describes the python modules required. To install them, use +pip: + + pip install -r python-requirements.txt + +If you don't want to run the test from your computer, you can run the tests from a docker container, see the _nginx-proxy-tester.sh_ script. + + # Images ## web @@ -33,3 +44,11 @@ answer from port 80 ``` + +## nginx-proxy-tester + +This is an optional requirement which is usefull if you cannot (or don't want to) install pytest and its requirements on your computer. In this case, you can use the `nginx-proxy-tester` docker image to run the test suite from a Docker container. + +To use this image, it is mandatory to run the container using the `nginx-proxy-tester.sh` shell script. + + diff --git a/test2/requirements/build.sh b/test2/requirements/build.sh index 8741d3c..f29897a 100755 --- a/test2/requirements/build.sh +++ b/test2/requirements/build.sh @@ -3,4 +3,4 @@ set -e DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -docker build -t web $DIR/web +docker build -t web $DIR/web \ No newline at end of file diff --git a/test2/requirements.txt b/test2/requirements/python-requirements.txt similarity index 100% rename from test2/requirements.txt rename to test2/requirements/python-requirements.txt