Skip to content

Commit

Permalink
SS-907 Fixed update of long app status texts (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredeen authored Feb 6, 2024
1 parent 5b21a95 commit 913c6f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,13 @@ def update_app_status(request):

# Required input
release = request.data["release"]

new_status = request.data["new-status"]

if len(new_status) > 15:
print(f"DEBUG: Status code is longer than 15 chars so shortening: {new_status}")
new_status = new_status[:15]

event_ts = datetime.strptime(request.data["event-ts"], "%Y-%m-%dT%H:%M:%S.%fZ")
event_ts = utc.localize(event_ts)

Expand Down
6 changes: 5 additions & 1 deletion apps/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,21 @@ def handle_update_status_request(
request should be performed or ignored.
:param release str: The release id of the app instance, stored in the AppInstance.parameters dict.
:param new_status str: The new status code.
:param new_status str: The new status code. Trimmed to max 15 chars if needed.
:param event_ts timestamp: A JSON-formatted timestamp in UTC, e.g. 2024-01-25T16:02:50.00Z.
:param event_msg json dict: An optional json dict containing pod-msg and/or container-msg.
:returns: A value from the HandleUpdateStatusResponseCode enum.
Raises an ObjectDoesNotExist exception if the app instance does not exist.
"""

if len(new_status) > 15:
new_status = new_status[:15]

try:
# Begin by verifying that the requested app instance exists
# We wrap the select and update tasks in a select_for_update lock
# to avoid race conditions.

with transaction.atomic():
app_instance = (
AppInstance.objects.select_for_update().filter(parameters__contains={"release": release}).last()
Expand Down
19 changes: 19 additions & 0 deletions apps/tests/test_update_status_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ def test_handle_missing_app_status_should_create_and_update_status(self):
assert actual_appstatus.status_type == new_status
assert actual_appstatus.time == newer_ts

def test_handle_long_status_text_should_trim_status(self):
newer_ts = self.INITIAL_EVENT_TS + timedelta(seconds=1)
new_status = "LongStatusText-ThisPartLongerThan15Chars"
expected_status_text = new_status[:15]
assert len(expected_status_text) == 15
actual = handle_update_status_request(self.ACTUAL_RELEASE_NAME, new_status, newer_ts)

assert actual == HandleUpdateStatusResponseCode.CREATED_FIRST_STATUS

# Fetch the app instance and status objects and verify values
actual_app_instance = AppInstance.objects.filter(
parameters__contains={"release": self.ACTUAL_RELEASE_NAME}
).last()

assert actual_app_instance.state == expected_status_text
actual_appstatus = actual_app_instance.status.latest()
assert actual_appstatus.status_type == expected_status_text
assert actual_appstatus.time == newer_ts


@pytest.mark.skip(
reason="This test requires a modification to the handle_update_status_request function to add a delay parameter."
Expand Down

0 comments on commit 913c6f7

Please sign in to comment.