Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #951/1a4595a5 backport][stable-4] Bump CloudRetry.backoff deprecation to 6.0.0 #952

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
2 changes: 2 additions & 0 deletions changelogs/fragments/951-cloudretry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deprecated_features:
- module_utils.cloud - removal of the ``CloudRetry.backoff`` has been delayed until release 6.0.0. It is recommended to update custom modules to use ``jittered_backoff`` or ``exponential_backoff`` instead (https://github.com/ansible-collections/amazon.aws/pull/951).
11 changes: 9 additions & 2 deletions plugins/module_utils/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import time
import functools
import random
import ansible.module_utils.common.warnings as ansible_warnings


class BackoffIterator:
Expand Down Expand Up @@ -100,7 +101,7 @@ def status_code_from_exception(error):
def found(response_code, catch_extra_error_codes=None):
def _is_iterable():
try:
it = iter(catch_extra_error_codes)
iter(catch_extra_error_codes)
except TypeError:
# not iterable
return False
Expand Down Expand Up @@ -184,7 +185,7 @@ def backoff(cls, tries=10, delay=3, backoff=1.1, catch_extra_error_codes=None):
"""
Wrap a callable with retry behavior.
Developers should use CloudRetry.exponential_backoff instead.
This method has been deprecated and will be removed in release 4.0.0, consider using exponential_backoff method instead.
This method has been deprecated and will be removed in release 6.0.0, consider using exponential_backoff method instead.
Args:
retries (int): Number of times to retry a failed request before giving up
default=10
Expand All @@ -197,6 +198,12 @@ def backoff(cls, tries=10, delay=3, backoff=1.1, catch_extra_error_codes=None):
Returns:
Callable: A generator that calls the decorated function using an exponential backoff.
"""
# This won't emit a warning (we don't have the context available to us), but will trigger
# sanity failures as we prepare for 6.0.0
ansible_warnings.deprecate(
'CloudRetry.backoff has been deprecated, please use CloudRetry.exponential_backoff instead',
version='6.0.0', collection_name='amazon.aws')

return cls.exponential_backoff(
retries=tries,
delay=delay,
Expand Down
55 changes: 32 additions & 23 deletions tests/unit/module_utils/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ def setUp(self):
# nothing to do on setup stage
pass

# ========================================================
# retry original backoff
# ========================================================
def test_retry_backoff(self):

@CloudRetryUtils.UnitTestsRetry.backoff(tries=3, delay=1, backoff=1.1, catch_extra_error_codes=CloudRetryUtils.error_codes)
def test_retry_func():
if test_retry_func.counter < 2:
test_retry_func.counter += 1
raise self.TestException(status=random.choice(CloudRetryUtils.error_codes))
else:
return True

test_retry_func.counter = 0
ret = test_retry_func()
assert ret is True

# ========================================================
# retry exponential backoff
# ========================================================
Expand Down Expand Up @@ -142,10 +159,10 @@ def test_retry_func():
raise unexpected_except

test_retry_func.counter = 0
try:
ret = test_retry_func()
except self.TestException as exc:
assert exc.status == unexpected_except.status
with self.assertRaises(self.TestException) as exc:
test_retry_func()

assert exc.exception.status == unexpected_except.status

# ========================================================
# retry jittered backoff
Expand Down Expand Up @@ -175,10 +192,10 @@ def test_retry_func():
raise unexpected_except

test_retry_func.counter = 0
try:
ret = test_retry_func()
except self.TestException as exc:
assert exc.status == unexpected_except.status
with self.assertRaises(self.TestException) as exc:
test_retry_func()

assert exc.exception.status == unexpected_except.status

# ========================================================
# retry with custom class
Expand Down Expand Up @@ -210,17 +227,13 @@ def _fail():

# run the method 3 times and assert that each it is retrying after 2secs
# the elapsed execution time should be closed to 2sec
for u in range(3):
for _i in range(3):
start = datetime.now()
raised = False
try:
with self.assertRaises(self.TestException):
_fail()
except self.TestException:
raised = True
duration = (datetime.now() - start).seconds
assert duration == 2
finally:
assert raised
duration = (datetime.now() - start).seconds
assert duration == 2

def test_only_base_exception(self):
def _fail_index():
Expand Down Expand Up @@ -253,11 +266,7 @@ def _fail_exception():

start = datetime.now()
raised = False
try:
with self.assertRaises(Exception):
decorator(function)()
except Exception:
raised = True
_duration = (datetime.now() - start).seconds
assert duration == _duration
finally:
assert raised
_duration = (datetime.now() - start).seconds
assert duration == _duration