From 803cbccbc3dff32227ab84cd56a0bf7cb0084654 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 3 Sep 2024 19:27:41 +0200 Subject: [PATCH] runtime-metadata sanity test: do not fail deprecation version checks if galaxy.yml has empty `version` (#83831) (#83856) * Do not create invalid SemanticVersion objects. * Fix SemanticVersion.parse(). * Add basic runtime-metadata tests. (cherry picked from commit faf446a895590344e1563455edba05809401f9c8) --- .../fragments/83831-runtime-metadata-fix.yml | 3 +++ lib/ansible/utils/version.py | 1 + .../aliases | 4 ++++ .../ns/no_version/galaxy.yml | 5 +++++ .../ns/no_version/meta/runtime.yml | 11 +++++++++++ .../ansible_collections/ns/version/galaxy.yml | 5 +++++ .../ns/version/meta/runtime.yml | 18 ++++++++++++++++++ .../expected-no_version.txt | 1 + .../expected-version.txt | 2 ++ .../runme.sh | 15 +++++++++++++++ test/integration/targets/collection/setup.sh | 2 +- .../sanity/code-smell/runtime-metadata.py | 4 +++- 12 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/83831-runtime-metadata-fix.yml create mode 100644 test/integration/targets/ansible-test-sanity-runtime-metadata/aliases create mode 100644 test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/galaxy.yml create mode 100644 test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/meta/runtime.yml create mode 100644 test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/galaxy.yml create mode 100644 test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/meta/runtime.yml create mode 100644 test/integration/targets/ansible-test-sanity-runtime-metadata/expected-no_version.txt create mode 100644 test/integration/targets/ansible-test-sanity-runtime-metadata/expected-version.txt create mode 100755 test/integration/targets/ansible-test-sanity-runtime-metadata/runme.sh diff --git a/changelogs/fragments/83831-runtime-metadata-fix.yml b/changelogs/fragments/83831-runtime-metadata-fix.yml new file mode 100644 index 00000000000..89e0ec7df22 --- /dev/null +++ b/changelogs/fragments/83831-runtime-metadata-fix.yml @@ -0,0 +1,3 @@ +bugfixes: + - "runtime-metadata sanity test - do not crash on deprecations if ``galaxy.yml`` contains an empty ``version`` field (https://github.com/ansible/ansible/pull/83831)." + - "Fix ``SemanticVersion.parse()`` to store the version string so that ``__repr__`` reports it instead of ``None`` (https://github.com/ansible/ansible/pull/83831)." diff --git a/lib/ansible/utils/version.py b/lib/ansible/utils/version.py index 77c8228a8af..19930d498b8 100644 --- a/lib/ansible/utils/version.py +++ b/lib/ansible/utils/version.py @@ -190,6 +190,7 @@ class SemanticVersion(Version): raise ValueError("invalid semantic version '%s'" % vstring) (major, minor, patch, prerelease, buildmetadata) = match.group(1, 2, 3, 4, 5) + self.vstring = vstring self.major = int(major) self.minor = int(minor) self.patch = int(patch) diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/aliases b/test/integration/targets/ansible-test-sanity-runtime-metadata/aliases new file mode 100644 index 00000000000..7741d444515 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/aliases @@ -0,0 +1,4 @@ +shippable/posix/group3 # runs in the distro test containers +shippable/generic/group1 # runs in the default test container +context/controller +needs/target/collection diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/galaxy.yml b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/galaxy.yml new file mode 100644 index 00000000000..a93128f40f9 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/galaxy.yml @@ -0,0 +1,5 @@ +namespace: ns +name: no_version +version: null +authors: + - Ansible diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/meta/runtime.yml b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/meta/runtime.yml new file mode 100644 index 00000000000..c27820e4afb --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/no_version/meta/runtime.yml @@ -0,0 +1,11 @@ +extra_key: true +plugin_routing: + modules: + deprecated_module: + deprecation: + removal_version: 2.0.0 + warning_text: Will no longer be there. + tombstoned_module: + tombstone: + removal_version: 1.0.0 + warning_text: Is no longer there. diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/galaxy.yml b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/galaxy.yml new file mode 100644 index 00000000000..de7c5780512 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/galaxy.yml @@ -0,0 +1,5 @@ +namespace: ns +name: version +version: 2.3.4 +authors: + - Ansible diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/meta/runtime.yml b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/meta/runtime.yml new file mode 100644 index 00000000000..1c002e17dfb --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/ansible_collections/ns/version/meta/runtime.yml @@ -0,0 +1,18 @@ +plugin_routing: + modules: + deprecated_module: + deprecation: + removal_version: 3.0.0 + warning_text: Will no longer be there. + tombstoned_module: + tombstone: + removal_version: 2.0.0 + warning_text: Is no longer there. + deprecated_module_wrong_version: + deprecation: + removal_version: 2.0.0 + warning_text: Will no longer be there. + tombstoned_module_wrong_version: + tombstone: + removal_version: 3.0.0 + warning_text: Is no longer there. diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/expected-no_version.txt b/test/integration/targets/ansible-test-sanity-runtime-metadata/expected-no_version.txt new file mode 100644 index 00000000000..ffe48a35cb9 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/expected-no_version.txt @@ -0,0 +1 @@ +meta/runtime.yml:0:0: extra keys not allowed @ data['extra_key']. Got True diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/expected-version.txt b/test/integration/targets/ansible-test-sanity-runtime-metadata/expected-version.txt new file mode 100644 index 00000000000..c3de212a642 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/expected-version.txt @@ -0,0 +1,2 @@ +meta/runtime.yml:0:0: The deprecation removal_version ('2.0.0') must be after the current version (SemanticVersion('2.3.4')) for dictionary value @ data['plugin_routing']['modules']['deprecated_module_wrong_version']['deprecation']['removal_version']. Got '2.0.0' +meta/runtime.yml:0:0: The tombstone removal_version ('3.0.0') must not be after the current version (SemanticVersion('2.3.4')) for dictionary value @ data['plugin_routing']['modules']['tombstoned_module_wrong_version']['tombstone']['removal_version']. Got '3.0.0' diff --git a/test/integration/targets/ansible-test-sanity-runtime-metadata/runme.sh b/test/integration/targets/ansible-test-sanity-runtime-metadata/runme.sh new file mode 100755 index 00000000000..c7b9b22a646 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-runtime-metadata/runme.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +COLLECTION_NAME=version source ../collection/setup.sh + +set -eux + +cd ../version +ansible-test sanity --test runtime-metadata --color --truncate 0 --failure-ok --lint "${@}" 1> actual-stdout.txt 2> actual-stderr.txt +diff -u "${TEST_DIR}/expected-version.txt" actual-stdout.txt +grep -F -f "${TEST_DIR}/expected-version.txt" actual-stderr.txt + +cd ../no_version +ansible-test sanity --test runtime-metadata --color --truncate 0 --failure-ok --lint "${@}" 1> actual-stdout.txt 2> actual-stderr.txt +diff -u "${TEST_DIR}/expected-no_version.txt" actual-stdout.txt +grep -F -f "${TEST_DIR}/expected-no_version.txt" actual-stderr.txt diff --git a/test/integration/targets/collection/setup.sh b/test/integration/targets/collection/setup.sh index f1b33a55b00..74466555e1c 100755 --- a/test/integration/targets/collection/setup.sh +++ b/test/integration/targets/collection/setup.sh @@ -24,6 +24,6 @@ WORK_DIR="$(mktemp -d)" trap 'rm -rf "${WORK_DIR}"' EXIT cp -a "${TEST_DIR}/ansible_collections" "${WORK_DIR}" -cd "${WORK_DIR}/ansible_collections/ns/col" +cd "${WORK_DIR}/ansible_collections/ns/${COLLECTION_NAME:-col}" "${TEST_DIR}/../collection/update-ignore.py" diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/runtime-metadata.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/runtime-metadata.py index 188d50fef58..ad7d017767e 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/runtime-metadata.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/runtime-metadata.py @@ -123,7 +123,9 @@ def get_collection_version(): # noinspection PyBroadException try: result = collection_detail.read_manifest_json('.') or collection_detail.read_galaxy_yml('.') - return SemanticVersion(result['version']) + version = SemanticVersion() + version.parse(result['version']) + return version except Exception: # pylint: disable=broad-except # We do not care why it fails, in case we cannot get the version # just return None to indicate "we don't know".