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/defaults/main.yml b/tests/integration/targets/lookup_filetree/defaults/main.yml new file mode 100644 index 0000000000..5a4e3371fe --- /dev/null +++ b/tests/integration/targets/lookup_filetree/defaults/main.yml @@ -0,0 +1,6 @@ +--- +# 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 + +filetree_test_root: "{{ remote_tmp_dir }}/data" diff --git a/tests/integration/targets/lookup_filetree/meta/main.yml b/tests/integration/targets/lookup_filetree/meta/main.yml new file mode 100644 index 0000000000..982de6eb03 --- /dev/null +++ b/tests/integration/targets/lookup_filetree/meta/main.yml @@ -0,0 +1,7 @@ +--- +# 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 + +dependencies: + - setup_remote_tmp_dir 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..0f9a1302c4 --- /dev/null +++ b/tests/integration/targets/lookup_filetree/tasks/main.yml @@ -0,0 +1,77 @@ +--- +#################################################################### +# 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 directories + ansible.builtin.file: + path: "{{ item }}" + state: directory + loop: + - "{{ filetree_test_root }}" + - "{{ filetree_test_root }}/subdir" + +- name: Create test files + ansible.builtin.copy: + content: "{{ item.content }}" + dest: "{{ item.dest }}" + loop: + - dest: "{{ filetree_test_root }}/app.conf" + content: "content\n" + - dest: "{{ filetree_test_root }}/subdir/nested.conf" + content: "nested\n" + +- name: Create .git directory for exclude test + ansible.builtin.file: + path: "{{ filetree_test_root }}/.git" + state: directory + +- name: Lookup filetree without exclude + ansible.builtin.set_fact: + filetree_all: "{{ lookup('community.general.filetree', filetree_test_root ~ '/') }}" + +- 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', filetree_test_root ~ '/', 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', filetree_test_root ~ '/', 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"