mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-06-10 18:15:39 +00:00
Replace filetree unit tests with integration tests
This commit is contained in:
parent
654a63ac4b
commit
c031bb1eb9
3 changed files with 74 additions and 80 deletions
5
tests/integration/targets/lookup_filetree/aliases
Normal file
5
tests/integration/targets/lookup_filetree/aliases
Normal file
|
|
@ -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
|
||||
69
tests/integration/targets/lookup_filetree/tasks/main.yml
Normal file
69
tests/integration/targets/lookup_filetree/tasks/main.yml
Normal file
|
|
@ -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"
|
||||
|
|
@ -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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue