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

Expand tagging unit tests #1147

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/unit-tests-tagging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trivial:
- module_utils.tagging - expand module_utils.tagging unit tests.
25 changes: 25 additions & 0 deletions tests/unit/module_utils/test_tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import pytest

from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications
Expand All @@ -26,6 +28,13 @@ def setup_method(self):
{'Key': 'lower case', 'Value': 'lower case value'}
]

self.tag_example_boto3_list_custom_key = [
{'MyKey': 'lowerCamel', 'MyValue': 'lowerCamelValue'},
{'MyKey': 'UpperCamel', 'MyValue': 'upperCamelValue'},
{'MyKey': 'Normal case', 'MyValue': 'Normal Value'},
{'MyKey': 'lower case', 'MyValue': 'lower case value'}
]

self.tag_example_dict = {
'lowerCamel': 'lowerCamelValue',
'UpperCamel': 'upperCamelValue',
Expand All @@ -52,6 +61,10 @@ def test_ansible_dict_to_boto3_tag_list(self):
sorted_list = sorted(self.tag_example_boto3_list, key=lambda i: (i['Key']))
assert sorted_converted_list == sorted_list

def test_ansible_dict_to_boto3_tag_list_empty(self):
assert ansible_dict_to_boto3_tag_list({}) == []
assert ansible_dict_to_boto3_tag_list(None) == []

# ========================================================
# tagging.boto3_tag_list_to_ansible_dict
# ========================================================
Expand All @@ -66,6 +79,14 @@ def test_boto3_tag_list_to_ansible_dict_empty(self):
# Minio returns [{}] when there are no tags
assert boto3_tag_list_to_ansible_dict([{}]) == {}

def test_boto3_tag_list_to_ansible_dict_nondefault_keys(self):
converted_dict = boto3_tag_list_to_ansible_dict(self.tag_example_boto3_list_custom_key, 'MyKey', 'MyValue')
assert converted_dict == self.tag_example_dict

with pytest.raises(ValueError) as context:
boto3_tag_list_to_ansible_dict(self.tag_example_boto3_list, 'MyKey', 'MyValue')
assert "Couldn't find tag key" in str(context.value)

# ========================================================
# tagging.compare_aws_tags
# ========================================================
Expand Down Expand Up @@ -175,6 +196,10 @@ def test_compare_aws_tags_aws_complex(self):
# tagging.boto3_tag_specifications
# ========================================================

def test_boto3_tag_specifications_empty(self):
assert boto3_tag_specifications(None) is None
assert boto3_tag_specifications({}) is None

# Builds upon ansible_dict_to_boto3_tag_list, assume that if a minimal tag
# dictionary behaves as expected, then all will behave
def test_boto3_tag_specifications_no_type(self):
Expand Down