| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2019, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
|
| 8 |
import json |
| 9 |
import os |
| 10 |
import functools |
| 11 |
import pytest |
| 12 |
import tempfile |
| 13 |
|
| 14 |
from io import StringIO |
| 15 |
from ansible import context |
| 16 |
from ansible.cli.galaxy import GalaxyCLI |
| 17 |
from ansible.galaxy import api, role, Galaxy |
| 18 |
from ansible.module_utils.common.text.converters import to_text |
| 19 |
from ansible.utils import context_objects as co |
| 20 |
|
| 21 |
|
| 22 |
def call_galaxy_cli(args): |
| 23 |
orig = co.GlobalCLIArgs._Singleton__instance |
| 24 |
co.GlobalCLIArgs._Singleton__instance = None |
| 25 |
try: |
| 26 |
return GalaxyCLI(args=['ansible-galaxy', 'role'] + args).run() |
| 27 |
finally: |
| 28 |
co.GlobalCLIArgs._Singleton__instance = orig |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture(autouse=True) |
| 32 |
def reset_cli_args(): |
| 33 |
co.GlobalCLIArgs._Singleton__instance = None |
| 34 |
yield |
| 35 |
co.GlobalCLIArgs._Singleton__instance = None |
| 36 |
|
| 37 |
|
| 38 |
@pytest.fixture(autouse=True) |
| 39 |
def galaxy_server(): |
| 40 |
context.CLIARGS._store = {'ignore_certs': False} |
| 41 |
galaxy_api = api.GalaxyAPI(None, 'test_server', 'https://galaxy.ansible.com') |
| 42 |
return galaxy_api |
| 43 |
|
| 44 |
|
| 45 |
@pytest.fixture(autouse=True) |
| 46 |
def init_role_dir(tmp_path_factory): |
| 47 |
test_dir = to_text(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Roles Input')) |
| 48 |
namespace = 'ansible_namespace' |
| 49 |
role = 'role' |
| 50 |
skeleton_path = os.path.join(os.path.dirname(os.path.split(__file__)[0]), 'cli', 'test_data', 'role_skeleton') |
| 51 |
call_galaxy_cli(['init', '%s.%s' % (namespace, role), '-c', '--init-path', test_dir, '--role-skeleton', skeleton_path]) |
| 52 |
|
| 53 |
|
| 54 |
def mock_NamedTemporaryFile(mocker, **args): |
| 55 |
mock_ntf = mocker.MagicMock() |
| 56 |
mock_ntf.write = mocker.MagicMock() |
| 57 |
mock_ntf.close = mocker.MagicMock() |
| 58 |
mock_ntf.name = None |
| 59 |
return mock_ntf |
| 60 |
|
| 61 |
|
| 62 |
@pytest.fixture |
| 63 |
def init_mock_temp_file(mocker, monkeypatch): |
| 64 |
monkeypatch.setattr(tempfile, 'NamedTemporaryFile', functools.partial(mock_NamedTemporaryFile, mocker)) |
| 65 |
|
| 66 |
|
| 67 |
@pytest.fixture(autouse=True) |
| 68 |
def mock_role_download_api(mocker, monkeypatch): |
| 69 |
mock_role_api = mocker.MagicMock() |
| 70 |
mock_role_api.side_effect = [ |
| 71 |
StringIO(u''), |
| 72 |
] |
| 73 |
monkeypatch.setattr(role, 'open_url', mock_role_api) |
| 74 |
return mock_role_api |
| 75 |
|
| 76 |
|
| 77 |
def test_role_download_github(init_mock_temp_file, mocker, galaxy_server, mock_role_download_api, monkeypatch): |
| 78 |
mock_api = mocker.MagicMock() |
| 79 |
mock_api.side_effect = [ |
| 80 |
StringIO(u'{"available_versions":{"v1":"v1/"}}'), |
| 81 |
StringIO(u'{"results":[{"id":"123","github_user":"test_owner","github_repo": "test_role"}]}'), |
| 82 |
StringIO(u'{"results":[{"name": "0.0.1"},{"name": "0.0.2"}]}'), |
| 83 |
] |
| 84 |
monkeypatch.setattr(api, 'open_url', mock_api) |
| 85 |
|
| 86 |
role.GalaxyRole(Galaxy(), galaxy_server, 'test_owner.test_role', version="0.0.1").install() |
| 87 |
|
| 88 |
assert mock_role_download_api.call_count == 1 |
| 89 |
assert mock_role_download_api.mock_calls[0][1][0] == 'https://github.com/test_owner/test_role/archive/0.0.1.tar.gz' |
| 90 |
|
| 91 |
|
| 92 |
def test_role_download_github_default_version(init_mock_temp_file, mocker, galaxy_server, mock_role_download_api, monkeypatch): |
| 93 |
mock_api = mocker.MagicMock() |
| 94 |
mock_api.side_effect = [ |
| 95 |
StringIO(u'{"available_versions":{"v1":"v1/"}}'), |
| 96 |
StringIO(u'{"results":[{"id":"123","github_user":"test_owner","github_repo": "test_role"}]}'), |
| 97 |
StringIO(u'{"results":[{"name": "0.0.1"},{"name": "0.0.2"}]}'), |
| 98 |
] |
| 99 |
monkeypatch.setattr(api, 'open_url', mock_api) |
| 100 |
|
| 101 |
role.GalaxyRole(Galaxy(), galaxy_server, 'test_owner.test_role').install() |
| 102 |
|
| 103 |
assert mock_role_download_api.call_count == 1 |
| 104 |
assert mock_role_download_api.mock_calls[0][1][0] == 'https://github.com/test_owner/test_role/archive/0.0.2.tar.gz' |
| 105 |
|
| 106 |
|
| 107 |
def test_role_download_github_no_download_url_for_version(init_mock_temp_file, mocker, galaxy_server, mock_role_download_api, monkeypatch): |
| 108 |
mock_api = mocker.MagicMock() |
| 109 |
mock_api.side_effect = [ |
| 110 |
StringIO(u'{"available_versions":{"v1":"v1/"}}'), |
| 111 |
StringIO(u'{"results":[{"id":"123","github_user":"test_owner","github_repo": "test_role"}]}'), |
| 112 |
StringIO(u'{"results":[{"name": "0.0.1"},{"name": "0.0.2","download_url":"http://localhost:8080/test_owner/test_role/0.0.2.tar.gz"}]}'), |
| 113 |
] |
| 114 |
monkeypatch.setattr(api, 'open_url', mock_api) |
| 115 |
|
| 116 |
role.GalaxyRole(Galaxy(), galaxy_server, 'test_owner.test_role', version="0.0.1").install() |
| 117 |
|
| 118 |
assert mock_role_download_api.call_count == 1 |
| 119 |
assert mock_role_download_api.mock_calls[0][1][0] == 'https://github.com/test_owner/test_role/archive/0.0.1.tar.gz' |
| 120 |
|
| 121 |
|
| 122 |
@pytest.mark.parametrize( |
| 123 |
'state,rc', |
| 124 |
[('SUCCESS', 0), ('FAILED', 1),] |
| 125 |
) |
| 126 |
def test_role_import(state, rc, mocker, galaxy_server, monkeypatch): |
| 127 |
responses = [ |
| 128 |
{"available_versions": {"v1": "v1/"}}, |
| 129 |
{"results": [{'id': 12345, 'github_user': 'user', 'github_repo': 'role', 'github_reference': None, 'summary_fields': {'role': {'name': 'role'}}}]}, |
| 130 |
{"results": [{'state': 'WAITING', 'id': 12345, 'summary_fields': {'task_messages': []}}]}, |
| 131 |
{"results": [{'state': state, 'id': 12345, 'summary_fields': {'task_messages': []}}]}, |
| 132 |
] |
| 133 |
mock_api = mocker.MagicMock(side_effect=[StringIO(json.dumps(rsp)) for rsp in responses]) |
| 134 |
monkeypatch.setattr(api, 'open_url', mock_api) |
| 135 |
monkeypatch.setattr(GalaxyCLI, '_task_check_delay_sec', 0.1) |
| 136 |
assert call_galaxy_cli(['import', 'user', 'role']) == rc |
| 137 |
|
| 138 |
|
| 139 |
def test_role_download_url(init_mock_temp_file, mocker, galaxy_server, mock_role_download_api, monkeypatch): |
| 140 |
mock_api = mocker.MagicMock() |
| 141 |
mock_api.side_effect = [ |
| 142 |
StringIO(u'{"available_versions":{"v1":"v1/"}}'), |
| 143 |
StringIO(u'{"results":[{"id":"123","github_user":"test_owner","github_repo": "test_role"}]}'), |
| 144 |
StringIO(u'{"results":[{"name": "0.0.1","download_url":"http://localhost:8080/test_owner/test_role/0.0.1.tar.gz"},' |
| 145 |
u'{"name": "0.0.2","download_url":"http://localhost:8080/test_owner/test_role/0.0.2.tar.gz"}]}'), |
| 146 |
] |
| 147 |
monkeypatch.setattr(api, 'open_url', mock_api) |
| 148 |
|
| 149 |
role.GalaxyRole(Galaxy(), galaxy_server, 'test_owner.test_role', version="0.0.1").install() |
| 150 |
|
| 151 |
assert mock_role_download_api.call_count == 1 |
| 152 |
assert mock_role_download_api.mock_calls[0][1][0] == 'http://localhost:8080/test_owner/test_role/0.0.1.tar.gz' |
| 153 |
|
| 154 |
|
| 155 |
def test_role_download_url_default_version(init_mock_temp_file, mocker, galaxy_server, mock_role_download_api, monkeypatch): |
| 156 |
mock_api = mocker.MagicMock() |
| 157 |
mock_api.side_effect = [ |
| 158 |
StringIO(u'{"available_versions":{"v1":"v1/"}}'), |
| 159 |
StringIO(u'{"results":[{"id":"123","github_user":"test_owner","github_repo": "test_role"}]}'), |
| 160 |
StringIO(u'{"results":[{"name": "0.0.1","download_url":"http://localhost:8080/test_owner/test_role/0.0.1.tar.gz"},' |
| 161 |
u'{"name": "0.0.2","download_url":"http://localhost:8080/test_owner/test_role/0.0.2.tar.gz"}]}'), |
| 162 |
] |
| 163 |
monkeypatch.setattr(api, 'open_url', mock_api) |
| 164 |
|
| 165 |
role.GalaxyRole(Galaxy(), galaxy_server, 'test_owner.test_role').install() |
| 166 |
|
| 167 |
assert mock_role_download_api.call_count == 1 |
| 168 |
assert mock_role_download_api.mock_calls[0][1][0] == 'http://localhost:8080/test_owner/test_role/0.0.2.tar.gz' |