Skip to content

Commit

Permalink
Add code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rosswhitfield committed Jun 20, 2024
1 parent b92ed80 commit 885e6d6
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,29 @@ jobs:
cache-downloads-key: ${{ runner.os }}-downloads-${{ hashFiles('**/environment.yml') }}
- name: Start docker containers
run: |
cp ./config/docker-compose.envlocal.yml docker-compose.yml
docker compose up --build -d
cp ./config/docker-compose.coverage.yml docker-compose.yml
docker-compose up --build -d
- name: Sleep, wait for containers to start up
run: sleep 30
- name: Run unit tests
run: python -m pytest tests/
- name: Stop the coverage process
# Stopping the coverage process allows the code coverage to be written to disk
run: docker exec live_data_server_livedata_1 /bin/bash -c "pkill coverage"
- name: Copy code coverage out of docker container
run: docker cp live_data_server_livedata_1:/var/www/livedata/app /tmp/
- name: Combine and show code coverage
shell: bash -l {0}
run: |
cd /tmp/app
coverage combine
coverage xml
cp coverage.xml $OLDPWD
coverage report
- name: Bring down docker containers completely now
# This will completely remove the containers
run: docker-compose down
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
18 changes: 18 additions & 0 deletions Dockerfile.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM continuumio/miniconda3:23.10.0-1

COPY environment.yml .
RUN conda env create

WORKDIR /var/www/livedata

COPY docker-entrypoint-coverage.sh /usr/bin/docker-entrypoint.sh

COPY src/live_data_server app
RUN mkdir ./static

WORKDIR /var/www/livedata/app

RUN echo "[run]\nsource=live_data_server,plots\nparallel=True\nrelative_files=True" > /var/www/livedata/app/.coveragerc

RUN chmod +x /usr/bin/docker-entrypoint.sh
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "livedata", "/usr/bin/docker-entrypoint.sh"]
61 changes: 61 additions & 0 deletions config/docker-compose.coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
services:

nginx:
image: nginx:1.21.1
ports:
- "80:80"
- "443:443"
volumes:
- web-static:/var/www/livedata/static
- ./config/nginx/envlocal.conf:/etc/nginx/conf.d/nginx.conf
depends_on:
livedata:
condition: service_healthy

livedata:
build:
context: .
dockerfile: Dockerfile.coverage
network: host
environment:
APP_DEBUG: 1 # 0 for False, otherwise will evaluate to True
DJANGO_SUPERUSER_USERNAME: ${DATABASE_USER}
DJANGO_SUPERUSER_PASSWORD: ${DATABASE_PASS}
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USER: ${DATABASE_USER}
DATABASE_PASS: ${DATABASE_PASS}
DATABASE_HOST: ${DATABASE_HOST}
DATABASE_PORT: ${DATABASE_PORT}
LIVE_PLOT_SECRET_KEY: ${LIVE_PLOT_SECRET_KEY}
command: /usr/bin/docker-entrypoint.sh
volumes:
- web-static:/var/www/livedata/static
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:8000/admin || exit 1
interval: 60s
retries: 5
start_period: 20s
timeout: 10s
ports:
- "8000:8000"
depends_on:
db:
condition: service_healthy

db:
# do not upgrade to version > 9.6.23 unless you also upgrade livedata image
image: postgres:9.6.23
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASS}
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DATABASE_USER} -d ${DATABASE_NAME}"]
interval: 5s
timeout: 5s
retries: 5
ports:
- "${DATABASE_PORT}:${DATABASE_PORT}"

volumes:
web-static:
27 changes: 27 additions & 0 deletions docker-entrypoint-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh
set -e

# wait for database
until PGPASSWORD=${DATABASE_PASS} psql -h "${DATABASE_HOST}" -U "${DATABASE_USER}" -d "${DATABASE_NAME}" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done

cd /var/www/livedata/app

# collect static files
python manage.py collectstatic --noinput

# migrate django models
python manage.py makemigrations --noinput
python manage.py migrate --noinput

# create superuser
#echo "from django.contrib.auth.models import User; User.objects.create_superuser('${DJANGO_SUPERUSER_USERNAME}', '${DJANGO_SUPERUSER_USERNAME}@example.com', '${DJANGO_SUPERUSER_PASSWORD}')" | python manage.py shell

# Create the webcache
python manage.py createcachetable webcache
python manage.py ensure_adminuser --username=${DJANGO_SUPERUSER_USERNAME} --email='workflow@example.com' --password=${DJANGO_SUPERUSER_PASSWORD}

# run application with code coverage
coverage run -m gunicorn live_data_server.wsgi:application -b :8000
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ dependencies:
- toml
- requests<2.31 # can remove this condition once we allow newer versions of openssl
- pre-commit
- coverage

0 comments on commit 885e6d6

Please sign in to comment.