From 93fdba70135cc00b0487d43db4e77b1a072075b8 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Tue, 13 Apr 2021 11:41:44 -0400 Subject: [PATCH] [bugfix][coverage] find module should consider file size with file_type=any (#74241) * add changelog * fix cl text * Update changelogs/fragments/74241-find-checks-size-with-any.yml Co-authored-by: Rick Elrod --- .../74241-find-checks-size-with-any.yml | 2 + lib/ansible/modules/find.py | 7 +- test/integration/targets/find/tasks/main.yml | 113 ++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/74241-find-checks-size-with-any.yml diff --git a/changelogs/fragments/74241-find-checks-size-with-any.yml b/changelogs/fragments/74241-find-checks-size-with-any.yml new file mode 100644 index 00000000000..725bde72e36 --- /dev/null +++ b/changelogs/fragments/74241-find-checks-size-with-any.yml @@ -0,0 +1,2 @@ +bugfixes: + - find - fix a bug where ``size`` argument was ignored for regular files with ``file_type`` of ``any``. diff --git a/lib/ansible/modules/find.py b/lib/ansible/modules/find.py index 7db5004ca34..47bba1666c9 100644 --- a/lib/ansible/modules/find.py +++ b/lib/ansible/modules/find.py @@ -466,7 +466,12 @@ def main(): r.update(statinfo(st)) if stat.S_ISREG(st.st_mode) and params['get_checksum']: r['checksum'] = module.sha1(fsname) - filelist.append(r) + + if stat.S_ISREG(st.st_mode): + if sizefilter(st, size): + filelist.append(r) + else: + filelist.append(r) elif stat.S_ISDIR(st.st_mode) and params['file_type'] == 'directory': if pfilter(fsobj, params['patterns'], params['excludes'], params['use_regex']) and agefilter(st, now, age, params['age_stamp']): diff --git a/test/integration/targets/find/tasks/main.yml b/test/integration/targets/find/tasks/main.yml index 91d924719e4..dad88b1f583 100644 --- a/test/integration/targets/find/tasks/main.yml +++ b/test/integration/targets/find/tasks/main.yml @@ -272,3 +272,116 @@ assert: that: - '"{{ output_dir_test }}/e/f/g/h/8.ogg" not in find_test3_list' + +- name: create our age/size testing sub-directory + file: + path: "{{ output_dir_test }}/astest" + state: directory + +- name: create test file with old timestamps + file: + path: "{{ output_dir_test }}/astest/old.txt" + state: touch + modification_time: "202001011200.0" + +- name: create test file with current timestamps + file: + path: "{{ output_dir_test }}/astest/new.txt" + state: touch + +- name: create hidden test file with current timestamps + file: + path: "{{ output_dir_test }}/astest/.hidden.txt" + state: touch + +- name: find files older than 1 week + find: + path: "{{ output_dir_test }}/astest" + age: 1w + hidden: true + register: result + +- set_fact: + astest_list: >- + [ {% for f in result.files %} + {{ f.path }} + {% if not loop.last %},{% endif %} + {% endfor %} + ] + +- name: assert we only find the old file + assert: + that: + - result.matched == 1 + - '"{{ output_dir_test }}/astest/old.txt" in astest_list' + +- name: find files newer than 1 week + find: + path: "{{ output_dir_test }}/astest" + age: -1w + register: result + +- set_fact: + astest_list: >- + [ {% for f in result.files %} + {{ f.path }} + {% if not loop.last %},{% endif %} + {% endfor %} + ] + +- name: assert we only find the current file + assert: + that: + - result.matched == 1 + - '"{{ output_dir_test }}/astest/new.txt" in astest_list' + +- name: add some content to the new file + shell: "echo hello world > {{ output_dir_test }}/astest/new.txt" + +- name: find files with MORE than 5 bytes, also get checksums + find: + path: "{{ output_dir_test }}/astest" + size: 5 + hidden: true + get_checksum: true + register: result + +- set_fact: + astest_list: >- + [ {% for f in result.files %} + {{ f.path }} + {% if not loop.last %},{% endif %} + {% endfor %} + ] + +- name: assert we only find the hello world file + assert: + that: + - result.matched == 1 + - '"{{ output_dir_test }}/astest/new.txt" in astest_list' + - '"checksum" in result.files[0]' + +- name: find ANY item with LESS than 5 bytes, also get checksums + find: + path: "{{ output_dir_test }}/astest" + size: -5 + hidden: true + get_checksum: true + file_type: any + register: result + +- set_fact: + astest_list: >- + [ {% for f in result.files %} + {{ f.path }} + {% if not loop.last %},{% endif %} + {% endfor %} + ] + +- name: assert we do not find the hello world file and a checksum is present + assert: + that: + - result.matched == 2 + - '"{{ output_dir_test }}/astest/old.txt" in astest_list' + - '"{{ output_dir_test }}/astest/.hidden.txt" in astest_list' + - '"checksum" in result.files[0]'