From 4703407951fb82e41a09953de8e9004b7717ec5c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Agneray?= <francois.agneray@lam.fr>
Date: Thu, 29 Sep 2022 12:00:36 +0200
Subject: [PATCH] Add test + sonar for tasks subproject

---
 Makefile                             |  4 ++++
 services/.gitignore                  |  1 +
 tasks/.gitignore                     |  3 +++
 tasks/.gitlab-ci.yml                 | 28 ++++++++++++++++++++++++++++
 tasks/requirements.txt               | 11 +++++++++++
 tasks/tests/anis_tasks/test_utils.py | 20 ++++++++++++++++++++
 6 files changed, 67 insertions(+)
 create mode 100644 tasks/tests/anis_tasks/test_utils.py

diff --git a/Makefile b/Makefile
index 91a150aa..993b6e2b 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,7 @@ list:
 	@echo "  test_services    > Starts the services unit tests"
 	@echo "  install_tasks    > install tasks dependencies (virtualenv)"
 	@echo "  shell_tasks      > Shell into python tasks"
+	@echo "  test_tasks       > Starts the tasks unit tests"
 	@echo "  create-db        > Create a database for dev only (need token_enabled=0)"
 	@echo "  remove-pgdata    > Remove the anis-next database"
 	@echo "  remove-matodata  > Remove the anis-next matomo volumes"
@@ -105,6 +106,9 @@ install_tasks:
 shell_tasks:
 	@docker-compose exec tasks bash
 
+test_tasks:
+	@docker-compose exec tasks /bin/bash -c "source ./venv/bin/activate && pytest"
+
 create-db:
 	@docker-compose exec server sh /mnt/init-keycloak.sh
 	@docker-compose exec server sh /mnt/create-db.sh
diff --git a/services/.gitignore b/services/.gitignore
index 555ddcf4..bccda115 100644
--- a/services/.gitignore
+++ b/services/.gitignore
@@ -58,3 +58,4 @@ eggs/
 *.egg
 __pycache__/
 *.pyc
+.pytest_cache
diff --git a/tasks/.gitignore b/tasks/.gitignore
index 9bb092ad..bccda115 100644
--- a/tasks/.gitignore
+++ b/tasks/.gitignore
@@ -5,6 +5,8 @@ dist
 var
 coverage
 data
+report
+.coverage
 
 # Only exists if Bazel was run
 /bazel-out
@@ -56,3 +58,4 @@ eggs/
 *.egg
 __pycache__/
 *.pyc
+.pytest_cache
diff --git a/tasks/.gitlab-ci.yml b/tasks/.gitlab-ci.yml
index e157cec8..fcc985fe 100644
--- a/tasks/.gitlab-ci.yml
+++ b/tasks/.gitlab-ci.yml
@@ -1,4 +1,6 @@
 stages:
+    - test
+    - sonar
     - dockerize
     - deploy
 
@@ -17,6 +19,32 @@ dockerize:
         - docker build --cache-from $CI_REGISTRY/anis/anis-next/tasks:latest -t $CI_REGISTRY/anis/anis-next/tasks:latest .
         - docker push $CI_REGISTRY/anis/anis-next/tasks:latest
 
+test:
+    image: python:3.8
+    stage: test
+    script:
+        - cd tasks
+        - export PYTHONPATH=/builds/anis/anis-next/tasks/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:
+            - tasks/report
+
+sonar_scanner:
+    image: sonarsource/sonar-scanner-cli:latest
+    stage: sonar
+    script:
+        - cd tasks
+        - sonar-scanner 
+            -Dsonar.projectKey=anis-tasks
+            -Dsonar.sources=src
+            -Dsonar.projectVersion=$VERSION
+            -Dsonar.host.url=$SONARQUBE_URL
+            -Dsonar.login=$SONAR_TOKEN_TASKS
+            -Dsonar.python.coverage.reportPaths=report/coverage.xml
+            -Dsonar.python.xunit.reportPath=report/pytest.xml
+
 deploy:
     image: alpine
     stage: deploy
diff --git a/tasks/requirements.txt b/tasks/requirements.txt
index 1bb63938..4bb97e09 100644
--- a/tasks/requirements.txt
+++ b/tasks/requirements.txt
@@ -1,9 +1,20 @@
+attrs==22.1.0
 certifi==2021.10.8
 charset-normalizer==2.0.12
+coverage==6.4.4
 decorator==5.1.1
 idna==3.3
+iniconfig==1.1.1
+packaging==21.3
 pika==1.2.0
+pluggy==1.0.0
 py==1.11.0
+pyparsing==3.0.9
+pytest==7.1.3
+pytest-asyncio==0.19.0
+pytest-cov==4.0.0
+pytest-mock==3.9.0
 requests==2.27.1
 retry==0.9.2
+tomli==2.0.1
 urllib3==1.26.8
diff --git a/tasks/tests/anis_tasks/test_utils.py b/tasks/tests/anis_tasks/test_utils.py
new file mode 100644
index 00000000..f5e50c55
--- /dev/null
+++ b/tasks/tests/anis_tasks/test_utils.py
@@ -0,0 +1,20 @@
+import os
+from unittest import mock
+from unittest.mock import call
+import pytest
+
+from anis_tasks.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