diff --git a/changelogs/fragments/2205-support-minmax-s3lifecycle.yml b/changelogs/fragments/2205-support-minmax-s3lifecycle.yml new file mode 100644 index 00000000000..2f192ea4ec6 --- /dev/null +++ b/changelogs/fragments/2205-support-minmax-s3lifecycle.yml @@ -0,0 +1,2 @@ +minor_changes: + - s3_lifecycle - Support for min and max object size when applying the filter rules. \ No newline at end of file diff --git a/plugins/modules/s3_lifecycle.py b/plugins/modules/s3_lifecycle.py index 96e63585e9d..c82290df250 100644 --- a/plugins/modules/s3_lifecycle.py +++ b/plugins/modules/s3_lifecycle.py @@ -629,8 +629,8 @@ def main(): expiration_days=dict(type="int"), expiration_date=dict(), expire_object_delete_marker=dict(type="bool"), - maximum_object_size=dict(type=int), - minimum_object_size=dict(type=int), + maximum_object_size=dict(type="int"), + minimum_object_size=dict(type="int"), noncurrent_version_expiration_days=dict(type="int"), noncurrent_version_keep_newer=dict(type="int"), noncurrent_version_storage_class=dict(default="glacier", type="str", choices=s3_storage_class), diff --git a/tests/unit/plugins/modules/test_s3_lifecycle_filters_are_equal.py b/tests/unit/plugins/modules/test_s3_lifecycle_filters_are_equal.py new file mode 100644 index 00000000000..8a3f0b4bbb0 --- /dev/null +++ b/tests/unit/plugins/modules/test_s3_lifecycle_filters_are_equal.py @@ -0,0 +1,108 @@ +# -*- coding: utf-8 -*- + +# Copyright: (c) 2023, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +import pytest + +from ansible_collections.community.aws.plugins.modules.s3_lifecycle import filters_are_equal + +def test_filters_are_equal(): + # Test case 1: Both filters are identical + filter1 = { + "And": { + "Prefix": "nested/", + "ObjectSizeGreaterThan": 150, + "ObjectSizeLessThan": 180, + }, + } + filter2 = filter1.copy() + assert filters_are_equal(filter1, filter2) is True + + # Test case 2: One filter is None + filter1 = None + filter2 = { + "Prefix": "test/" + } + assert filters_are_equal(filter1, filter2) is False + + # Test case 3: One filter is empty + filter1 = {} + filter2 = { + "ObjectSizeGreaterThan": 100, + } + assert filters_are_equal(filter1, filter2) is False + + # Test case 4: Filters differ in a single key + filter1 = { + "ObjectSizeGreaterThan": 100, + } + filter2 = { + "ObjectSizeGreaterThan": 200, # Different value + } + assert filters_are_equal(filter1, filter2) is False + + # Test case 5: Filters with missing `And` key in one filter + filter1 = { + "Prefix": "test/", + } + filter2 = { + "And": { + "Prefix": "nested/", + "ObjectSizeGreaterThan": 100, + }, + } + assert filters_are_equal(filter1, filter2) is False + + # Test case 6: Filters with nested `And` keys matching + filter1 = { + "And": { + "Prefix": "nested/", + "ObjectSizeGreaterThan": 150, + }, + } + filter2 = { + "And": { + "Prefix": "nested/", + "ObjectSizeGreaterThan": 150, + }, + } + assert filters_are_equal(filter1, filter2) is True + + # Test case 7: Filters with nested `And` keys differing + filter1 = { + "And": { + "Prefix": "test/", + "ObjectSizeGreaterThan": 150, + }, + } + filter2 = { + "And": { + "Prefix": "nested/", # Different key/value + "ObjectSizeLessThan": 150, + }, + } + assert filters_are_equal(filter1, filter2) is False + + # Test case 8: Both filters are None + filter1 = None + filter2 = None + assert filters_are_equal(filter1, filter2) is False + + # Test case 9: Filters with different `Prefix` values + filter1 = { + "Prefix": "test/" + } + filter2 = { + "Prefix": "different/" + } + assert filters_are_equal(filter1, filter2) is False + + # Test case 10: Filters with empty strings for `Prefix` + filter1 = { + "Prefix": "" + } + filter2 = { + "Prefix": "" + } + assert filters_are_equal(filter1, filter2) is True \ No newline at end of file