Debian Codegraph @

ansible_collections/amazon/aws/tests/unit/plugins/module_utils/test_common.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright: (c) 2026, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from ansible_collections.amazon.aws.plugins.module_utils.common import AMAZON_AWS_COLLECTION_NAME
7 from ansible_collections.amazon.aws.plugins.module_utils.common import AMAZON_AWS_COLLECTION_VERSION
8 from ansible_collections.amazon.aws.plugins.module_utils.common import get_collection_info
9 from ansible_collections.amazon.aws.plugins.module_utils.common import set_collection_info
10
11
12 class TestCommon:
13 """Test suite for common.py module utilities."""
14
15 def setup_method(self):
16 """Reset collection info to defaults before each test."""
17 set_collection_info(
18 collection_name=AMAZON_AWS_COLLECTION_NAME,
19 collection_version=AMAZON_AWS_COLLECTION_VERSION,
20 )
21
22 def teardown_method(self):
23 """Reset collection info to defaults after each test to prevent pollution."""
24 set_collection_info(
25 collection_name=AMAZON_AWS_COLLECTION_NAME,
26 collection_version=AMAZON_AWS_COLLECTION_VERSION,
27 )
28
29 def test_default_collection_name(self):
30 """Test that AMAZON_AWS_COLLECTION_NAME has the expected value."""
31 assert AMAZON_AWS_COLLECTION_NAME == "amazon.aws"
32
33 def test_get_collection_info_default(self):
34 """Test get_collection_info returns default values."""
35 info = get_collection_info()
36 assert info["name"] == AMAZON_AWS_COLLECTION_NAME
37 assert info["version"] == AMAZON_AWS_COLLECTION_VERSION
38
39 def test_get_collection_info_returns_dict(self):
40 """Test get_collection_info returns a dictionary."""
41 info = get_collection_info()
42 assert isinstance(info, dict)
43 assert "name" in info
44 assert "version" in info
45
46 def test_set_collection_info_name_only(self):
47 """Test set_collection_info with collection_name parameter only."""
48 set_collection_info(collection_name="custom.collection")
49 info = get_collection_info()
50 assert info["name"] == "custom.collection"
51 # Version should remain unchanged from default
52 assert info["version"] == AMAZON_AWS_COLLECTION_VERSION
53
54 def test_set_collection_info_version_only(self):
55 """Test set_collection_info with collection_version parameter only."""
56 set_collection_info(collection_version="1.2.3")
57 info = get_collection_info()
58 # Name should remain unchanged from default
59 assert info["name"] == AMAZON_AWS_COLLECTION_NAME
60 assert info["version"] == "1.2.3"
61
62 def test_set_collection_info_both_parameters(self):
63 """Test set_collection_info with both parameters."""
64 set_collection_info(collection_name="community.aws", collection_version="2.0.0")
65 info = get_collection_info()
66 assert info["name"] == "community.aws"
67 assert info["version"] == "2.0.0"
68
69 def test_set_collection_info_none_parameters(self):
70 """Test set_collection_info with None parameters doesn't change values."""
71 # Set to known values first
72 set_collection_info(collection_name="test.collection", collection_version="1.0.0")
73
74 # Call with None parameters
75 set_collection_info(collection_name=None, collection_version=None)
76
77 # Values should remain unchanged
78 info = get_collection_info()
79 assert info["name"] == "test.collection"
80 assert info["version"] == "1.0.0"
81
82 def test_set_collection_info_no_parameters(self):
83 """Test set_collection_info with no parameters doesn't change values."""
84 # Set to known values first
85 set_collection_info(collection_name="test.collection", collection_version="1.0.0")
86
87 # Call with no parameters
88 set_collection_info()
89
90 # Values should remain unchanged
91 info = get_collection_info()
92 assert info["name"] == "test.collection"
93 assert info["version"] == "1.0.0"
94
95 def test_collection_info_persistence(self):
96 """Test that collection info persists between get_collection_info calls."""
97 set_collection_info(collection_name="persistent.test", collection_version="3.0.0")
98
99 # Call get_collection_info multiple times
100 info1 = get_collection_info()
101 info2 = get_collection_info()
102
103 # Both calls should return the same values
104 assert info1["name"] == "persistent.test"
105 assert info1["version"] == "3.0.0"
106 assert info2["name"] == "persistent.test"
107 assert info2["version"] == "3.0.0"
108
109 def test_collection_info_successive_updates(self):
110 """Test that successive set_collection_info calls update the context."""
111 # First update
112 set_collection_info(collection_name="first.collection")
113 info = get_collection_info()
114 assert info["name"] == "first.collection"
115
116 # Second update
117 set_collection_info(collection_name="second.collection")
118 info = get_collection_info()
119 assert info["name"] == "second.collection"
120
121 # Third update with version
122 set_collection_info(collection_version="5.0.0")
123 info = get_collection_info()
124 assert info["name"] == "second.collection" # Name unchanged
125 assert info["version"] == "5.0.0"
126
127 def test_collection_info_empty_string(self):
128 """Test set_collection_info with empty strings (should not update)."""
129 # Set to known values
130 set_collection_info(collection_name="test.collection", collection_version="1.0.0")
131
132 # Empty strings are falsy in Python, so they shouldn't update
133 set_collection_info(collection_name="", collection_version="")
134
135 info = get_collection_info()
136 # Values should remain unchanged (empty strings are falsy)
137 assert info["name"] == "test.collection"
138 assert info["version"] == "1.0.0"
139
140 def test_community_aws_use_case(self):
141 """Test the actual use case from community.aws collection.
142
143 This simulates how community.aws uses set_collection_info to override
144 the collection name and version for user agent tracking.
145 """
146 # Simulate community.aws setting its own collection info
147 set_collection_info(collection_name="community.aws", collection_version="12.0.0-dev0")
148
149 info = get_collection_info()
150 assert info["name"] == "community.aws"
151 assert info["version"] == "12.0.0-dev0"
152
153 # This would then be used in botocore._get_user_agent_string()
154 # to build a user agent like: "APN/1.0 Ansible/2.15.0 community.aws/12.0.0-dev0"