ansible-galaxy - fix traceback error for invalid req file (#81917)

Provide a better error message when encountering a YAML requirements file that is not a dictionary or list.

Fixes: #81901
pull/81915/head
Jordan Borean 12 months ago committed by GitHub
parent 976067c15f
commit 8a5ccc9d63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- ansible-galaxy - Provide a better error message when using a requirements file with an invalid format - https://github.com/ansible/ansible/issues/81901

@ -834,7 +834,7 @@ class GalaxyCLI(CLI):
for role_req in file_requirements:
requirements['roles'] += parse_role_req(role_req)
else:
elif isinstance(file_requirements, dict):
# Newer format with a collections and/or roles key
extra_keys = set(file_requirements.keys()).difference(set(['roles', 'collections']))
if extra_keys:
@ -853,6 +853,9 @@ class GalaxyCLI(CLI):
for collection_req in file_requirements.get('collections') or []
]
else:
raise AnsibleError(f"Expecting requirements yaml to be a list or dictionary but got {type(file_requirements).__name__}")
return requirements
def _init_coll_req_dict(self, coll_req):

@ -769,6 +769,20 @@ def test_collection_install_with_names(collection_install):
assert mock_install.call_args[0][6] is False # force_deps
def test_collection_install_with_invalid_requirements_format(collection_install):
output_dir = collection_install[2]
requirements_file = os.path.join(output_dir, 'requirements.yml')
with open(requirements_file, 'wb') as req_obj:
req_obj.write(b'"invalid"')
galaxy_args = ['ansible-galaxy', 'collection', 'install', '--requirements-file', requirements_file,
'--collections-path', output_dir]
with pytest.raises(AnsibleError, match="Expecting requirements yaml to be a list or dictionary but got str"):
GalaxyCLI(args=galaxy_args).run()
def test_collection_install_with_requirements_file(collection_install):
mock_install, mock_warning, output_dir = collection_install

Loading…
Cancel
Save