diff --git a/changelogs/fragments/82175-fix-ansible-galaxy-role-import-rc.yml b/changelogs/fragments/82175-fix-ansible-galaxy-role-import-rc.yml new file mode 100644 index 00000000000..ec225b65cbb --- /dev/null +++ b/changelogs/fragments/82175-fix-ansible-galaxy-role-import-rc.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-galaxy role import - exit with 1 when the import fails (https://github.com/ansible/ansible/issues/82175). diff --git a/lib/ansible/cli/galaxy.py b/lib/ansible/cli/galaxy.py index 07e652a2e95..7803bdd9a3f 100755 --- a/lib/ansible/cli/galaxy.py +++ b/lib/ansible/cli/galaxy.py @@ -1791,6 +1791,7 @@ class GalaxyCLI(CLI): github_user = to_text(context.CLIARGS['github_user'], errors='surrogate_or_strict') github_repo = to_text(context.CLIARGS['github_repo'], errors='surrogate_or_strict') + rc = 0 if context.CLIARGS['check_status']: task = self.api.get_import_task(github_user=github_user, github_repo=github_repo) else: @@ -1808,7 +1809,7 @@ class GalaxyCLI(CLI): display.display('%s.%s' % (t['summary_fields']['role']['namespace'], t['summary_fields']['role']['name']), color=C.COLOR_CHANGED) display.display(u'\nTo properly namespace this role, remove each of the above and re-import %s/%s from scratch' % (github_user, github_repo), color=C.COLOR_CHANGED) - return 0 + return rc # found a single role as expected display.display("Successfully submitted import request %d" % task[0]['id']) if not context.CLIARGS['wait']: @@ -1825,12 +1826,13 @@ class GalaxyCLI(CLI): if msg['id'] not in msg_list: display.display(msg['message_text'], color=colors[msg['message_type']]) msg_list.append(msg['id']) - if task[0]['state'] in ['SUCCESS', 'FAILED']: + if (state := task[0]['state']) in ['SUCCESS', 'FAILED']: + rc = ['SUCCESS', 'FAILED'].index(state) finished = True else: time.sleep(10) - return 0 + return rc def execute_setup(self): """ Setup an integration from Github or Travis for Ansible Galaxy roles""" diff --git a/test/units/galaxy/test_role_install.py b/test/units/galaxy/test_role_install.py index 404c8e91e01..25dff80cd63 100644 --- a/test/units/galaxy/test_role_install.py +++ b/test/units/galaxy/test_role_install.py @@ -5,6 +5,7 @@ from __future__ import annotations +import json import os import functools import pytest @@ -22,7 +23,7 @@ def call_galaxy_cli(args): orig = co.GlobalCLIArgs._Singleton__instance co.GlobalCLIArgs._Singleton__instance = None try: - GalaxyCLI(args=['ansible-galaxy', 'role'] + args).run() + return GalaxyCLI(args=['ansible-galaxy', 'role'] + args).run() finally: co.GlobalCLIArgs._Singleton__instance = orig @@ -118,6 +119,22 @@ def test_role_download_github_no_download_url_for_version(init_mock_temp_file, m assert mock_role_download_api.mock_calls[0][1][0] == 'https://github.com/test_owner/test_role/archive/0.0.1.tar.gz' +@pytest.mark.parametrize( + 'state,rc', + [('SUCCESS', 0), ('FAILED', 1),] +) +def test_role_import(state, rc, mocker, galaxy_server, monkeypatch): + responses = [ + {"available_versions": {"v1": "v1/"}}, + {"results": [{'id': 12345, 'github_user': 'user', 'github_repo': 'role', 'github_reference': None, 'summary_fields': {'role': {'name': 'role'}}}]}, + {"results": [{'state': 'WAITING', 'id': 12345, 'summary_fields': {'task_messages': []}}]}, + {"results": [{'state': state, 'id': 12345, 'summary_fields': {'task_messages': []}}]}, + ] + mock_api = mocker.MagicMock(side_effect=[StringIO(json.dumps(rsp)) for rsp in responses]) + monkeypatch.setattr(api, 'open_url', mock_api) + assert call_galaxy_cli(['import', 'user', 'role']) == rc + + def test_role_download_url(init_mock_temp_file, mocker, galaxy_server, mock_role_download_api, monkeypatch): mock_api = mocker.MagicMock() mock_api.side_effect = [