Skip to content

Commit

Permalink
Extended a unit test. Added env var TEST_LOG_STREAM to enable log out…
Browse files Browse the repository at this point in the history
…put to standard out during unit testing.
  • Loading branch information
alfredeen committed Sep 5, 2024
1 parent 2fe018a commit cd7160a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export TOKEN_API_ENDPOINT=<end point for fetching token> (optional, is set to BA
export APP_STATUS_API_ENDPOINT=<end point for status updates> (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:
Expand Down
5 changes: 5 additions & 0 deletions serve_event_listener/status_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion tests/test_status_data.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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.
"""

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cd7160a

Please sign in to comment.