From 75c397d01d23b0bec8f0a39ce858db01337355f6 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Sat, 5 Jun 2021 10:07:59 +0100 Subject: [PATCH] Fix ansible version parsing (#3143) Reuse code from ansible-lint for reading ansible-version and avoid bug where presence of ANSIBLE_DEBUG would break version loading. --- .github/workflows/tox.yml | 12 +++++----- src/molecule/config.py | 34 ++------------------------- src/molecule/test/unit/test_config.py | 19 --------------- 3 files changed, 8 insertions(+), 57 deletions(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 9eca0a33ae..bfc48728f2 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -25,17 +25,17 @@ jobs: - tox_env: lint - tox_env: docs - tox_env: py36 - PREFIX: PYTEST_REQPASS=438 + PREFIX: PYTEST_REQPASS=436 - tox_env: py37 - PREFIX: PYTEST_REQPASS=438 + PREFIX: PYTEST_REQPASS=436 - tox_env: py38 - PREFIX: PYTEST_REQPASS=438 + PREFIX: PYTEST_REQPASS=436 - tox_env: py39 - PREFIX: PYTEST_REQPASS=438 + PREFIX: PYTEST_REQPASS=436 - tox_env: py36-devel - PREFIX: PYTEST_REQPASS=438 + PREFIX: PYTEST_REQPASS=436 - tox_env: py39-devel - PREFIX: PYTEST_REQPASS=438 + PREFIX: PYTEST_REQPASS=436 - tox_env: packaging - tox_env: eco - tox_env: dockerfile diff --git a/src/molecule/config.py b/src/molecule/config.py index a4a341d261..d926b8c728 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -26,14 +26,13 @@ from typing import Callable, MutableMapping, TypeVar from uuid import uuid4 -from packaging.version import Version +from ansiblelint.config import ansible_version from molecule import api, interpolation, platforms, scenario, state, util -from molecule.constants import RC_SETUP_ERROR from molecule.dependency import ansible_galaxy, shell from molecule.model import schema_v3 from molecule.provisioner import ansible -from molecule.util import boolean, lru_cache, run_command, sysexit +from molecule.util import boolean LOG = logging.getLogger(__name__) MOLECULE_DEBUG = boolean(os.environ.get("MOLECULE_DEBUG", "False")) @@ -455,32 +454,3 @@ def set_env_from_file(env: MutableMapping[str, str], env_file: str) -> MutableMa return env return env - - -@lru_cache() -def ansible_version(version: str = "") -> Version: - """Return current Version object for Ansible. - - If version is not mentioned, it returns current version as detected. - When version argument is mentioned, it return converts the version string - to Version object in order to make it usable in comparisons. - """ - if not version: - proc = run_command(["ansible", "--version"], quiet=True) - if proc.returncode != 0: - LOG.fatal( - "Unable to find a working copy of ansible executable. Read https://molecule.readthedocs.io/en/latest/installation.html\n%s", - proc, - ) - sysexit(RC_SETUP_ERROR) - - # First line of the `ansible --version` output is: - # - # 1. `ansible ` on ansible < 2.10 and on ansible-base. - # 2. `ansible [core ]` on ansible-core. - # - # The code below grabs the last component in that line and strips trailing ] if - # present. - version = proc.stdout.splitlines()[0].split()[-1].rstrip("]") - - return Version(version) diff --git a/src/molecule/test/unit/test_config.py b/src/molecule/test/unit/test_config.py index 359edb8c82..9c08c616d5 100644 --- a/src/molecule/test/unit/test_config.py +++ b/src/molecule/test/unit/test_config.py @@ -360,22 +360,3 @@ def test_write_config(config_instance): config_instance.write() assert os.path.isfile(config_instance.config_file) - - -@pytest.mark.parametrize( - "input,output", - [ - ("ansible 2.9.18", "2.9.18"), # ansible < 2.10 and ansible-base - ("ansible [core 2.11.0b1.post0]", "2.11.0b1.post0"), # ansible-core - ], -) -def test_ansible_version_parsing(mocker, input, output): - run_command = mocker.patch.object(config, "run_command") - run_command.return_value.returncode = 0 - run_command.return_value.stdout = input + "\nother stuff\n here\n" - - # Results from ansible_version are cached, so we need to kill the cache before each - # test so that we can actually test parsing of different strings. - config.ansible_version.cache_clear() - - assert output == str(config.ansible_version())