From c031bb1eb92cad9f2263bd17c52bbf2e7bef2514 Mon Sep 17 00:00:00 2001 From: Santosh Mahale Date: Tue, 9 Jun 2026 21:03:51 +0530 Subject: [PATCH] Replace filetree unit tests with integration tests --- .../targets/lookup_filetree/aliases | 5 ++ .../targets/lookup_filetree/tasks/main.yml | 69 ++++++++++++++++ tests/unit/plugins/lookup/test_filetree.py | 80 ------------------- 3 files changed, 74 insertions(+), 80 deletions(-) create mode 100644 tests/integration/targets/lookup_filetree/aliases create mode 100644 tests/integration/targets/lookup_filetree/tasks/main.yml delete mode 100644 tests/unit/plugins/lookup/test_filetree.py diff --git a/tests/integration/targets/lookup_filetree/aliases b/tests/integration/targets/lookup_filetree/aliases new file mode 100644 index 0000000000..12d1d6617e --- /dev/null +++ b/tests/integration/targets/lookup_filetree/aliases @@ -0,0 +1,5 @@ +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +azp/posix/2 diff --git a/tests/integration/targets/lookup_filetree/tasks/main.yml b/tests/integration/targets/lookup_filetree/tasks/main.yml new file mode 100644 index 0000000000..409da43686 --- /dev/null +++ b/tests/integration/targets/lookup_filetree/tasks/main.yml @@ -0,0 +1,69 @@ +--- +#################################################################### +# WARNING: These are designed specifically for Ansible tests # +# and should not be used as examples of how to write Ansible roles # +#################################################################### + +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +- name: Create test file tree + ansible.builtin.copy: + content: "{{ item.content }}" + dest: "{{ item.dest }}" + loop: + - dest: files/data/app.conf + content: "content\n" + - dest: files/data/subdir/nested.conf + content: "nested\n" + +- name: Create .git directory for exclude test + ansible.builtin.file: + path: files/data/.git + state: directory + +- name: Lookup filetree without exclude + ansible.builtin.set_fact: + filetree_all: "{{ lookup('community.general.filetree', 'data') }}" + +- name: Verify all entries are listed + ansible.builtin.assert: + that: + - filetree_all | selectattr('path', 'equalto', 'app.conf') | list | length == 1 + - filetree_all | selectattr('path', 'equalto', '.git') | list | length == 1 + - filetree_all | selectattr('path', 'equalto', 'subdir') | list | length == 1 + - filetree_all | selectattr('path', 'equalto', 'subdir/nested.conf') | list | length == 1 + +- name: Lookup filetree with valid exclude regex + ansible.builtin.set_fact: + filetree_filtered: "{{ lookup('community.general.filetree', 'data', exclude='^\\.git$') }}" + +- name: Verify exclude filters matching basenames + ansible.builtin.assert: + that: + - filetree_filtered | selectattr('path', 'equalto', '.git') | list | length == 0 + - filetree_filtered | selectattr('path', 'equalto', 'app.conf') | list | length == 1 + - filetree_filtered | selectattr('path', 'equalto', 'subdir/nested.conf') | list | length == 1 + +- name: Verify file entry properties + ansible.builtin.assert: + that: + - (filetree_all | selectattr('path', 'equalto', 'app.conf') | first).state == 'file' + - (filetree_all | selectattr('path', 'equalto', 'app.conf') | first).src is defined + - (filetree_all | selectattr('path', 'equalto', 'app.conf') | first).mode is defined + - (filetree_all | selectattr('path', 'equalto', 'app.conf') | first).owner is defined + - (filetree_all | selectattr('path', 'equalto', 'app.conf') | first).group is defined + +- name: Lookup filetree with invalid exclude regex + ansible.builtin.debug: + msg: "{{ lookup('community.general.filetree', 'data', exclude='temp[1') }}" + ignore_errors: true + register: invalid_exclude + +- name: Verify invalid exclude raises AnsibleLookupError + ansible.builtin.assert: + that: + - invalid_exclude is failed + - "'Invalid exclude regular expression' in invalid_exclude.msg" + - "'temp[1' in invalid_exclude.msg" diff --git a/tests/unit/plugins/lookup/test_filetree.py b/tests/unit/plugins/lookup/test_filetree.py deleted file mode 100644 index 8377b04035..0000000000 --- a/tests/unit/plugins/lookup/test_filetree.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright (c) 2026, Ansible Project -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -from __future__ import annotations - -import os -import shutil -import tempfile -import unittest -from unittest.mock import patch - -from ansible.errors import AnsibleLookupError -from ansible.plugins.loader import lookup_loader -from ansible.template import Templar -from ansible_collections.community.internal_test_tools.tests.unit.mock.loader import DictDataLoader - - -class TestFiletreeLookup(unittest.TestCase): - def setUp(self): - self.loader = DictDataLoader({}) - self.templar = Templar(loader=self.loader, variables={}) - self.lookup = lookup_loader.get( - "community.general.filetree", - loader=self.loader, - templar=self.templar, - ) - self.tmpdir = tempfile.mkdtemp(prefix="ansible_test_filetree_") - self.tree_root = os.path.join(self.tmpdir, "data") - os.makedirs(os.path.join(self.tree_root, ".git")) - os.makedirs(os.path.join(self.tree_root, "subdir")) - with open(os.path.join(self.tree_root, "app.conf"), "w", encoding="utf-8") as handle: - handle.write("content\n") - with open(os.path.join(self.tree_root, "subdir", "nested.conf"), "w", encoding="utf-8") as handle: - handle.write("nested\n") - - def tearDown(self): - shutil.rmtree(self.tmpdir) - - def _run_lookup(self, **kwargs): - with ( - patch.object(self.lookup, "get_basedir", return_value=self.tmpdir), - patch.object(self.loader, "path_dwim_relative", return_value=self.tmpdir), - ): - return self.lookup.run(["data"], {}, **kwargs) - - def test_invalid_exclude_regex_raises_lookup_error(self): - with self.assertRaises(AnsibleLookupError) as ctx: - self._run_lookup(exclude="temp[1") - - self.assertIn("Invalid exclude regular expression", str(ctx.exception)) - self.assertIn("temp[1", str(ctx.exception)) - - def test_valid_exclude_skips_matching_basenames(self): - result = self._run_lookup(exclude=r"^\.git$") - - paths = {entry["path"] for entry in result} - self.assertIn("app.conf", paths) - self.assertIn(os.path.join("subdir", "nested.conf"), paths) - self.assertNotIn(".git", paths) - - def test_lookup_without_exclude_lists_entries(self): - result = self._run_lookup() - - paths = {entry["path"] for entry in result} - self.assertIn("app.conf", paths) - self.assertIn(".git", paths) - self.assertIn("subdir", paths) - self.assertIn(os.path.join("subdir", "nested.conf"), paths) - - def test_file_entry_includes_expected_properties(self): - result = self._run_lookup() - app_conf = next(entry for entry in result if entry["path"] == "app.conf") - - self.assertEqual(app_conf["state"], "file") - self.assertEqual(app_conf["root"], self.tree_root) - self.assertEqual(app_conf["src"], os.path.join(self.tree_root, "app.conf")) - self.assertIn("mode", app_conf) - self.assertIn("owner", app_conf) - self.assertIn("group", app_conf)