Skip to content

Commit

Permalink
First tests and updates to default apps to expose Python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo committed Jan 9, 2019
1 parent 3796b51 commit 86b4d0c
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 30 deletions.
12 changes: 9 additions & 3 deletions python2.7-alpine3.7/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return ["Hello World from a default Nginx uWSGI Python 2.7 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container in Alpine (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python2.7-alpine3.8/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return ["Hello World from a default Nginx uWSGI Python 2.7 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container in Alpine (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python2.7/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return ["Hello World from a default Nginx uWSGI Python 2.7 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python3.5/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World from a default Nginx uWSGI Python 3.5 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python3.6-alpine3.7/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World from a default Nginx uWSGI Python 3.6 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container in Alpine (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python3.6-alpine3.8/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World from a default Nginx uWSGI Python 3.6 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container in Alpine (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python3.6/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World from a default Nginx uWSGI Python 3.6 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python3.7-alpine3.7/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World from a default Nginx uWSGI Python 3.7 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container in Alpine (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python3.7-alpine3.8/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World from a default Nginx uWSGI Python 3.7 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container in Alpine (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 9 additions & 3 deletions python3.7/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import sys


def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World from a default Nginx uWSGI Python 3.7 app in a\
Docker container (default)"]
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container (default)".format(
version
)
return [message.encode("utf-8")]
Empty file added tests/__init__.py
Empty file.
Empty file added tests/test_01_main/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions tests/test_01_main/test_defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import time

import docker
import pytest
import requests
from requests import Response

from ..utils import CONTAINER_NAME, stop_previous_container, get_uwsgi_config, get_nginx_config

client = docker.from_env()


@pytest.mark.parametrize(
"image,response_text",
[
(
"tiangolo/uwsgi-nginx:python2.7",
"Hello World from a default Nginx uWSGI Python 2.7 app in a Docker container (default)",
),
(
"tiangolo/uwsgi-nginx:python2.7-alpine3.7",
"Hello World from a default Nginx uWSGI Python 2.7 app in a Docker container in Alpine (default)",
),
(
"tiangolo/uwsgi-nginx:python2.7-alpine3.8",
"Hello World from a default Nginx uWSGI Python 2.7 app in a Docker container in Alpine (default)",
),
(
"tiangolo/uwsgi-nginx:python3.5",
"Hello World from a default Nginx uWSGI Python 3.5 app in a Docker container (default)",
),
(
"tiangolo/uwsgi-nginx:python3.6",
"Hello World from a default Nginx uWSGI Python 3.6 app in a Docker container (default)",
),
(
"tiangolo/uwsgi-nginx:python3.6-alpine3.7",
"Hello World from a default Nginx uWSGI Python 3.6 app in a Docker container in Alpine (default)",
),
(
"tiangolo/uwsgi-nginx:python3.6-alpine3.8",
"Hello World from a default Nginx uWSGI Python 3.6 app in a Docker container in Alpine (default)",
),
(
"tiangolo/uwsgi-nginx:python3.7",
"Hello World from a default Nginx uWSGI Python 3.7 app in a Docker container (default)",
),
# (
# "tiangolo/uwsgi-nginx:python3.7-alpine3.7",
# "Hello World from a default Nginx uWSGI Python 3.7 app in a Docker container in Alpine (default)",
# ),
# (
# "tiangolo/uwsgi-nginx:python3.7-alpine3.8",
# "Hello World from a default Nginx uWSGI Python 3.7 app in a Docker container in Alpine (default)",
# ),
],
)
def test_defaults(image, response_text):
stop_previous_container(client)
container = client.containers.run(
image, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True
)
uwsgi_config = get_uwsgi_config(container)
assert "ini = /app/uwsgi.ini" in uwsgi_config
assert "wsgi-file = /app/main.py" in uwsgi_config
assert "processes = 16" in uwsgi_config
assert "cheaper = 2" in uwsgi_config
nginx_config = get_nginx_config(container)
assert "client_max_body_size 0;" in nginx_config
assert "worker_processes 1;" in nginx_config
assert "listen 80;" in nginx_config
time.sleep(5)
response: Response = requests.get("http://127.0.0.1:8000")
assert response.status_code == 200
assert response.text == response_text
container.stop()
container.remove()
24 changes: 24 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json

from docker.errors import NotFound

CONTAINER_NAME = "uwsgi-nginx-text"


def get_uwsgi_config(container):
result = container.exec_run(f"uwsgi --show-config")
return result.output.decode()


def get_nginx_config(container):
result = container.exec_run(f"/usr/sbin/nginx -T")
return result.output.decode()


def stop_previous_container(client):
try:
previous = client.containers.get(CONTAINER_NAME)
previous.stop()
previous.remove()
except NotFound:
return None

0 comments on commit 86b4d0c

Please sign in to comment.