From 6d70e81d8a50360d6a31d312e00b2856fa0cc836 Mon Sep 17 00:00:00 2001 From: Lev Goncharov Date: Fri, 9 Feb 2024 11:28:08 +0100 Subject: [PATCH 1/7] molecule uses ANSIBLE_FILTER_PLUGINS --- src/molecule/provisioner/ansible.py | 61 ++++++++++++++++------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/molecule/provisioner/ansible.py b/src/molecule/provisioner/ansible.py index 3a4e85261..9cfd0fddc 100644 --- a/src/molecule/provisioner/ansible.py +++ b/src/molecule/provisioner/ansible.py @@ -507,6 +507,38 @@ def default_env(self): list(map(util.abs_path, os.environ["ANSIBLE_ROLES_PATH"].split(":"))), ) + filter_plugins_path_list = [ + self._get_filter_plugin_directory(), + util.abs_path( + os.path.join( + self._config.scenario.ephemeral_directory, + "plugins", + "filter", + ), + ), + util.abs_path( + os.path.join( + self._config.project_directory, + "plugins", + "filter", + ), + ), + util.abs_path( + os.path.join( + os.path.expanduser("~"), + ".ansible", + "plugins", + "filter", + ), + ), + "/usr/share/ansible/plugins/filter", + ] + if os.environ.get("ANSIBLE_FILTER_PLUGINS", ""): + filter_plugins_path_list.extend( + list(map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":"))), + ) + + env = util.merge_dicts( os.environ, { @@ -514,34 +546,7 @@ def default_env(self): "ANSIBLE_ROLES_PATH": ":".join(roles_path_list), self._config.ansible_collections_path: ":".join(collections_path_list), "ANSIBLE_LIBRARY": ":".join(self._get_modules_directories()), - "ANSIBLE_FILTER_PLUGINS": ":".join( - [ - self._get_filter_plugin_directory(), - util.abs_path( - os.path.join( - self._config.scenario.ephemeral_directory, - "plugins", - "filter", - ), - ), - util.abs_path( - os.path.join( - self._config.project_directory, - "plugins", - "filter", - ), - ), - util.abs_path( - os.path.join( - os.path.expanduser("~"), - ".ansible", - "plugins", - "filter", - ), - ), - "/usr/share/ansible/plugins/filter", - ], - ), + "ANSIBLE_FILTER_PLUGINS": ":".join(filter_plugins_path_list), }, ) env = util.merge_dicts(env, self._config.env) From 98ce613a8c8ad38213d2fca16f4fa9a44343426d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:32:18 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/molecule/provisioner/ansible.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/molecule/provisioner/ansible.py b/src/molecule/provisioner/ansible.py index 9cfd0fddc..7cf355fb6 100644 --- a/src/molecule/provisioner/ansible.py +++ b/src/molecule/provisioner/ansible.py @@ -535,10 +535,11 @@ def default_env(self): ] if os.environ.get("ANSIBLE_FILTER_PLUGINS", ""): filter_plugins_path_list.extend( - list(map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":"))), + list( + map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":")), + ), ) - env = util.merge_dicts( os.environ, { From b2e191d03084f35ad8f8608fc8c3c1aa218f10cf Mon Sep 17 00:00:00 2001 From: Lev Goncharov Date: Tue, 13 Feb 2024 11:57:28 +0100 Subject: [PATCH 3/7] add tests for ANSIBLE_FILTER_PLUGINS --- src/molecule/provisioner/ansible.py | 73 +++++++++++++------------ test/a_unit/provisioner/test_ansible.py | 32 +++++++++++ 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/src/molecule/provisioner/ansible.py b/src/molecule/provisioner/ansible.py index 7cf355fb6..9c0ff10ca 100644 --- a/src/molecule/provisioner/ansible.py +++ b/src/molecule/provisioner/ansible.py @@ -507,39 +507,6 @@ def default_env(self): list(map(util.abs_path, os.environ["ANSIBLE_ROLES_PATH"].split(":"))), ) - filter_plugins_path_list = [ - self._get_filter_plugin_directory(), - util.abs_path( - os.path.join( - self._config.scenario.ephemeral_directory, - "plugins", - "filter", - ), - ), - util.abs_path( - os.path.join( - self._config.project_directory, - "plugins", - "filter", - ), - ), - util.abs_path( - os.path.join( - os.path.expanduser("~"), - ".ansible", - "plugins", - "filter", - ), - ), - "/usr/share/ansible/plugins/filter", - ] - if os.environ.get("ANSIBLE_FILTER_PLUGINS", ""): - filter_plugins_path_list.extend( - list( - map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":")), - ), - ) - env = util.merge_dicts( os.environ, { @@ -547,7 +514,7 @@ def default_env(self): "ANSIBLE_ROLES_PATH": ":".join(roles_path_list), self._config.ansible_collections_path: ":".join(collections_path_list), "ANSIBLE_LIBRARY": ":".join(self._get_modules_directories()), - "ANSIBLE_FILTER_PLUGINS": ":".join(filter_plugins_path_list), + "ANSIBLE_FILTER_PLUGINS": ":".join(self._get_filter_plugins_directories()), }, ) env = util.merge_dicts(env, self._config.env) @@ -1010,5 +977,43 @@ def _get_modules_directories(self) -> list[str]: def _get_filter_plugin_directory(self): return util.abs_path(os.path.join(self._get_plugin_directory(), "filter")) + def _get_filter_plugins_directories(self) -> list[str]: + """Return list of ansilbe filter plugins includes directories. + """ + paths: list[str | None] = [] + if os.environ.get("ANSIBLE_FILTER_PLUGINS"): + paths = list(map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":"))) + + paths.extend( + [ + self._get_filter_plugin_directory(), + util.abs_path( + os.path.join( + self._config.scenario.ephemeral_directory, + "plugins", + "filter", + ), + ), + util.abs_path( + os.path.join( + self._config.project_directory, + "plugins", + "filter", + ), + ), + util.abs_path( + os.path.join( + os.path.expanduser("~"), + ".ansible", + "plugins", + "filter", + ), + ), + "/usr/share/ansible/plugins/filter", + ], + ) + + return [path for path in paths if path is not None] + def _absolute_path_for(self, env, key): return ":".join([self.abs_path(p) for p in env[key].split(":")]) diff --git a/test/a_unit/provisioner/test_ansible.py b/test/a_unit/provisioner/test_ansible.py index 58f3c6163..ffaa0ff8e 100644 --- a/test/a_unit/provisioner/test_ansible.py +++ b/test/a_unit/provisioner/test_ansible.py @@ -749,6 +749,38 @@ def test_get_filter_plugin_directory(_instance): assert x == parts[-5:] +def test_get_filter_plugins_directories_default(_instance, monkeypatch): + monkeypatch.delenv("ANSIBLE_FILTER_PLUGINS", raising=False) + + paths = _instance._get_filter_plugins_directories() + + assert len(paths) == 5 + assert re.search(r"molecule/provisioner/ansible/plugins/filter$", paths[0]) + assert re.search(r"\.cache/molecule/[^/]+/default/filter$", paths[1]) + assert re.search(r"/filter$", paths[2]) + assert re.search(r"\.ansible/plugins/filter$", paths[3]) + assert re.search(r"/usr/share/ansible/plugins/filter$", paths[4]) + + +def tes_get_filter_plugins_directories_single_ansible_filter_plugins(_instance, monkeypatch): + monkeypatch.setenv("ANSIBLE_FILTER_PLUGINS", "/abs/path/plugins/filter") + + paths = _instance._get_filter_plugins_directories() + + assert len(paths) == 6 + assert paths[0] == "/abs/path/plugins/filter" + + +def test_get_filter_plugins_directories_multi_ansible_filter_plugins(_instance, monkeypatch): + monkeypatch.setenv("ANSIBLE_FILTER_PLUGINS", "relpath/plugins/filter:/abs/path/plugins/filter") + + paths = _instance._get_filter_plugins_directories() + + assert len(paths) == 7 + assert paths[0].endswith("relpath/plugins/filter") + assert paths[1] == "/abs/path/plugins/filter" + + def test_absolute_path_for(_instance): env = {"foo": "foo:bar"} x = ":".join( From 87ff7963a9d8f71a2ce04d29f41b0ed7dc9adb52 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Feb 2024 11:01:02 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/molecule/provisioner/ansible.py | 11 +++++++---- test/a_unit/provisioner/test_ansible.py | 12 +++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/molecule/provisioner/ansible.py b/src/molecule/provisioner/ansible.py index 9c0ff10ca..178006859 100644 --- a/src/molecule/provisioner/ansible.py +++ b/src/molecule/provisioner/ansible.py @@ -514,7 +514,9 @@ def default_env(self): "ANSIBLE_ROLES_PATH": ":".join(roles_path_list), self._config.ansible_collections_path: ":".join(collections_path_list), "ANSIBLE_LIBRARY": ":".join(self._get_modules_directories()), - "ANSIBLE_FILTER_PLUGINS": ":".join(self._get_filter_plugins_directories()), + "ANSIBLE_FILTER_PLUGINS": ":".join( + self._get_filter_plugins_directories(), + ), }, ) env = util.merge_dicts(env, self._config.env) @@ -978,11 +980,12 @@ def _get_filter_plugin_directory(self): return util.abs_path(os.path.join(self._get_plugin_directory(), "filter")) def _get_filter_plugins_directories(self) -> list[str]: - """Return list of ansilbe filter plugins includes directories. - """ + """Return list of ansilbe filter plugins includes directories.""" paths: list[str | None] = [] if os.environ.get("ANSIBLE_FILTER_PLUGINS"): - paths = list(map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":"))) + paths = list( + map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":")), + ) paths.extend( [ diff --git a/test/a_unit/provisioner/test_ansible.py b/test/a_unit/provisioner/test_ansible.py index ffaa0ff8e..f14c0a781 100644 --- a/test/a_unit/provisioner/test_ansible.py +++ b/test/a_unit/provisioner/test_ansible.py @@ -762,7 +762,9 @@ def test_get_filter_plugins_directories_default(_instance, monkeypatch): assert re.search(r"/usr/share/ansible/plugins/filter$", paths[4]) -def tes_get_filter_plugins_directories_single_ansible_filter_plugins(_instance, monkeypatch): +def tes_get_filter_plugins_directories_single_ansible_filter_plugins( + _instance, monkeypatch, +): monkeypatch.setenv("ANSIBLE_FILTER_PLUGINS", "/abs/path/plugins/filter") paths = _instance._get_filter_plugins_directories() @@ -771,8 +773,12 @@ def tes_get_filter_plugins_directories_single_ansible_filter_plugins(_instance, assert paths[0] == "/abs/path/plugins/filter" -def test_get_filter_plugins_directories_multi_ansible_filter_plugins(_instance, monkeypatch): - monkeypatch.setenv("ANSIBLE_FILTER_PLUGINS", "relpath/plugins/filter:/abs/path/plugins/filter") +def test_get_filter_plugins_directories_multi_ansible_filter_plugins( + _instance, monkeypatch, +): + monkeypatch.setenv( + "ANSIBLE_FILTER_PLUGINS", "relpath/plugins/filter:/abs/path/plugins/filter", + ) paths = _instance._get_filter_plugins_directories() From e076470481ccfaa0cdbeb66e597941e1cec9de35 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Feb 2024 11:02:52 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test/a_unit/provisioner/test_ansible.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/a_unit/provisioner/test_ansible.py b/test/a_unit/provisioner/test_ansible.py index f14c0a781..1779b2786 100644 --- a/test/a_unit/provisioner/test_ansible.py +++ b/test/a_unit/provisioner/test_ansible.py @@ -763,7 +763,8 @@ def test_get_filter_plugins_directories_default(_instance, monkeypatch): def tes_get_filter_plugins_directories_single_ansible_filter_plugins( - _instance, monkeypatch, + _instance, + monkeypatch, ): monkeypatch.setenv("ANSIBLE_FILTER_PLUGINS", "/abs/path/plugins/filter") @@ -774,10 +775,12 @@ def tes_get_filter_plugins_directories_single_ansible_filter_plugins( def test_get_filter_plugins_directories_multi_ansible_filter_plugins( - _instance, monkeypatch, + _instance, + monkeypatch, ): monkeypatch.setenv( - "ANSIBLE_FILTER_PLUGINS", "relpath/plugins/filter:/abs/path/plugins/filter", + "ANSIBLE_FILTER_PLUGINS", + "relpath/plugins/filter:/abs/path/plugins/filter", ) paths = _instance._get_filter_plugins_directories() From aa75eeb42e9600953f9abe2274f6c647ada155f0 Mon Sep 17 00:00:00 2001 From: Lev Goncharov Date: Tue, 13 Feb 2024 12:11:27 +0100 Subject: [PATCH 6/7] fix path in test --- test/a_unit/provisioner/test_ansible.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/a_unit/provisioner/test_ansible.py b/test/a_unit/provisioner/test_ansible.py index 1779b2786..9e4a36578 100644 --- a/test/a_unit/provisioner/test_ansible.py +++ b/test/a_unit/provisioner/test_ansible.py @@ -756,7 +756,7 @@ def test_get_filter_plugins_directories_default(_instance, monkeypatch): assert len(paths) == 5 assert re.search(r"molecule/provisioner/ansible/plugins/filter$", paths[0]) - assert re.search(r"\.cache/molecule/[^/]+/default/filter$", paths[1]) + assert re.search(r"\.cache/molecule/[^/]+/default/plugins/filter$", paths[1]) assert re.search(r"/filter$", paths[2]) assert re.search(r"\.ansible/plugins/filter$", paths[3]) assert re.search(r"/usr/share/ansible/plugins/filter$", paths[4]) From ceae5f725aeafbc06058aad9278e8872d5849097 Mon Sep 17 00:00:00 2001 From: Lev Goncharov Date: Tue, 13 Feb 2024 12:21:44 +0100 Subject: [PATCH 7/7] update amount of tests --- .github/workflows/tox.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index fc4e0465c..2bb6b5077 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -47,7 +47,7 @@ jobs: matrix: ${{ fromJson(needs.pre.outputs.matrix) }} env: - PYTEST_REQPASS: 450 + PYTEST_REQPASS: 452 environment: test steps: - uses: actions/checkout@v4