From 1022b2cebe73db426241c2f420d4ee9f6f3c1bed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 02:24:37 +0200 Subject: Adding upstream version 4.1.11. Signed-off-by: Daniel Baumann --- test/test_config.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 test/test_config.py (limited to 'test/test_config.py') diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 0000000..4f854ae --- /dev/null +++ b/test/test_config.py @@ -0,0 +1,86 @@ +"""Tests for ansible_compat.config submodule.""" +import copy +import subprocess + +import pytest +from _pytest.monkeypatch import MonkeyPatch +from packaging.version import Version + +from ansible_compat.config import AnsibleConfig, ansible_version, parse_ansible_version +from ansible_compat.errors import InvalidPrerequisiteError, MissingAnsibleError + + +def test_config() -> None: + """Checks that config vars are loaded with their expected type.""" + config = AnsibleConfig() + assert isinstance(config.ACTION_WARNINGS, bool) + assert isinstance(config.CACHE_PLUGIN_PREFIX, str) + assert isinstance(config.CONNECTION_FACTS_MODULES, dict) + assert config.ANSIBLE_COW_PATH is None + assert isinstance(config.NETWORK_GROUP_MODULES, list) + assert isinstance(config.DEFAULT_GATHER_TIMEOUT, (int, type(None))) + + # check lowercase and older name aliasing + assert isinstance(config.collections_paths, list) + assert isinstance(config.collections_path, list) + assert config.collections_paths == config.collections_path + + # check if we can access the special data member + assert config.data["ACTION_WARNINGS"] == config.ACTION_WARNINGS + + with pytest.raises(AttributeError): + _ = config.THIS_DOES_NOT_EXIST + + +def test_config_with_dump() -> None: + """Tests that config can parse given dumps.""" + config = AnsibleConfig(config_dump="ACTION_WARNINGS(default) = True") + assert config.ACTION_WARNINGS is True + + +def test_config_copy() -> None: + """Checks ability to use copy/deepcopy.""" + config = AnsibleConfig() + new_config = copy.copy(config) + assert isinstance(new_config, AnsibleConfig) + assert new_config is not config + # deepcopy testing + new_config = copy.deepcopy(config) + assert isinstance(new_config, AnsibleConfig) + assert new_config is not config + + +def test_parse_ansible_version_fail() -> None: + """Checks that parse_ansible_version raises an error on invalid input.""" + with pytest.raises( + InvalidPrerequisiteError, + match="Unable to parse ansible cli version", + ): + parse_ansible_version("foo") + + +def test_ansible_version_missing(monkeypatch: MonkeyPatch) -> None: + """Validate ansible_version behavior when ansible is missing.""" + monkeypatch.setattr( + "subprocess.run", + lambda *args, **kwargs: subprocess.CompletedProcess( # noqa: ARG005 + args=[], + returncode=1, + ), + ) + with pytest.raises( + MissingAnsibleError, + match="Unable to find a working copy of ansible executable.", + ): + # bypassing lru cache + ansible_version.__wrapped__() + + +def test_ansible_version() -> None: + """Validate ansible_version behavior.""" + assert ansible_version() >= Version("1.0") + + +def test_ansible_version_arg() -> None: + """Validate ansible_version behavior.""" + assert ansible_version("2.0") >= Version("1.0") -- cgit v1.2.3