Skip to content

Commit

Permalink
[bugfix][coverage] find module should consider file size with file_ty…
Browse files Browse the repository at this point in the history
…pe=any (ansible#74241)

* add changelog
* fix cl text
* Update changelogs/fragments/74241-find-checks-size-with-any.yml

Co-authored-by: Rick Elrod <[email protected]>
  • Loading branch information
Shrews and relrod authored Apr 13, 2021
1 parent fa0bccf commit 93fdba7
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/74241-find-checks-size-with-any.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- find - fix a bug where ``size`` argument was ignored for regular files with ``file_type`` of ``any``.
7 changes: 6 additions & 1 deletion lib/ansible/modules/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']):
Expand Down
113 changes: 113 additions & 0 deletions test/integration/targets/find/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]'

0 comments on commit 93fdba7

Please sign in to comment.