Skip to content

Commit

Permalink
ansible-galaxy - fix exit code for failed role import (#82193)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hertel committed Dec 12, 2023
1 parent 7f2ad7e commit fe81164
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ansible-galaxy role import - exit with 1 when the import fails (https://github.com/ansible/ansible/issues/82175).
8 changes: 5 additions & 3 deletions lib/ansible/cli/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,7 @@ def execute_import(self):
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:
Expand All @@ -1808,7 +1809,7 @@ def execute_import(self):
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']:
Expand All @@ -1825,12 +1826,13 @@ def execute_import(self):
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"""
Expand Down
19 changes: 18 additions & 1 deletion test/units/galaxy/test_role_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations


import json
import os
import functools
import pytest
Expand All @@ -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

Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit fe81164

Please sign in to comment.