Skip to content

Commit

Permalink
Create test-specific 🐳 file (#1082)
Browse files Browse the repository at this point in the history
## Related to:
- #1054

## Description of Changes
Creating a test-specific 🐳 Docker file. This will be the first step in
implementing PostgreSQL in the testing environment so that we can get a
true 1:1 comparison between the environments.

## Tests and linting
 - [x] This branch is up-to-date with the `develop` branch.
 - [x] `pytest` passes on my local development environment.
 - [x] `pre-commit` passes on my local development environment.
  • Loading branch information
michplunkett authored May 4, 2024
1 parent ec9a1ec commit 2d82e06
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 36 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/test_prs.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
name: CI

# Controls when the action will run.
on:
pull_request:
branches:
- develop
- main

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-20.04
env:
FLASK_APP: OpenOversight.app
strategy:
matrix:
python-version: ["3.11", "3.12"]
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
- name: Run tests
run: PYTHON_VERSION=${{ matrix.python-version }} make test_with_version
Expand Down
36 changes: 23 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ export UID=$(shell id -u)

default: build start create_db populate test stop clean

# Build containers
.PHONY: build
build: ## Build containers
build:
docker-compose build

.PHONY: build_with_version
Expand All @@ -12,10 +13,11 @@ build_with_version: create_empty_secret

.PHONY: test_with_version
test_with_version: build_with_version assets
ENV=testing docker-compose run --rm web pytest --cov=OpenOversight --cov-report xml:OpenOversight/tests/coverage.xml --doctest-modules -n 4 --dist=loadfile -v OpenOversight/tests/
docker-compose run --rm web-test pytest --cov=OpenOversight --cov-report xml:OpenOversight/tests/coverage.xml --doctest-modules -n 4 --dist=loadfile -v OpenOversight/tests/

# Run containers
.PHONY: start
start: build ## Run containers
start: build
docker-compose up -d

.PHONY: create_db
Expand All @@ -35,8 +37,9 @@ assets:
.PHONY: dev
dev: create_empty_secret build start create_db populate

# Build and run containers
.PHONY: populate
populate: create_db ## Build and run containers
populate: create_db
@until docker-compose exec postgres psql -h localhost -U openoversight -c '\l' postgres &>/dev/null; do \
echo "Postgres is unavailable - sleeping..."; \
sleep 1; \
Expand All @@ -45,12 +48,13 @@ populate: create_db ## Build and run containers
## Populate database with test data
docker-compose run --rm web python ./test_data.py -p

# Run tests
.PHONY: test
test: start ## Run tests
test: start
if [ -z "$(name)" ]; then \
ENV=testing docker-compose run --rm web pytest --cov --doctest-modules -n auto --dist=loadfile -v OpenOversight/tests/; \
docker-compose run --rm web-test pytest --cov --doctest-modules -n auto --dist=loadfile -v OpenOversight/tests/; \
else \
ENV=testing docker-compose run --rm web pytest --cov --doctest-modules -v OpenOversight/tests/ -k $(name); \
docker-compose run --rm web-test pytest --cov --doctest-modules -v OpenOversight/tests/ -k $(name); \
fi

.PHONY: lint
Expand All @@ -61,24 +65,29 @@ lint:
clean_assets:
rm -rf ./OpenOversight/app/static/dist/

# Stop containers
.PHONY: stop
stop: ## Stop containers
stop:
docker-compose stop

# Remove containers
.PHONY: clean
clean: clean_assets stop ## Remove containers
clean: clean_assets stop
docker-compose rm -f

# Wipe database
.PHONY: clean_all
clean_all: clean stop ## Wipe database
clean_all: clean stop
docker-compose down -v

# Build project documentation in live reload for editing
.PHONY: docs
docs: ## Build project documentation in live reload for editing
docs:
make -C docs/ clean && sphinx-autobuild docs/ docs/_build/html

# Print this message and exit
.PHONY: help
help: ## Print this message and exit
help:
@printf "OpenOversight: Makefile for development, documentation and testing.\n"
@printf "Subcommands:\n\n"
@awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {printf "\033[36m%s\033[0m : %s\n", $$1, $$2}' $(MAKEFILE_LIST) \
Expand All @@ -89,8 +98,9 @@ help: ## Print this message and exit
attach:
docker-compose exec postgres psql -h localhost -U openoversight openoversight-dev

# This is needed to make sure docker doesn't create an empty directory, or delete that directory first
.PHONY: create_empty_secret
create_empty_secret: ## This is needed to make sure docker doesn't create an empty directory, or delete that directory first
create_empty_secret:
touch service_account_key.json || \
(echo "Need to delete that empty directory first"; \
sudo rm -d service_account_key.json/; \
Expand Down
23 changes: 18 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ services:
restart: always
build:
context: .
args:
- DOCKER_BUILD_ENV
- MAKE_PYTHON_VERSION
dockerfile: ./dockerfiles/web/Dockerfile
dockerfile: dockerfiles/web/Dockerfile-dev
environment:
APPROVE_REGISTRATIONS: "${APPROVE_REGISTRATIONS}"
AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
AWS_DEFAULT_REGION: "${AWS_DEFAULT_REGION}"
AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
ENV: "${ENV:-development}"
ENV: "development"
FLASK_APP: OpenOversight.app
FLASK_DEBUG: 1
OO_HELP_EMAIL: "[email protected]"
Expand All @@ -55,3 +52,19 @@ services:
- "3000"
ports:
- "3000:3000"

web-test:
restart: always
build:
context: .
args:
- MAKE_PYTHON_VERSION
dockerfile: dockerfiles/web/Dockerfile-test
environment:
ENV: "testing"
FLASK_APP: OpenOversight.app
OO_HELP_EMAIL: "[email protected]"
OO_SERVICE_EMAIL: "[email protected]"
TIMEZONE: "America/Chicago"
volumes:
- ./OpenOversight/:/usr/src/app/OpenOversight/:z
43 changes: 43 additions & 0 deletions dockerfiles/web/Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM python:3.11-bullseye

WORKDIR /usr/src/app

ENV CURL_FLAGS="--proto =https --tlsv1.2 -sSf -L --max-redirs 1 -O"

ENV DEBIAN-FRONTEND noninteractive
ENV DISPLAY=:1

# install apt dependencies
RUN apt-get update && apt-get install -y xvfb libpq-dev python3-dev && \
apt-get clean

# install node
ENV NODE_SETUP_SHA=5d07994f59e3edc2904c547e772b818d10abb066f6ff36ab3db5d686b0fe9a73
RUN curl ${CURL_FLAGS} \
https://raw.githubusercontent.com/nodesource/distributions/b8510857fb4ce4b023161be8490b00119884974c/deb/setup_12.x
RUN echo "${NODE_SETUP_SHA} setup_12.x" | sha256sum --check -
RUN bash setup_12.x
RUN apt-get install -y nodejs

# install yarn
RUN npm install -g yarn
RUN mkdir /var/www ./node_modules /.cache /.yarn /.mozilla
RUN touch /usr/src/app/yarn-error.log
COPY yarn.lock /usr/src/app/
RUN chmod -R 777 /usr/src/app/ /var/lib/xkb /.cache /.yarn /.mozilla


COPY requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r requirements.txt

COPY package.json /usr/src/app/
RUN yarn

COPY test_data.py /usr/src/app/

ENV SECRET_KEY 4Q6ZaQQdiqtmvZaxP1If
ENV SQLALCHEMY_DATABASE_URI postgresql://openoversight:terriblepassword@postgres/openoversight-dev

WORKDIR /usr/src/app/

CMD ["OpenOversight/scripts/entrypoint_dev.sh"]
10 changes: 1 addition & 9 deletions dockerfiles/web/Dockerfile → dockerfiles/web/Dockerfile-test
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ARG MAKE_PYTHON_VERSION
ARG DOCKER_BUILD_ENV
FROM python:${MAKE_PYTHON_VERSION:-3.11}-bullseye

WORKDIR /usr/src/app
Expand Down Expand Up @@ -40,21 +39,14 @@ RUN chmod -R 777 /usr/src/app/ /var/lib/xkb /.cache /.yarn /.mozilla


COPY requirements.txt dev-requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r requirements.txt

RUN test "${DOCKER_BUILD_ENV}" = production || pip3 install --no-cache-dir -r dev-requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt && pip3 install --no-cache-dir -r dev-requirements.txt

COPY package.json /usr/src/app/
RUN yarn

COPY test_data.py /usr/src/app/
COPY mypy.ini /usr/src/app/
EXPOSE 3000

ENV PATH="/usr/src/app/geckodriver:${PATH}"
ENV SECRET_KEY 4Q6ZaQQdiqtmvZaxP1If
ENV SQLALCHEMY_DATABASE_URI postgresql://openoversight:terriblepassword@postgres/openoversight-dev

WORKDIR /usr/src/app/

CMD ["OpenOversight/scripts/entrypoint_dev.sh"]

0 comments on commit 2d82e06

Please sign in to comment.