Skip to content

Commit

Permalink
🔥 Refactor tests, generate Dockerfiles during testing (#35)
Browse files Browse the repository at this point in the history
* 🔥 Generate each Dockerfile during tests

* 🙈 Add testing (generated) Dockerfiles to .gitignore

* 🔧 Update format scripts

* 🎨 Add types and format to tests

* 🐳 Update pip command order to disable cache
  • Loading branch information
tiangolo authored Apr 26, 2020
1 parent 9b08cb4 commit 7d7b335
Show file tree
Hide file tree
Showing 69 changed files with 164 additions and 216 deletions.
2 changes: 1 addition & 1 deletion docker-images/python3.6.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.6

LABEL maintainer="Sebastian Ramirez <[email protected]>"

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
Expand Down
2 changes: 1 addition & 1 deletion docker-images/python3.7.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.7

LABEL maintainer="Sebastian Ramirez <[email protected]>"

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
Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mypy]
disallow_untyped_defs = True
ignore_missing_imports = True
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 6 additions & 0 deletions scripts/format-imports.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
5 changes: 3 additions & 2 deletions tests/test_01_main/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import docker
import requests
from docker.client import DockerClient

from ..utils import (
CONTAINER_NAME,
Expand All @@ -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"
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_01_main/test_env_vars_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import docker
import requests
from docker.client import DockerClient

from ..utils import (
CONTAINER_NAME,
Expand All @@ -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"
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_01_main/test_env_vars_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time

import docker
from docker.models.containers import Container

from ..utils import (
CONTAINER_NAME,
Expand All @@ -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
Expand All @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions tests/test_01_main/test_env_vars_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import docker
import requests
from docker.client import DockerClient

from ..utils import (
CONTAINER_NAME,
Expand All @@ -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"
Expand All @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions tests/test_02_app/custom_app/latest.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/custom_app/python3.6-alpine3.8.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/custom_app/python3.6.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/custom_app/python3.7-alpine3.8.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/custom_app/python3.7.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app/latest.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app/python3.6-alpine3.8.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app/python3.6.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app/python3.7-alpine3.8.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app/python3.7.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app_config/latest.dockerfile

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app_config/python3.6.dockerfile

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app_config/python3.7.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app_custom_config/latest.dockerfile

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app_sub_config/latest.dockerfile

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app_sub_config/python3.6.dockerfile

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/package_app_sub_config/python3.7.dockerfile

This file was deleted.

2 changes: 0 additions & 2 deletions tests/test_02_app/simple_app/latest.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/simple_app/python3.6-alpine3.8.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/simple_app/python3.6.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/simple_app/python3.7-alpine3.8.dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_02_app/simple_app/python3.7.dockerfile

This file was deleted.

20 changes: 14 additions & 6 deletions tests/test_02_app/test_custom_app.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions tests/test_02_app/test_package_app.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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"
Expand All @@ -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
Expand Down
Loading

0 comments on commit 7d7b335

Please sign in to comment.