diff options
author | Abhijeet Kasurde <akasurde@redhat.com> | 2023-08-03 22:59:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-03 22:59:59 +0200 |
commit | 95fdd555b38f4fa885f46454675b293e8021cd85 (patch) | |
tree | 9c5d1d375b52e075d7e1cf2a27c214ebf25ebbee | |
parent | Overhaul package-data sanity test (#81427) (diff) | |
download | ansible-95fdd555b38f4fa885f46454675b293e8021cd85.tar.xz ansible-95fdd555b38f4fa885f46454675b293e8021cd85.zip |
galaxy: Cross check the collection type (#81423)
* User-provided collection type might differ from collection
source. Cross-check the type before proceeding
Fixes: #79463
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r-- | changelogs/fragments/galaxy_check_type.yml | 3 | ||||
-rw-r--r-- | lib/ansible/galaxy/collection/concrete_artifact_manager.py | 7 | ||||
-rw-r--r-- | test/units/galaxy/test_collection_install.py | 21 |
3 files changed, 30 insertions, 1 deletions
diff --git a/changelogs/fragments/galaxy_check_type.yml b/changelogs/fragments/galaxy_check_type.yml new file mode 100644 index 0000000000..fc2603c592 --- /dev/null +++ b/changelogs/fragments/galaxy_check_type.yml @@ -0,0 +1,3 @@ +--- +bugfixes: +- galaxy - cross check the collection type and collection source (https://github.com/ansible/ansible/issues/79463). diff --git a/lib/ansible/galaxy/collection/concrete_artifact_manager.py b/lib/ansible/galaxy/collection/concrete_artifact_manager.py index fe1a81b15a..d251127dec 100644 --- a/lib/ansible/galaxy/collection/concrete_artifact_manager.py +++ b/lib/ansible/galaxy/collection/concrete_artifact_manager.py @@ -140,7 +140,7 @@ class ConcreteArtifactsManager: url, sha256_hash, token = self._galaxy_collection_cache[collection] except KeyError as key_err: raise RuntimeError( - 'The is no known source for {coll!s}'. + 'There is no known source for {coll!s}'. format(coll=collection), ) from key_err @@ -702,6 +702,11 @@ def _get_meta_from_installed_dir( def _get_meta_from_tar( b_path, # type: bytes ): # type: (...) -> dict[str, t.Union[str, list[str], dict[str, str], None, t.Type[Sentinel]]] + if not os.path.exists(b_path): + raise AnsibleError( + f"Unable to find collection artifact file at '{to_native(b_path)}'." + ) + if not tarfile.is_tarfile(b_path): raise AnsibleError( "Collection artifact at '{path!s}' is not a valid tar file.". diff --git a/test/units/galaxy/test_collection_install.py b/test/units/galaxy/test_collection_install.py index 9f599f7d9b..a61ae40683 100644 --- a/test/units/galaxy/test_collection_install.py +++ b/test/units/galaxy/test_collection_install.py @@ -298,6 +298,27 @@ def test_build_requirement_from_tar(collection_artifact): assert actual.ver == u'0.1.0' +def test_build_requirement_from_tar_url(tmp_path_factory): + test_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections Input')) + concrete_artifact_cm = collection.concrete_artifact_manager.ConcreteArtifactsManager(test_dir, validate_certs=False) + test_url = 'https://example.com/org/repo/sample.tar.gz' + expected = fr"^Failed to download collection tar from '{to_text(test_url)}'" + + with pytest.raises(AnsibleError, match=expected): + Requirement.from_requirement_dict({'name': test_url, 'type': 'url'}, concrete_artifact_cm) + + +def test_build_requirement_from_tar_url_wrong_type(tmp_path_factory): + test_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections Input')) + concrete_artifact_cm = collection.concrete_artifact_manager.ConcreteArtifactsManager(test_dir, validate_certs=False) + test_url = 'https://example.com/org/repo/sample.tar.gz' + expected = fr"^Unable to find collection artifact file at '{to_text(test_url)}'\.$" + + with pytest.raises(AnsibleError, match=expected): + # Specified wrong collection type for http URL + Requirement.from_requirement_dict({'name': test_url, 'type': 'file'}, concrete_artifact_cm) + + def test_build_requirement_from_tar_fail_not_tar(tmp_path_factory): test_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections Input')) test_file = os.path.join(test_dir, b'fake.tar.gz') |