Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_maven_artifact.py

raw
1 # Copyright (c) 2017 Ansible Project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4 from __future__ import annotations
5
6 import pytest
7 from ansible.module_utils import basic
8
9 from ansible_collections.community.general.plugins.modules import maven_artifact
10
11 pytestmark = pytest.mark.usefixtures("patch_ansible_module")
12
13 maven_metadata_example = b"""<?xml version="1.0" encoding="UTF-8"?>
14 <metadata>
15 <groupId>junit</groupId>
16 <artifactId>junit</artifactId>
17 <versioning>
18 <latest>4.13-beta-2</latest>
19 <release>4.13-beta-2</release>
20 <versions>
21 <version>3.7</version>
22 <version>3.8</version>
23 <version>3.8.1</version>
24 <version>3.8.2</version>
25 <version>4.0</version>
26 <version>4.1</version>
27 <version>4.2</version>
28 <version>4.3</version>
29 <version>4.3.1</version>
30 <version>4.4</version>
31 <version>4.5</version>
32 <version>4.6</version>
33 <version>4.7</version>
34 <version>4.8</version>
35 <version>4.8.1</version>
36 <version>4.8.2</version>
37 <version>4.9</version>
38 <version>4.10</version>
39 <version>4.11-beta-1</version>
40 <version>4.11</version>
41 <version>4.12-beta-1</version>
42 <version>4.12-beta-2</version>
43 <version>4.12-beta-3</version>
44 <version>4.12</version>
45 <version>4.13-beta-1</version>
46 <version>4.13-beta-2</version>
47 </versions>
48 <lastUpdated>20190202141051</lastUpdated>
49 </versioning>
50 </metadata>
51 """
52
53
54 @pytest.mark.parametrize(
55 "patch_ansible_module, version_by_spec, version_choosed",
56 [
57 (None, "(,3.9]", "3.8.2"),
58 (None, "3.0", "3.8.2"),
59 (None, "[3.7]", "3.7"),
60 (None, "[4.10, 4.12]", "4.12"),
61 (None, "[4.10, 4.12)", "4.11"),
62 (None, "[2.0,)", "4.13-beta-2"),
63 ],
64 )
65 def test_find_version_by_spec(mocker, version_by_spec, version_choosed):
66 _getContent = mocker.patch(
67 "ansible_collections.community.general.plugins.modules.maven_artifact.MavenDownloader._getContent"
68 )
69 _getContent.return_value = maven_metadata_example
70
71 artifact = maven_artifact.Artifact("junit", "junit", None, version_by_spec, "jar")
72 mvn_downloader = maven_artifact.MavenDownloader(basic.AnsibleModule, "https://repo1.maven.org/maven2")
73
74 assert mvn_downloader.find_version_by_spec(artifact) == version_choosed
75
76
77 # Metadata with multiple snapshotVersion entries per extension (as produced by GitHub Packages).
78 # The entries are deliberately NOT in chronological order to verify that
79 # resolution uses the <updated> timestamp rather than relying on list position.
80 snapshot_metadata_multiple_entries = b"""<?xml version="1.0" encoding="UTF-8"?>
81 <metadata>
82 <groupId>com.example</groupId>
83 <artifactId>my-lib</artifactId>
84 <version>1.0.0-SNAPSHOT</version>
85 <versioning>
86 <snapshot>
87 <timestamp>20260210.152345</timestamp>
88 <buildNumber>3</buildNumber>
89 </snapshot>
90 <lastUpdated>20260210153158</lastUpdated>
91 <snapshotVersions>
92 <snapshotVersion>
93 <extension>jar</extension>
94 <value>1.0.0-20260205.091032-2</value>
95 <updated>20260205091858</updated>
96 </snapshotVersion>
97 <snapshotVersion>
98 <extension>jar</extension>
99 <value>1.0.0-20260210.152345-3</value>
100 <updated>20260210153154</updated>
101 </snapshotVersion>
102 <snapshotVersion>
103 <extension>pom</extension>
104 <value>1.0.0-20260210.152345-3</value>
105 <updated>20260210153153</updated>
106 </snapshotVersion>
107 <snapshotVersion>
108 <extension>jar</extension>
109 <value>1.0.0-20260203.123107-1</value>
110 <updated>20260203123944</updated>
111 </snapshotVersion>
112 <snapshotVersion>
113 <extension>pom</extension>
114 <value>1.0.0-20260203.123107-1</value>
115 <updated>20260203123943</updated>
116 </snapshotVersion>
117 <snapshotVersion>
118 <extension>pom</extension>
119 <value>1.0.0-20260205.091032-2</value>
120 <updated>20260205091857</updated>
121 </snapshotVersion>
122 </snapshotVersions>
123 </versioning>
124 </metadata>
125 """
126
127 # Metadata without a <snapshot> block but with <snapshotVersions> only.
128 snapshot_metadata_no_snapshot_block = b"""<?xml version="1.0" encoding="UTF-8"?>
129 <metadata>
130 <groupId>com.example</groupId>
131 <artifactId>my-lib</artifactId>
132 <version>1.0.0-SNAPSHOT</version>
133 <versioning>
134 <lastUpdated>20260210153158</lastUpdated>
135 <snapshotVersions>
136 <snapshotVersion>
137 <extension>jar</extension>
138 <value>1.0.0-20260203.123107-1</value>
139 <updated>20260203123944</updated>
140 </snapshotVersion>
141 <snapshotVersion>
142 <extension>pom</extension>
143 <value>1.0.0-20260203.123107-1</value>
144 <updated>20260203123943</updated>
145 </snapshotVersion>
146 </snapshotVersions>
147 </versioning>
148 </metadata>
149 """
150
151
152 @pytest.mark.parametrize("patch_ansible_module", [None])
153 def test_find_uri_for_snapshot_resolves_to_latest(mocker):
154 """When metadata has multiple snapshotVersion entries per extension,
155 the entry with the newest updated timestamp should be resolved."""
156 _getContent = mocker.patch(
157 "ansible_collections.community.general.plugins.modules.maven_artifact.MavenDownloader._getContent"
158 )
159 _getContent.return_value = snapshot_metadata_multiple_entries
160
161 artifact = maven_artifact.Artifact("com.example", "my-lib", "1.0.0-SNAPSHOT", None, "", "jar")
162 mvn_downloader = maven_artifact.MavenDownloader(basic.AnsibleModule, "https://repo.example.com")
163
164 uri = mvn_downloader.find_uri_for_artifact(artifact)
165 assert "1.0.0-20260210.152345-3.jar" in uri
166
167
168 @pytest.mark.parametrize("patch_ansible_module", [None])
169 def test_find_uri_for_snapshot_without_snapshot_block_uses_snapshot_versions(mocker):
170 """When metadata lacks a <snapshot> block, fall back to scanning
171 <snapshotVersions> entries."""
172 _getContent = mocker.patch(
173 "ansible_collections.community.general.plugins.modules.maven_artifact.MavenDownloader._getContent"
174 )
175 _getContent.return_value = snapshot_metadata_no_snapshot_block
176
177 artifact = maven_artifact.Artifact("com.example", "my-lib", "1.0.0-SNAPSHOT", None, "", "jar")
178 mvn_downloader = maven_artifact.MavenDownloader(basic.AnsibleModule, "https://repo.example.com")
179
180 uri = mvn_downloader.find_uri_for_artifact(artifact)
181 assert "1.0.0-20260203.123107-1.jar" in uri
182
183
184 # Metadata with a <snapshot> block that has <timestamp> but no <buildNumber>.
185 # This is schema-valid but non-standard (e.g. produced by non-Maven tools).
186 # The module should fall back to <snapshotVersions> scanning.
187 snapshot_metadata_incomplete_snapshot_block = b"""<?xml version="1.0" encoding="UTF-8"?>
188 <metadata>
189 <groupId>com.example</groupId>
190 <artifactId>my-lib</artifactId>
191 <version>1.0.0-SNAPSHOT</version>
192 <versioning>
193 <snapshot>
194 <timestamp>20260210.152345</timestamp>
195 </snapshot>
196 <lastUpdated>20260210153158</lastUpdated>
197 <snapshotVersions>
198 <snapshotVersion>
199 <extension>jar</extension>
200 <value>1.0.0-20260210.152345-3</value>
201 <updated>20260210153154</updated>
202 </snapshotVersion>
203 <snapshotVersion>
204 <extension>pom</extension>
205 <value>1.0.0-20260210.152345-3</value>
206 <updated>20260210153153</updated>
207 </snapshotVersion>
208 </snapshotVersions>
209 </versioning>
210 </metadata>
211 """
212
213
214 @pytest.mark.parametrize("patch_ansible_module", [None])
215 def test_find_uri_for_snapshot_incomplete_snapshot_block_uses_snapshot_versions(mocker):
216 """When the <snapshot> block is incomplete (e.g. missing <buildNumber>),
217 fall back to <snapshotVersions> instead of raising an error."""
218 _getContent = mocker.patch(
219 "ansible_collections.community.general.plugins.modules.maven_artifact.MavenDownloader._getContent"
220 )
221 _getContent.return_value = snapshot_metadata_incomplete_snapshot_block
222
223 artifact = maven_artifact.Artifact("com.example", "my-lib", "1.0.0-SNAPSHOT", None, "", "jar")
224 mvn_downloader = maven_artifact.MavenDownloader(basic.AnsibleModule, "https://repo.example.com")
225
226 uri = mvn_downloader.find_uri_for_artifact(artifact)
227 assert "1.0.0-20260210.152345-3.jar" in uri
228
229
230 @pytest.mark.parametrize("patch_ansible_module", [None])
231 def test_find_uri_for_release_version_unaffected(mocker):
232 """Non-SNAPSHOT versions must not be affected by snapshot resolution logic."""
233 artifact = maven_artifact.Artifact("com.example", "my-lib", "2.1.0", None, "", "jar")
234 mvn_downloader = maven_artifact.MavenDownloader(basic.AnsibleModule, "https://repo.example.com")
235
236 uri = mvn_downloader.find_uri_for_artifact(artifact)
237 assert uri == "https://repo.example.com/com/example/my-lib/2.1.0/my-lib-2.1.0.jar"