Skip to content

Commit

Permalink
Added a unit test for scenario of editing a release with valid image …
Browse files Browse the repository at this point in the history
…to invalid image back to valid image.
  • Loading branch information
alfredeen committed Aug 20, 2024
1 parent bedddba commit a9308c9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
6 changes: 4 additions & 2 deletions serve_event_listener/status_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
8 changes: 6 additions & 2 deletions serve_event_listener/status_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
66 changes: 66 additions & 0 deletions tests/test_status_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit a9308c9

Please sign in to comment.