From 52edeb7cc53c1da7d7deaa09a6312d2e974ba24d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Agneray?= <francois.agneray@lam.fr> Date: Thu, 29 Sep 2022 11:13:07 +0200 Subject: [PATCH] Add test + sonar for services subproject --- Makefile | 4 ++++ services/.gitlab-ci.yml | 28 ++++++++++++++++++++++ services/tests/anis_services/test_utils.py | 20 ++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 services/tests/anis_services/test_utils.py diff --git a/Makefile b/Makefile index b770e718..91a150aa 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ list: @echo " phpcs > Run php code sniffer test suite" @echo " install_services > install services dependencies (virtualenv)" @echo " shell_services > Shell into python services" + @echo " test_services > Starts the services unit tests" @echo " install_tasks > install tasks dependencies (virtualenv)" @echo " shell_tasks > Shell into python tasks" @echo " create-db > Create a database for dev only (need token_enabled=0)" @@ -95,6 +96,9 @@ install_services: shell_services: @docker-compose exec services bash +test_services: + @docker-compose exec services /bin/bash -c "source ./venv/bin/activate && pytest" + install_tasks: @docker run --init -it --rm --user $(UID):$(GID) -v $(CURDIR)/tasks:/project -w /project python:3.8 /bin/bash -c "python3 -m venv venv && source /project/venv/bin/activate && pip install -r requirements.txt" diff --git a/services/.gitlab-ci.yml b/services/.gitlab-ci.yml index 4bfe6473..db67bb77 100644 --- a/services/.gitlab-ci.yml +++ b/services/.gitlab-ci.yml @@ -1,4 +1,6 @@ stages: + - test + - sonar - dockerize - deploy @@ -6,6 +8,32 @@ variables: VERSION: "3.7" SONARQUBE_URL: https://sonarqube.lam.fr +test: + image: python:3.8 + stage: test + script: + - cd services + - export PYTHONPATH=/builds/anis/anis-next/services/src + - /bin/bash -c "python3 -m venv venv && source ./venv/bin/activate && pip install -r requirements.txt" + - /bin/bash -c "source ./venv/bin/activate && pytest --cov=src --cov-report xml:report/coverage.xml --junitxml=report/pytest.xml" + artifacts: + paths: + - services/report + +sonar_scanner: + image: sonarsource/sonar-scanner-cli:latest + stage: sonar + script: + - cd services + - sonar-scanner + -Dsonar.projectKey=anis-services + -Dsonar.sources=src + -Dsonar.projectVersion=$VERSION + -Dsonar.host.url=$SONARQUBE_URL + -Dsonar.login=$SONAR_TOKEN_SERVICES + -Dsonar.python.coverage.reportPaths=report/coverage.xml + -Dsonar.python.xunit.reportPath=report/pytest.xml + dockerize: image: docker:stable stage: dockerize diff --git a/services/tests/anis_services/test_utils.py b/services/tests/anis_services/test_utils.py new file mode 100644 index 00000000..22ad5d31 --- /dev/null +++ b/services/tests/anis_services/test_utils.py @@ -0,0 +1,20 @@ +import os +from unittest import mock +from unittest.mock import call +import pytest + +from anis_services.utils import * + +class Object(object): + pass + +class MockJWT: + def decode(self, *args): + pass + +class TestIfsctoolsUtils: + + @mock.patch.dict(os.environ, {}, clear=True) + def test_check_config(self): + with pytest.raises(ConfigKeyNotFound): + check_config() -- GitLab