| 1 |
# Copyright (c) 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 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import json |
| 8 |
from collections import OrderedDict |
| 9 |
from collections.abc import Mapping |
| 10 |
from io import BytesIO |
| 11 |
from unittest.mock import ( |
| 12 |
MagicMock, |
| 13 |
patch, |
| 14 |
) |
| 15 |
|
| 16 |
from ansible.module_utils.urls import basic_auth_header |
| 17 |
|
| 18 |
from ansible_collections.community.general.plugins.modules.jenkins_plugin import JenkinsPlugin |
| 19 |
|
| 20 |
|
| 21 |
def pass_function(*args, **kwargs): |
| 22 |
pass |
| 23 |
|
| 24 |
|
| 25 |
GITHUB_DATA = { |
| 26 |
"url": "https://api.github.com/repos/ansible/ansible", |
| 27 |
"response": b""" |
| 28 |
{ |
| 29 |
"id": 3638964, |
| 30 |
"name": "ansible", |
| 31 |
"full_name": "ansible/ansible", |
| 32 |
"owner": { |
| 33 |
"login": "ansible", |
| 34 |
"id": 1507452, |
| 35 |
"avatar_url": "https://avatars2.githubusercontent.com/u/1507452?v=4", |
| 36 |
"gravatar_id": "", |
| 37 |
"url": "https://api.github.com/users/ansible", |
| 38 |
"html_url": "https://github.com/ansible", |
| 39 |
"followers_url": "https://api.github.com/users/ansible/followers", |
| 40 |
"following_url": "https://api.github.com/users/ansible/following{/other_user}", |
| 41 |
"gists_url": "https://api.github.com/users/ansible/gists{/gist_id}", |
| 42 |
"starred_url": "https://api.github.com/users/ansible/starred{/owner}{/repo}", |
| 43 |
"subscriptions_url": "https://api.github.com/users/ansible/subscriptions", |
| 44 |
"organizations_url": "https://api.github.com/users/ansible/orgs", |
| 45 |
"repos_url": "https://api.github.com/users/ansible/repos", |
| 46 |
"events_url": "https://api.github.com/users/ansible/events{/privacy}", |
| 47 |
"received_events_url": "https://api.github.com/users/ansible/received_events", |
| 48 |
"type": "Organization", |
| 49 |
"site_admin": false |
| 50 |
}, |
| 51 |
"private": false, |
| 52 |
"html_url": "https://github.com/ansible/ansible", |
| 53 |
"description": "Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy.", |
| 54 |
"fork": false, |
| 55 |
"url": "https://api.github.com/repos/ansible/ansible", |
| 56 |
"forks_url": "https://api.github.com/repos/ansible/ansible/forks", |
| 57 |
"keys_url": "https://api.github.com/repos/ansible/ansible/keys{/key_id}", |
| 58 |
"collaborators_url": "https://api.github.com/repos/ansible/ansible/collaborators{/collaborator}", |
| 59 |
"teams_url": "https://api.github.com/repos/ansible/ansible/teams", |
| 60 |
"hooks_url": "https://api.github.com/repos/ansible/ansible/hooks", |
| 61 |
"issue_events_url": "https://api.github.com/repos/ansible/ansible/issues/events{/number}", |
| 62 |
"events_url": "https://api.github.com/repos/ansible/ansible/events", |
| 63 |
"assignees_url": "https://api.github.com/repos/ansible/ansible/assignees{/user}", |
| 64 |
"branches_url": "https://api.github.com/repos/ansible/ansible/branches{/branch}", |
| 65 |
"tags_url": "https://api.github.com/repos/ansible/ansible/tags", |
| 66 |
"blobs_url": "https://api.github.com/repos/ansible/ansible/git/blobs{/sha}", |
| 67 |
"git_tags_url": "https://api.github.com/repos/ansible/ansible/git/tags{/sha}", |
| 68 |
"git_refs_url": "https://api.github.com/repos/ansible/ansible/git/refs{/sha}", |
| 69 |
"trees_url": "https://api.github.com/repos/ansible/ansible/git/trees{/sha}", |
| 70 |
"statuses_url": "https://api.github.com/repos/ansible/ansible/statuses/{sha}", |
| 71 |
"languages_url": "https://api.github.com/repos/ansible/ansible/languages", |
| 72 |
"stargazers_url": "https://api.github.com/repos/ansible/ansible/stargazers", |
| 73 |
"contributors_url": "https://api.github.com/repos/ansible/ansible/contributors", |
| 74 |
"subscribers_url": "https://api.github.com/repos/ansible/ansible/subscribers", |
| 75 |
"subscription_url": "https://api.github.com/repos/ansible/ansible/subscription", |
| 76 |
"commits_url": "https://api.github.com/repos/ansible/ansible/commits{/sha}", |
| 77 |
"git_commits_url": "https://api.github.com/repos/ansible/ansible/git/commits{/sha}", |
| 78 |
"comments_url": "https://api.github.com/repos/ansible/ansible/comments{/number}", |
| 79 |
"issue_comment_url": "https://api.github.com/repos/ansible/ansible/issues/comments{/number}", |
| 80 |
"contents_url": "https://api.github.com/repos/ansible/ansible/contents/{+path}", |
| 81 |
"compare_url": "https://api.github.com/repos/ansible/ansible/compare/{base}...{head}", |
| 82 |
"merges_url": "https://api.github.com/repos/ansible/ansible/merges", |
| 83 |
"archive_url": "https://api.github.com/repos/ansible/ansible/{archive_format}{/ref}", |
| 84 |
"downloads_url": "https://api.github.com/repos/ansible/ansible/downloads", |
| 85 |
"issues_url": "https://api.github.com/repos/ansible/ansible/issues{/number}", |
| 86 |
"pulls_url": "https://api.github.com/repos/ansible/ansible/pulls{/number}", |
| 87 |
"milestones_url": "https://api.github.com/repos/ansible/ansible/milestones{/number}", |
| 88 |
"notifications_url": "https://api.github.com/repos/ansible/ansible/notifications{?since,all,participating}", |
| 89 |
"labels_url": "https://api.github.com/repos/ansible/ansible/labels{/name}", |
| 90 |
"releases_url": "https://api.github.com/repos/ansible/ansible/releases{/id}", |
| 91 |
"deployments_url": "https://api.github.com/repos/ansible/ansible/deployments", |
| 92 |
"created_at": "2012-03-06T14:58:02Z", |
| 93 |
"updated_at": "2017-09-19T18:10:54Z", |
| 94 |
"pushed_at": "2017-09-19T18:04:51Z", |
| 95 |
"git_url": "git://github.com/ansible/ansible.git", |
| 96 |
"ssh_url": "[email protected]:ansible/ansible.git", |
| 97 |
"clone_url": "https://github.com/ansible/ansible.git", |
| 98 |
"svn_url": "https://github.com/ansible/ansible", |
| 99 |
"homepage": "https://www.ansible.com/", |
| 100 |
"size": 91174, |
| 101 |
"stargazers_count": 25552, |
| 102 |
"watchers_count": 25552, |
| 103 |
"language": "Python", |
| 104 |
"has_issues": true, |
| 105 |
"has_projects": true, |
| 106 |
"has_downloads": true, |
| 107 |
"has_wiki": false, |
| 108 |
"has_pages": false, |
| 109 |
"forks_count": 8893, |
| 110 |
"mirror_url": null, |
| 111 |
"open_issues_count": 4283, |
| 112 |
"forks": 8893, |
| 113 |
"open_issues": 4283, |
| 114 |
"watchers": 25552, |
| 115 |
"default_branch": "devel", |
| 116 |
"organization": { |
| 117 |
"login": "ansible", |
| 118 |
"id": 1507452, |
| 119 |
"avatar_url": "https://avatars2.githubusercontent.com/u/1507452?v=4", |
| 120 |
"gravatar_id": "", |
| 121 |
"url": "https://api.github.com/users/ansible", |
| 122 |
"html_url": "https://github.com/ansible", |
| 123 |
"followers_url": "https://api.github.com/users/ansible/followers", |
| 124 |
"following_url": "https://api.github.com/users/ansible/following{/other_user}", |
| 125 |
"gists_url": "https://api.github.com/users/ansible/gists{/gist_id}", |
| 126 |
"starred_url": "https://api.github.com/users/ansible/starred{/owner}{/repo}", |
| 127 |
"subscriptions_url": "https://api.github.com/users/ansible/subscriptions", |
| 128 |
"organizations_url": "https://api.github.com/users/ansible/orgs", |
| 129 |
"repos_url": "https://api.github.com/users/ansible/repos", |
| 130 |
"events_url": "https://api.github.com/users/ansible/events{/privacy}", |
| 131 |
"received_events_url": "https://api.github.com/users/ansible/received_events", |
| 132 |
"type": "Organization", |
| 133 |
"site_admin": false |
| 134 |
}, |
| 135 |
"network_count": 8893, |
| 136 |
"subscribers_count": 1733 |
| 137 |
} |
| 138 |
""", |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
def test__get_json_data(mocker): |
| 143 |
"test the json conversion of _get_url_data" |
| 144 |
|
| 145 |
timeout = 30 |
| 146 |
params = {"url": GITHUB_DATA["url"], "timeout": timeout} |
| 147 |
module = mocker.Mock() |
| 148 |
module.params = params |
| 149 |
|
| 150 |
JenkinsPlugin._csrf_enabled = pass_function |
| 151 |
JenkinsPlugin._get_installed_plugins = pass_function |
| 152 |
JenkinsPlugin._get_url_data = mocker.Mock() |
| 153 |
JenkinsPlugin._get_url_data.return_value = BytesIO(GITHUB_DATA["response"]) |
| 154 |
jenkins_plugin = JenkinsPlugin(module) |
| 155 |
|
| 156 |
json_data = jenkins_plugin._get_json_data(f"{GITHUB_DATA['url']}", "CSRF") |
| 157 |
|
| 158 |
assert isinstance(json_data, Mapping) |
| 159 |
|
| 160 |
|
| 161 |
def test__new_fallback_urls(mocker): |
| 162 |
"test generation of new fallback URLs" |
| 163 |
|
| 164 |
params = { |
| 165 |
"url": "http://fake.jenkins.server", |
| 166 |
"timeout": 30, |
| 167 |
"name": "test-plugin", |
| 168 |
"version": "1.2.3", |
| 169 |
"updates_url": ["https://some.base.url"], |
| 170 |
"latest_plugins_url_segments": ["test_latest"], |
| 171 |
"versioned_plugins_url_segments": ["ansible", "versioned_plugins"], |
| 172 |
"update_json_url_segment": ["unreachable", "updates/update-center.json"], |
| 173 |
} |
| 174 |
module = mocker.Mock() |
| 175 |
module.params = params |
| 176 |
|
| 177 |
JenkinsPlugin._csrf_enabled = pass_function |
| 178 |
JenkinsPlugin._get_installed_plugins = pass_function |
| 179 |
|
| 180 |
jenkins_plugin = JenkinsPlugin(module) |
| 181 |
|
| 182 |
latest_urls = jenkins_plugin._get_latest_plugin_urls() |
| 183 |
assert isInList(latest_urls, "https://some.base.url/test_latest/test-plugin.hpi") |
| 184 |
versioned_urls = jenkins_plugin._get_versioned_plugin_urls() |
| 185 |
assert isInList(versioned_urls, "https://some.base.url/versioned_plugins/test-plugin/1.2.3/test-plugin.hpi") |
| 186 |
json_urls = jenkins_plugin._get_update_center_urls() |
| 187 |
assert isInList(json_urls, "https://some.base.url/updates/update-center.json") |
| 188 |
|
| 189 |
|
| 190 |
def isInList(l, i): |
| 191 |
print(f"checking if {i} in {l}") |
| 192 |
return any(item == i for item in l) # TODO: check if return `i in l` works |
| 193 |
|
| 194 |
|
| 195 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_plugin.fetch_url") |
| 196 |
def test__get_latest_compatible_plugin_version(fetch_mock, mocker): |
| 197 |
"test the latest compatible plugin version retrieval" |
| 198 |
|
| 199 |
params = { |
| 200 |
"url": "http://fake.jenkins.server", |
| 201 |
"timeout": 30, |
| 202 |
"name": "git", |
| 203 |
"version": "latest", |
| 204 |
"updates_url": ["https://some.base.url"], |
| 205 |
"plugin_versions_url_segment": ["plugin-versions.json"], |
| 206 |
"latest_plugins_url_segments": ["test_latest"], |
| 207 |
"jenkins_home": "/var/lib/jenkins", |
| 208 |
} |
| 209 |
module = mocker.Mock() |
| 210 |
module.params = params |
| 211 |
|
| 212 |
jenkins_info = {"x-jenkins": "2.263.1"} |
| 213 |
jenkins_response = MagicMock() |
| 214 |
jenkins_response.read.return_value = b"{}" |
| 215 |
|
| 216 |
plugin_data = { |
| 217 |
"plugins": { |
| 218 |
"git": OrderedDict( |
| 219 |
[ |
| 220 |
("4.8.2", {"requiredCore": "2.263.1"}), |
| 221 |
("4.8.3", {"requiredCore": "2.263.1"}), |
| 222 |
("4.9.0", {"requiredCore": "2.289.1"}), |
| 223 |
("4.9.1", {"requiredCore": "2.289.1"}), |
| 224 |
] |
| 225 |
) |
| 226 |
} |
| 227 |
} |
| 228 |
plugin_versions_response = MagicMock() |
| 229 |
plugin_versions_response.read.return_value = json.dumps(plugin_data).encode("utf-8") |
| 230 |
plugin_versions_info = {"status": 200} |
| 231 |
|
| 232 |
def fetch_url_side_effect(module, url, **kwargs): |
| 233 |
if "plugin-versions.json" in url: |
| 234 |
return (plugin_versions_response, plugin_versions_info) |
| 235 |
else: |
| 236 |
return (jenkins_response, jenkins_info) |
| 237 |
|
| 238 |
fetch_mock.side_effect = fetch_url_side_effect |
| 239 |
|
| 240 |
JenkinsPlugin._csrf_enabled = lambda self: False |
| 241 |
JenkinsPlugin._get_installed_plugins = lambda self: None |
| 242 |
|
| 243 |
jenkins_plugin = JenkinsPlugin(module) |
| 244 |
latest_version = jenkins_plugin._get_latest_compatible_plugin_version() |
| 245 |
assert latest_version == "4.8.3" |
| 246 |
|
| 247 |
|
| 248 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_plugin.fetch_url") |
| 249 |
def test__get_urls_data_sets_correct_headers(fetch_mock, mocker): |
| 250 |
params = { |
| 251 |
"url": "http://jenkins.example.com", |
| 252 |
"timeout": 30, |
| 253 |
"name": "git", |
| 254 |
"jenkins_home": "/var/lib/jenkins", |
| 255 |
"updates_url": ["http://updates.example.com"], |
| 256 |
"latest_plugins_url_segments": ["latest"], |
| 257 |
"update_json_url_segment": ["update-center.json"], |
| 258 |
"versioned_plugins_url_segments": ["plugins"], |
| 259 |
"url_username": "jenkins_user", |
| 260 |
"url_password": "jenkins_pass", |
| 261 |
"updates_url_username": "update_user", |
| 262 |
"updates_url_password": "update_pass", |
| 263 |
} |
| 264 |
module = mocker.Mock() |
| 265 |
module.params = params |
| 266 |
|
| 267 |
dummy_response = MagicMock() |
| 268 |
fetch_mock.return_value = (dummy_response, {"status": 200}) |
| 269 |
|
| 270 |
JenkinsPlugin._csrf_enabled = lambda self: False |
| 271 |
JenkinsPlugin._get_installed_plugins = lambda self: None |
| 272 |
|
| 273 |
jp = JenkinsPlugin(module) |
| 274 |
|
| 275 |
update_url = "http://updates.example.com/plugin-versions.json" |
| 276 |
jp._get_urls_data([update_url]) |
| 277 |
|
| 278 |
jenkins_url = "http://jenkins.example.com/some-endpoint" |
| 279 |
jp._get_urls_data([jenkins_url]) |
| 280 |
|
| 281 |
calls = fetch_mock.call_args_list |
| 282 |
|
| 283 |
dummy, kwargs_2 = calls[1] |
| 284 |
jenkins_auth = basic_auth_header("jenkins_user", "jenkins_pass") |
| 285 |
assert kwargs_2["headers"]["Authorization"] == jenkins_auth |
| 286 |
|
| 287 |
dummy, kwargs_1 = calls[0] |
| 288 |
updates_auth = basic_auth_header("update_user", "update_pass") |
| 289 |
assert kwargs_1["headers"]["Authorization"] == updates_auth |