Galaxy - make versions list consistent across versions (#72932)
* Galaxy - make versions list consistent across versions * Fix up unit tests
This commit is contained in:
parent
51e00c5e86
commit
bc60d8ccda
4 changed files with 40 additions and 28 deletions
4
changelogs/fragments/ansible-galaxy-version-response.yml
Normal file
4
changelogs/fragments/ansible-galaxy-version-response.yml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
minor_changes:
|
||||||
|
- >-
|
||||||
|
ansible-galaxy - Ensure ``get_collection_versions`` returns an empty list when a collection does
|
||||||
|
not exist for consistency across API versions.
|
|
@ -751,9 +751,15 @@ class GalaxyAPI:
|
||||||
server_cache = self._cache.setdefault(get_cache_id(versions_url), {})
|
server_cache = self._cache.setdefault(get_cache_id(versions_url), {})
|
||||||
modified_cache = server_cache.setdefault('modified', {})
|
modified_cache = server_cache.setdefault('modified', {})
|
||||||
|
|
||||||
|
try:
|
||||||
modified_date = self.get_collection_metadata(namespace, name).modified_str
|
modified_date = self.get_collection_metadata(namespace, name).modified_str
|
||||||
cached_modified_date = modified_cache.get('%s.%s' % (namespace, name), None)
|
except GalaxyError as err:
|
||||||
|
if err.http_code != 404:
|
||||||
|
raise
|
||||||
|
# No collection found, return an empty list to keep things consistent with the various APIs
|
||||||
|
return []
|
||||||
|
|
||||||
|
cached_modified_date = modified_cache.get('%s.%s' % (namespace, name), None)
|
||||||
if cached_modified_date != modified_date:
|
if cached_modified_date != modified_date:
|
||||||
modified_cache['%s.%s' % (namespace, name)] = modified_date
|
modified_cache['%s.%s' % (namespace, name)] = modified_date
|
||||||
if versions_url_info.path in server_cache:
|
if versions_url_info.path in server_cache:
|
||||||
|
@ -763,7 +769,14 @@ class GalaxyAPI:
|
||||||
|
|
||||||
error_context_msg = 'Error when getting available collection versions for %s.%s from %s (%s)' \
|
error_context_msg = 'Error when getting available collection versions for %s.%s from %s (%s)' \
|
||||||
% (namespace, name, self.name, self.api_server)
|
% (namespace, name, self.name, self.api_server)
|
||||||
|
|
||||||
|
try:
|
||||||
data = self._call_galaxy(versions_url, error_context_msg=error_context_msg, cache=True)
|
data = self._call_galaxy(versions_url, error_context_msg=error_context_msg, cache=True)
|
||||||
|
except GalaxyError as err:
|
||||||
|
if err.http_code != 404:
|
||||||
|
raise
|
||||||
|
# v3 doesn't raise a 404 so we need to mimick the empty response from APIs that do.
|
||||||
|
return []
|
||||||
|
|
||||||
if 'data' in data:
|
if 'data' in data:
|
||||||
# v3 automation-hub is the only known API that uses `data`
|
# v3 automation-hub is the only known API that uses `data`
|
||||||
|
|
|
@ -511,7 +511,6 @@ class CollectionRequirement:
|
||||||
galaxy_meta = None
|
galaxy_meta = None
|
||||||
|
|
||||||
for api in apis:
|
for api in apis:
|
||||||
try:
|
|
||||||
if not (requirement == '*' or requirement.startswith('<') or requirement.startswith('>') or
|
if not (requirement == '*' or requirement.startswith('<') or requirement.startswith('>') or
|
||||||
requirement.startswith('!=')):
|
requirement.startswith('!=')):
|
||||||
# Exact requirement
|
# Exact requirement
|
||||||
|
@ -520,20 +519,18 @@ class CollectionRequirement:
|
||||||
if requirement.startswith('='):
|
if requirement.startswith('='):
|
||||||
requirement = requirement.lstrip('=')
|
requirement = requirement.lstrip('=')
|
||||||
|
|
||||||
|
try:
|
||||||
resp = api.get_collection_version_metadata(namespace, name, requirement)
|
resp = api.get_collection_version_metadata(namespace, name, requirement)
|
||||||
|
except GalaxyError as err:
|
||||||
|
if err.http_code != 404:
|
||||||
|
raise
|
||||||
|
versions = []
|
||||||
|
else:
|
||||||
galaxy_meta = resp
|
galaxy_meta = resp
|
||||||
versions = [resp.version]
|
versions = [resp.version]
|
||||||
else:
|
else:
|
||||||
versions = api.get_collection_versions(namespace, name)
|
versions = api.get_collection_versions(namespace, name)
|
||||||
except GalaxyError as err:
|
|
||||||
if err.http_code != 404:
|
|
||||||
raise
|
|
||||||
|
|
||||||
versions = []
|
|
||||||
|
|
||||||
# Automation Hub doesn't return a 404 but an empty version list so we check that to align both AH and
|
|
||||||
# Galaxy when the collection is not available on that server.
|
|
||||||
if not versions:
|
if not versions:
|
||||||
display.vvv("Collection '%s' is not available from server %s %s" % (collection, api.name,
|
display.vvv("Collection '%s' is not available from server %s %s" % (collection, api.name,
|
||||||
api.api_server))
|
api.api_server))
|
||||||
|
|
|
@ -403,10 +403,9 @@ def test_build_requirement_from_name_second_server(galaxy_server, monkeypatch):
|
||||||
|
|
||||||
broken_server = copy.copy(galaxy_server)
|
broken_server = copy.copy(galaxy_server)
|
||||||
broken_server.api_server = 'https://broken.com/'
|
broken_server.api_server = 'https://broken.com/'
|
||||||
mock_404 = MagicMock()
|
mock_version_list = MagicMock()
|
||||||
mock_404.side_effect = api.GalaxyError(urllib_error.HTTPError('https://galaxy.server.com', 404, 'msg', {},
|
mock_version_list.return_value = []
|
||||||
StringIO()), "custom msg")
|
monkeypatch.setattr(broken_server, 'get_collection_versions', mock_version_list)
|
||||||
monkeypatch.setattr(broken_server, 'get_collection_versions', mock_404)
|
|
||||||
|
|
||||||
actual = collection.CollectionRequirement.from_name('namespace.collection', [broken_server, galaxy_server],
|
actual = collection.CollectionRequirement.from_name('namespace.collection', [broken_server, galaxy_server],
|
||||||
'>1.0.1', False, True)
|
'>1.0.1', False, True)
|
||||||
|
@ -420,8 +419,8 @@ def test_build_requirement_from_name_second_server(galaxy_server, monkeypatch):
|
||||||
assert actual.latest_version == u'1.0.3'
|
assert actual.latest_version == u'1.0.3'
|
||||||
assert actual.dependencies == {}
|
assert actual.dependencies == {}
|
||||||
|
|
||||||
assert mock_404.call_count == 1
|
assert mock_version_list.call_count == 1
|
||||||
assert mock_404.mock_calls[0][1] == ('namespace', 'collection')
|
assert mock_version_list.mock_calls[0][1] == ('namespace', 'collection')
|
||||||
|
|
||||||
assert mock_get_versions.call_count == 1
|
assert mock_get_versions.call_count == 1
|
||||||
assert mock_get_versions.mock_calls[0][1] == ('namespace', 'collection')
|
assert mock_get_versions.mock_calls[0][1] == ('namespace', 'collection')
|
||||||
|
@ -429,8 +428,7 @@ def test_build_requirement_from_name_second_server(galaxy_server, monkeypatch):
|
||||||
|
|
||||||
def test_build_requirement_from_name_missing(galaxy_server, monkeypatch):
|
def test_build_requirement_from_name_missing(galaxy_server, monkeypatch):
|
||||||
mock_open = MagicMock()
|
mock_open = MagicMock()
|
||||||
mock_open.side_effect = api.GalaxyError(urllib_error.HTTPError('https://galaxy.server.com', 404, 'msg', {},
|
mock_open.return_value = []
|
||||||
StringIO()), "")
|
|
||||||
|
|
||||||
monkeypatch.setattr(galaxy_server, 'get_collection_versions', mock_open)
|
monkeypatch.setattr(galaxy_server, 'get_collection_versions', mock_open)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue