]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
Support testing with PostgreSQL
authorDaniel Axtens <dja@axtens.net>
Fri, 20 Oct 2017 05:36:55 +0000 (16:36 +1100)
committerDaniel Axtens <dja@axtens.net>
Sat, 28 Oct 2017 01:39:17 +0000 (12:39 +1100)
This allows us to easily test against PostgreSQL using the same
tooling we normally use. This is helpful in (for example) shaking
out the test failures that were observed on ozlabs.org

To use it:
  docker-compose -f docker-compose-pg.yml <usual argument>

(You may find in necessary to do a 'docker-compose down' first,
depending on what state the system is in and what command you're
running.)

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Stephen Finucane <stephen@that.guru>
docker-compose-pg.yml [new file with mode: 0644]
requirements-test.txt
tools/docker/Dockerfile
tools/docker/entrypoint.sh

diff --git a/docker-compose-pg.yml b/docker-compose-pg.yml
new file mode 100644 (file)
index 0000000..31ef843
--- /dev/null
@@ -0,0 +1,30 @@
+# the version of docker-compose shipped in ubuntu 16.04 is
+# 1.5.2, which doesn't support version 2 syntax. Yay!
+# also, v1 doesn't support explicit build args, so if you're not
+# uid 1000, you will either need to manually hack the Dockerfile
+# or upgrade to v2 and use the build-arg to override it.
+
+db:
+  image: postgres
+  environment:
+    - POSTGRES_PASSWORD=password
+  volumes:
+    - ./tools/docker/db/postdata:/var/lib/postgresql/data
+
+web:
+  build: .
+  dockerfile: ./tools/docker/Dockerfile
+  command: python3 manage.py runserver 0.0.0.0:8000
+  volumes:
+    - .:/home/patchwork/patchwork/
+  ports:
+    - "8000:8000"
+  links:
+    - db
+  environment:
+    - PGPASSWORD=password
+    - PW_TEST_DB_HOST=db
+    - PW_TEST_DB_PORT=5432
+    - PW_TEST_DB_TYPE=postgres
+    - PW_TEST_DB_USER=postgres
+    - PW_TEST_DB_PASS=password
index cead336c7c7794fa795633feaad46483634d81ec..141cf661663d55b117daab5ae67b3bb0b486850a 100644 (file)
@@ -1,4 +1,5 @@
-mysqlclient>=1.3,<1.4  # replace this with psycopg2 for a PostgreSQL backend
+mysqlclient>=1.3,<1.4
+psycopg2>=2.7,<2.8
 django-debug-toolbar==1.8
 python-dateutil>2.0,<3.0
 selenium>=3.0,<4.0
index ff05707a60490da6edd0f268f68465193cbcfe44..946c646188e52ac53779556614c676637e92d59d 100644 (file)
@@ -21,7 +21,7 @@ RUN apt-get update -qq && \
     python3.5-dev python3-pip python3-setuptools python3-wheel \
     python3.4-dev findutils=4.4.2-7 \
     libmysqlclient-dev mysql-client curl unzip xvfb chromium-chromedriver \
-    chromium-browser build-essential git && \
+    chromium-browser build-essential git postgresql-client && \
     ln -s /usr/lib/chromium-browser/chromedriver /usr/bin/
 
 # User
index 2f413b02e83eb008442141b50ac3711f0afb9b14..7e05f46daffc15e1e97321eeb9ed3d3447b421fe 100755 (executable)
@@ -1,13 +1,27 @@
 #!/bin/bash
 set -euo pipefail
 
+PW_TEST_DB_TYPE=${PW_TEST_DB_TYPE:-mysql}
+
 # functions
 
 test_db_connection() {
-    mysqladmin -h $PW_TEST_DB_HOST -u patchwork --password=password ping > /dev/null 2> /dev/null
+    if [ ${PW_TEST_DB_TYPE} = "postgres" ]; then
+       echo ';' | psql -h $PW_TEST_DB_HOST -U postgres 2> /dev/null > /dev/null
+    else
+       mysqladmin -h $PW_TEST_DB_HOST -u patchwork --password=password ping > /dev/null 2> /dev/null
+    fi
 }
 
-reset_data() {
+test_database() {
+    if [ ${PW_TEST_DB_TYPE} = "postgres" ]; then
+       echo ';' | psql -h $PW_TEST_DB_HOST -U postgres patchwork 2> /dev/null
+    else
+       echo ';' | mysql -h $PW_TEST_DB_HOST -u patchwork -ppassword patchwork 2> /dev/null
+    fi
+}
+
+reset_data_mysql() {
     mysql -u$db_user -p$db_pass -h $PW_TEST_DB_HOST << EOF
 DROP DATABASE IF EXISTS patchwork;
 CREATE DATABASE patchwork CHARACTER SET utf8;
@@ -15,6 +29,21 @@ GRANT ALL ON patchwork.* TO 'patchwork' IDENTIFIED BY 'password';
 GRANT ALL PRIVILEGES ON test_patchwork.* TO 'patchwork'@'%';
 FLUSH PRIVILEGES;
 EOF
+}
+
+reset_data_postgres() {
+    psql -h $PW_TEST_DB_HOST -U postgres <<EOF
+DROP DATABASE IF EXISTS patchwork;
+CREATE DATABASE patchwork WITH ENCODING = 'UTF8';
+EOF
+}
+
+reset_data() {
+    if [ x${PW_TEST_DB_TYPE} = x"postgres" ]; then
+       reset_data_postgres
+    else
+       reset_data_mysql
+    fi
 
     # load initial data
     python3 $PROJECT_HOME/manage.py migrate #> /dev/null
@@ -46,13 +75,13 @@ for x in /tmp/requirements-*.txt; do
     fi
 done
 
-# check if mysql is connected
+# check if db is connected
 if ! test_db_connection; then
-    echo "MySQL seems not to be connected, or the patchwork user is broken"
-    echo "MySQL may still be starting. Waiting 5 seconds."
+    echo "The database seems not to be connected, or the patchwork user is broken"
+    echo "MySQL/Postgres may still be starting. Waiting 5 seconds."
     sleep 5
     if ! test_db_connection; then
-        echo "Still cannot connect to MySQL."
+        echo "Still cannot connect to database."
         echo "Maybe you are starting the db for the first time. Waiting up to 60 seconds."
         for i in {0..9}; do
             sleep 5
@@ -61,19 +90,19 @@ if ! test_db_connection; then
             fi
         done
         if ! test_db_connection; then
-            echo "Still cannot connect to MySQL. Giving up."
+            echo "Still cannot connect to database. Giving up."
             echo "Are you using docker-compose? If not, have you set up the link correctly?"
             exit 1
         fi
     fi
 fi
 
-# rebuild mysql db
+# rebuild db
 # do this on --reset or if the db doesn't exist
 if [[ "$1" == "--reset" ]]; then
     shift
     reset_data
-elif ! ( echo ';' | mysql -h db -u patchwork -ppassword patchwork 2> /dev/null ); then
+elif ! test_database; then
     reset_data
 fi