Skip to content

Commit 8674184

Browse files
committed
Fix webhook mock expectations for retry in tests
1 parent ba0749d commit 8674184

File tree

9 files changed

+49
-20
lines changed

9 files changed

+49
-20
lines changed

crate/operator/webhooks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@
3030
from aiohttp import ClientError, ClientResponseError, TCPConnector
3131

3232
from crate.operator import __version__
33+
from crate.operator.config import config
3334
from crate.operator.utils.crd import has_compute_changed
3435

3536
RETRYABLE_STATUS_CODES = {502, 503, 504}
3637
RETRY_MAX_ATTEMPTS = 5
3738
RETRY_BASE_DELAY = 0.5
3839

3940

41+
def should_retry(retry: bool) -> bool:
42+
return retry and not config.TESTING
43+
44+
4045
class WebhookEvent(str, enum.Enum):
4146
SCALE = "scale"
4247
UPGRADE = "upgrade"
@@ -306,6 +311,8 @@ async def _send(
306311
)
307312
return None
308313

314+
retry = should_retry(retry)
315+
309316
payload = WebhookPayload(
310317
event=event,
311318
status=status,

tests/test_change_compute.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ async def test_change_compute_from_request_to_limit(
274274
name,
275275
compute_changed_data=mock.ANY,
276276
unsafe=mock.ANY,
277+
retry=mock.ANY,
277278
logger=mock.ANY,
278279
)
279280
assert await was_notification_sent(

tests/test_cratedb.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ async def test_cratedb_health_ping(
232232
WebhookClusterHealthPayload(status="GREEN"),
233233
WebhookStatus.SUCCESS,
234234
mock.ANY,
235+
retry=False,
235236
),
236237
err_msg="Failed to ping cluster.",
237238
timeout=DEFAULT_TIMEOUT * 5,

tests/test_expand_volume.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ async def test_expand_cluster_storage(
8787
name,
8888
feedback_data=mock.ANY,
8989
unsafe=mock.ANY,
90+
retry=mock.ANY,
9091
logger=mock.ANY,
9192
)
9293

tests/test_restore_backup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ async def test_restore_backup_aws(
221221
"action": WebhookAction.RESTORE_SNAPSHOT.value,
222222
},
223223
unsafe=mock.ANY,
224+
retry=mock.ANY,
224225
logger=mock.ANY,
225226
),
226227
err_msg="In progress notification has not been sent.",
@@ -369,6 +370,7 @@ async def test_restore_backup_aws(
369370
"action": WebhookAction.RESTORE_SNAPSHOT.value,
370371
},
371372
unsafe=mock.ANY,
373+
retry=mock.ANY,
372374
logger=mock.ANY,
373375
),
374376
err_msg="Success notification has not been sent.",
@@ -510,6 +512,7 @@ async def test_restore_backup_azure_blob(
510512
"action": WebhookAction.RESTORE_SNAPSHOT.value,
511513
},
512514
unsafe=mock.ANY,
515+
retry=mock.ANY,
513516
logger=mock.ANY,
514517
),
515518
err_msg="In progress notification has not been sent.",
@@ -655,6 +658,7 @@ async def test_restore_backup_azure_blob(
655658
"action": WebhookAction.RESTORE_SNAPSHOT.value,
656659
},
657660
unsafe=mock.ANY,
661+
retry=mock.ANY,
658662
logger=mock.ANY,
659663
),
660664
err_msg="Success notification has not been sent.",
@@ -770,6 +774,7 @@ async def test_restore_backup_create_repo_fails(
770774
"action": WebhookAction.RESTORE_SNAPSHOT.value,
771775
},
772776
unsafe=mock.ANY,
777+
retry=mock.ANY,
773778
logger=mock.ANY,
774779
),
775780
err_msg="In progress notification has not been sent.",
@@ -792,6 +797,7 @@ async def test_restore_backup_create_repo_fails(
792797
"action": WebhookAction.RESTORE_SNAPSHOT.value,
793798
},
794799
unsafe=mock.ANY,
800+
retry=mock.ANY,
795801
logger=mock.ANY,
796802
),
797803
err_msg="Exception notification has not been sent.",

tests/test_scale.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ async def test_scale_cluster(
277277
name,
278278
scale_data=mock.ANY,
279279
unsafe=mock.ANY,
280+
retry=mock.ANY,
280281
logger=mock.ANY,
281282
)
282283
assert await was_notification_sent(

tests/test_update_backups_schedule.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ async def test_update_backups_schedule(
126126
name,
127127
backup_schedule_data=mock.ANY,
128128
unsafe=mock.ANY,
129+
retry=mock.ANY,
129130
logger=mock.ANY,
130131
)
131132

tests/test_upgrade.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ async def test_upgrade_cluster(
185185
name,
186186
upgrade_data=mock.ANY,
187187
unsafe=mock.ANY,
188+
retry=mock.ANY,
188189
logger=mock.ANY,
189190
)
190191
assert await was_notification_sent(
@@ -426,6 +427,7 @@ async def test_upgrade_rollback_on_failure(
426427
"action": WebhookAction.UPGRADE.value,
427428
},
428429
unsafe=mock.ANY,
430+
retry=mock.ANY,
429431
logger=mock.ANY,
430432
)
431433
assert await was_notification_sent(

tests/test_webhooks.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import json
2323
import logging
24+
from unittest import mock
2425

2526
import pytest
2627
from aiohttp import web
@@ -430,16 +431,20 @@ async def test_retries_then_success(self):
430431
"secr3t password",
431432
)
432433

433-
response = await client.send_notification(
434-
"my-namespace",
435-
"my-cluster",
436-
WebhookEvent.UPGRADE,
437-
WebhookUpgradePayload(
438-
old_registry="a", new_registry="b", old_version="c", new_version="d"
439-
),
440-
WebhookStatus.SUCCESS,
441-
logging.getLogger(__name__),
442-
)
434+
with (
435+
mock.patch("crate.operator.config.config.TESTING", False),
436+
mock.patch("crate.operator.webhooks.RETRY_BASE_DELAY", 0.001),
437+
):
438+
response = await client.send_notification(
439+
"my-namespace",
440+
"my-cluster",
441+
WebhookEvent.UPGRADE,
442+
WebhookUpgradePayload(
443+
old_registry="a", new_registry="b", old_version="c", new_version="d"
444+
),
445+
WebhookStatus.SUCCESS,
446+
logging.getLogger(__name__),
447+
)
443448

444449
assert response.status == 200
445450
assert self.app["calls"] == 3
@@ -473,16 +478,20 @@ async def test_retry_exhausted(self):
473478
"secr3t password",
474479
)
475480

476-
response = await client.send_notification(
477-
"my-namespace",
478-
"my-cluster",
479-
WebhookEvent.UPGRADE,
480-
WebhookUpgradePayload(
481-
old_registry="a", new_registry="b", old_version="c", new_version="d"
482-
),
483-
WebhookStatus.SUCCESS,
484-
logging.getLogger(__name__),
485-
)
481+
with (
482+
mock.patch("crate.operator.config.config.TESTING", False),
483+
mock.patch("crate.operator.webhooks.RETRY_BASE_DELAY", 0.001),
484+
):
485+
response = await client.send_notification(
486+
"my-namespace",
487+
"my-cluster",
488+
WebhookEvent.UPGRADE,
489+
WebhookUpgradePayload(
490+
old_registry="a", new_registry="b", old_version="c", new_version="d"
491+
),
492+
WebhookStatus.SUCCESS,
493+
logging.getLogger(__name__),
494+
)
486495

487496
assert response.status == 502
488497
assert self.app["calls"] == RETRY_MAX_ATTEMPTS

0 commit comments

Comments
 (0)