From f5e253022383dae5715f8148ef3fee0efcb8463e Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Nov 27 2019 10:38:26 +0000
Subject: [PATCH 1/2] Use podman-compose by default if available


Signed-off-by: Lukas Holecek <hluk@email.cz>

---

diff --git a/Dockerfile b/Dockerfile
index b5c9dd6..cbd4406 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -34,5 +34,5 @@ RUN pip3 install . --no-deps
 RUN rm -rf ./fedmsg.d
 USER 1001
 EXPOSE 8080
-ENTRYPOINT ["docker/docker-entrypoint.sh"]
+ENTRYPOINT ["/src/docker/docker-entrypoint.sh"]
 CMD ["/usr/bin/gunicorn-3", "--workers", "8", "--bind", "0.0.0.0:8080", "--access-logfile", "-", "--enable-stdio-inheritance", "greenwave.wsgi:app"]
diff --git a/Makefile b/Makefile
index b3a7b19..fbac9d6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,21 @@
-COMPOSE := docker-compose
+# Use podman-compose by default if available.
+ifeq (, $(shell which podman-compose))
+    COMPOSE := docker-compose
+    PODMAN := docker
+else
+    COMPOSE := podman-compose
+    PODMAN := podman
+endif
+
 BROWSER := xdg-open
+SERVICE := dev
+TEST_REQUIREMENTS := dev-requirements.txt
+
+PYTHON := python3
+PIP := $(PYTHON) -m pip
+PYTEST := $(PYTHON) -m pytest --color=yes
+FLAKE8 := $(PYTHON) -m flake8
+PYLINT := $(PYTHON) -m pylint greenwave/
 
 all: help
 
@@ -27,6 +43,20 @@ help:
 	@echo '  make test - alias for "make pytest flake8 pylint"'
 	@echo
 	@echo '  make coverage [ARGS=".."] - generates and shows test code coverage'
+	@echo
+	@echo 'Variables:'
+	@echo
+	@echo '  COMPOSE=docker-compose|podman-compose'
+	@echo '    - docker-compose or podman-compose command'
+	@echo '      (default is "podman-compose" if available)'
+	@echo
+	@echo '  PODMAN=docker|podman'
+	@echo '    - docker or podman command'
+	@echo '      (default is "podman" if "podman-compose" is available)'
+	@echo
+	@echo '  SERVICE={dev|waiverdb|resultsdb|waiverdb-db|resultsdb-db|memcached}'
+	@echo '    - service for which to run `make exec` and similar (default is "dev")'
+	@echo '      Example: make exec SERVICE=waiverdb CMD=flake8'
 
 up:
 	$(COMPOSE) up -d
@@ -40,26 +70,27 @@ build:
 recreate:
 	$(COMPOSE) up -d --force-recreate
 
-# Executes CMD in dev container.
-# Usage: make exec CMD="python3 -m pytest -x"
-exec: up
-	$(COMPOSE) exec dev bash -c '$(CMD)'
+exec:
+	$(PODMAN) exec greenwave_$(SERVICE)_1 bash -c '$(CMD)'
+
+sudo:
+	$(PODMAN) exec -u root greenwave_$(SERVICE)_1 bash -c '$(CMD)'
 
-sudo: up
-	$(COMPOSE) exec -u root dev bash -c '$(CMD)'
+test: test_requirements pytest flake8 pylint
 
-test: pytest flake8 pylint
+test_requirements:
+	$(MAKE) exec CMD="$(PIP) install --user -r $(TEST_REQUIREMENTS)"
 
 pytest:
 	$(MAKE) exec \
-	    CMD="pip3 install --user -r dev-requirements.txt && COVERAGE_FILE=/home/dev/.coverage python3 -m pytest $(ARGS)"
+	    CMD="COVERAGE_FILE=/home/dev/.coverage-$(SERVICE) $(PYTEST) $(ARGS)"
 
 flake8:
-	python -m flake8
+	$(FLAKE8)
 
 pylint:
-	python -m pylint greenwave/
+	$(PYLINT) greenwave/
 
 coverage:
-	$(MAKE) pytest ARGS="--cov-config .coveragerc --cov=greenwave --cov-report html:/home/dev/htmlcov $(ARGS)"
-	$(BROWSER) docker/home/htmlcov/index.html
+	$(MAKE) pytest ARGS="--cov-config .coveragerc --cov=greenwave --cov-report html:/home/dev/htmlcov-$(SERVICE) $(ARGS)"
+	$(BROWSER) docker/home/htmlcov-$(SERVICE)/index.html
diff --git a/docker-compose.yml b/docker-compose.yml
index 462b80a..c0c031b 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -4,7 +4,7 @@ services:
     build: .
     user: ${DEV_USER_ID:-1000}
     working_dir: /code
-    env_file: docker/dev.env
+    env_file: ["docker/dev.env"]
     command: ["bash", "-c", "./docker/dev-command.sh"]
     volumes:
       - ./:/code:ro,z
@@ -22,7 +22,7 @@ services:
   resultsdb-db:
     image: postgres:9.5.2
     restart: always
-    env_file: docker/resultsdb-db.env
+    env_file: ["docker/resultsdb-db.env"]
     healthcheck:
       test: ["CMD-SHELL", "pg_isready -U postgres"]
       interval: 5s
@@ -47,14 +47,14 @@ services:
   waiverdb-db:
     image: postgres:9.5.2
     restart: always
-    env_file: docker/waiverdb-db.env
+    env_file: ["docker/waiverdb-db.env"]
 
   waiverdb:
     build:
       context: ../waiverdb
       dockerfile: openshift/containers/waiverdb/Dockerfile
     working_dir: /code
-    env_file: docker/waiverdb.env
+    env_file: ["docker/waiverdb.env"]
     command: ["bash", "-c", "/start.sh"]
     volumes:
       - ../waiverdb:/code:ro,Z
diff --git a/docker/dev-command.sh b/docker/dev-command.sh
index 2a014f3..c81434e 100755
--- a/docker/dev-command.sh
+++ b/docker/dev-command.sh
@@ -4,7 +4,8 @@ set -e
 # Check if UID is same as owner of the mounted home directory
 # (avoids creating files with wrong owner).
 real_dev_id="$(stat --format %u ~)"
-if [[ $real_dev_id != "$UID" ]]; then
+# The owner of mounted directories is root (uid=0) when using podman.
+if [[ ($real_dev_id == 0 && $UID == 0) || ($real_dev_id != 0 && $real_dev_id != "$UID") ]]; then
     echo "Set correct DEV_USER_ID in .env file (should be same as owner of docker/home)."
     exit 1
 fi
diff --git a/docker/waiverdb-db.env b/docker/waiverdb-db.env
index 2109fef..1ea5a31 100644
--- a/docker/waiverdb-db.env
+++ b/docker/waiverdb-db.env
@@ -1,4 +1,5 @@
 POSTGRES_USER=waiverdb
 POSTGRES_PASSWORD=waiverdb
 POSTGRES_DB=waiverdb
+PGPORT=5433
 POSTGRES_INITDB_ARGS=--auth=ident --auth=trust
diff --git a/docker/waiverdb-settings.py b/docker/waiverdb-settings.py
index 00edd9e..8263709 100644
--- a/docker/waiverdb-settings.py
+++ b/docker/waiverdb-settings.py
@@ -1,4 +1,7 @@
-DATABASE_URI = 'postgresql+psycopg2://waiverdb:waiverdb@waiverdb-db/waiverdb'
+import os
+
+DATABASE_URI = 'postgresql+psycopg2://waiverdb:waiverdb@waiverdb-db:5433/waiverdb'
+
 HOST = '0.0.0.0'
 PORT = 5004
 #AUTH_METHOD = 'OIDC'

From adce519b6078c43355de2e57ee855c6f249ddf42 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Nov 27 2019 10:38:26 +0000
Subject: [PATCH 2/2] Allow running tests for resultsdb and waiverdb with docker-compose


Signed-off-by: Lukas Holecek <hluk@email.cz>

---

diff --git a/Makefile b/Makefile
index fbac9d6..14eac44 100644
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,7 @@ PYTHON := python3
 PIP := $(PYTHON) -m pip
 PYTEST := $(PYTHON) -m pytest --color=yes
 FLAKE8 := $(PYTHON) -m flake8
-PYLINT := $(PYTHON) -m pylint greenwave/
+PYLINT := $(PYTHON) -m pylint
 
 all: help
 
@@ -44,6 +44,8 @@ help:
 	@echo
 	@echo '  make coverage [ARGS=".."] - generates and shows test code coverage'
 	@echo
+	@echo '  make test-waiverdb - run tests for WaiverDB'
+	@echo
 	@echo 'Variables:'
 	@echo
 	@echo '  COMPOSE=docker-compose|podman-compose'
@@ -94,3 +96,12 @@ pylint:
 coverage:
 	$(MAKE) pytest ARGS="--cov-config .coveragerc --cov=greenwave --cov-report html:/home/dev/htmlcov-$(SERVICE) $(ARGS)"
 	$(BROWSER) docker/home/htmlcov-$(SERVICE)/index.html
+
+test-waiverdb:
+	$(MAKE) SERVICE=waiverdb TEST_REQUIREMENTS=test-requirements.txt test_requirements
+	$(MAKE) SERVICE=waiverdb pytest
+	$(MAKE) exec CMD="$(PIP) install --user flake8 && $(FLAKE8) waiverdb"
+
+test-resultsdb:
+	$(MAKE) SERVICE=resultsdb TEST_REQUIREMENTS=requirements.txt test_requirements
+	$(MAKE) SERVICE=resultsdb pytest
diff --git a/docker-compose.yml b/docker-compose.yml
index c0c031b..3e0bf9d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -33,8 +33,11 @@ services:
     build:
       context: ../resultsdb
       dockerfile: openshift/Dockerfile
+    working_dir: /code
     command: ["bash", "-c", "/start.sh"]
     volumes:
+      - ../resultsdb:/code:ro,Z
+      - ./docker/home:/home/dev:Z
       - ./docker/resultsdb-command.sh:/start.sh:ro,z
       - ./docker/resultsdb-settings.py:/etc/resultsdb/settings.py:ro,z
       - ./docker/resultsdb.conf:/etc/httpd/conf.d/resultsdb.conf:ro,z
@@ -58,6 +61,7 @@ services:
     command: ["bash", "-c", "/start.sh"]
     volumes:
       - ../waiverdb:/code:ro,Z
+      - ./docker/home:/home/dev:Z
       - ./docker/waiverdb-command.sh:/start.sh:ro,z
       - ./docker/waiverdb-settings.py:/etc/waiverdb/settings.py:ro,z
       - ./docker/client_secrets.json:/etc/secret/client_secrets.json:ro,z
diff --git a/docker/waiverdb-settings.py b/docker/waiverdb-settings.py
index 8263709..813aa85 100644
--- a/docker/waiverdb-settings.py
+++ b/docker/waiverdb-settings.py
@@ -2,6 +2,9 @@ import os
 
 DATABASE_URI = 'postgresql+psycopg2://waiverdb:waiverdb@waiverdb-db:5433/waiverdb'
 
+if os.getenv('TEST') == 'true':
+    DATABASE_URI += '_test'
+
 HOST = '0.0.0.0'
 PORT = 5004
 #AUTH_METHOD = 'OIDC'
diff --git a/docker/waiverdb.env b/docker/waiverdb.env
index 9b1b228..bd3f546 100644
--- a/docker/waiverdb.env
+++ b/docker/waiverdb.env
@@ -1,4 +1,5 @@
 DATABASE_PASSWORD=waiverdb
 SECRET_KEY=waiverdb
 WAIVERDB_CONFIG=/etc/waiverdb/settings.py
+WAIVERDB_TEST_URL=http://waiverdb:5004/
 PYTEST_ADDOPTS=-o cache_dir=/tmp/.pytest_cache

