From 7d7b3356dd03f99fbf010db7166f24f5a6d25fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 26 Apr 2020 19:19:48 +0200 Subject: [PATCH] :fire: Refactor tests, generate Dockerfiles during testing (#35) * :fire: Generate each Dockerfile during tests * :see_no_evil: Add testing (generated) Dockerfiles to .gitignore * :wrench: Update format scripts * :art: Add types and format to tests * :whale: Update pip command order to disable cache --- docker-images/python3.6.dockerfile | 2 +- docker-images/python3.7.dockerfile | 2 +- mypy.ini | 3 +++ pyproject.toml | 4 ++++ scripts/format-imports.sh | 6 ++++++ scripts/lint.sh | 8 +++++++ tests/.gitignore | 1 + tests/test_01_main/test_defaults.py | 5 +++-- tests/test_01_main/test_env_vars_1.py | 5 +++-- tests/test_01_main/test_env_vars_2.py | 5 +++-- tests/test_01_main/test_env_vars_3.py | 5 +++-- .../test_02_app/custom_app/latest.dockerfile | 3 --- .../custom_app/python3.6-alpine3.8.dockerfile | 3 --- .../custom_app/python3.6.dockerfile | 3 --- .../custom_app/python3.7-alpine3.8.dockerfile | 3 --- .../custom_app/python3.7.dockerfile | 3 --- .../test_02_app/package_app/latest.dockerfile | 3 --- .../python3.6-alpine3.8.dockerfile | 3 --- .../package_app/python3.6.dockerfile | 3 --- .../python3.7-alpine3.8.dockerfile | 3 --- .../package_app/python3.7.dockerfile | 3 --- .../package_app_config/latest.dockerfile | 3 --- .../python3.6-alpine3.8.dockerfile | 3 --- .../package_app_config/python3.6.dockerfile | 3 --- .../python3.7-alpine3.8.dockerfile | 3 --- .../package_app_config/python3.7.dockerfile | 3 --- .../latest.dockerfile | 3 --- .../python3.6-alpine3.8.dockerfile | 3 --- .../python3.6.dockerfile | 3 --- .../python3.7-alpine3.8.dockerfile | 3 --- .../python3.7.dockerfile | 3 --- .../package_app_sub_config/latest.dockerfile | 3 --- .../python3.6-alpine3.8.dockerfile | 3 --- .../python3.6.dockerfile | 3 --- .../python3.7-alpine3.8.dockerfile | 3 --- .../python3.7.dockerfile | 3 --- .../test_02_app/simple_app/latest.dockerfile | 2 -- .../simple_app/python3.6-alpine3.8.dockerfile | 3 --- .../simple_app/python3.6.dockerfile | 3 --- .../simple_app/python3.7-alpine3.8.dockerfile | 3 --- .../simple_app/python3.7.dockerfile | 3 --- tests/test_02_app/test_custom_app.py | 20 ++++++++++++------ tests/test_02_app/test_package_app.py | 17 +++++++++------ tests/test_02_app/test_package_app_config.py | 17 +++++++++------ .../test_package_app_custom_config.py | 17 +++++++++------ .../test_package_app_sub_config.py | 17 +++++++++------ tests/test_02_app/test_simple_app.py | 17 +++++++++------ tests/test_03_reload/test_defaults.py | 5 +++-- tests/test_03_reload/test_env_vars_1.py | 5 +++-- tests/test_03_reload/test_env_vars_2.py | 13 ++++-------- .../custom_app/latest.dockerfile | 3 --- .../custom_app/python3.6-alpine3.8.dockerfile | 3 --- .../custom_app/python3.6.dockerfile | 3 --- .../custom_app/python3.7-alpine3.8.dockerfile | 3 --- .../custom_app/python3.7.dockerfile | 3 --- .../package_app/latest.dockerfile | 3 --- .../python3.6-alpine3.8.dockerfile | 3 --- .../package_app/python3.6.dockerfile | 3 --- .../python3.7-alpine3.8.dockerfile | 3 --- .../package_app/python3.7.dockerfile | 3 --- .../simple_app/latest.dockerfile | 2 -- .../simple_app/python3.6-alpine3.8.dockerfile | 3 --- .../simple_app/python3.6.dockerfile | 3 --- .../simple_app/python3.7-alpine3.8.dockerfile | 3 --- .../simple_app/python3.7.dockerfile | 3 --- tests/test_04_app_reload/test_custom_app.py | 18 ++++++++++------ tests/test_04_app_reload/test_package_app.py | 17 +++++++++------ tests/test_04_app_reload/test_simple_app.py | 17 +++++++++------ tests/utils.py | 21 +++++++++++++------ 69 files changed, 164 insertions(+), 216 deletions(-) create mode 100644 mypy.ini create mode 100755 scripts/format-imports.sh create mode 100755 scripts/lint.sh create mode 100644 tests/.gitignore delete mode 100644 tests/test_02_app/custom_app/latest.dockerfile delete mode 100644 tests/test_02_app/custom_app/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/custom_app/python3.6.dockerfile delete mode 100644 tests/test_02_app/custom_app/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/custom_app/python3.7.dockerfile delete mode 100644 tests/test_02_app/package_app/latest.dockerfile delete mode 100644 tests/test_02_app/package_app/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app/python3.6.dockerfile delete mode 100644 tests/test_02_app/package_app/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app/python3.7.dockerfile delete mode 100644 tests/test_02_app/package_app_config/latest.dockerfile delete mode 100644 tests/test_02_app/package_app_config/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app_config/python3.6.dockerfile delete mode 100644 tests/test_02_app/package_app_config/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app_config/python3.7.dockerfile delete mode 100644 tests/test_02_app/package_app_custom_config/latest.dockerfile delete mode 100644 tests/test_02_app/package_app_custom_config/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app_custom_config/python3.6.dockerfile delete mode 100644 tests/test_02_app/package_app_custom_config/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app_custom_config/python3.7.dockerfile delete mode 100644 tests/test_02_app/package_app_sub_config/latest.dockerfile delete mode 100644 tests/test_02_app/package_app_sub_config/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app_sub_config/python3.6.dockerfile delete mode 100644 tests/test_02_app/package_app_sub_config/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/package_app_sub_config/python3.7.dockerfile delete mode 100644 tests/test_02_app/simple_app/latest.dockerfile delete mode 100644 tests/test_02_app/simple_app/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/simple_app/python3.6.dockerfile delete mode 100644 tests/test_02_app/simple_app/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_02_app/simple_app/python3.7.dockerfile delete mode 100644 tests/test_04_app_reload/custom_app/latest.dockerfile delete mode 100644 tests/test_04_app_reload/custom_app/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_04_app_reload/custom_app/python3.6.dockerfile delete mode 100644 tests/test_04_app_reload/custom_app/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_04_app_reload/custom_app/python3.7.dockerfile delete mode 100644 tests/test_04_app_reload/package_app/latest.dockerfile delete mode 100644 tests/test_04_app_reload/package_app/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_04_app_reload/package_app/python3.6.dockerfile delete mode 100644 tests/test_04_app_reload/package_app/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_04_app_reload/package_app/python3.7.dockerfile delete mode 100644 tests/test_04_app_reload/simple_app/latest.dockerfile delete mode 100644 tests/test_04_app_reload/simple_app/python3.6-alpine3.8.dockerfile delete mode 100644 tests/test_04_app_reload/simple_app/python3.6.dockerfile delete mode 100644 tests/test_04_app_reload/simple_app/python3.7-alpine3.8.dockerfile delete mode 100644 tests/test_04_app_reload/simple_app/python3.7.dockerfile diff --git a/docker-images/python3.6.dockerfile b/docker-images/python3.6.dockerfile index 417c588a..8c5ad007 100644 --- a/docker-images/python3.6.dockerfile +++ b/docker-images/python3.6.dockerfile @@ -2,7 +2,7 @@ FROM python:3.6 LABEL maintainer="Sebastian Ramirez " -RUN pip --no-cache-dir install uvicorn gunicorn +RUN pip install --no-cache-dir uvicorn gunicorn COPY ./start.sh /start.sh RUN chmod +x /start.sh diff --git a/docker-images/python3.7.dockerfile b/docker-images/python3.7.dockerfile index fdfcacf8..381d0121 100644 --- a/docker-images/python3.7.dockerfile +++ b/docker-images/python3.7.dockerfile @@ -2,7 +2,7 @@ FROM python:3.7 LABEL maintainer="Sebastian Ramirez " -RUN pip --no-cache-dir install uvicorn gunicorn +RUN pip install --no-cache-dir uvicorn gunicorn COPY ./start.sh /start.sh RUN chmod +x /start.sh diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 00000000..4ff4483a --- /dev/null +++ b/mypy.ini @@ -0,0 +1,3 @@ +[mypy] +disallow_untyped_defs = True +ignore_missing_imports = True diff --git a/pyproject.toml b/pyproject.toml index 91bfb9d6..2e526a4a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,10 @@ docker = "^4.2.0" pytest = "^5.4.1" [tool.poetry.dev-dependencies] +black = "^19.10b0" +isort = "^4.3.21" +autoflake = "^1.3.1" +mypy = "^0.770" [build-system] requires = ["poetry>=0.12"] diff --git a/scripts/format-imports.sh b/scripts/format-imports.sh new file mode 100755 index 00000000..8710c79d --- /dev/null +++ b/scripts/format-imports.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -x + +# Sort imports one per line, so autoflake can remove unused imports +isort --recursive --force-single-line-imports --apply ./ +sh ./scripts/format.sh diff --git a/scripts/lint.sh b/scripts/lint.sh new file mode 100755 index 00000000..8198da08 --- /dev/null +++ b/scripts/lint.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e +set -x + +mypy ./ +black ./ --check +isort --multi-line=3 --trailing-comma --force-grid-wrap=0 --combine-as --line-width 88 --recursive --check-only diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 00000000..94143827 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +Dockerfile diff --git a/tests/test_01_main/test_defaults.py b/tests/test_01_main/test_defaults.py index 8a58d1b7..e90f3845 100644 --- a/tests/test_01_main/test_defaults.py +++ b/tests/test_01_main/test_defaults.py @@ -3,6 +3,7 @@ import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, @@ -15,7 +16,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: config_data = get_config(container) assert config_data["workers_per_core"] == 1 assert config_data["host"] == "0.0.0.0" @@ -33,7 +34,7 @@ def verify_container(container, response_text): assert response.text == response_text -def test_defaults(): +def test_defaults() -> None: name = os.getenv("NAME") image = f"tiangolo/uvicorn-gunicorn:{name}" response_text = get_response_text1() diff --git a/tests/test_01_main/test_env_vars_1.py b/tests/test_01_main/test_env_vars_1.py index 6ab3f083..88135546 100644 --- a/tests/test_01_main/test_env_vars_1.py +++ b/tests/test_01_main/test_env_vars_1.py @@ -3,6 +3,7 @@ import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, @@ -15,7 +16,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: config_data = get_config(container) assert config_data["workers_per_core"] == 2 assert config_data["host"] == "0.0.0.0" @@ -32,7 +33,7 @@ def verify_container(container, response_text): assert response.text == response_text -def test_env_vars_1(): +def test_env_vars_1() -> None: name = os.getenv("NAME") image = f"tiangolo/uvicorn-gunicorn:{name}" response_text = get_response_text1() diff --git a/tests/test_01_main/test_env_vars_2.py b/tests/test_01_main/test_env_vars_2.py index d43bb560..70fcbf3d 100644 --- a/tests/test_01_main/test_env_vars_2.py +++ b/tests/test_01_main/test_env_vars_2.py @@ -2,6 +2,7 @@ import time import docker +from docker.models.containers import Container from ..utils import ( CONTAINER_NAME, @@ -14,7 +15,7 @@ client = docker.from_env() -def verify_container(container): +def verify_container(container: Container) -> None: process_names = get_process_names(container) config_data = get_config(container) assert config_data["workers"] == 1 @@ -31,7 +32,7 @@ def verify_container(container): ) -def test_env_vars_2(): +def test_env_vars_2() -> None: name = os.getenv("NAME") image = f"tiangolo/uvicorn-gunicorn:{name}" sleep_time = int(os.getenv("SLEEP_TIME", 1)) diff --git a/tests/test_01_main/test_env_vars_3.py b/tests/test_01_main/test_env_vars_3.py index 54e3fc13..b7eaeade 100644 --- a/tests/test_01_main/test_env_vars_3.py +++ b/tests/test_01_main/test_env_vars_3.py @@ -3,6 +3,7 @@ import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, @@ -15,7 +16,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: config_data = get_config(container) assert config_data["host"] == "127.0.0.1" assert config_data["port"] == "9000" @@ -30,7 +31,7 @@ def verify_container(container, response_text): assert response.text == response_text -def test_env_bind(): +def test_env_bind() -> None: name = os.getenv("NAME") image = f"tiangolo/uvicorn-gunicorn:{name}" response_text = get_response_text1() diff --git a/tests/test_02_app/custom_app/latest.dockerfile b/tests/test_02_app/custom_app/latest.dockerfile deleted file mode 100644 index e1d7ff14..00000000 --- a/tests/test_02_app/custom_app/latest.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest - -COPY ./app /app diff --git a/tests/test_02_app/custom_app/python3.6-alpine3.8.dockerfile b/tests/test_02_app/custom_app/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_02_app/custom_app/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/custom_app/python3.6.dockerfile b/tests/test_02_app/custom_app/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_02_app/custom_app/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_02_app/custom_app/python3.7-alpine3.8.dockerfile b/tests/test_02_app/custom_app/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_02_app/custom_app/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/custom_app/python3.7.dockerfile b/tests/test_02_app/custom_app/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_02_app/custom_app/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_02_app/package_app/latest.dockerfile b/tests/test_02_app/package_app/latest.dockerfile deleted file mode 100644 index e1d7ff14..00000000 --- a/tests/test_02_app/package_app/latest.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest - -COPY ./app /app diff --git a/tests/test_02_app/package_app/python3.6-alpine3.8.dockerfile b/tests/test_02_app/package_app/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_02_app/package_app/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app/python3.6.dockerfile b/tests/test_02_app/package_app/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_02_app/package_app/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_02_app/package_app/python3.7-alpine3.8.dockerfile b/tests/test_02_app/package_app/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_02_app/package_app/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app/python3.7.dockerfile b/tests/test_02_app/package_app/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_02_app/package_app/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_config/latest.dockerfile b/tests/test_02_app/package_app_config/latest.dockerfile deleted file mode 100644 index e1d7ff14..00000000 --- a/tests/test_02_app/package_app_config/latest.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest - -COPY ./app /app diff --git a/tests/test_02_app/package_app_config/python3.6-alpine3.8.dockerfile b/tests/test_02_app/package_app_config/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_02_app/package_app_config/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_config/python3.6.dockerfile b/tests/test_02_app/package_app_config/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_02_app/package_app_config/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_config/python3.7-alpine3.8.dockerfile b/tests/test_02_app/package_app_config/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_02_app/package_app_config/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_config/python3.7.dockerfile b/tests/test_02_app/package_app_config/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_02_app/package_app_config/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_custom_config/latest.dockerfile b/tests/test_02_app/package_app_custom_config/latest.dockerfile deleted file mode 100644 index e1d7ff14..00000000 --- a/tests/test_02_app/package_app_custom_config/latest.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest - -COPY ./app /app diff --git a/tests/test_02_app/package_app_custom_config/python3.6-alpine3.8.dockerfile b/tests/test_02_app/package_app_custom_config/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_02_app/package_app_custom_config/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_custom_config/python3.6.dockerfile b/tests/test_02_app/package_app_custom_config/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_02_app/package_app_custom_config/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_custom_config/python3.7-alpine3.8.dockerfile b/tests/test_02_app/package_app_custom_config/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_02_app/package_app_custom_config/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_custom_config/python3.7.dockerfile b/tests/test_02_app/package_app_custom_config/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_02_app/package_app_custom_config/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_sub_config/latest.dockerfile b/tests/test_02_app/package_app_sub_config/latest.dockerfile deleted file mode 100644 index e1d7ff14..00000000 --- a/tests/test_02_app/package_app_sub_config/latest.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest - -COPY ./app /app diff --git a/tests/test_02_app/package_app_sub_config/python3.6-alpine3.8.dockerfile b/tests/test_02_app/package_app_sub_config/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_02_app/package_app_sub_config/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_sub_config/python3.6.dockerfile b/tests/test_02_app/package_app_sub_config/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_02_app/package_app_sub_config/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_sub_config/python3.7-alpine3.8.dockerfile b/tests/test_02_app/package_app_sub_config/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_02_app/package_app_sub_config/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/package_app_sub_config/python3.7.dockerfile b/tests/test_02_app/package_app_sub_config/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_02_app/package_app_sub_config/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_02_app/simple_app/latest.dockerfile b/tests/test_02_app/simple_app/latest.dockerfile deleted file mode 100644 index e24982e0..00000000 --- a/tests/test_02_app/simple_app/latest.dockerfile +++ /dev/null @@ -1,2 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest -COPY ./app /app diff --git a/tests/test_02_app/simple_app/python3.6-alpine3.8.dockerfile b/tests/test_02_app/simple_app/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_02_app/simple_app/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/simple_app/python3.6.dockerfile b/tests/test_02_app/simple_app/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_02_app/simple_app/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_02_app/simple_app/python3.7-alpine3.8.dockerfile b/tests/test_02_app/simple_app/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_02_app/simple_app/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_02_app/simple_app/python3.7.dockerfile b/tests/test_02_app/simple_app/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_02_app/simple_app/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_02_app/test_custom_app.py b/tests/test_02_app/test_custom_app.py index d6320f81..e590a0e2 100644 --- a/tests/test_02_app/test_custom_app.py +++ b/tests/test_02_app/test_custom_app.py @@ -1,14 +1,17 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path +from typing import Dict import docker import pytest import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_config, get_logs, get_response_text2, @@ -18,7 +21,9 @@ client = docker.from_env() -def verify_container(container, response_text, prestart_str): +def verify_container( + container: DockerClient, response_text: str, prestart_str: str +) -> None: config_data = get_config(container) assert config_data["workers_per_core"] == 1 assert config_data["host"] == "0.0.0.0" @@ -60,14 +65,17 @@ def verify_container(container, response_text, prestart_str): ), ], ) -def test_custom_app(environment, prestart_str): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_custom_app(environment: Dict[str, str], prestart_str: str) -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "custom_app" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) client.images.build(path=str(path), dockerfile=dockerfile, tag=IMAGE_NAME) container = client.containers.run( IMAGE_NAME, diff --git a/tests/test_02_app/test_package_app.py b/tests/test_02_app/test_package_app.py index 9e3d3b15..4d4a462b 100644 --- a/tests/test_02_app/test_package_app.py +++ b/tests/test_02_app/test_package_app.py @@ -1,13 +1,15 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_config, get_logs, get_response_text2, @@ -17,7 +19,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: config_data = get_config(container) assert config_data["workers_per_core"] == 1 assert config_data["host"] == "0.0.0.0" @@ -35,14 +37,17 @@ def verify_container(container, response_text): assert response.text == response_text -def test_package_app(): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_package_app() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "package_app" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) 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 diff --git a/tests/test_02_app/test_package_app_config.py b/tests/test_02_app/test_package_app_config.py index 3dae9bab..9d8e1a27 100644 --- a/tests/test_02_app/test_package_app_config.py +++ b/tests/test_02_app/test_package_app_config.py @@ -1,13 +1,15 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_config, get_gunicorn_conf_path, get_logs, @@ -18,7 +20,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: gunicorn_conf_path = get_gunicorn_conf_path(container) config_data = get_config(container) assert gunicorn_conf_path == "/app/gunicorn_conf.py" @@ -35,14 +37,17 @@ def verify_container(container, response_text): assert response.text == response_text -def test_package_app_config(): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_package_app_config() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "package_app_config" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) client.images.build(path=str(path), dockerfile=dockerfile, tag=IMAGE_NAME) container = client.containers.run( IMAGE_NAME, name=CONTAINER_NAME, ports={"8000": "8000"}, detach=True 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 89aa0cb2..6793b9a0 100644 --- a/tests/test_02_app/test_package_app_custom_config.py +++ b/tests/test_02_app/test_package_app_custom_config.py @@ -1,13 +1,15 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_config, get_gunicorn_conf_path, get_logs, @@ -18,7 +20,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: gunicorn_conf_path = get_gunicorn_conf_path(container) config_data = get_config(container) assert gunicorn_conf_path == "/app/custom_gunicorn_conf.py" @@ -35,14 +37,17 @@ def verify_container(container, response_text): assert response.text == response_text -def test_package_app_custom_config(): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_package_app_custom_config() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "package_app_custom_config" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) client.images.build(path=str(path), dockerfile=dockerfile, tag=IMAGE_NAME) container = client.containers.run( IMAGE_NAME, 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 f5c69471..a52243cf 100644 --- a/tests/test_02_app/test_package_app_sub_config.py +++ b/tests/test_02_app/test_package_app_sub_config.py @@ -1,13 +1,15 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_config, get_gunicorn_conf_path, get_logs, @@ -18,7 +20,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: gunicorn_conf_path = get_gunicorn_conf_path(container) config_data = get_config(container) assert gunicorn_conf_path == "/app/app/gunicorn_conf.py" @@ -35,14 +37,17 @@ def verify_container(container, response_text): assert response.text == response_text -def test_package_app_sub_config(): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_package_app_sub_config() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "package_app_sub_config" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) client.images.build(path=str(path), dockerfile=dockerfile, tag=IMAGE_NAME) container = client.containers.run( IMAGE_NAME, name=CONTAINER_NAME, ports={"8000": "8000"}, detach=True diff --git a/tests/test_02_app/test_simple_app.py b/tests/test_02_app/test_simple_app.py index e89508fa..bd194aae 100644 --- a/tests/test_02_app/test_simple_app.py +++ b/tests/test_02_app/test_simple_app.py @@ -1,13 +1,15 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_config, get_logs, get_response_text2, @@ -17,7 +19,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: config_data = get_config(container) assert config_data["workers_per_core"] == 1 assert config_data["host"] == "0.0.0.0" @@ -35,14 +37,17 @@ def verify_container(container, response_text): assert response.text == response_text -def test_simple_app(): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_simple_app() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "simple_app" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) 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 diff --git a/tests/test_03_reload/test_defaults.py b/tests/test_03_reload/test_defaults.py index fdc38bfb..3332013b 100644 --- a/tests/test_03_reload/test_defaults.py +++ b/tests/test_03_reload/test_defaults.py @@ -3,6 +3,7 @@ import docker import requests +from docker.client import DockerClient from docker.models.containers import Container from ..utils import ( @@ -15,7 +16,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: logs = get_logs(container) assert "Checking for script in /app/prestart.sh" in logs assert "Running script /app/prestart.sh" in logs @@ -27,7 +28,7 @@ def verify_container(container, response_text): assert response.text == response_text -def test_defaults(): +def test_defaults() -> None: name = os.getenv("NAME") image = f"tiangolo/uvicorn-gunicorn:{name}" response_text = get_response_text1() diff --git a/tests/test_03_reload/test_env_vars_1.py b/tests/test_03_reload/test_env_vars_1.py index b9a49d0c..87547d4c 100644 --- a/tests/test_03_reload/test_env_vars_1.py +++ b/tests/test_03_reload/test_env_vars_1.py @@ -3,6 +3,7 @@ import docker import requests +from docker.client import DockerClient from ..utils import ( CONTAINER_NAME, @@ -14,7 +15,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: DockerClient, response_text: str) -> None: logs = get_logs(container) assert "Checking for script in /app/prestart.sh" in logs assert "Running script /app/prestart.sh" in logs @@ -26,7 +27,7 @@ def verify_container(container, response_text): assert response.text == response_text -def test_env_vars_1(): +def test_env_vars_1() -> None: name = os.getenv("NAME") image = f"tiangolo/uvicorn-gunicorn:{name}" response_text = get_response_text1() diff --git a/tests/test_03_reload/test_env_vars_2.py b/tests/test_03_reload/test_env_vars_2.py index 1fe4db6f..1a6c01d7 100644 --- a/tests/test_03_reload/test_env_vars_2.py +++ b/tests/test_03_reload/test_env_vars_2.py @@ -2,19 +2,14 @@ import time import docker +from docker.models.containers import Container -from ..utils import ( - CONTAINER_NAME, - get_config, - get_logs, - get_process_names, - remove_previous_container, -) +from ..utils import CONTAINER_NAME, get_logs, remove_previous_container client = docker.from_env() -def verify_container(container): +def verify_container(container: Container) -> None: logs = get_logs(container) assert "Checking for script in /app/prestart.sh" in logs assert "Running script /app/prestart.sh" in logs @@ -24,7 +19,7 @@ def verify_container(container): assert "Uvicorn running on http://127.0.0.1:80" in logs -def test_env_vars_2(): +def test_env_vars_2() -> None: name = os.getenv("NAME") image = f"tiangolo/uvicorn-gunicorn:{name}" sleep_time = int(os.getenv("SLEEP_TIME", 1)) diff --git a/tests/test_04_app_reload/custom_app/latest.dockerfile b/tests/test_04_app_reload/custom_app/latest.dockerfile deleted file mode 100644 index e1d7ff14..00000000 --- a/tests/test_04_app_reload/custom_app/latest.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest - -COPY ./app /app diff --git a/tests/test_04_app_reload/custom_app/python3.6-alpine3.8.dockerfile b/tests/test_04_app_reload/custom_app/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_04_app_reload/custom_app/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_04_app_reload/custom_app/python3.6.dockerfile b/tests/test_04_app_reload/custom_app/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_04_app_reload/custom_app/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_04_app_reload/custom_app/python3.7-alpine3.8.dockerfile b/tests/test_04_app_reload/custom_app/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_04_app_reload/custom_app/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_04_app_reload/custom_app/python3.7.dockerfile b/tests/test_04_app_reload/custom_app/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_04_app_reload/custom_app/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_04_app_reload/package_app/latest.dockerfile b/tests/test_04_app_reload/package_app/latest.dockerfile deleted file mode 100644 index e1d7ff14..00000000 --- a/tests/test_04_app_reload/package_app/latest.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest - -COPY ./app /app diff --git a/tests/test_04_app_reload/package_app/python3.6-alpine3.8.dockerfile b/tests/test_04_app_reload/package_app/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_04_app_reload/package_app/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_04_app_reload/package_app/python3.6.dockerfile b/tests/test_04_app_reload/package_app/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_04_app_reload/package_app/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_04_app_reload/package_app/python3.7-alpine3.8.dockerfile b/tests/test_04_app_reload/package_app/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_04_app_reload/package_app/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_04_app_reload/package_app/python3.7.dockerfile b/tests/test_04_app_reload/package_app/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_04_app_reload/package_app/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_04_app_reload/simple_app/latest.dockerfile b/tests/test_04_app_reload/simple_app/latest.dockerfile deleted file mode 100644 index e24982e0..00000000 --- a/tests/test_04_app_reload/simple_app/latest.dockerfile +++ /dev/null @@ -1,2 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:latest -COPY ./app /app diff --git a/tests/test_04_app_reload/simple_app/python3.6-alpine3.8.dockerfile b/tests/test_04_app_reload/simple_app/python3.6-alpine3.8.dockerfile deleted file mode 100644 index 4ca240f4..00000000 --- a/tests/test_04_app_reload/simple_app/python3.6-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6-alpine3.8 - -COPY ./app /app diff --git a/tests/test_04_app_reload/simple_app/python3.6.dockerfile b/tests/test_04_app_reload/simple_app/python3.6.dockerfile deleted file mode 100644 index 5373fb6b..00000000 --- a/tests/test_04_app_reload/simple_app/python3.6.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.6 - -COPY ./app /app diff --git a/tests/test_04_app_reload/simple_app/python3.7-alpine3.8.dockerfile b/tests/test_04_app_reload/simple_app/python3.7-alpine3.8.dockerfile deleted file mode 100644 index 4c4e7769..00000000 --- a/tests/test_04_app_reload/simple_app/python3.7-alpine3.8.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 - -COPY ./app /app diff --git a/tests/test_04_app_reload/simple_app/python3.7.dockerfile b/tests/test_04_app_reload/simple_app/python3.7.dockerfile deleted file mode 100644 index b9be60e5..00000000 --- a/tests/test_04_app_reload/simple_app/python3.7.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM tiangolo/uvicorn-gunicorn:python3.7 - -COPY ./app /app diff --git a/tests/test_04_app_reload/test_custom_app.py b/tests/test_04_app_reload/test_custom_app.py index 9229d531..85d76bef 100644 --- a/tests/test_04_app_reload/test_custom_app.py +++ b/tests/test_04_app_reload/test_custom_app.py @@ -1,14 +1,17 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path +from typing import Dict import docker import pytest import requests +from docker.models.containers import Container from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_logs, get_response_text2, remove_previous_container, @@ -17,7 +20,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: Container, response_text: str) -> None: logs = get_logs(container) assert "Checking for script in /app/prestart.sh" in logs assert "Running script /app/prestart.sh" in logs @@ -36,14 +39,17 @@ def verify_container(container, response_text): {"APP_MODULE": "custom_app.custom_main:custom_var"}, ], ) -def test_custom_app(environment): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_custom_app(environment: Dict[str, str]) -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "custom_app" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) client.images.build(path=str(path), dockerfile=dockerfile, tag=IMAGE_NAME) container = client.containers.run( IMAGE_NAME, diff --git a/tests/test_04_app_reload/test_package_app.py b/tests/test_04_app_reload/test_package_app.py index 9e3d3b15..6843d1a2 100644 --- a/tests/test_04_app_reload/test_package_app.py +++ b/tests/test_04_app_reload/test_package_app.py @@ -1,13 +1,15 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path import docker import requests +from docker.models.containers import Container from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_config, get_logs, get_response_text2, @@ -17,7 +19,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: Container, response_text: str) -> None: config_data = get_config(container) assert config_data["workers_per_core"] == 1 assert config_data["host"] == "0.0.0.0" @@ -35,14 +37,17 @@ def verify_container(container, response_text): assert response.text == response_text -def test_package_app(): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_package_app() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "package_app" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) 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 diff --git a/tests/test_04_app_reload/test_simple_app.py b/tests/test_04_app_reload/test_simple_app.py index 8e825703..22b136e0 100644 --- a/tests/test_04_app_reload/test_simple_app.py +++ b/tests/test_04_app_reload/test_simple_app.py @@ -1,13 +1,15 @@ import os import time -from pathlib import Path, PurePath +from pathlib import Path import docker import requests +from docker.models.containers import Container from ..utils import ( CONTAINER_NAME, IMAGE_NAME, + generate_dockerfile_content, get_logs, get_response_text2, remove_previous_container, @@ -16,7 +18,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container: Container, response_text: str) -> None: logs = get_logs(container) assert "Checking for script in /app/prestart.sh" in logs assert "Running script /app/prestart.sh" in logs @@ -28,15 +30,18 @@ def verify_container(container, response_text): assert response.text == response_text -def test_simple_app(): - name = os.getenv("NAME") - dockerfile = f"{name}.dockerfile" +def test_simple_app() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content(name) + dockerfile = "Dockerfile" response_text = get_response_text2() sleep_time = int(os.getenv("SLEEP_TIME", 1)) remove_previous_container(client) IMAGE_NAME - test_path: PurePath = Path(__file__) + test_path = Path(__file__) path = test_path.parent / "simple_app" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) client.images.build(path=str(path), dockerfile=dockerfile, tag=IMAGE_NAME) container = client.containers.run( IMAGE_NAME, diff --git a/tests/utils.py b/tests/utils.py index 4136dee0..eec27c2f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,20 +1,23 @@ import json import os +from typing import Any, Dict, List +from docker.client import DockerClient from docker.errors import NotFound +from docker.models.containers import Container CONTAINER_NAME = "uvicorn-gunicorn-test" IMAGE_NAME = "uvicorn-gunicorn-testimage" -def get_process_names(container): +def get_process_names(container: Container) -> List[str]: top = container.top() process_commands = [p[7] for p in top["Processes"]] gunicorn_processes = [p for p in process_commands if "gunicorn" in p] return gunicorn_processes -def get_gunicorn_conf_path(container): +def get_gunicorn_conf_path(container: Container) -> str: gunicorn_processes = get_process_names(container) first_process = gunicorn_processes[0] first_part, partition, last_part = first_process.partition("-c") @@ -22,13 +25,13 @@ def get_gunicorn_conf_path(container): return gunicorn_conf -def get_config(container): +def get_config(container: Container) -> Dict[str, Any]: gunicorn_conf = get_gunicorn_conf_path(container) result = container.exec_run(f"python {gunicorn_conf}") return json.loads(result.output.decode()) -def remove_previous_container(client): +def remove_previous_container(client: DockerClient) -> None: try: previous = client.containers.get(CONTAINER_NAME) previous.stop() @@ -37,8 +40,8 @@ def remove_previous_container(client): return None -def get_logs(container): - logs: str = container.logs() +def get_logs(container: DockerClient) -> str: + logs = container.logs() return logs.decode("utf-8") @@ -50,3 +53,9 @@ def get_response_text1() -> str: def get_response_text2() -> str: python_version = os.getenv("PYTHON_VERSION") return f"Test app. From Uvicorn with Gunicorn. Using Python {python_version}" + + +def generate_dockerfile_content(name: str) -> str: + content = f"FROM tiangolo/uvicorn-gunicorn:{name}\n" + content += "COPY ./app /app" + return content