From 006d00062eed42b7cfad3d648c8c23cb3342cfba Mon Sep 17 00:00:00 2001 From: Sergio Date: Thu, 28 Oct 2021 11:53:19 +0200 Subject: [PATCH] Unit tests mlonitoring influxdb --- manifests/monitoring.yaml | 1 + src/monitoring/.gitlab-ci.yml | 15 ++++++++------- src/monitoring/Dockerfile | 2 ++ .../service/MonitoringServiceServicerImpl.py | 3 ++- src/monitoring/tests/Dockerfile | 2 ++ 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/manifests/monitoring.yaml b/manifests/monitoring.yaml index 26e594a36..fc53381e7 100644 --- a/manifests/monitoring.yaml +++ b/manifests/monitoring.yaml @@ -13,6 +13,7 @@ spec: app: monitoring spec: terminationGracePeriodSeconds: 5 + restartPolicy: Always # initContainers: # - name: influxdb # image: influxdb:1.8 diff --git a/src/monitoring/.gitlab-ci.yml b/src/monitoring/.gitlab-ci.yml index 6ca42207f..165d1d913 100644 --- a/src/monitoring/.gitlab-ci.yml +++ b/src/monitoring/.gitlab-ci.yml @@ -7,7 +7,7 @@ build monitoring: before_script: - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY script: - - docker build --build-arg user=$INFLUXDB_USER --build-arg pass=$INFLUXDB_PASSWORD --build-arg db=$INFLUXDB_DATABASE -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/ + - docker build --build-arg hostname=localhost --build-arg user=$INFLUXDB_USER --build-arg pass=$INFLUXDB_PASSWORD --build-arg db=$INFLUXDB_DATABASE -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/ - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG" - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG" after_script: @@ -85,14 +85,15 @@ test monitoring pytest: - build monitoring before_script: - if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi - - if docker container ls | grep influxdb-tests; then docker rm influxdb-tests; fi + - if docker container ls | grep influxdb; then docker rm influxdb; fi script: - - docker run -d -p 8086:8086 -e INFLUXDB_DB=$INFLUXDB_DATABASE -e INFLUXDB_ADMIN_USER=$INFLUXDB_USER -e INFLUXDB_ADMIN_PASSWORD=$INFLUXDB_PASSWORD -e INXLUXDB_HTTP_AUTH_ENABLED=True --network=teraflowbridge --name influxdb-tests influxdb:1.8 - - docker build -t "$IMAGE_NAME_TEST:$IMAGE_TAG" -f ./src/$IMAGE_NAME/tests/Dockerfile ./src/ - - docker run -v "$PWD/src/$IMAGE_NAME/tests:/opt/results" --build-arg user=$INFLUXDB_USER --build-arg pass=$INFLUXDB_PASSWORD --build-arg db=$INFLUXDB_DATABASE --network=teraflowbridge $IMAGE_NAME_TEST:$IMAGE_TAG + - docker run --name influxdb -d -p 8086:8086 -e INFLUXDB_DB=$INFLUXDB_DATABASE -e INFLUXDB_ADMIN_USER=$INFLUXDB_USER -e INFLUXDB_ADMIN_PASSWORD=$INFLUXDB_PASSWORD -e INXLUXDB_HTTP_AUTH_ENABLED=True --network=teraflowbridge influxdb:1.8 + - docker build --build-arg hostname=influxdb --build-arg user=$INFLUXDB_USER --build-arg pass=$INFLUXDB_PASSWORD --build-arg db=$INFLUXDB_DATABASE -t "$IMAGE_NAME_TEST:$IMAGE_TAG" -f ./src/$IMAGE_NAME/tests/Dockerfile ./src/ + - docker run --name $IMAGE_NAME_TEST -v "$PWD/src/$IMAGE_NAME/tests:/opt/results" --network=teraflowbridge $IMAGE_NAME_TEST:$IMAGE_TAG after_script: - - docker stop influxdb-tests - - docker rm influxdb-tests + - docker stop influxdb + - docker rm influxdb + - docker rm $IMAGE_NAME_TEST - docker network rm teraflowbridge rules: - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)' diff --git a/src/monitoring/Dockerfile b/src/monitoring/Dockerfile index 2db68030f..1a7a289eb 100644 --- a/src/monitoring/Dockerfile +++ b/src/monitoring/Dockerfile @@ -37,6 +37,8 @@ ENV PORT=7070 ARG user ARG pass ARG db +ARG hostname +ENV INFLUXDB_HOSTNAME=$hostname ENV INFLUXDB_USER=$user ENV INFLUXDB_PASSWORD=$pass ENV INFLUXDB_DATABASE=$db diff --git a/src/monitoring/service/MonitoringServiceServicerImpl.py b/src/monitoring/service/MonitoringServiceServicerImpl.py index 0e85d6bb4..6664440cf 100644 --- a/src/monitoring/service/MonitoringServiceServicerImpl.py +++ b/src/monitoring/service/MonitoringServiceServicerImpl.py @@ -19,6 +19,7 @@ MONITORING_INCLUDEKPI_COUNTER = Counter('monitoring_includekpi_counter', 'Monito INFLUXDB_USER = os.environ.get("INFLUXDB_USER") INFLUXDB_PASS = os.environ.get("INFLUXDB_PASSWORD") INFLUXDB_DB = os.environ.get("INFLUXDB_DATABASE") +INFLUXDB_HOST = os.environ.get("INFLUXDB_HOSTNAME") class MonitoringServiceServicerImpl(monitoring_pb2_grpc.MonitoringServiceServicer): def __init__(self): @@ -28,7 +29,7 @@ class MonitoringServiceServicerImpl(monitoring_pb2_grpc.MonitoringServiceService self.sql_db = sqlite_tools.SQLite('monitoring.db') # Create influx_db client - self.influx_db = influx_tools.Influx("influxdb-tests","8086",INFLUXDB_USER,INFLUXDB_PASS,INFLUXDB_DB) + self.influx_db = influx_tools.Influx(INFLUXDB_HOST,"8086",INFLUXDB_USER,INFLUXDB_PASS,INFLUXDB_DB) # CreateKpi (CreateKpiRequest) returns (KpiId) {} def CreateKpi(self, request : monitoring_pb2.CreateKpiRequest, context) -> monitoring_pb2.KpiId : diff --git a/src/monitoring/tests/Dockerfile b/src/monitoring/tests/Dockerfile index 13524052d..c14e75343 100644 --- a/src/monitoring/tests/Dockerfile +++ b/src/monitoring/tests/Dockerfile @@ -35,6 +35,8 @@ COPY common/logger.py common ARG user ARG pass ARG db +ARG hostname +ENV INFLUXDB_HOSTNAME=$hostname ENV INFLUXDB_USER=$user ENV INFLUXDB_PASSWORD=$pass ENV INFLUXDB_DATABASE=$db -- GitLab