diff options
author | Matt Clay <matt@mystile.com> | 2023-05-27 01:54:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-27 01:54:11 +0200 |
commit | 585b82838b667d2c0828d937eea67ec4659a1649 (patch) | |
tree | 83c5ff45e583f9081c2929a4f5b0de2b271b533e /test/units/galaxy | |
parent | ansible-test - local change detection without --fork-point (#79734) (diff) | |
download | ansible-585b82838b667d2c0828d937eea67ec4659a1649.tar.xz ansible-585b82838b667d2c0828d937eea67ec4659a1649.zip |
Improve code coverage of unit tests (#80904)
* Improve coverage of validate-modules unit tests
* Remove unused galaxy unit test code
* Fix galaxy unit test teardown logic
* Improve coverage of galaxy unit test code
* Improve coverage of galaxy unit tests
* Remove unused code in galaxy API tests
* Remove unused galaxy collection unit test code
* Improve coverage of galaxy collection unit tests
* Remove unused galaxy unit test code
Diffstat (limited to 'test/units/galaxy')
-rw-r--r-- | test/units/galaxy/test_api.py | 35 | ||||
-rw-r--r-- | test/units/galaxy/test_collection.py | 32 | ||||
-rw-r--r-- | test/units/galaxy/test_collection_install.py | 72 |
3 files changed, 20 insertions, 119 deletions
diff --git a/test/units/galaxy/test_api.py b/test/units/galaxy/test_api.py index 1eec9b68cd..b019f1aa16 100644 --- a/test/units/galaxy/test_api.py +++ b/test/units/galaxy/test_api.py @@ -463,10 +463,9 @@ def test_publish_failure(api_version, collection_url, response, expected, collec def test_wait_import_task(server_url, api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch): api = get_test_galaxy_api(server_url, api_version, token_ins=token_ins) - if token_ins: - mock_token_get = MagicMock() - mock_token_get.return_value = 'my token' - monkeypatch.setattr(token_ins, 'get', mock_token_get) + mock_token_get = MagicMock() + mock_token_get.return_value = 'my token' + monkeypatch.setattr(token_ins, 'get', mock_token_get) mock_open = MagicMock() mock_open.return_value = StringIO(u'{"state":"success","finished_at":"time"}') @@ -496,10 +495,9 @@ def test_wait_import_task(server_url, api_version, token_type, token_ins, import def test_wait_import_task_multiple_requests(server_url, api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch): api = get_test_galaxy_api(server_url, api_version, token_ins=token_ins) - if token_ins: - mock_token_get = MagicMock() - mock_token_get.return_value = 'my token' - monkeypatch.setattr(token_ins, 'get', mock_token_get) + mock_token_get = MagicMock() + mock_token_get.return_value = 'my token' + monkeypatch.setattr(token_ins, 'get', mock_token_get) mock_open = MagicMock() mock_open.side_effect = [ @@ -543,10 +541,9 @@ def test_wait_import_task_multiple_requests(server_url, api_version, token_type, def test_wait_import_task_with_failure(server_url, api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch): api = get_test_galaxy_api(server_url, api_version, token_ins=token_ins) - if token_ins: - mock_token_get = MagicMock() - mock_token_get.return_value = 'my token' - monkeypatch.setattr(token_ins, 'get', mock_token_get) + mock_token_get = MagicMock() + mock_token_get.return_value = 'my token' + monkeypatch.setattr(token_ins, 'get', mock_token_get) mock_open = MagicMock() mock_open.side_effect = [ @@ -620,10 +617,9 @@ def test_wait_import_task_with_failure(server_url, api_version, token_type, toke def test_wait_import_task_with_failure_no_error(server_url, api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch): api = get_test_galaxy_api(server_url, api_version, token_ins=token_ins) - if token_ins: - mock_token_get = MagicMock() - mock_token_get.return_value = 'my token' - monkeypatch.setattr(token_ins, 'get', mock_token_get) + mock_token_get = MagicMock() + mock_token_get.return_value = 'my token' + monkeypatch.setattr(token_ins, 'get', mock_token_get) mock_open = MagicMock() mock_open.side_effect = [ @@ -693,10 +689,9 @@ def test_wait_import_task_with_failure_no_error(server_url, api_version, token_t def test_wait_import_task_timeout(server_url, api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch): api = get_test_galaxy_api(server_url, api_version, token_ins=token_ins) - if token_ins: - mock_token_get = MagicMock() - mock_token_get.return_value = 'my token' - monkeypatch.setattr(token_ins, 'get', mock_token_get) + mock_token_get = MagicMock() + mock_token_get.return_value = 'my token' + monkeypatch.setattr(token_ins, 'get', mock_token_get) def return_response(*args, **kwargs): return StringIO(u'{"state":"waiting"}') diff --git a/test/units/galaxy/test_collection.py b/test/units/galaxy/test_collection.py index 420bea209c..6e8d50000a 100644 --- a/test/units/galaxy/test_collection.py +++ b/test/units/galaxy/test_collection.py @@ -171,28 +171,6 @@ def manifest_info(manifest_template): @pytest.fixture() -def files_manifest_info(): - return { - "files": [ - { - "name": ".", - "ftype": "dir", - "chksum_type": None, - "chksum_sha256": None, - "format": 1 - }, - { - "name": "README.md", - "ftype": "file", - "chksum_type": "sha256", - "chksum_sha256": "individual_file_checksum", - "format": 1 - } - ], - "format": 1} - - -@pytest.fixture() def manifest(manifest_info): b_data = to_bytes(json.dumps(manifest_info)) @@ -479,19 +457,19 @@ def test_build_with_existing_files_and_manifest(collection_input): with tarfile.open(output_artifact, mode='r') as actual: members = actual.getmembers() - manifest_file = next(m for m in members if m.path == "MANIFEST.json") + manifest_file = [m for m in members if m.path == "MANIFEST.json"][0] manifest_file_obj = actual.extractfile(manifest_file.name) manifest_file_text = manifest_file_obj.read() manifest_file_obj.close() assert manifest_file_text != b'{"collection_info": {"version": "6.6.6"}, "version": 1}' - json_file = next(m for m in members if m.path == "MANIFEST.json") + json_file = [m for m in members if m.path == "MANIFEST.json"][0] json_file_obj = actual.extractfile(json_file.name) json_file_text = json_file_obj.read() json_file_obj.close() assert json_file_text != b'{"files": [], "format": 1}' - sub_manifest_file = next(m for m in members if m.path == "plugins/MANIFEST.json") + sub_manifest_file = [m for m in members if m.path == "plugins/MANIFEST.json"][0] sub_manifest_file_obj = actual.extractfile(sub_manifest_file.name) sub_manifest_file_text = sub_manifest_file_obj.read() sub_manifest_file_obj.close() @@ -790,11 +768,11 @@ def test_build_with_symlink_inside_collection(collection_input): with tarfile.open(output_artifact, mode='r') as actual: members = actual.getmembers() - linked_folder = next(m for m in members if m.path == 'playbooks/roles/linked') + linked_folder = [m for m in members if m.path == 'playbooks/roles/linked'][0] assert linked_folder.type == tarfile.SYMTYPE assert linked_folder.linkname == '../../roles/linked' - linked_file = next(m for m in members if m.path == 'docs/README.md') + linked_file = [m for m in members if m.path == 'docs/README.md'][0] assert linked_file.type == tarfile.SYMTYPE assert linked_file.linkname == '../README.md' diff --git a/test/units/galaxy/test_collection_install.py b/test/units/galaxy/test_collection_install.py index 69948fbdd4..130934b442 100644 --- a/test/units/galaxy/test_collection_install.py +++ b/test/units/galaxy/test_collection_install.py @@ -52,78 +52,6 @@ def call_galaxy_cli(args): co.GlobalCLIArgs._Singleton__instance = orig -def artifact_json(namespace, name, version, dependencies, server): - json_str = json.dumps({ - 'artifact': { - 'filename': '%s-%s-%s.tar.gz' % (namespace, name, version), - 'sha256': '2d76f3b8c4bab1072848107fb3914c345f71a12a1722f25c08f5d3f51f4ab5fd', - 'size': 1234, - }, - 'download_url': '%s/download/%s-%s-%s.tar.gz' % (server, namespace, name, version), - 'metadata': { - 'namespace': namespace, - 'name': name, - 'dependencies': dependencies, - }, - 'version': version - }) - return to_text(json_str) - - -def artifact_versions_json(namespace, name, versions, galaxy_api, available_api_versions=None): - results = [] - available_api_versions = available_api_versions or {} - api_version = 'v2' - if 'v3' in available_api_versions: - api_version = 'v3' - for version in versions: - results.append({ - 'href': '%s/api/%s/%s/%s/versions/%s/' % (galaxy_api.api_server, api_version, namespace, name, version), - 'version': version, - }) - - if api_version == 'v2': - json_str = json.dumps({ - 'count': len(versions), - 'next': None, - 'previous': None, - 'results': results - }) - - if api_version == 'v3': - response = {'meta': {'count': len(versions)}, - 'data': results, - 'links': {'first': None, - 'last': None, - 'next': None, - 'previous': None}, - } - json_str = json.dumps(response) - return to_text(json_str) - - -def error_json(galaxy_api, errors_to_return=None, available_api_versions=None): - errors_to_return = errors_to_return or [] - available_api_versions = available_api_versions or {} - - response = {} - - api_version = 'v2' - if 'v3' in available_api_versions: - api_version = 'v3' - - if api_version == 'v2': - assert len(errors_to_return) <= 1 - if errors_to_return: - response = errors_to_return[0] - - if api_version == 'v3': - response['errors'] = errors_to_return - - json_str = json.dumps(response) - return to_text(json_str) - - @pytest.fixture(autouse='function') def reset_cli_args(): co.GlobalCLIArgs._Singleton__instance = None |