From e0894180f10971126965bb81a4decf064899a140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 9 May 2020 22:12:54 +0200 Subject: [PATCH 1/3] :bug: Fix Alpine environment for Python 3.8 --- docker-images/python3.8-alpine.dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-images/python3.8-alpine.dockerfile b/docker-images/python3.8-alpine.dockerfile index bff6225..a76316b 100644 --- a/docker-images/python3.8-alpine.dockerfile +++ b/docker-images/python3.8-alpine.dockerfile @@ -51,7 +51,7 @@ ENV LISTEN_PORT 80 # Used by the entrypoint to explicitly add installed Python packages # and uWSGI Python packages to PYTHONPATH otherwise uWSGI can't import Flask -ENV ALPINEPYTHON python3.6 +ENV ALPINEPYTHON python3.8 # Copy start.sh script that will check for a /app/prestart.sh script and run it before starting the app COPY start.sh /start.sh From 890ab77e2282de5c83a89d5b78558493ff92656c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 9 May 2020 22:13:35 +0200 Subject: [PATCH 2/3] :white_check_mark: Add tests for applications with installs, to prevent Alpine without environment --- .../test_02_app/app_with_installs/app/main.py | 14 +++ tests/test_02_app/test_app_with_installs.py | 85 +++++++++++++++++++ tests/utils.py | 7 ++ 3 files changed, 106 insertions(+) create mode 100644 tests/test_02_app/app_with_installs/app/main.py create mode 100644 tests/test_02_app/test_app_with_installs.py diff --git a/tests/test_02_app/app_with_installs/app/main.py b/tests/test_02_app/app_with_installs/app/main.py new file mode 100644 index 0000000..d092edb --- /dev/null +++ b/tests/test_02_app/app_with_installs/app/main.py @@ -0,0 +1,14 @@ +import sys + +from flask import Flask + +application = Flask(__name__) + + +@application.route("/") +def hello(): + version = "{}.{}".format(sys.version_info.major, sys.version_info.minor) + message = "Hello World from Nginx uWSGI Python {} app in a Docker container".format( + version + ) + return message diff --git a/tests/test_02_app/test_app_with_installs.py b/tests/test_02_app/test_app_with_installs.py new file mode 100644 index 0000000..093e1c7 --- /dev/null +++ b/tests/test_02_app/test_app_with_installs.py @@ -0,0 +1,85 @@ +import os +import time +from pathlib import Path + +import docker +import requests +from docker.models.containers import Container + +from ..utils import ( + CONTAINER_NAME, generate_dockerfile_content_app_with_installs, + get_logs, + get_nginx_config, + get_response_text2, + remove_previous_container, +) + +client = docker.from_env() + + +def verify_container(container: Container, response_text: str) -> None: + response = requests.get("http://127.0.0.1:8000") + assert response.text == response_text + 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 + assert "worker_connections 1024;" in nginx_config + assert "worker_rlimit_nofile;" not in nginx_config + assert "daemon off;" in nginx_config + assert "include uwsgi_params;" in nginx_config + assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config + logs = get_logs(container) + assert "getting INI configuration from /app/uwsgi.ini" in logs + assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs + assert "ini = /app/uwsgi.ini" in logs + assert "ini = /etc/uwsgi/uwsgi.ini" in logs + assert "socket = /tmp/uwsgi.sock" in logs + assert "chown-socket = nginx:nginx" in logs + assert "chmod-socket = 664" in logs + assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs + assert "need-app = true" in logs + assert "die-on-term = true" in logs + assert "show-config = true" in logs + assert "wsgi-file = /app/main.py" in logs + assert "processes = 16" in logs + assert "cheaper = 2" in logs + assert "Checking for script in /app/prestart.sh" in logs + assert "Running script /app/prestart.sh" in logs + assert ( + "Running inside /app/prestart.sh, you could add migrations to this file" in logs + ) + assert "spawned uWSGI master process" in logs + assert "spawned uWSGI worker 1" in logs + assert "spawned uWSGI worker 2" in logs + assert "spawned uWSGI worker 3" not in logs + assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs + assert "success: nginx entered RUNNING state, process has stayed up for" in logs + assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs + + +def test_env_vars_1() -> None: + name = os.getenv("NAME", "") + dockerfile_content = generate_dockerfile_content_app_with_installs(name) + dockerfile = "Dockerfile" + response_text = get_response_text2() + sleep_time = int(os.getenv("SLEEP_TIME", 3)) + remove_previous_container(client) + tag = "uwsgi-nginx-testimage" + test_path = Path(__file__) + path = test_path.parent / "app_with_installs" + dockerfile_path = path / dockerfile + dockerfile_path.write_text(dockerfile_content) + client.images.build(path=str(path), dockerfile=dockerfile, tag=tag) + container = client.containers.run( + tag, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True + ) + time.sleep(sleep_time) + verify_container(container, response_text) + container.stop() + # Test that everything works after restarting too + container.start() + time.sleep(sleep_time) + verify_container(container, response_text) + container.stop() + container.remove() diff --git a/tests/utils.py b/tests/utils.py index 25f5083..f4bac1a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -54,3 +54,10 @@ def generate_dockerfile_content_simple_app(name: str) -> str: content = f"FROM tiangolo/uwsgi-nginx:{name}\n" content += "COPY ./app/main.py /app/main.py\n" return content + + +def generate_dockerfile_content_app_with_installs(name: str) -> str: + content = f"FROM tiangolo/uwsgi-nginx:{name}\n" + content += "RUN pip install flask\n" + content += "COPY ./app/main.py /app/main.py\n" + return content From 82c63759744598b553f2c9912fb1b3d9946165db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 9 May 2020 22:14:05 +0200 Subject: [PATCH 3/3] :art: Format funding --- .github/FUNDING.yml | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 39c0637..0ffc101 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,12 +1 @@ -# These are supported funding model platforms - -github: [tiangolo] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] -# patreon: # Replace with a single Patreon username -# open_collective: # Replace with a single Open Collective username -# ko_fi: # Replace with a single Ko-fi username -# tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel -# community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry -# liberapay: # Replace with a single Liberapay username -# issuehunt: # Replace with a single IssueHunt username -# otechie: # Replace with a single Otechie username -# custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] +github: [tiangolo]