Skip to content

Commit

Permalink
Expand tagging unit tests (#1147)
Browse files Browse the repository at this point in the history
Expand tagging unit tests

SUMMARY
Catch a couple of extra, simple, cases in the tagging unit tests
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
plugins/module_utils/tagging.py
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
  • Loading branch information
tremble authored Oct 10, 2022
1 parent 226ea7c commit ffd8f84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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

0 comments on commit ffd8f84

Please sign in to comment.