Debian Codegraph @

ansible_collections/amazon/aws/tests/unit/plugins/module_utils/retries/test_awsretry.py

raw
1 # -*- coding: utf-8 -*-
2 # (c) 2015, Allen Sanabria <[email protected]>
3 #
4 # This file is part of Ansible
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 try:
8 import botocore
9 except ImportError:
10 pass
11
12 import pytest
13
14 from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3
15 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
16
17 if not HAS_BOTO3:
18 pytestmark = pytest.mark.skip("test_awsretry.py requires the python modules 'boto3' and 'botocore'")
19
20
21 class TestAWSRetry:
22 def test_no_failures(self):
23 self.counter = 0
24
25 @AWSRetry.exponential_backoff(retries=2, delay=0.1)
26 def no_failures():
27 self.counter += 1
28
29 no_failures()
30 assert self.counter == 1
31
32 def test_extend_boto3_failures(self):
33 self.counter = 0
34 err_response = {"Error": {"Code": "MalformedPolicyDocument"}}
35
36 @AWSRetry.exponential_backoff(retries=2, delay=0.1, catch_extra_error_codes=["MalformedPolicyDocument"])
37 def extend_failures():
38 self.counter += 1
39 if self.counter < 2:
40 raise botocore.exceptions.ClientError(err_response, "You did something wrong.")
41 else:
42 return "success"
43
44 result = extend_failures()
45 assert result == "success"
46 assert self.counter == 2
47
48 def test_retry_once(self):
49 self.counter = 0
50 err_response = {"Error": {"Code": "InternalFailure"}}
51
52 @AWSRetry.exponential_backoff(retries=2, delay=0.1)
53 def retry_once():
54 self.counter += 1
55 if self.counter < 2:
56 raise botocore.exceptions.ClientError(err_response, "Something went wrong!")
57 else:
58 return "success"
59
60 result = retry_once()
61 assert result == "success"
62 assert self.counter == 2
63
64 def test_reached_limit(self):
65 self.counter = 0
66 err_response = {"Error": {"Code": "RequestLimitExceeded"}}
67
68 @AWSRetry.exponential_backoff(retries=4, delay=0.1)
69 def fail():
70 self.counter += 1
71 raise botocore.exceptions.ClientError(err_response, "toooo fast!!")
72
73 with pytest.raises(botocore.exceptions.ClientError) as context:
74 fail()
75 response = context.value.response
76 assert response["Error"]["Code"] == "RequestLimitExceeded"
77 assert self.counter == 4
78
79 def test_unexpected_exception_does_not_retry(self):
80 self.counter = 0
81 err_response = {"Error": {"Code": "AuthFailure"}}
82
83 @AWSRetry.exponential_backoff(retries=4, delay=0.1)
84 def raise_unexpected_error():
85 self.counter += 1
86 raise botocore.exceptions.ClientError(err_response, "unexpected error")
87
88 with pytest.raises(botocore.exceptions.ClientError) as context:
89 raise_unexpected_error()
90 response = context.value.response
91 assert response["Error"]["Code"] == "AuthFailure"
92 assert self.counter == 1