Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions linode_api4/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@
from linode_api4.objects import Event


class EventError(Exception):
"""
Represents a failed Linode event.
"""

def __init__(self, event_id: int, message: Optional[str]):
# Edge case, sometimes the message is populated with an empty string
if len(message) < 1:
message = None

self.event_id = event_id
self.message = message

error_fmt = f"Event {event_id} failed"
if message is not None:
error_fmt += f": {message}"

super().__init__(error_fmt)


class TimeoutContext:
"""
TimeoutContext should be used by polling resources to track their provisioning time.
Expand Down Expand Up @@ -212,6 +232,10 @@ def wait_for_next_event_finished(

def poll_func():
event._api_get()

if event.status == "failed":
raise EventError(event.id, event.message)

return event.status in ["finished", "notification"]

if poll_func():
Expand Down
58 changes: 57 additions & 1 deletion test/unit/objects/polling_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
from typing import Optional

import httpretty
import pytest

from linode_api4 import LinodeClient
from linode_api4.polling import EventError


class TestPolling:
Expand All @@ -12,7 +14,11 @@ def client(self):
return LinodeClient("testing", base_url="https://localhost")

@staticmethod
def body_event_status(status: str, action: str = "linode_shutdown"):
def body_event_status(
status: str,
action: str = "linode_shutdown",
message: Optional[str] = None,
):
return {
"action": action,
"entity": {
Expand All @@ -21,6 +27,7 @@ def body_event_status(status: str, action: str = "linode_shutdown"):
},
"id": 123,
"status": status,
"message": message,
}

@staticmethod
Expand Down Expand Up @@ -272,3 +279,52 @@ def test_wait_for_event_finished_creation(
assert len(get_requests) == 3
assert result.entity.id == 11111
assert result.status == "finished"

@httpretty.activate
def test_wait_for_event_finished_failed(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted not to implement an integration test for this change because we generally shouldn't rely on runtime errors.

self,
client,
):
"""
Tests that the EventPoller.wait_for_event_finished method raises errors for failed events.
"""

httpretty.register_uri(
httpretty.GET,
"https://localhost/account/events/123",
responses=[
httpretty.Response(
body=json.dumps(self.body_event_status("started")),
),
httpretty.Response(
body=json.dumps(
self.body_event_status("failed", message="oh no!")
),
),
],
)

httpretty.register_uri(
httpretty.GET,
"https://localhost/account/events",
responses=[
httpretty.Response(
body=json.dumps(self.body_event_list_empty()),
status=200,
),
httpretty.Response(
body=json.dumps(self.body_event_list_status("started")),
status=200,
),
],
)

try:
client.polling.event_poller_create(
"linode", "linode_shutdown", entity_id=11111
).wait_for_next_event_finished(interval=0.1)
except EventError as err:
assert err.event_id == 123
assert err.message == "oh no!"
else:
raise Exception("Expected event error, got none")