Skip to content

Commit

Permalink
Add docker-compose tests
Browse files Browse the repository at this point in the history
This repository includes a Docker Compose configuration to simplify running tests. The CKAN image is built using the Dockerfile located in the `docker/` directory.
  • Loading branch information
mjanez committed Aug 27, 2024
1 parent 5c183ac commit 9b16a8f
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
omit =
*/site-packages/*
*/python?.?/*
ckan/*
ckan/*
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
build
dist
*.egg-info/
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
.ropeproject
node_modules
**/node_modules/
bower_components
.vscode
.idea
*.log

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*.pyc
.pytest_cache

# C extensions
*.so
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,21 @@ To run the tests:
pytest --ckan-ini=test.ini ckanext/schemingdcat/tests
### Run tests quickly with Docker Compose
This repository includes a Docker Compose configuration to simplify running tests. The CKAN image is built using the Dockerfile located in the `docker/` directory.
To test against the CKAN version you want to use, proceed as follows
1. Build the necessary Docker images. This step ensures that all dependencies and configurations are correctly set up.
```shell
docker compose build
```
3. After building the images, you can run the tests. The Docker Compose configuration mounts the root of the repository into the CKAN container as a volume. This means that any changes you make to the code will be reflected inside the container without needing to rebuild the image, unless you modify the extension's dependencies.
```shell
docker compose up
```


[^1]: An improvement to [`ckanext-fluent`] (https://github.com/ckan/ckanext-fluent) to allow more versatility in multilingual schema creation and metadata validation.
45 changes: 45 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
services:
ckan:
build:
context: .
dockerfile: docker/Dockerfile
environment:
TZ: UTC
CKAN_SQLALCHEMY_URL: postgresql://ckan_default:pass@postgres/ckan_test
CKAN_DATASTORE_WRITE_URL: postgresql://datastore_write:pass@postgres/datastore_test
CKAN_DATASTORE_READ_URL: postgresql://datastore_read:pass@postgres/datastore_test
CKAN_SOLR_URL: http://solr:8983/solr/ckan
CKAN_REDIS_URL: redis://redis:6379/1
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
depends_on:
- postgres
- solr
- redis
volumes:
- ./ckanext:/srv/app/src/ckanext-schemingdcat/ckanext

solr:
image: ckan/ckan-solr:2.9-solr9-spatial
logging:
driver: none
healthcheck:
test: ["CMD", "wget", "-qO", "/dev/null", "http://localhost:8983/solr/"]

postgres:
image: ckan/ckan-postgres-dev:2.9
logging:
driver: none
healthcheck:
test: ["CMD", "pg_isready", "-U", "ckan_default", "-d", "ckan_test"]
interval: 10s
timeout: 5s
retries: 5

redis:
image: redis:latest
logging:
driver: none
healthcheck:
test: ["CMD", "redis-cli", "-e", "QUIT"]
48 changes: 48 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FROM ghcr.io/mjanez/ckan-spatial-test:2.10.5

ENV CKAN_VERSION_MAYOR=ckan-2.9
ENV APP_DIR=/srv/app
ENV CKAN_DIR=${APP_DIR}/src/ckan
ENV TZ=UTC

WORKDIR ${APP_DIR}/src/ckanext-schemingdcat

# Conditionally install pytest-rerunfailures if CKAN version is 2.9
RUN if [ "$CKAN_VERSION_MAYOR" = "2.9" ]; then \
pip3 install -U pytest-rerunfailures; \
fi

# Copy extension files to the container
COPY . .

# ## Override test_ckan.sh
# - Make sure to put the docker/setup/test_ckan.sh.override path because of the COPY context
# COPY docker/setup/test_ckan.sh.override ${APP_DIR}/test_ckan.sh
# RUN chmod +x ${APP_DIR}/test_ckan.sh

# Install the base + test dependencies
RUN pip3 install --no-cache-dir -r ${APP_DIR}/src/ckanext-schemingdcat/requirements.txt && \
# ignore installed packaging required version (fixed pyshacl issues)
pip3 install --no-cache-dir -r ${APP_DIR}/src/ckanext-schemingdcat/dev-requirements.txt && \
pip3 install -e ${APP_DIR}/src/ckanext-schemingdcat && \
# Replace default path to CKAN core config file with the one on the container
sed -i -e 's/use = config:.*/use = config:\/srv\/app\/src\/ckan\/test-core.ini/' test.ini

WORKDIR ${APP_DIR}

# Setup other extensions
RUN echo "mjanez/ckanext-dcat" && \
pip3 install --no-cache-dir -e git+https://github.com/mjanez/ckanext-dcat.git#egg=ckanext-dcat && \
pip3 install --no-cache-dir -r ${APP_DIR}/src/ckanext-dcat/requirements.txt && \
echo "ckan/ckanext-harvest" && \
pip3 install --no-cache-dir -e git+https://github.com/ckan/ckanext-harvest.git#egg=ckanext-harvest && \
pip3 install --no-cache-dir -r ${APP_DIR}/src/ckanext-harvest/requirements.txt && \
echo "ckan/ckanext-scheming" && \
pip3 install --no-cache-dir -e git+https://github.com/ckan/ckanext-scheming.git#egg=ckanext-scheming && \
echo "mjanez/ckanext-fluent" && \
pip3 install --no-cache-dir -e git+https://github.com/mjanez/ckanext-fluent.git#egg=ckanext-fluent

WORKDIR ${APP_DIR}/src/ckanext-schemingdcat

# Running the tests with coverage output
CMD ["/bin/sh", "-c", "$APP_DIR/test_ckan.sh -d ckanext/schemingdcat/tests ckanext.schemingdcat"]
41 changes: 41 additions & 0 deletions docker/setup/test_ckan.sh.override
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Setup extension
echo "[ckan-test.run-tests] Database init"
ckan -c test.ini db init

echo "[ckan-test.run-tests] Database pending migrations"
ckan -c test.ini db pending-migrations --apply

# Default test directory and output file
TEST_DIR="tests"

# Parse options
while getopts 'cd:o:' OPTION; do
case "$OPTION" in
c)
RUN_COVERALLS=1;;
d)
TEST_DIR="$OPTARG";;
?)
RUN_COVERALLS=0;
exit 1;;
esac
done
shift "$(($OPTIND -1))"

if [ -n "$1" ]; then
echo "[ckan-test.run-tests] pytest --ckan-ini=test.ini --cov=\"$1\" --cov-report=term-missing --cov-append --disable-warnings $TEST_DIR"
pytest --ckan-ini=test.ini --cov="$1" --cov-report=term-missing --cov-append --disable-warnings "$TEST_DIR"
test_exit_code=$?
else
echo "[ckan-test.run-tests] pytest --ckan-ini=test.ini --cov-report=term-missing --cov-append --disable-warnings $TEST_DIR"
pytest --ckan-ini=test.ini --cov-report=term-missing --cov-append --disable-warnings "$TEST_DIR"
test_exit_code=$?
fi

if [ "$RUN_COVERALLS" = 1 ]; then
coveralls;
fi

exit $test_exit_code

0 comments on commit 9b16a8f

Please sign in to comment.