Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration test for nginx and django socket #733 #727 #734

Closed
wants to merge 13 commits into from
19 changes: 19 additions & 0 deletions .github/workflows/contrib.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: Test Integration

on:
push:
pull_request:

jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: nginx_django
working-directory: ./contrib/tests/nginx_django
run: |
chmod +x ./runtests.sh
./runtests.sh
16 changes: 16 additions & 0 deletions contrib/tests/nginx_django/django.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ARG PYTHON_VERSION="3.8"
FROM python:${PYTHON_VERSION}-slim

ARG DJANGO_VERSION="3.0.8"
# if set to any value then latest version of django will be installed
ARG DJANGO_LATEST

RUN if [ -z "${DJANGO_LATEST+x}" ]; then pip install django==${DJANGO_VERSION}; else pip install django; fi

WORKDIR /app
RUN django-admin startproject example
WORKDIR /app/example

COPY django.entrypoint.sh .
RUN chmod +x ./django.entrypoint.sh
ENTRYPOINT ["./django.entrypoint.sh"]
9 changes: 9 additions & 0 deletions contrib/tests/nginx_django/django.entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
pip install /app/uvicorn

rm -rf /app/uvicorn/.*
rm -rf /app/uvicorn/*

printf "ALLOWED_HOSTS=['*']" >> /app/example/example/settings.py

"$@"
32 changes: 32 additions & 0 deletions contrib/tests/nginx_django/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3.8'

services:

django:
build:
context: .
dockerfile: django.Dockerfile
command: >
uvicorn --uds /socket/socket.sock --log-level=trace --use-colors --proxy-headers example.asgi:application
environment:
FORWARDED_ALLOW_IPS: "0.0.0.0/0"
volumes:
- django-socket:/socket
- ./uvicorn:/app/uvicorn

nginx:
build:
context: .
dockerfile: nginx.Dockerfile
environment:
LISTEN_PORT: "9000"
SOCKET_PATH: "/socket/socket.sock"
volumes:
- django-socket:/socket
depends_on:
- django
ports:
- 9000:9000

volumes:
django-socket:
7 changes: 7 additions & 0 deletions contrib/tests/nginx_django/nginx.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM nginx:alpine
# nginx:1.19.1-alpine

COPY nginx.conf /etc/nginx/templates/default.conf.template
ENV SCHEME="http"
ENV LISTEN_PORT="9000"
ENV SOCKET_PATH="/socket/socket.sock"
24 changes: 24 additions & 0 deletions contrib/tests/nginx_django/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
upstream django {
server unix:${SOCKET_PATH};
}

server {

listen ${LISTEN_PORT};
server_name localhost;

location /static/ {
autoindex off;
alias /static/;
}

location / {
proxy_pass http://django/;
}

set_real_ip_from 0.0.0.0/0;
proxy_set_header Forwarded $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_protocol_addr;
proxy_set_header X-Forwarded-Proto $scheme;

}
43 changes: 43 additions & 0 deletions contrib/tests/nginx_django/runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
set -euo pipefail

rm -rfv ./uvicorn || true
mkdir ./uvicorn

# save current dir into var
CURDIR=${PWD}
# cd into base repo dir
cd ../../../.
# save base repo dir into var
REPODIR=${PWD}

# copy uvicorn skipping `contrib` dir
cp -r `ls -A $REPODIR | grep -v "contrib"` "${CURDIR}"/uvicorn

cd "${CURDIR}"
docker-compose up -d --build

echo "Waiting for uvicorn..."

for ((n=0;n<10;n++));
do
if [[ $(docker-compose logs) == *" Uvicorn running on unix socket"* ]]; then
echo "Uvicorn is ready, sending http request to nginx"
STATUS=$(curl -m 3 -o /dev/null -s -w "%{http_code}\n" http://localhost:9000 || true)
break
else
echo "Uvicorn not yet ready"; sleep 1s
fi
done

if [[ "$STATUS" == 200 ]]; then
echo "Success! Nginx responded with status code ${STATUS}"
exit 0
else
echo "Error! Nginx responded with status code ${STATUS}"
docker-compose logs -t
fi

docker-compose down -v
rm -rf ./uvicorn || true
exit 1