From cd7160a2d7034509a89b1cc85ca572ddd6349011 Mon Sep 17 00:00:00 2001 From: alfredeen Date: Thu, 5 Sep 2024 10:39:10 +0200 Subject: [PATCH] Extended a unit test. Added env var TEST_LOG_STREAM to enable log output to standard out during unit testing. --- README.md | 7 +++++++ serve_event_listener/status_data.py | 5 +++++ tests/test_status_data.py | 32 ++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f69fa7..d6a0229 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,13 @@ export TOKEN_API_ENDPOINT= (optional, is set to BA export APP_STATUS_API_ENDPOINT= (optional, is set to BASE_URL + "/api/v1/app-status/" if not defined) ``` +To retrieve additional log messages, set: + +```bash +export DEBUG=True +export TEST_LOG_STREAM=sys.stdout +``` + ### Running the Service Navigate to the project directory and execute the following command to run the service: diff --git a/serve_event_listener/status_data.py b/serve_event_listener/status_data.py index 436543c..d841b5b 100644 --- a/serve_event_listener/status_data.py +++ b/serve_event_listener/status_data.py @@ -283,14 +283,19 @@ def update_or_create_status( if status == "Deleted": # Status Deleted is a destructive action # Therefore we double-check the k8s status directly upon detecting this + if self.k8s_api_client is None: + logger.warning("No k8s API client: k8s_api_client is None") + if self.k8s_api_client: # Only use if the k8s client api has been set # Unit tests for example do not currently set a k8s api status, *_ = self.fetch_status_from_k8s_api(release) + logger.debug(f"Fetched release status from k8s: {status}") if status is None: # No pod with this release found. Set status to Deleted status = "Deleted" + logger.info("k8s returned status None. Setting to Deleted") if status != "Deleted": deletion_timestamp = None diff --git a/tests/test_status_data.py b/tests/test_status_data.py index e9acb21..eb532af 100644 --- a/tests/test_status_data.py +++ b/tests/test_status_data.py @@ -1,9 +1,26 @@ +import logging +import os +import sys import time import unittest from serve_event_listener.status_data import StatusData from tests.create_pods import Pod, PodStatus +# Setup logging output for unit test execution +DEBUG = os.getenv("DEBUG", default="False").lower() in ("true", "1", "t") +# Set up logging configuration with the ColoredFormatter +if DEBUG: + level = logging.DEBUG +else: + level = logging.INFO + +TEST_LOG_STREAM = os.environ.get("TEST_LOG_STREAM", None) +if TEST_LOG_STREAM and eval(TEST_LOG_STREAM) is not None: + logging.basicConfig(stream=eval(TEST_LOG_STREAM), level=level) +else: + logging.basicConfig(handlers=[logging.NullHandler()], level=level) + class TestPodProcessing(unittest.TestCase): release = "some_release" @@ -97,8 +114,9 @@ def test_replica_scenario(self): def test_valid_and_invalid_image_edits(self): """ This scenario creates a pod, then creates a pod with an invalid image, and finally - it created a pod with a valid image. + it creates a pod with a valid image. After the third pod is created, the first two are deleted. + Finally the valid pod is also deleted. This occurs when a user chnages the image to an invalid image and then valid image. """ @@ -168,6 +186,18 @@ def test_valid_and_invalid_image_edits(self): ts valid_pod created={self.valid_pod.metadata.creation_timestamp}, {msg}", ) + # Finally also delete the valid pod + self.valid_pod.delete() + self.status_data.update({"object": self.valid_pod}) + + time.sleep(0.01) + + self.assertEqual( + self.status_data.status_data[release].get("status"), + "Deleted", + "Release should be Deleted after delete of the last, valid pod.", + ) + class TestStatusConverter(unittest.TestCase): """Verifies the translation logic of k8s status objects to app status codes.