5.8 KiB
cic-internal-integration
Backend Requirements
Backend local development
- Start the stack with Docker Compose:
docker-compose up -d
- Now you can open your browser and interact with these URLs:
Frontend (CICADA), built with Docker, with routes handled based on the path: http://localhost
PGAdmin, PostgreSQL web administration: http://localhost:5050
Flower, administration of Celery tasks: http://localhost:5555
Traefik UI, to see how the routes are being handled by the proxy: http://localhost:8090
Note: The first time you start your stack, it might take a minute for it to be ready. While the backend waits for the database to be ready and configures everything. You can check the logs to monitor it.
To check the logs, run:
docker-compose logs
To check the logs of a specific service, add the name of the service, e.g.:
docker-compose logs backend
If your Docker is not running in localhost
(the URLs above wouldn't work) check the sections below on Development with Docker Toolbox and Development with a custom IP.
Backend local development, additional details
fill me in
Docker Compose Override
During development, you can change Docker Compose settings that will only affect the local development environment, in the file docker-compose.override.yml
.
The changes to that file only affect the local development environment, not the production environment. So, you can add "temporary" changes that help the development workflow.
For example, the directory with the backend code is mounted as a Docker "host volume", mapping the code you change live to the directory inside the container. That allows you to test your changes right away, without having to build the Docker image again. It should only be done during development, for production, you should build the Docker image with a recent version of the backend code. But during development, it allows you to iterate very fast.
There is also a command override that runs /start-reload.sh
(included in the base image) instead of the default /start.sh
(also included in the base image). It starts a single server process (instead of multiple, as would be for production) and reloads the process whenever the code changes. Have in mind that if you have a syntax error and save the Python file, it will break and exit, and the container will stop. After that, you can restart the container by fixing the error and running again:
$ docker-compose up -d
There is also a commented out command
override, you can uncomment it and comment the default one. It makes the backend container run a process that does "nothing", but keeps the container alive. That allows you to get inside your running container and execute commands inside, for example a Python interpreter to test installed dependencies, or start the development server that reloads when it detects changes, or start a Jupyter Notebook session.
To get inside the container with a bash
session you can start the stack with:
$ docker-compose up -d
and then exec
inside the running container:
$ docker-compose exec backend bash
You should see an output like:
root@7f2607af31c3:/app#
that means that you are in a bash
session inside your container, as a root
user, under the /app
directory.
There you can use the script /start-reload.sh
to run the debug live reloading server. You can run that script from inside the container with:
$ bash /start-reload.sh
...it will look like:
root@7f2607af31c3:/app# bash /start-reload.sh
and then hit enter. That runs the live reloading server that auto reloads when it detects code changes.
Nevertheless, if it doesn't detect a change but a syntax error, it will just stop with an error. But as the container is still alive and you are in a Bash session, you can quickly restart it after fixing the error, running the same command ("up arrow" and "Enter").
...this previous detail is what makes it useful to have the container alive doing nothing and then, in a Bash session, make it run the live reload server.
Backend tests
To test the backend run:
$ DOMAIN=backend sh ./scripts/test.sh
The file ./scripts/test.sh
has the commands to generate a testing docker-stack.yml
file, start the stack and test it.
The tests run with Pytest, modify and add tests to ./backend/app/app/tests/
.
If you use GitLab CI the tests will run automatically.
Local tests
Start the stack with this command:
DOMAIN=backend sh ./scripts/test-local.sh
The ./backend/app
directory is mounted as a "host volume" inside the docker container (set in the file docker-compose.dev.volumes.yml
).
You can rerun the test on live code:
docker-compose exec backend /app/tests-start.sh
Test running stack
If your stack is already up and you just want to run the tests, you can use:
docker-compose exec backend /app/tests-start.sh
That /app/tests-start.sh
script just calls pytest
after making sure that the rest of the stack is running. If you need to pass extra arguments to pytest
, you can pass them to that command and they will be forwarded.
For example, to stop on first error:
docker-compose exec backend bash /app/tests-start.sh -x
Test Coverage
Because the test scripts forward arguments to pytest
, you can enable test coverage HTML report generation by passing --cov-report=html
.
To run the local tests with coverage HTML reports:
DOMAIN=backend sh ./scripts/test-local.sh --cov-report=html
To run the tests in a running stack with coverage HTML reports:
docker-compose exec backend bash /app/tests-start.sh --cov-report=html