File tree Expand file tree Collapse file tree
src/djangoproject/django_project Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,5 +12,6 @@ COPY tests/ /tests/
1212
1313WORKDIR /src
1414ENV DJANGO_SETTINGS_MODULE=djangoproject.django_project.settings
15- RUN python /src/djangoproject/manage.py migrate
16- CMD python /src/djangoproject/manage.py runserver 0.0.0.0:80
15+ CMD python /tests/wait_for_postgres.py && \
16+ python /src/djangoproject/manage.py migrate && \
17+ python /src/djangoproject/manage.py runserver 0.0.0.0:80
Original file line number Diff line number Diff line change 11# app
22psycopg2-binary
33django
4+ dj-database-url
45
56# tests
67pytest
Original file line number Diff line number Diff line change 1111"""
1212
1313import os
14+ import dj_database_url
15+ from allocation import config
1416
1517# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
1618BASE_DIR = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
7375
7476# Database
7577# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
76-
77- DATABASES = {
78- "default" : {
79- "ENGINE" : "django.db.backends.sqlite3" ,
80- "NAME" : os .path .join (BASE_DIR , "db.sqlite3" ),
81- }
82- }
83-
78+ DATABASES = {"default" : dj_database_url .parse (config .get_postgres_uri ())}
8479
8580# Password validation
8681# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
Original file line number Diff line number Diff line change 11# pylint: disable=redefined-outer-name
22import time
33from pathlib import Path
4+
45import pytest
56import requests
6- from requests .exceptions import ConnectionError
7+
8+ from requests .exceptions import RequestException
9+
710from allocation import config
11+ import wait_for_postgres
812
913
1014def wait_for_webapp_to_come_up ():
@@ -13,7 +17,7 @@ def wait_for_webapp_to_come_up():
1317 while time .time () < deadline :
1418 try :
1519 return requests .get (url )
16- except ConnectionError :
20+ except RequestException :
1721 time .sleep (0.5 )
1822 pytest .fail ("API never came up" )
1923
@@ -23,3 +27,8 @@ def restart_api():
2327 (Path (__file__ ).parent / "../src/djangoproject/manage.py" ).touch ()
2428 time .sleep (0.5 )
2529 wait_for_webapp_to_come_up ()
30+
31+
32+ @pytest .fixture (autouse = True , scope = "session" )
33+ def wait_for_postgres_to_come_up ():
34+ wait_for_postgres .wait_for_postgres_to_come_up ()
Original file line number Diff line number Diff line change 1+ import time
2+ import psycopg2
3+ from allocation import config
4+
5+
6+ def wait_for_postgres_to_come_up ():
7+ deadline = time .time () + 10
8+ while time .time () < deadline :
9+ try :
10+ return psycopg2 .connect (config .get_postgres_uri ())
11+ except psycopg2 .OperationalError :
12+ time .sleep (0.5 )
13+ raise Exception ("Postgres never came up" )
14+
15+
16+ if __name__ == "__main__" :
17+ wait_for_postgres_to_come_up ()
You can’t perform that action at this time.
0 commit comments