summaryrefslogtreecommitdiffstats
path: root/ansible_collections/cloud/common/tests/unit
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:04:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:04:41 +0000
commit975f66f2eebe9dadba04f275774d4ab83f74cf25 (patch)
tree89bd26a93aaae6a25749145b7e4bca4a1e75b2be /ansible_collections/cloud/common/tests/unit
parentInitial commit. (diff)
downloadansible-975f66f2eebe9dadba04f275774d4ab83f74cf25.tar.xz
ansible-975f66f2eebe9dadba04f275774d4ab83f74cf25.zip
Adding upstream version 7.7.0+dfsg.upstream/7.7.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/cloud/common/tests/unit')
-rw-r--r--ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/conftest.py20
-rw-r--r--ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_module.py59
-rw-r--r--ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_turbo_module.py140
-rw-r--r--ansible_collections/cloud/common/tests/unit/requirements.txt4
4 files changed, 223 insertions, 0 deletions
diff --git a/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/conftest.py b/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/conftest.py
new file mode 100644
index 000000000..8f8f44e78
--- /dev/null
+++ b/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/conftest.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Copyright 2021 XLAB Steampunk
+# GNU General Public License v3.0+
+# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
+
+import json
+
+import pytest
+
+from ansible.module_utils import basic
+from ansible.module_utils.common.text.converters import to_bytes
+
+
+@pytest.fixture
+def set_module_args(monkeypatch):
+ def wrapper(args=None):
+ module_args = dict(ANSIBLE_MODULE_ARGS=args or {})
+ monkeypatch.setattr(basic, "_ANSIBLE_ARGS", to_bytes(json.dumps(module_args)))
+
+ return wrapper
diff --git a/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_module.py b/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_module.py
new file mode 100644
index 000000000..61a02e145
--- /dev/null
+++ b/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_module.py
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+# Copyright 2021 XLAB Steampunk
+# GNU General Public License v3.0+
+# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
+
+import sys
+
+import pytest
+
+from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
+ AnsibleTurboModule,
+)
+
+
+def _patch_globals(monkeypatch):
+ # Patch sys.argv so that module does not try to spin up the server on
+ # initialization. The purpose is to make sure AnsibleTurboModule.embedded_in_server
+ # is set to True.
+ monkeypatch.setattr(sys, "argv", ["something/that/ends/on/server.py"])
+
+ # Collection name detection will fail in unit tests, so we patch it here directly
+ # and bypass the detection process.
+ monkeypatch.setattr(AnsibleTurboModule, "collection_name", "namespace.name")
+
+
+def test_module_socket_path_remote_tmp_not_set(monkeypatch, set_module_args):
+ _patch_globals(monkeypatch)
+ set_module_args()
+ module = AnsibleTurboModule(argument_spec={})
+
+ path = module.socket_path()
+
+ # We cannot know what tmp dir python uses, but we do know that it is a full path
+ # that ends with deterministc suffix.
+ assert path.startswith("/")
+ assert path.endswith("/turbo_mode.namespace.name.socket")
+
+
+@pytest.mark.parametrize("tmp_path", ["/tmp", "/tmp/"])
+def test_module_socket_path_from_remote_tmp(monkeypatch, set_module_args, tmp_path):
+ _patch_globals(monkeypatch)
+ set_module_args(dict(_ansible_remote_tmp=tmp_path))
+ module = AnsibleTurboModule(argument_spec={})
+
+ assert module.socket_path() == "/tmp/turbo_mode.namespace.name.socket"
+
+
+@pytest.mark.parametrize(
+ "tmp_path", ["/t/$MY_VAR", "/t/${MY_VAR}", "/t/$MY_VAR/", "/t/${MY_VAR}/"]
+)
+def test_module_socket_path_env_vars_in_remote_tmp(
+ monkeypatch, set_module_args, tmp_path
+):
+ _patch_globals(monkeypatch)
+ set_module_args(dict(_ansible_remote_tmp=tmp_path))
+ monkeypatch.setenv("MY_VAR", "my_var_value")
+ module = AnsibleTurboModule(argument_spec={})
+
+ assert module.socket_path() == "/t/my_var_value/turbo_mode.namespace.name.socket"
diff --git a/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_turbo_module.py b/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_turbo_module.py
new file mode 100644
index 000000000..00756a0d3
--- /dev/null
+++ b/ansible_collections/cloud/common/tests/unit/plugins/module_utils/turbo/test_turbo_module.py
@@ -0,0 +1,140 @@
+# -*- coding: utf-8 -*-
+# Copyright 2020 Red Hat
+# GNU General Public License v3.0+
+# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
+
+# py38 only, See: https://github.com/PyCQA/pylint/issues/2976
+from posixpath import dirname
+from unittest.mock import Mock, ANY # pylint: disable=syntax-error
+import time
+import pytest
+import socket
+import subprocess
+import os
+import ansible.module_utils.basic
+from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
+ get_collection_name_from_path,
+ expand_argument_specs_aliases,
+ prepare_args,
+)
+import ansible_collections.cloud.common.plugins.module_utils.turbo.common as turbo_common
+
+
+@pytest.mark.parametrize(
+ "my_module_path,my_collection_name",
+ [
+ (
+ "/tmp/ansible_vmware.vmware_rest.vcenter_vm_info_payload_548h2lev/ansible_vmware.vmware_rest.vcenter_vm_info_payload.zip/ansible/module_utils",
+ "vmware.vmware_rest",
+ )
+ ],
+)
+def test_collection_name(monkeypatch, my_module_path, my_collection_name):
+ def mocked_func():
+ return my_module_path
+
+ monkeypatch.setattr(ansible.module_utils.basic, "get_module_path", mocked_func)
+ assert get_collection_name_from_path() == my_collection_name
+
+
+def test_start_daemon_from_module(monkeypatch):
+ mocked_Popen = Mock()
+ monkeypatch.setattr(subprocess, "Popen", mocked_Popen)
+ turbo_socket = turbo_common.AnsibleTurboSocket(socket_path="/aa")
+ assert turbo_socket.start_server()
+ mocked_Popen.assert_called_once_with(
+ [
+ ANY,
+ "-m",
+ "ansible_collections.cloud.common.plugins.module_utils.turbo.server",
+ "--fork",
+ "--socket-path",
+ "/aa",
+ ],
+ env=ANY,
+ close_fds=True,
+ )
+
+
+def test_start_daemon_from_lookup(monkeypatch):
+ mocked_Popen = Mock()
+ monkeypatch.setattr(subprocess, "Popen", mocked_Popen)
+ turbo_socket = turbo_common.AnsibleTurboSocket(
+ socket_path="/aa", plugin="lookup", ttl=150
+ )
+ assert turbo_socket.start_server()
+ mocked_Popen.assert_called_once_with(
+ [
+ ANY,
+ os.path.join(os.path.dirname(turbo_common.__file__), "server.py"),
+ "--fork",
+ "--socket-path",
+ "/aa",
+ "--ttl",
+ "150",
+ ],
+ env=ANY,
+ close_fds=True,
+ )
+
+
+def test_start_daemon_with_no_mock(tmp_path):
+ my_socket = tmp_path / "socket"
+ turbo_socket = turbo_common.AnsibleTurboSocket(socket_path=str(my_socket), ttl=1)
+ assert turbo_socket.start_server()
+ time.sleep(0.5)
+ assert my_socket.is_socket()
+ time.sleep(0.8)
+ assert not my_socket.exists()
+
+
+def test_connect(monkeypatch):
+ mocked_socket = Mock()
+ monkeypatch.setattr(socket, "socket", mocked_socket)
+ turbo_socket = turbo_common.AnsibleTurboSocket(socket_path="/nowhere")
+ assert turbo_socket.bind()
+ mocked_socket.connect_assert_called_once_with("/nowhere")
+
+
+def test_expand_argument_specs_aliases():
+ argspec = {"foo": {"type": int, "aliases": ["bar"]}}
+ assert expand_argument_specs_aliases(argspec) == {
+ "foo": {"type": int, "aliases": ["bar"]},
+ "bar": {"type": int, "aliases": ["bar"]},
+ }
+
+
+def test_prepare_args():
+ argspec = {"foo": {"type": int}}
+ params = {"foo": 1}
+ assert prepare_args(argspec, params) == {"ANSIBLE_MODULE_ARGS": {"foo": 1}}
+
+
+def test_prepare_args_ignore_none():
+ argspec = {"foo": {"type": int}}
+ params = {"foo": None}
+ assert prepare_args(argspec, params) == {"ANSIBLE_MODULE_ARGS": {}}
+
+
+def test_prepare_args_subkey_freeform():
+ argspec = {"foo": {"type": dict, "default": {}}}
+ params = {"foo": {"bar": 1}}
+ assert prepare_args(argspec, params) == {"ANSIBLE_MODULE_ARGS": {"foo": {"bar": 1}}}
+
+
+def test_prepare_args_subkey_with_default():
+ argspec = {"foo": {"bar": {"default": 1}}}
+ params = {"foo": {"bar": 1}}
+ assert prepare_args(argspec, params) == {"ANSIBLE_MODULE_ARGS": {"foo": {}}}
+
+
+def test_prepare_args_dedup_aliases():
+ argspec = {"foo": {"aliases": ["bar"], "type": int}}
+ params = {"foo": 1, "bar": 1}
+ assert prepare_args(argspec, params) == {"ANSIBLE_MODULE_ARGS": {"foo": 1}}
+
+
+def test_prepare_args_with_aliases():
+ argspec = {"foo": {"aliases": ["bar"], "type": int}}
+ params = {"foo": 1}
+ assert prepare_args(argspec, params) == {"ANSIBLE_MODULE_ARGS": {"foo": 1}}
diff --git a/ansible_collections/cloud/common/tests/unit/requirements.txt b/ansible_collections/cloud/common/tests/unit/requirements.txt
new file mode 100644
index 000000000..2975a6e97
--- /dev/null
+++ b/ansible_collections/cloud/common/tests/unit/requirements.txt
@@ -0,0 +1,4 @@
+pytest
+pytest-xdist
+pytest-mock
+mock