From 90547d2a24f8ae5fe5f5bdf0f73ef46519bee717 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Fri, 13 Dec 2024 13:39:14 -0500 Subject: [PATCH] Add test for broken galaxy.yml detection --- src/molecule/config.py | 2 +- .../resources/broken-collection/CHANGELOG.md | 1 + .../resources/broken-collection/galaxy.yml | 33 +++++++++++++++++++ .../broken-collection/meta/runtime.yml | 1 + .../molecule/default/converge.yml | 7 ++++ .../molecule/default/molecule.yml | 11 +++++++ .../roles/get_rich/tasks/main.yml | 3 ++ tests/unit/test_config.py | 24 ++++++++++++++ 8 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/resources/broken-collection/CHANGELOG.md create mode 100644 tests/fixtures/resources/broken-collection/galaxy.yml create mode 100644 tests/fixtures/resources/broken-collection/meta/runtime.yml create mode 100644 tests/fixtures/resources/broken-collection/molecule/default/converge.yml create mode 100644 tests/fixtures/resources/broken-collection/molecule/default/molecule.yml create mode 100644 tests/fixtures/resources/broken-collection/roles/get_rich/tasks/main.yml diff --git a/src/molecule/config.py b/src/molecule/config.py index 1e56ebdf3a..05ffa4970f 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -279,7 +279,7 @@ def collection(self) -> CollectionData | None: important_keys = {"name", "namespace"} if missing_keys := important_keys.difference(galaxy_data.keys()): LOG.warning( - "The detected galaxy.yml file (%s) is incomplete, missing %s", + "The detected galaxy.yml file (%s) is invalid, missing mandatory field %s", galaxy_file, util.oxford_comma(missing_keys), ) diff --git a/tests/fixtures/resources/broken-collection/CHANGELOG.md b/tests/fixtures/resources/broken-collection/CHANGELOG.md new file mode 100644 index 0000000000..825c32f0d0 --- /dev/null +++ b/tests/fixtures/resources/broken-collection/CHANGELOG.md @@ -0,0 +1 @@ +# Changelog diff --git a/tests/fixtures/resources/broken-collection/galaxy.yml b/tests/fixtures/resources/broken-collection/galaxy.yml new file mode 100644 index 0000000000..4bbf2064e2 --- /dev/null +++ b/tests/fixtures/resources/broken-collection/galaxy.yml @@ -0,0 +1,33 @@ +name: goodies +name_space: acme +version: 1.0.0 +readme: README.md +authors: + - Red Hat +description: Acme Goodies Collection +build_ignore: + - "*.egg-info" + - .DS_Store + - .eggs + - .gitignore + - .mypy_cache + - .pytest_cache + - .stestr + - .stestr.conf + - .tox + - .vscode + - MANIFEST.in + - build + - dist + - doc + - report.html + - setup.cfg + - setup.py + - "tests/unit/*.*" + - README.rst + - tox.ini + +repository: https://opendev.org/openstack/tripleo-repos +license_file: LICENSE +tags: + - tools diff --git a/tests/fixtures/resources/broken-collection/meta/runtime.yml b/tests/fixtures/resources/broken-collection/meta/runtime.yml new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/tests/fixtures/resources/broken-collection/meta/runtime.yml @@ -0,0 +1 @@ +{} diff --git a/tests/fixtures/resources/broken-collection/molecule/default/converge.yml b/tests/fixtures/resources/broken-collection/molecule/default/converge.yml new file mode 100644 index 0000000000..73f7cfb3c3 --- /dev/null +++ b/tests/fixtures/resources/broken-collection/molecule/default/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: localhost + tasks: + - name: "Include sample role from current collection" + ansible.builtin.include_role: + name: acme.goodies.get_rich diff --git a/tests/fixtures/resources/broken-collection/molecule/default/molecule.yml b/tests/fixtures/resources/broken-collection/molecule/default/molecule.yml new file mode 100644 index 0000000000..1cec4936a1 --- /dev/null +++ b/tests/fixtures/resources/broken-collection/molecule/default/molecule.yml @@ -0,0 +1,11 @@ +--- +dependency: + name: galaxy +driver: + name: default +platforms: + - name: instance +provisioner: + name: ansible +verifier: + name: ansible diff --git a/tests/fixtures/resources/broken-collection/roles/get_rich/tasks/main.yml b/tests/fixtures/resources/broken-collection/roles/get_rich/tasks/main.yml new file mode 100644 index 0000000000..cbab44da7d --- /dev/null +++ b/tests/fixtures/resources/broken-collection/roles/get_rich/tasks/main.yml @@ -0,0 +1,3 @@ +- name: Some task inside foo.bar collection + ansible.builtin.debug: + msg: "hello world!" diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index bddde78e12..9024e903e5 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -139,6 +139,30 @@ def test_collection_property( assert modified_instance.collection["namespace"] == "acme" +def test_collection_property_broken_collection( + caplog: pytest.LogCaptureFixture, + config_instance: config.Config, + resources_folder_path: Path, +) -> None: + """Test collection property with a malformed galaxy.yml. + + Args: + caplog: pytest log capture fixture. + config_instance: Instance of Config. + resources_folder_path: Path to resources directory holding a valid collection. + """ + modified_instance = copy.copy(config_instance) + + # Alter config_instance to start at path of a collection + collection_path = resources_folder_path / "broken-collection" + modified_instance.project_directory = str(collection_path) + + assert modified_instance.collection is None + + msg = "missing mandatory field 'namespace'" + assert msg in caplog.text + + def test_dependency_property(config_instance: config.Config) -> None: # noqa: D103 assert isinstance(config_instance.dependency, ansible_galaxy.AnsibleGalaxy)