From 29f9f3f13f466dedc25ddc05012109054b6cc965 Mon Sep 17 00:00:00 2001
From: Javi Moreno <francisco.moreno.external@atos.net>
Date: Mon, 19 Jul 2021 08:05:51 -0400
Subject: [PATCH] Initial commit of the branch aimed at adding new tests to
 monitoring (WIP)

---
 src/monitoring/tests/Dockerfile         | 28 -------------
 src/monitoring/tests/test_monitoring.py | 53 +++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 28 deletions(-)
 delete mode 100644 src/monitoring/tests/Dockerfile
 create mode 100644 src/monitoring/tests/test_monitoring.py

diff --git a/src/monitoring/tests/Dockerfile b/src/monitoring/tests/Dockerfile
deleted file mode 100644
index 32a8af2f7..000000000
--- a/src/monitoring/tests/Dockerfile
+++ /dev/null
@@ -1,28 +0,0 @@
-FROM python:3-slim
-RUN apt-get update -qqy && \
-	apt-get -qqy install wget g++ && \
-	rm -rf /var/lib/apt/lists/*
-# show python logs as they occur
-ENV PYTHONUNBUFFERED=0
-
-# download the grpc health probe
-RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \
-    wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
-    chmod +x /bin/grpc_health_probe
-
-# get packages
-WORKDIR /monitoring
-COPY monitoring/requirements.in requirements.in
-RUN python3 -m pip install pip-tools
-RUN pip-compile --output-file=requirements.txt requirements.in
-RUN python3 -m pip install -r requirements.txt
-
-# add files into working directory
-COPY /monitoring/. .
-COPY /common/logger.py .
-
-# set listen port
-
-ENTRYPOINT ["python", "/monitoring/monitoring_client.py"]
-
-
diff --git a/src/monitoring/tests/test_monitoring.py b/src/monitoring/tests/test_monitoring.py
new file mode 100644
index 000000000..905b0de67
--- /dev/null
+++ b/src/monitoring/tests/test_monitoring.py
@@ -0,0 +1,53 @@
+import logging, pytest, time
+from . import context_pb2
+from . import monitoring_pb2
+from .monitoring_server import start_server, stop_server
+from .monitoring_client import MonitoringClient
+
+LOGGER = logging.getLogger(__name__)
+LOGGER.setLevel(logging.DEBUG)
+
+SERVER_ADDRESS = '127.0.0.1'
+LISTEN_ADDRESS = '[::]'
+PORT = 7070
+
+# This fixture will be requested by test cases and last during testing session
+@pytest.fixture(scope='session')
+def monitoring_service():
+    LOGGER.warning('monitoring_service begin')
+
+    LOGGER.info('Initializing MonitoringService...')
+    server = start_server(address=LISTEN_ADDRESS, port=PORT)
+
+    # yield the server, when test finishes, execution will resume to stop it
+    LOGGER.warning('monitoring_service yielding')
+    yield server
+
+    LOGGER.info('Terminating MonitoringService...')
+    stop_server(server)
+
+# This fixture will be requested by test cases and last during testing session.
+# The client requires the server, so client fixture has the server as dependency.
+@pytest.fixture(scope='session')
+def monitoring_client(monitoring_service):
+    LOGGER.warning('monitoring_client begin')
+    client = MonitoringClient(server=SERVER_ADDRESS, port=PORT)    # instantiate the client
+    LOGGER.warning('monitoring_client returning')
+    return client
+
+# Test case that makes use of client fixture to test server's IncludeKpi method
+def test_include_kpi(monitoring_client):
+    LOGGER.warning('test_include_kpi begin')
+    # form request
+    kpi = monitoring_pb2.Kpi()
+    kpi.kpi_id.kpi_id.uuid = 'KPIID0000'    # pylint: disable=maybe-no-member
+    kpi.kpiDescription = 'KPI Desc'
+
+    # make call to server
+    LOGGER.warning('test_include_kpi requesting')
+    response = monitoring_client.IncludeKpi(kpi)
+    LOGGER.debug(str(response))
+    assert isinstance(response, context_pb2.Empty)
+
+# You can add as many tests as you want. Just copy the "def test_include_kpi(monitoring_client):" and implement
+# appropriate tests. monitoring_client and monitoring_service fixtures and their connection are reused along tests.
-- 
GitLab