Skip to content

Commit

Permalink
Add ability to check minimal ansible version
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Jul 20, 2021
1 parent cb73383 commit 2fc10f3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(
self,
project_dir: Optional[str] = None,
isolated: bool = False,
version: Optional[str] = None,
) -> None:
"""Initialize Ansible runtime environment.
Expand All @@ -59,13 +60,21 @@ def __init__(
:param isolated: Assure that installation of collections or roles
does not affect Ansible installation, an unique cache
directory being used instead.
:param version: Minimal version of Ansible required. If not found,
a RuntimError exception is raised.
"""
self.project_dir = project_dir or os.getcwd()
self.isolated = isolated
if isolated:
self.cache_dir = get_cache_dir(self.project_dir)
self.config = AnsibleConfig()

if version and packaging.version.Version(version) > self.version:
raise RuntimeError(
"Found incompatible version of ansible runtime %s, instead of %s or newer."
% (self.version, version)
)

def clean(self) -> None:
"""Remove content of cache_dir."""
if self.cache_dir:
Expand Down
6 changes: 6 additions & 0 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def test_runtime_version(runtime: Runtime) -> None:
assert version == runtime.version


def test_runtime_version_outdated() -> None:
"""Checks that instantiation raises if version is outdated."""
with pytest.raises(RuntimeError):
Runtime(version="9999.9.9")


def test_runtime_version_fail(mocker: MockerFixture) -> None:
"""Tests for failure to detect Ansible version."""
mocker.patch(
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ setenv =
PIP_DISABLE_PIP_VERSION_CHECK = 1
PIP_CONSTRAINT = {toxinidir}/constraints.txt
PRE_COMMIT_COLOR = always
PYTEST_REQPASS = 29
PYTEST_REQPASS = 30
FORCE_COLOR = 1
allowlist_externals =
sh
Expand Down

0 comments on commit 2fc10f3

Please sign in to comment.