-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
287 additions
and
22 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
tests/unit/module_utils/autoscaling/test_autoscaling_error_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: Contributors to the Ansible project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
try: | ||
import botocore | ||
except ImportError: | ||
pass | ||
|
||
import pytest | ||
|
||
from ansible_collections.amazon.aws.plugins.module_utils.autoscaling import AnsibleAutoScalingError | ||
from ansible_collections.amazon.aws.plugins.module_utils.autoscaling import AutoScalingErrorHandler | ||
from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3 | ||
|
||
if not HAS_BOTO3: | ||
pytestmark = pytest.mark.skip("test_iam_error_handler.py requires the python modules 'boto3' and 'botocore'") | ||
|
||
|
||
class TestAutoScalingDeletionHandler: | ||
def test_no_failures(self): | ||
self.counter = 0 | ||
|
||
@AutoScalingErrorHandler.deletion_error_handler("no error") | ||
def no_failures(): | ||
self.counter += 1 | ||
|
||
no_failures() | ||
assert self.counter == 1 | ||
|
||
def test_client_error(self): | ||
self.counter = 0 | ||
err_response = {"Error": {"Code": "MalformedPolicyDocument"}} | ||
|
||
@AutoScalingErrorHandler.deletion_error_handler("do something") | ||
def raise_client_error(): | ||
self.counter += 1 | ||
raise botocore.exceptions.ClientError(err_response, "Something bad") | ||
|
||
with pytest.raises(AnsibleAutoScalingError) as e_info: | ||
raise_client_error() | ||
assert self.counter == 1 | ||
raised = e_info.value | ||
assert isinstance(raised.exception, botocore.exceptions.ClientError) | ||
assert "do something" in raised.message | ||
assert "Something bad" in str(raised.exception) | ||
|
||
def test_ignore_error(self): | ||
self.counter = 0 | ||
err_response = {"Error": {"Code": "NoSuchEntity"}} | ||
|
||
@AutoScalingErrorHandler.deletion_error_handler("do something") | ||
def raise_client_error(): | ||
self.counter += 1 | ||
raise botocore.exceptions.ClientError(err_response, "I couldn't find it") | ||
|
||
ret_val = raise_client_error() | ||
assert self.counter == 1 | ||
assert ret_val is False | ||
|
||
|
||
class TestIamListHandler: | ||
def test_no_failures(self): | ||
self.counter = 0 | ||
|
||
@AutoScalingErrorHandler.list_error_handler("no error") | ||
def no_failures(): | ||
self.counter += 1 | ||
|
||
no_failures() | ||
assert self.counter == 1 | ||
|
||
def test_client_error(self): | ||
self.counter = 0 | ||
err_response = {"Error": {"Code": "MalformedPolicyDocument"}} | ||
|
||
@AutoScalingErrorHandler.list_error_handler("do something") | ||
def raise_client_error(): | ||
self.counter += 1 | ||
raise botocore.exceptions.ClientError(err_response, "Something bad") | ||
|
||
with pytest.raises(AnsibleAutoScalingError) as e_info: | ||
raise_client_error() | ||
assert self.counter == 1 | ||
raised = e_info.value | ||
assert isinstance(raised.exception, botocore.exceptions.ClientError) | ||
assert "do something" in raised.message | ||
assert "Something bad" in str(raised.exception) | ||
|
||
def test_list_error(self): | ||
self.counter = 0 | ||
err_response = {"Error": {"Code": "NoSuchEntity"}} | ||
|
||
@AutoScalingErrorHandler.list_error_handler("do something") | ||
def raise_client_error(): | ||
self.counter += 1 | ||
raise botocore.exceptions.ClientError(err_response, "I couldn't find it") | ||
|
||
ret_val = raise_client_error() | ||
assert self.counter == 1 | ||
assert ret_val is None | ||
|
||
|
||
class TestIamCommonHandler: | ||
def test_no_failures(self): | ||
self.counter = 0 | ||
|
||
@AutoScalingErrorHandler.common_error_handler("no error") | ||
def no_failures(): | ||
self.counter += 1 | ||
|
||
no_failures() | ||
assert self.counter == 1 | ||
|
||
def test_client_error(self): | ||
self.counter = 0 | ||
err_response = {"Error": {"Code": "MalformedPolicyDocument"}} | ||
|
||
@AutoScalingErrorHandler.common_error_handler("do something") | ||
def raise_client_error(): | ||
self.counter += 1 | ||
raise botocore.exceptions.ClientError(err_response, "Something bad") | ||
|
||
with pytest.raises(AnsibleAutoScalingError) as e_info: | ||
raise_client_error() | ||
assert self.counter == 1 | ||
raised = e_info.value | ||
assert isinstance(raised.exception, botocore.exceptions.ClientError) | ||
assert "do something" in raised.message | ||
assert "Something bad" in str(raised.exception) |
Oops, something went wrong.