summaryrefslogtreecommitdiff
path: root/test/units
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2019-10-16 15:23:12 -0700
committerGitHub <noreply@github.com>2019-10-16 15:23:12 -0700
commit4cad7e479c922cd973015fd1cf4bd5e5890158de (patch)
treef75942c7c67e0673681e0da16a8de56a7fb8e537 /test/units
parent82ee341fe03e2bdb3c3d14cda0953469b80750e7 (diff)
downloadansible-4cad7e479c922cd973015fd1cf4bd5e5890158de.tar.gz
Galaxy publish fix (#63580)
* Handle galaxy v2/v3 API diffs for artifact publish response For publishing a collection artifact (POST /v3/collections/artifacts/), the response format is different between v2 and v3. For v2 galaxy, the 'task' url returned is a full url with scheme: {"task": "https://galaxy-dev.ansible.com/api/v2/collection-imports/35573/"} For v3 galaxy, the task url is relative: {"task": "/api/automation-hub/v3/imports/collections/838d1308-a8f4-402c-95cb-7823f3806cd8/"} So check which API we are using and update the task url approriately. * Use full url for all wait_for_import messages Update unit tests to parameterize the expected responses and urls. * update explanatory comment * Rename n_url to full_url. * Fix issue with overwrite of the complete path * Fixes overwrite of the complete path in case there's extra path stored in self.api_sever * Normalizes the input to the wait_import_task function so it receives the same value on both v2 and v3 Builds on #63523 * Update unittests for new call signature * Add changelog for ansible-galaxy publish API fixes.
Diffstat (limited to 'test/units')
-rw-r--r--test/units/galaxy/test_api.py95
-rw-r--r--test/units/galaxy/test_collection.py2
2 files changed, 56 insertions, 41 deletions
diff --git a/test/units/galaxy/test_api.py b/test/units/galaxy/test_api.py
index d557178696..8c69489e03 100644
--- a/test/units/galaxy/test_api.py
+++ b/test/units/galaxy/test_api.py
@@ -332,13 +332,16 @@ def test_publish_failure(api_version, collection_url, response, expected, collec
api.publish_collection(collection_artifact)
-@pytest.mark.parametrize('api_version, token_type, token_ins', [
- ('v2', 'Token', GalaxyToken('my token')),
- ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/')),
+@pytest.mark.parametrize('api_version, token_type, token_ins, import_uri, full_import_uri', [
+ ('v2', 'Token', GalaxyToken('my token'),
+ '1234',
+ 'https://galaxy.server.com/api/v2/collection-imports/1234'),
+ ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/'),
+ '1234',
+ 'https://galaxy.server.com/api/automation-hub/v3/imports/collections/1234/'),
])
-def test_wait_import_task(api_version, token_type, token_ins, monkeypatch):
+def test_wait_import_task(api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch):
api = get_test_galaxy_api('https://galaxy.server.com/api/', api_version, token_ins=token_ins)
- import_uri = 'https://galaxy.server.com/api/%s/task/1234' % api_version
if token_ins:
mock_token_get = MagicMock()
@@ -355,20 +358,23 @@ def test_wait_import_task(api_version, token_type, token_ins, monkeypatch):
api.wait_import_task(import_uri)
assert mock_open.call_count == 1
- assert mock_open.mock_calls[0][1][0] == import_uri
+ assert mock_open.mock_calls[0][1][0] == full_import_uri
assert mock_open.mock_calls[0][2]['headers']['Authorization'] == '%s my token' % token_type
assert mock_display.call_count == 1
- assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % import_uri
+ assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % full_import_uri
-@pytest.mark.parametrize('api_version, token_type, token_ins', [
- ('v2', 'Token', GalaxyToken('my token')),
- ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/')),
+@pytest.mark.parametrize('api_version, token_type, token_ins, import_uri, full_import_uri', [
+ ('v2', 'Token', GalaxyToken('my token'),
+ '1234',
+ 'https://galaxy.server.com/api/v2/collection-imports/1234'),
+ ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/'),
+ '1234',
+ 'https://galaxy.server.com/api/automation-hub/v3/imports/collections/1234/'),
])
-def test_wait_import_task_multiple_requests(api_version, token_type, token_ins, monkeypatch):
+def test_wait_import_task_multiple_requests(api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch):
api = get_test_galaxy_api('https://galaxy.server.com/api/', api_version, token_ins=token_ins)
- import_uri = 'https://galaxy.server.com/api/%s/task/1234' % api_version
if token_ins:
mock_token_get = MagicMock()
@@ -393,26 +399,29 @@ def test_wait_import_task_multiple_requests(api_version, token_type, token_ins,
api.wait_import_task(import_uri)
assert mock_open.call_count == 2
- assert mock_open.mock_calls[0][1][0] == import_uri
+ assert mock_open.mock_calls[0][1][0] == full_import_uri
assert mock_open.mock_calls[0][2]['headers']['Authorization'] == '%s my token' % token_type
- assert mock_open.mock_calls[1][1][0] == import_uri
+ assert mock_open.mock_calls[1][1][0] == full_import_uri
assert mock_open.mock_calls[1][2]['headers']['Authorization'] == '%s my token' % token_type
assert mock_display.call_count == 1
- assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % import_uri
+ assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % full_import_uri
assert mock_vvv.call_count == 1
assert mock_vvv.mock_calls[0][1][0] == \
'Galaxy import process has a status of test, wait 2 seconds before trying again'
-@pytest.mark.parametrize('api_version, token_type, token_ins', [
- ('v2', 'Token', GalaxyToken('my token')),
- ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/')),
+@pytest.mark.parametrize('api_version, token_type, token_ins, import_uri, full_import_uri,', [
+ ('v2', 'Token', GalaxyToken('my token'),
+ '1234',
+ 'https://galaxy.server.com/api/v2/collection-imports/1234'),
+ ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/'),
+ '1234',
+ 'https://galaxy.server.com/api/automation-hub/v3/imports/collections/1234/'),
])
-def test_wait_import_task_with_failure(api_version, token_type, token_ins, monkeypatch):
+def test_wait_import_task_with_failure(api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch):
api = get_test_galaxy_api('https://galaxy.server.com/api/', api_version, token_ins=token_ins)
- import_uri = 'https://galaxy.server.com/api/%s/task/1234' % api_version
if token_ins:
mock_token_get = MagicMock()
@@ -464,11 +473,11 @@ def test_wait_import_task_with_failure(api_version, token_type, token_ins, monke
api.wait_import_task(import_uri)
assert mock_open.call_count == 1
- assert mock_open.mock_calls[0][1][0] == import_uri
+ assert mock_open.mock_calls[0][1][0] == full_import_uri
assert mock_open.mock_calls[0][2]['headers']['Authorization'] == '%s my token' % token_type
assert mock_display.call_count == 1
- assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % import_uri
+ assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % full_import_uri
assert mock_vvv.call_count == 1
assert mock_vvv.mock_calls[0][1][0] == u'Galaxy import message: info - Somé info'
@@ -480,13 +489,16 @@ def test_wait_import_task_with_failure(api_version, token_type, token_ins, monke
assert mock_err.mock_calls[0][1][0] == u'Galaxy import error message: Somé error'
-@pytest.mark.parametrize('api_version, token_type, token_ins', [
- ('v2', 'Token', GalaxyToken('my_token')),
- ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/')),
+@pytest.mark.parametrize('api_version, token_type, token_ins, import_uri, full_import_uri', [
+ ('v2', 'Token', GalaxyToken('my_token'),
+ '1234',
+ 'https://galaxy.server.com/api/v2/collection-imports/1234'),
+ ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/'),
+ '1234',
+ 'https://galaxy.server.com/api/automation-hub/v3/imports/collections/1234/'),
])
-def test_wait_import_task_with_failure_no_error(api_version, token_type, token_ins, monkeypatch):
+def test_wait_import_task_with_failure_no_error(api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch):
api = get_test_galaxy_api('https://galaxy.server.com/api/', api_version, token_ins=token_ins)
- import_uri = 'https://galaxy.server.com/api/%s/task/1234' % api_version
if token_ins:
mock_token_get = MagicMock()
@@ -529,16 +541,16 @@ def test_wait_import_task_with_failure_no_error(api_version, token_type, token_i
mock_err = MagicMock()
monkeypatch.setattr(Display, 'error', mock_err)
- expected = 'Galaxy import process failed: Unknown error, see %s for more details (Code: UNKNOWN)' % import_uri
- with pytest.raises(AnsibleError, match=re.escape(expected)):
+ expected = 'Galaxy import process failed: Unknown error, see %s for more details \\(Code: UNKNOWN\\)' % full_import_uri
+ with pytest.raises(AnsibleError, match=expected):
api.wait_import_task(import_uri)
assert mock_open.call_count == 1
- assert mock_open.mock_calls[0][1][0] == import_uri
+ assert mock_open.mock_calls[0][1][0] == full_import_uri
assert mock_open.mock_calls[0][2]['headers']['Authorization'] == '%s my token' % token_type
assert mock_display.call_count == 1
- assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % import_uri
+ assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % full_import_uri
assert mock_vvv.call_count == 1
assert mock_vvv.mock_calls[0][1][0] == u'Galaxy import message: info - Somé info'
@@ -550,13 +562,16 @@ def test_wait_import_task_with_failure_no_error(api_version, token_type, token_i
assert mock_err.mock_calls[0][1][0] == u'Galaxy import error message: Somé error'
-@pytest.mark.parametrize('api_version, token_type, token_ins', [
- ('v2', 'Token', GalaxyToken('my token')),
- ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/')),
+@pytest.mark.parametrize('api_version, token_type, token_ins, import_uri, full_import_uri', [
+ ('v2', 'Token', GalaxyToken('my token'),
+ '1234',
+ 'https://galaxy.server.com/api/v2/collection-imports/1234'),
+ ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/'),
+ '1234',
+ 'https://galaxy.server.com/api/automation-hub/v3/imports/collections/1234/'),
])
-def test_wait_import_task_timeout(api_version, token_type, token_ins, monkeypatch):
+def test_wait_import_task_timeout(api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch):
api = get_test_galaxy_api('https://galaxy.server.com/api/', api_version, token_ins=token_ins)
- import_uri = 'https://galaxy.server.com/api/%s/task/1234' % api_version
if token_ins:
mock_token_get = MagicMock()
@@ -578,18 +593,18 @@ def test_wait_import_task_timeout(api_version, token_type, token_ins, monkeypatc
monkeypatch.setattr(time, 'sleep', MagicMock())
- expected = "Timeout while waiting for the Galaxy import process to finish, check progress at '%s'" % import_uri
+ expected = "Timeout while waiting for the Galaxy import process to finish, check progress at '%s'" % full_import_uri
with pytest.raises(AnsibleError, match=expected):
api.wait_import_task(import_uri, 1)
assert mock_open.call_count > 1
- assert mock_open.mock_calls[0][1][0] == import_uri
+ assert mock_open.mock_calls[0][1][0] == full_import_uri
assert mock_open.mock_calls[0][2]['headers']['Authorization'] == '%s my token' % token_type
- assert mock_open.mock_calls[1][1][0] == import_uri
+ assert mock_open.mock_calls[1][1][0] == full_import_uri
assert mock_open.mock_calls[1][2]['headers']['Authorization'] == '%s my token' % token_type
assert mock_display.call_count == 1
- assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % import_uri
+ assert mock_display.mock_calls[0][1][0] == 'Waiting until Galaxy import task %s has completed' % full_import_uri
# expected_wait_msg = 'Galaxy import process has a status of waiting, wait {0} seconds before trying again'
assert mock_vvv.call_count > 9 # 1st is opening Galaxy token file.
diff --git a/test/units/galaxy/test_collection.py b/test/units/galaxy/test_collection.py
index c86a524a09..18df8bbdcd 100644
--- a/test/units/galaxy/test_collection.py
+++ b/test/units/galaxy/test_collection.py
@@ -439,7 +439,7 @@ def test_publish_with_wait(galaxy_server, collection_artifact, monkeypatch):
assert mock_publish.mock_calls[0][1][0] == artifact_path
assert mock_wait.call_count == 1
- assert mock_wait.mock_calls[0][1][0] == fake_import_uri
+ assert mock_wait.mock_calls[0][1][0] == '1234'
assert mock_display.mock_calls[0][1][0] == "Collection has been published to the Galaxy server test_server %s" \
% galaxy_server.api_server