From f133d5f5e92eca21f8df0f578339ca63f4dfd28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Tue, 15 Oct 2019 09:04:54 -0500 Subject: [PATCH] :recycle: Refactor tests to use env vars and push dated tags --- .travis.yml | 23 ++++-- docker-compose.build.yml | 17 ---- scripts/build-push-all.sh | 7 ++ scripts/build-push.sh | 11 ++- scripts/deploy.sh | 2 +- scripts/process_all.py | 78 +++++++++++++++++++ scripts/test-all.sh | 4 + scripts/test.sh | 4 +- tests/test_01_main/test_defaults.py | 37 ++------- tests/test_01_main/test_env_vars_1.py | 37 ++------- tests/test_01_main/test_env_vars_2.py | 21 ++--- tests/test_01_main/test_env_vars_3.py | 37 ++------- tests/test_02_app/test_custom_app.py | 65 +++------------- tests/test_02_app/test_package_app.py | 34 ++------ tests/test_02_app/test_package_app_config.py | 34 ++------ .../test_package_app_custom_config.py | 34 ++------ .../test_package_app_sub_config.py | 34 ++------ tests/test_02_app/test_simple_app.py | 35 ++------- tests/test_03_reload/test_defaults.py | 37 ++------- tests/test_03_reload/test_env_vars_1.py | 37 ++------- tests/test_03_reload/test_env_vars_2.py | 20 ++--- tests/test_04_app_reload/test_custom_app.py | 65 +++------------- tests/test_04_app_reload/test_package_app.py | 34 ++------ tests/test_04_app_reload/test_simple_app.py | 34 ++------ 24 files changed, 250 insertions(+), 491 deletions(-) delete mode 100644 docker-compose.build.yml create mode 100644 scripts/build-push-all.sh create mode 100644 scripts/process_all.py create mode 100644 scripts/test-all.sh diff --git a/.travis.yml b/.travis.yml index 2d5174e9..ba54358b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,11 +11,24 @@ install: services: - docker +env: + - NAME='latest' BUILD_PATH='python3.7' TEST_STR1='Hello world! From Uvicorn with Gunicorn. Using Python 3.7' TEST_STR2='Test app. From Uvicorn with Gunicorn. Using Python 3.7' + - NAME='python3.7' BUILD_PATH='python3.7' TEST_STR1='Hello world! From Uvicorn with Gunicorn. Using Python 3.7' TEST_STR2='Test app. From Uvicorn with Gunicorn. Using Python 3.7' + - NAME='python3.6' BUILD_PATH='python3.6' TEST_STR1='Hello world! From Uvicorn with Gunicorn. Using Python 3.6' TEST_STR2='Test app. From Uvicorn with Gunicorn. Using Python 3.6' + - NAME='python3.7-alpine3.8' BUILD_PATH='python3.7-alpine3.8' TEST_STR1='Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7' TEST_STR2='Test app. From Uvicorn with Gunicorn. Using Python 3.7' + - NAME='python3.6-alpine3.8' BUILD_PATH='python3.6-alpine3.8' TEST_STR1='Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6' TEST_STR2='Test app. From Uvicorn with Gunicorn. Using Python 3.6' + + script: - bash scripts/test.sh -deploy: - provider: script - script: bash scripts/deploy.sh - on: - branch: master +jobs: + include: + - script: bash scripts/test.sh + - stage: deploy + script: skip + deploy: + provider: script + script: bash scripts/build-push-all.sh + on: + branch: master diff --git a/docker-compose.build.yml b/docker-compose.build.yml deleted file mode 100644 index cbdc5783..00000000 --- a/docker-compose.build.yml +++ /dev/null @@ -1,17 +0,0 @@ -version: '3' -services: - latest: - build: ./python3.7 - image: tiangolo/uvicorn-gunicorn:latest - python3.6: - build: ./python3.6 - image: tiangolo/uvicorn-gunicorn:python3.6 - python3.6-alpine3.8: - build: ./python3.6-alpine3.8 - image: tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - python3.7: - build: ./python3.7 - image: tiangolo/uvicorn-gunicorn:python3.7 - python3.7-alpine3.8: - build: ./python3.7-alpine3.8 - image: tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 diff --git a/scripts/build-push-all.sh b/scripts/build-push-all.sh new file mode 100644 index 00000000..e54d3f10 --- /dev/null +++ b/scripts/build-push-all.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + +BUILD_PUSH=1 python scripts/process_all.py diff --git a/scripts/build-push.sh b/scripts/build-push.sh index 21df633b..856960d0 100644 --- a/scripts/build-push.sh +++ b/scripts/build-push.sh @@ -1,10 +1,13 @@ #!/usr/bin/env bash set -e -set -x -echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin +use_tag="tiangolo/uvicorn-gunicorn:$NAME" +use_dated_tag="${use_tag}-$(date -I)" -docker-compose -f docker-compose.build.yml build +docker build -t "$use_tag" "$BUILD_PATH" -docker-compose -f docker-compose.build.yml push +docker tag "$use_tag" "$use_dated_tag" + +docker push "$use_tag" +docker push "$use_dated_tag" diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 23e37e78..b409afd4 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -3,6 +3,6 @@ set -e set -x -bash scripts/build-push.sh +bash scripts/build-push-all.sh bash scripts/trigger-children.sh diff --git a/scripts/process_all.py b/scripts/process_all.py new file mode 100644 index 00000000..ec72f807 --- /dev/null +++ b/scripts/process_all.py @@ -0,0 +1,78 @@ +import os +import subprocess +import sys + +environments = [ + { + "NAME": "latest", + "BUILD_PATH": "python3.7", + "TEST_STR1": "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", + "TEST_STR2": "Test app. From Uvicorn with Gunicorn. Using Python 3.7", + }, + { + "NAME": "python3.7", + "BUILD_PATH": "python3.7", + "TEST_STR1": "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", + "TEST_STR2": "Test app. From Uvicorn with Gunicorn. Using Python 3.7", + }, + { + "NAME": "python3.6", + "BUILD_PATH": "python3.6", + "TEST_STR1": "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", + "TEST_STR2": "Test app. From Uvicorn with Gunicorn. Using Python 3.6", + }, + { + "NAME": "python3.7-alpine3.8", + "BUILD_PATH": "python3.7-alpine3.8", + "TEST_STR1": "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", + "TEST_STR2": "Test app. From Uvicorn with Gunicorn. Using Python 3.7", + }, + { + "NAME": "python3.6-alpine3.8", + "BUILD_PATH": "python3.6-alpine3.8", + "TEST_STR1": "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", + "TEST_STR2": "Test app. From Uvicorn with Gunicorn. Using Python 3.6", + }, +] + +start_with = os.environ.get("START_WITH") +build_push = os.environ.get("BUILD_PUSH") + + +def process_tag(*, env: dict): + use_env = {**os.environ, **env} + script = "scripts/test.sh" + if build_push: + script = "scripts/build-push.sh" + return_code = subprocess.call(["bash", script], env=use_env) + if return_code != 0: + sys.exit(return_code) + + +def print_version_envs(): + env_lines = [] + for env in environments: + env_vars = [] + for key, value in env.items(): + env_vars.append(f"{key}='{value}'") + env_lines.append(" ".join(env_vars)) + for line in env_lines: + print(line) + + +def main(): + start_at = 0 + if start_with: + start_at = [ + i for i, env in enumerate((environments)) if env["NAME"] == start_with + ][0] + for i, env in enumerate(environments[start_at:]): + print(f"Processing tag: {env['NAME']}") + process_tag(env=env) + + +if __name__ == "__main__": + if len(sys.argv) > 1: + print_version_envs() + else: + main() diff --git a/scripts/test-all.sh b/scripts/test-all.sh new file mode 100644 index 00000000..89b34d38 --- /dev/null +++ b/scripts/test-all.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -e + +python scripts/process_all.py diff --git a/scripts/test.sh b/scripts/test.sh index dfcc3a92..d0281669 100644 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash set -e -docker-compose -f docker-compose.build.yml build +use_tag="tiangolo/uvicorn-gunicorn:$NAME" + +docker build -t "$use_tag" "$BUILD_PATH" pytest tests diff --git a/tests/test_01_main/test_defaults.py b/tests/test_01_main/test_defaults.py index 3d1b43a2..65428213 100644 --- a/tests/test_01_main/test_defaults.py +++ b/tests/test_01_main/test_defaults.py @@ -1,7 +1,7 @@ +import os import time import docker -import pytest import requests from ..utils import CONTAINER_NAME, get_config, get_logs, remove_previous_container @@ -27,42 +27,21 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "image,response_text", - [ - ( - "tiangolo/uvicorn-gunicorn:python3.6", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:latest", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", - ), - ], -) -def test_defaults(image, response_text): +def test_defaults(): + name = os.getenv("NAME") + image = f"tiangolo/uvicorn-gunicorn:{name}" + response_text = os.getenv("TEST_STR1") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) container = client.containers.run( image, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_01_main/test_env_vars_1.py b/tests/test_01_main/test_env_vars_1.py index 3b4f22bc..b6b640d6 100644 --- a/tests/test_01_main/test_env_vars_1.py +++ b/tests/test_01_main/test_env_vars_1.py @@ -1,7 +1,7 @@ +import os import time import docker -import pytest import requests from ..utils import CONTAINER_NAME, get_config, get_logs, remove_previous_container @@ -26,32 +26,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "image,response_text", - [ - ( - "tiangolo/uvicorn-gunicorn:python3.6", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:latest", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", - ), - ], -) -def test_env_vars_1(image, response_text): +def test_env_vars_1(): + name = os.getenv("NAME") + image = f"tiangolo/uvicorn-gunicorn:{name}" + response_text = os.getenv("TEST_STR1") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) container = client.containers.run( image, @@ -60,12 +39,12 @@ def test_env_vars_1(image, response_text): ports={"8000": "8000"}, detach=True, ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_01_main/test_env_vars_2.py b/tests/test_01_main/test_env_vars_2.py index 07c8f4a7..d43bb560 100644 --- a/tests/test_01_main/test_env_vars_2.py +++ b/tests/test_01_main/test_env_vars_2.py @@ -1,7 +1,7 @@ +import os import time import docker -import pytest from ..utils import ( CONTAINER_NAME, @@ -31,17 +31,10 @@ def verify_container(container): ) -@pytest.mark.parametrize( - "image", - [ - ("tiangolo/uvicorn-gunicorn:python3.6"), - ("tiangolo/uvicorn-gunicorn:python3.7"), - ("tiangolo/uvicorn-gunicorn:latest"), - ("tiangolo/uvicorn-gunicorn:python3.6-alpine3.8"), - ("tiangolo/uvicorn-gunicorn:python3.7-alpine3.8"), - ], -) -def test_env_vars_2(image): +def test_env_vars_2(): + name = os.getenv("NAME") + image = f"tiangolo/uvicorn-gunicorn:{name}" + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) container = client.containers.run( image, @@ -50,12 +43,12 @@ def test_env_vars_2(image): ports={"80": "8000"}, detach=True, ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container) container.stop() container.remove() diff --git a/tests/test_01_main/test_env_vars_3.py b/tests/test_01_main/test_env_vars_3.py index b04783f0..745229db 100644 --- a/tests/test_01_main/test_env_vars_3.py +++ b/tests/test_01_main/test_env_vars_3.py @@ -1,7 +1,7 @@ +import os import time import docker -import pytest import requests from ..utils import CONTAINER_NAME, get_config, get_logs, remove_previous_container @@ -24,32 +24,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "image,response_text", - [ - ( - "tiangolo/uvicorn-gunicorn:python3.6", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:latest", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", - ), - ], -) -def test_env_bind(image, response_text): +def test_env_bind(): + name = os.getenv("NAME") + image = f"tiangolo/uvicorn-gunicorn:{name}" + response_text = os.getenv("TEST_STR1") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) container = client.containers.run( image, @@ -58,12 +37,12 @@ def test_env_bind(image, response_text): ports={"8080": "8000"}, detach=True, ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_02_app/test_custom_app.py b/tests/test_02_app/test_custom_app.py index fecbf43f..c8bc0386 100644 --- a/tests/test_02_app/test_custom_app.py +++ b/tests/test_02_app/test_custom_app.py @@ -1,3 +1,4 @@ +import os import time from pathlib import Path, PurePath @@ -35,61 +36,17 @@ def verify_container(container, response_text): @pytest.mark.parametrize( - "dockerfile,environment,response_text", + "environment", [ - ( - "python3.6.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "latest.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "python3.6-alpine3.8.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "python3.6.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "latest.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "python3.6-alpine3.8.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), + {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, + {"APP_MODULE": "custom_app.custom_main:custom_var"}, ], ) -def test_custom_app(dockerfile, environment, response_text): +def test_custom_app(environment): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) test_path: PurePath = Path(__file__) path = test_path.parent / "custom_app" @@ -101,12 +58,12 @@ def test_custom_app(dockerfile, environment, response_text): ports={"80": "8000"}, detach=True, ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_02_app/test_package_app.py b/tests/test_02_app/test_package_app.py index ae0bbd2f..55a2e937 100644 --- a/tests/test_02_app/test_package_app.py +++ b/tests/test_02_app/test_package_app.py @@ -1,8 +1,8 @@ +import os import time from pathlib import Path, PurePath import docker -import pytest import requests from ..utils import ( @@ -34,29 +34,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "dockerfile,response_text", - [ - ( - "python3.6.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ("latest.dockerfile", "Test app. From Uvicorn with Gunicorn. Using Python 3.7"), - ( - "python3.6-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ], -) -def test_package_app(dockerfile, response_text): +def test_package_app(): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) test_path: PurePath = Path(__file__) path = test_path.parent / "package_app" @@ -64,12 +46,12 @@ def test_package_app(dockerfile, response_text): container = client.containers.run( IMAGE_NAME, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_02_app/test_package_app_config.py b/tests/test_02_app/test_package_app_config.py index 8dc4c682..6e17a08c 100644 --- a/tests/test_02_app/test_package_app_config.py +++ b/tests/test_02_app/test_package_app_config.py @@ -1,8 +1,8 @@ +import os import time from pathlib import Path, PurePath import docker -import pytest import requests from ..utils import ( @@ -34,29 +34,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "dockerfile,response_text", - [ - ( - "python3.6.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ("latest.dockerfile", "Test app. From Uvicorn with Gunicorn. Using Python 3.7"), - ( - "python3.6-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ], -) -def test_package_app_config(dockerfile, response_text): +def test_package_app_config(): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) test_path: PurePath = Path(__file__) path = test_path.parent / "package_app_config" @@ -64,12 +46,12 @@ def test_package_app_config(dockerfile, response_text): container = client.containers.run( IMAGE_NAME, name=CONTAINER_NAME, ports={"8000": "8000"}, detach=True ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_02_app/test_package_app_custom_config.py b/tests/test_02_app/test_package_app_custom_config.py index c5433b45..2a4103c2 100644 --- a/tests/test_02_app/test_package_app_custom_config.py +++ b/tests/test_02_app/test_package_app_custom_config.py @@ -1,8 +1,8 @@ +import os import time from pathlib import Path, PurePath import docker -import pytest import requests from ..utils import ( @@ -34,29 +34,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "dockerfile,response_text", - [ - ( - "python3.6.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ("latest.dockerfile", "Test app. From Uvicorn with Gunicorn. Using Python 3.7"), - ( - "python3.6-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ], -) -def test_package_app_custom_config(dockerfile, response_text): +def test_package_app_custom_config(): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) test_path: PurePath = Path(__file__) path = test_path.parent / "package_app_custom_config" @@ -68,12 +50,12 @@ def test_package_app_custom_config(dockerfile, response_text): ports={"8000": "8000"}, detach=True, ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_02_app/test_package_app_sub_config.py b/tests/test_02_app/test_package_app_sub_config.py index 1ceda897..84456c33 100644 --- a/tests/test_02_app/test_package_app_sub_config.py +++ b/tests/test_02_app/test_package_app_sub_config.py @@ -1,8 +1,8 @@ +import os import time from pathlib import Path, PurePath import docker -import pytest import requests from ..utils import ( @@ -34,29 +34,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "dockerfile,response_text", - [ - ( - "python3.6.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ("latest.dockerfile", "Test app. From Uvicorn with Gunicorn. Using Python 3.7"), - ( - "python3.6-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ], -) -def test_package_app_sub_config(dockerfile, response_text): +def test_package_app_sub_config(): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) test_path: PurePath = Path(__file__) path = test_path.parent / "package_app_sub_config" @@ -64,12 +46,12 @@ def test_package_app_sub_config(dockerfile, response_text): container = client.containers.run( IMAGE_NAME, name=CONTAINER_NAME, ports={"8000": "8000"}, detach=True ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_02_app/test_simple_app.py b/tests/test_02_app/test_simple_app.py index 3b609dc2..b7b61e57 100644 --- a/tests/test_02_app/test_simple_app.py +++ b/tests/test_02_app/test_simple_app.py @@ -1,8 +1,8 @@ +import os import time from pathlib import Path, PurePath import docker -import pytest import requests from ..utils import ( @@ -34,43 +34,24 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "dockerfile,response_text", - [ - ( - "python3.6.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ("latest.dockerfile", "Test app. From Uvicorn with Gunicorn. Using Python 3.7"), - ( - "python3.6-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ], -) -def test_simple_app(dockerfile, response_text): +def test_simple_app(): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - IMAGE_NAME test_path: PurePath = Path(__file__) path = test_path.parent / "simple_app" client.images.build(path=str(path), dockerfile=dockerfile, tag=IMAGE_NAME) container = client.containers.run( IMAGE_NAME, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_03_reload/test_defaults.py b/tests/test_03_reload/test_defaults.py index 087dace1..2fcecf93 100644 --- a/tests/test_03_reload/test_defaults.py +++ b/tests/test_03_reload/test_defaults.py @@ -1,7 +1,7 @@ +import os import time import docker -import pytest import requests from docker.models.containers import Container @@ -22,32 +22,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "image,response_text", - [ - ( - "tiangolo/uvicorn-gunicorn:python3.6", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:latest", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", - ), - ], -) -def test_defaults(image: str, response_text: str): +def test_defaults(): + name = os.getenv("NAME") + image = f"tiangolo/uvicorn-gunicorn:{name}" + response_text = os.getenv("TEST_STR1") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) container: Container = client.containers.run( image, @@ -56,7 +35,7 @@ def test_defaults(image: str, response_text: str): detach=True, command="/start-reload.sh", ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.exec_run( "sed -i 's|Uvicorn with Gunicorn|Uvicorn with autoreload|' /app/main.py" @@ -64,7 +43,7 @@ def test_defaults(image: str, response_text: str): new_response_text = response_text.replace( "Uvicorn with Gunicorn", "Uvicorn with autoreload" ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, new_response_text) container.stop() container.remove() diff --git a/tests/test_03_reload/test_env_vars_1.py b/tests/test_03_reload/test_env_vars_1.py index 1d5e5838..e07ebdd5 100644 --- a/tests/test_03_reload/test_env_vars_1.py +++ b/tests/test_03_reload/test_env_vars_1.py @@ -1,7 +1,7 @@ +import os import time import docker -import pytest import requests from ..utils import CONTAINER_NAME, get_logs, remove_previous_container @@ -21,32 +21,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "image,response_text", - [ - ( - "tiangolo/uvicorn-gunicorn:python3.6", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:latest", - "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", - ), - ( - "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", - "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", - ), - ], -) -def test_env_vars_1(image, response_text): +def test_env_vars_1(): + name = os.getenv("NAME") + image = f"tiangolo/uvicorn-gunicorn:{name}" + response_text = os.getenv("TEST_STR1") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) container = client.containers.run( image, @@ -56,7 +35,7 @@ def test_env_vars_1(image, response_text): detach=True, command="/start-reload.sh", ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.exec_run( "sed -i 's|Uvicorn with Gunicorn|Uvicorn with autoreload|' /app/main.py" @@ -64,7 +43,7 @@ def test_env_vars_1(image, response_text): new_response_text = response_text.replace( "Uvicorn with Gunicorn", "Uvicorn with autoreload" ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, new_response_text) container.stop() container.remove() diff --git a/tests/test_03_reload/test_env_vars_2.py b/tests/test_03_reload/test_env_vars_2.py index 0b23324b..1fe4db6f 100644 --- a/tests/test_03_reload/test_env_vars_2.py +++ b/tests/test_03_reload/test_env_vars_2.py @@ -1,7 +1,7 @@ +import os import time import docker -import pytest from ..utils import ( CONTAINER_NAME, @@ -24,17 +24,11 @@ def verify_container(container): assert "Uvicorn running on http://127.0.0.1:80" in logs -@pytest.mark.parametrize( - "image", - [ - ("tiangolo/uvicorn-gunicorn:python3.6"), - ("tiangolo/uvicorn-gunicorn:python3.7"), - ("tiangolo/uvicorn-gunicorn:latest"), - ("tiangolo/uvicorn-gunicorn:python3.6-alpine3.8"), - ("tiangolo/uvicorn-gunicorn:python3.7-alpine3.8"), - ], -) -def test_env_vars_2(image): +def test_env_vars_2(): + name = os.getenv("NAME") + image = f"tiangolo/uvicorn-gunicorn:{name}" + sleep_time = int(os.getenv("SLEEP_TIME", 1)) + time.sleep(sleep_time) remove_previous_container(client) container = client.containers.run( image, @@ -44,7 +38,7 @@ def test_env_vars_2(image): detach=True, command="/start-reload.sh", ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container) container.stop() container.remove() diff --git a/tests/test_04_app_reload/test_custom_app.py b/tests/test_04_app_reload/test_custom_app.py index 0a64e0dd..9cbe8d20 100644 --- a/tests/test_04_app_reload/test_custom_app.py +++ b/tests/test_04_app_reload/test_custom_app.py @@ -1,3 +1,4 @@ +import os import time from pathlib import Path, PurePath @@ -29,61 +30,17 @@ def verify_container(container, response_text): @pytest.mark.parametrize( - "dockerfile,environment,response_text", + "environment", [ - ( - "python3.6.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "latest.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "python3.6-alpine3.8.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "python3.6.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "latest.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ( - "python3.6-alpine3.8.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - {"APP_MODULE": "custom_app.custom_main:custom_var"}, - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), + {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, + {"APP_MODULE": "custom_app.custom_main:custom_var"}, ], ) -def test_custom_app(dockerfile, environment, response_text): +def test_custom_app(environment): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) test_path: PurePath = Path(__file__) path = test_path.parent / "custom_app" @@ -96,7 +53,7 @@ def test_custom_app(dockerfile, environment, response_text): detach=True, command="/start-reload.sh", ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.exec_run( "sed -i 's|Uvicorn with Gunicorn|Uvicorn with autoreload|' /app/custom_app/custom_main.py" @@ -104,7 +61,7 @@ def test_custom_app(dockerfile, environment, response_text): new_response_text = response_text.replace( "Uvicorn with Gunicorn", "Uvicorn with autoreload" ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, new_response_text) container.stop() container.remove() diff --git a/tests/test_04_app_reload/test_package_app.py b/tests/test_04_app_reload/test_package_app.py index ae0bbd2f..55a2e937 100644 --- a/tests/test_04_app_reload/test_package_app.py +++ b/tests/test_04_app_reload/test_package_app.py @@ -1,8 +1,8 @@ +import os import time from pathlib import Path, PurePath import docker -import pytest import requests from ..utils import ( @@ -34,29 +34,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "dockerfile,response_text", - [ - ( - "python3.6.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ("latest.dockerfile", "Test app. From Uvicorn with Gunicorn. Using Python 3.7"), - ( - "python3.6-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ], -) -def test_package_app(dockerfile, response_text): +def test_package_app(): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) test_path: PurePath = Path(__file__) path = test_path.parent / "package_app" @@ -64,12 +46,12 @@ def test_package_app(dockerfile, response_text): container = client.containers.run( IMAGE_NAME, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() # Test that everything works after restarting too container.start() - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.stop() container.remove() diff --git a/tests/test_04_app_reload/test_simple_app.py b/tests/test_04_app_reload/test_simple_app.py index 9cf4ba6f..81c7dd16 100644 --- a/tests/test_04_app_reload/test_simple_app.py +++ b/tests/test_04_app_reload/test_simple_app.py @@ -1,8 +1,8 @@ +import os import time from pathlib import Path, PurePath import docker -import pytest import requests from ..utils import ( @@ -28,29 +28,11 @@ def verify_container(container, response_text): assert response.text == response_text -@pytest.mark.parametrize( - "dockerfile,response_text", - [ - ( - "python3.6.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ("latest.dockerfile", "Test app. From Uvicorn with Gunicorn. Using Python 3.7"), - ( - "python3.6-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.6", - ), - ( - "python3.7-alpine3.8.dockerfile", - "Test app. From Uvicorn with Gunicorn. Using Python 3.7", - ), - ], -) -def test_simple_app(dockerfile, response_text): +def test_simple_app(): + name = os.getenv("NAME") + dockerfile = f"{name}.dockerfile" + response_text = os.getenv("TEST_STR2") + sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) IMAGE_NAME test_path: PurePath = Path(__file__) @@ -63,7 +45,7 @@ def test_simple_app(dockerfile, response_text): detach=True, command="/start-reload.sh", ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, response_text) container.exec_run( "sed -i 's|Uvicorn with Gunicorn|Uvicorn with autoreload|' /app/main.py" @@ -71,7 +53,7 @@ def test_simple_app(dockerfile, response_text): new_response_text = response_text.replace( "Uvicorn with Gunicorn", "Uvicorn with autoreload" ) - time.sleep(1) + time.sleep(sleep_time) verify_container(container, new_response_text) container.stop() container.remove()