Skip to content

Commit

Permalink
Add ansible-core version string parsing (#3068)
Browse files Browse the repository at this point in the history
Core developers change the `ansible --version` output a bit. Changes
in this commit make sure molecule can parse both pre- and
post-ansible-core version strings.
  • Loading branch information
tadeboro authored Mar 8, 2021
1 parent 692c3c2 commit 969b111
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ jobs:
- tox_env: lint
- tox_env: docs
- tox_env: py36
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=429
- tox_env: py37
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=429
- tox_env: py38
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=429
- tox_env: py39
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=429
- tox_env: py36-devel
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=429
- tox_env: py39-devel
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=429
- tox_env: packaging
- tox_env: eco
- tox_env: dockerfile
Expand Down
14 changes: 11 additions & 3 deletions src/molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,20 @@ def ansible_version(version: str = "") -> Version:
"""
if not version:
proc = run_command(["ansible", "--version"], quiet=True)
if proc.returncode == 0:
version = proc.stdout.splitlines()[0].split()[1]
else:
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 <version>` on ansible < 2.10 and on ansible-base.
# 2. `ansible [core <version>]` 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)
19 changes: 19 additions & 0 deletions src/molecule/test/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,22 @@ 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())

0 comments on commit 969b111

Please sign in to comment.