Commit f1d1c3b7 authored by Jorge Moratinos's avatar Jorge Moratinos
Browse files

Unit tests with docker image

parent 0774359c
Loading
Loading
Loading
Loading
Loading
+10 −0
Changes for Makefile: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,12 @@ fix_cmd := $(lint_cmd) --fix
# 'python -m pytest' runs from the repository root, so the tests use this code and not an installed opencapif_sdk.
PYTHON ?= $(if $(wildcard $(VENV_BIN)/python),$(VENV_BIN)/python,python3)
unit_tests_cmd := $(PYTHON) -m pytest test/unit
# Unit tests in a Docker container (test/Dockerfile), without a local Python environment.
# PYTHON_IMAGE selects the Python version (e.g. PYTHON_IMAGE=python:3.9-slim); UNIT_TESTS_IMAGE is the image built.
PYTHON_IMAGE ?= labs.etsi.org:5050/ocf/capif/python:3.13-slim
UNIT_TESTS_IMAGE ?= opencapif_sdk_unit_tests
unit_tests_docker_cmd := docker build -f test/Dockerfile --build-arg PYTHON_IMAGE=$(PYTHON_IMAGE) \
	-t $(UNIT_TESTS_IMAGE) . && docker run --rm $(UNIT_TESTS_IMAGE)


.PHONY: lint
@@ -20,3 +26,7 @@ fix:
.PHONY: unit_tests
unit_tests:
	$(unit_tests_cmd)

.PHONY: unit_tests_docker
unit_tests_docker:
	$(unit_tests_docker_cmd)
+9 −1
Changes for doc/sdk_developers.md: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -89,7 +89,15 @@ make unit_tests

It runs `python -m pytest test/unit` with the Python of the repository `venv` if it exists, or `python3` otherwise (choose another one with `make unit_tests PYTHON=/path/to/python`). Running it as `python -m pytest` from the repository root makes the tests use the code of the repository, and not an `opencapif_sdk` installed in your environment.

The unit tests also run in the staging CI pipeline (`staging_unit_tests` job), which installs `installation/requirements.txt` in a `venv` and runs `make unit_tests`.
To run them without a local Python environment, only with Docker:

```console
make unit_tests_docker
```

It builds a Docker image with the dependencies of `installation/requirements.txt`, the SDK code and the tests (`test/Dockerfile`), and runs the unit tests in a container. The image uses Python 3.13 by default; set `PYTHON_IMAGE` to use another version, e.g. `make unit_tests_docker PYTHON_IMAGE=python:3.9-slim` (with Python 3.12 or earlier, the tests that need TLS-PSK are skipped).

The unit tests also run in the staging CI pipeline (`staging_unit_tests` job) with `make unit_tests_docker`, so the runner only needs Docker.

The other tests of the `test` folder are integration tests that need a running OpenCAPIF.

test/Dockerfile

0 → 100644
+20 −0
Changes for test/Dockerfile: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Image to run the unit tests without a local Python environment: make unit_tests_docker
# The build context is the project root. Only the files listed in Dockerfile.dockerignore are sent.
ARG PYTHON_IMAGE=labs.etsi.org:5050/ocf/capif/python:3.13-slim
FROM ${PYTHON_IMAGE}

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_ROOT_USER_ACTION=ignore \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /sdk

# Dependencies first, so this layer is reused while requirements.txt does not change
COPY installation/requirements.txt installation/requirements.txt
RUN pip install --no-cache-dir -r installation/requirements.txt

COPY opencapif_sdk opencapif_sdk
COPY test test

# 'python -m pytest' from the project root, so the tests use this code (as make unit_tests)
ENTRYPOINT ["python", "-m", "pytest", "test/unit", "-p", "no:cacheprovider"]
+7 −0
Changes for test/Dockerfile.dockerignore: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Build context of test/Dockerfile: only the files needed by the unit tests
*
!installation/requirements.txt
!opencapif_sdk/
!test/
**/__pycache__
**/*.pyc