11import json
2+ from typing import Optional
23
34import httpretty
45import pytest
56
67from linode_api4 import LinodeClient
8+ from linode_api4 .polling import EventError
79
810
911class TestPolling :
@@ -12,7 +14,11 @@ def client(self):
1214 return LinodeClient ("testing" , base_url = "https://localhost" )
1315
1416 @staticmethod
15- def body_event_status (status : str , action : str = "linode_shutdown" ):
17+ def body_event_status (
18+ status : str ,
19+ action : str = "linode_shutdown" ,
20+ message : Optional [str ] = None ,
21+ ):
1622 return {
1723 "action" : action ,
1824 "entity" : {
@@ -21,6 +27,7 @@ def body_event_status(status: str, action: str = "linode_shutdown"):
2127 },
2228 "id" : 123 ,
2329 "status" : status ,
30+ "message" : message ,
2431 }
2532
2633 @staticmethod
@@ -272,3 +279,52 @@ def test_wait_for_event_finished_creation(
272279 assert len (get_requests ) == 3
273280 assert result .entity .id == 11111
274281 assert result .status == "finished"
282+
283+ @httpretty .activate
284+ def test_wait_for_event_finished_failed (
285+ self ,
286+ client ,
287+ ):
288+ """
289+ Tests that the EventPoller.wait_for_event_finished method raises errors for failed events.
290+ """
291+
292+ httpretty .register_uri (
293+ httpretty .GET ,
294+ "https://localhost/account/events/123" ,
295+ responses = [
296+ httpretty .Response (
297+ body = json .dumps (self .body_event_status ("started" )),
298+ ),
299+ httpretty .Response (
300+ body = json .dumps (
301+ self .body_event_status ("failed" , message = "oh no!" )
302+ ),
303+ ),
304+ ],
305+ )
306+
307+ httpretty .register_uri (
308+ httpretty .GET ,
309+ "https://localhost/account/events" ,
310+ responses = [
311+ httpretty .Response (
312+ body = json .dumps (self .body_event_list_empty ()),
313+ status = 200 ,
314+ ),
315+ httpretty .Response (
316+ body = json .dumps (self .body_event_list_status ("started" )),
317+ status = 200 ,
318+ ),
319+ ],
320+ )
321+
322+ try :
323+ client .polling .event_poller_create (
324+ "linode" , "linode_shutdown" , entity_id = 11111
325+ ).wait_for_next_event_finished (interval = 0.1 )
326+ except EventError as err :
327+ assert err .event_id == 123
328+ assert err .message == "oh no!"
329+ else :
330+ raise Exception ("Expected event error, got none" )
0 commit comments