From a9308c9aeaebbef03e21befb4407af09d48df97c Mon Sep 17 00:00:00 2001 From: alfredeen Date: Tue, 20 Aug 2024 16:05:00 +0200 Subject: [PATCH] Added a unit test for scenario of editing a release with valid image to invalid image back to valid image. --- serve_event_listener/status_data.py | 6 ++- serve_event_listener/status_queue.py | 8 +++- tests/test_status_data.py | 66 ++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/serve_event_listener/status_data.py b/serve_event_listener/status_data.py index 97de6a3..0d11bfb 100644 --- a/serve_event_listener/status_data.py +++ b/serve_event_listener/status_data.py @@ -182,9 +182,11 @@ def update_or_create_status( Returns: Dict: Updated status data. """ - logger.debug(f"Release {release}. Status data before update:{status_data}. \ + logger.debug( + f"Release {release}. Status data before update:{status_data}. \ release in status data? {release not in status_data}. \ - creation_timestamp={creation_timestamp}, deletion_timestamp={deletion_timestamp}") + creation_timestamp={creation_timestamp}, deletion_timestamp={deletion_timestamp}" + ) if ( release not in status_data or creation_timestamp >= status_data[release]["creation_timestamp"] diff --git a/serve_event_listener/status_queue.py b/serve_event_listener/status_queue.py index ff958d2..ee77a05 100644 --- a/serve_event_listener/status_queue.py +++ b/serve_event_listener/status_queue.py @@ -31,7 +31,9 @@ def process(self): release = status_data["release"] new_status = status_data["new-status"] if new_status == "Deleted": - logger.info(f"Processing release: {release}. New status is Deleted!") + logger.info( + f"Processing release: {release}. New status is Deleted!" + ) self.post_handler( data=status_data, @@ -40,7 +42,9 @@ def process(self): self.queue.task_done() - logger.debug(f"Processed queue successfully of release {release}, new status={new_status}") + logger.debug( + f"Processed queue successfully of release {release}, new status={new_status}" + ) except queue.Empty: pass # Continue looping if the queue is empty diff --git a/tests/test_status_data.py b/tests/test_status_data.py index 2a1a293..a2c9e08 100644 --- a/tests/test_status_data.py +++ b/tests/test_status_data.py @@ -87,6 +87,72 @@ def test_replica_scenario(self): self.assertEqual(self.status_data.status_data[release].get("status"), "Deleted") + 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. + After the third pod is created, the first two are deleted. + This occurs when a user chnages the image to an invalid image and then valid image. + """ + + release = "r-valid-invalid-images" + + # Pod: pod + self.pod.create(release) + self.status_data.update({"object": self.pod}) + + assert self.status_data.status_data[release].get("status") == "Created" + + self.pod.running() + self.status_data.update({"object": self.pod}) + assert self.status_data.status_data[release].get("status") == "Running" + + # Pod: invalid_pod + self.invalid_pod = Pod() + self.invalid_pod.create(release) + self.invalid_pod.error_image_pull() + self.status_data.update({"object": self.invalid_pod}) + assert self.status_data.status_data[release].get("status") == "Image Error" + + # Now there are two pods in the release, one older Running and one newer Image Error + + # Pod: valid_pod + self.valid_pod = Pod() + self.valid_pod.create(release) + self.valid_pod.running() + self.status_data.update({"object": self.valid_pod}) + assert self.status_data.status_data[release].get("status") == "Running" + + # The first two pods are deleted but the last pod should remain running + self.pod.delete() + self.invalid_pod.delete() + + self.status_data.update({"object": self.pod}) + + msg = f"Release created ts={self.status_data.status_data[release].get("creation_timestamp")}, \ + deleted ts={self.status_data.status_data[release].get("deletion_timestamp")}" + + print(msg) + + self.assertEqual( + self.status_data.status_data[release].get("status"), + "Running", + f"Release should be Running after delete of first pod, \ + ts pod deleted={self.pod.metadata.deletion_timestamp} vs \ + ts invalid_pod deleted={self.invalid_pod.metadata.deletion_timestamp} vs \ + ts valid_pod created={self.valid_pod.metadata.creation_timestamp}, {msg}", + ) + + self.status_data.update({"object": self.invalid_pod}) + self.assertEqual( + self.status_data.status_data[release].get("status"), + "Running", + f"Release should be Running after delete of 2nd invalid pod, \ + ts pod deleted={self.pod.metadata.deletion_timestamp} vs \ + ts invalid_pod deleted={self.invalid_pod.metadata.deletion_timestamp} vs \ + ts valid_pod created={self.valid_pod.metadata.creation_timestamp}, {msg}", + ) + if __name__ == "__main__": unittest.main()