| 1 |
# Test code for the find module. |
| 2 |
# (c) 2017, James Tanner <[email protected]> |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
- set_fact: remote_tmp_dir_test={{remote_tmp_dir}}/test_find |
| 20 |
|
| 21 |
- name: make sure our testing sub-directory does not exist |
| 22 |
file: |
| 23 |
path: "{{ remote_tmp_dir_test }}" |
| 24 |
state: absent |
| 25 |
|
| 26 |
- name: create our testing sub-directory |
| 27 |
file: |
| 28 |
path: "{{ remote_tmp_dir_test }}" |
| 29 |
state: directory |
| 30 |
|
| 31 |
## |
| 32 |
## find |
| 33 |
## |
| 34 |
|
| 35 |
- name: make some directories |
| 36 |
file: |
| 37 |
path: "{{ remote_tmp_dir_test }}/{{ item }}" |
| 38 |
state: directory |
| 39 |
with_items: |
| 40 |
- a/b/c/d |
| 41 |
- e/f/g/h |
| 42 |
|
| 43 |
- name: make some files |
| 44 |
copy: |
| 45 |
dest: "{{ remote_tmp_dir_test }}/{{ item }}" |
| 46 |
content: 'data' |
| 47 |
with_items: |
| 48 |
- a/1.txt |
| 49 |
- a/b/2.jpg |
| 50 |
- a/b/c/3 |
| 51 |
- a/b/c/d/4.xml |
| 52 |
- e/5.json |
| 53 |
- e/f/6.swp |
| 54 |
- e/f/g/7.img |
| 55 |
- e/f/g/h/8.ogg |
| 56 |
|
| 57 |
- name: find the directories |
| 58 |
find: |
| 59 |
paths: "{{ remote_tmp_dir_test }}" |
| 60 |
file_type: directory |
| 61 |
recurse: yes |
| 62 |
register: find_test0 |
| 63 |
- debug: var=find_test0 |
| 64 |
- name: validate directory results |
| 65 |
assert: |
| 66 |
that: |
| 67 |
- 'find_test0.changed is defined' |
| 68 |
- 'find_test0.examined is defined' |
| 69 |
- 'find_test0.files is defined' |
| 70 |
- 'find_test0.matched is defined' |
| 71 |
- 'find_test0.msg is defined' |
| 72 |
- 'find_test0.matched == 8' |
| 73 |
- 'find_test0.files | length == 8' |
| 74 |
- 'find_test0.examined == 16' |
| 75 |
|
| 76 |
- name: find the xml and img files |
| 77 |
find: |
| 78 |
paths: "{{ remote_tmp_dir_test }}" |
| 79 |
file_type: file |
| 80 |
patterns: "*.xml,*.img" |
| 81 |
recurse: yes |
| 82 |
register: find_test1 |
| 83 |
- debug: var=find_test1 |
| 84 |
- name: validate directory results |
| 85 |
assert: |
| 86 |
that: |
| 87 |
- 'find_test1.matched == 2' |
| 88 |
- 'find_test1.files | length == 2' |
| 89 |
|
| 90 |
- name: find the xml file |
| 91 |
find: |
| 92 |
paths: "{{ remote_tmp_dir_test }}" |
| 93 |
patterns: "*.xml" |
| 94 |
recurse: yes |
| 95 |
register: find_test2 |
| 96 |
- debug: var=find_test2 |
| 97 |
- name: validate gr_name and pw_name are defined |
| 98 |
assert: |
| 99 |
that: |
| 100 |
- 'find_test2.matched == 1' |
| 101 |
- 'find_test2.files[0].pw_name is defined' |
| 102 |
- 'find_test2.files[0].gr_name is defined' |
| 103 |
|
| 104 |
- name: find the xml file with empty excludes |
| 105 |
find: |
| 106 |
paths: "{{ remote_tmp_dir_test }}" |
| 107 |
patterns: "*.xml" |
| 108 |
recurse: yes |
| 109 |
excludes: [] |
| 110 |
register: find_test3 |
| 111 |
- debug: var=find_test3 |
| 112 |
- name: validate gr_name and pw_name are defined |
| 113 |
assert: |
| 114 |
that: |
| 115 |
- 'find_test3.matched == 1' |
| 116 |
- 'find_test3.files[0].pw_name is defined' |
| 117 |
- 'find_test3.files[0].gr_name is defined' |
| 118 |
|
| 119 |
- name: Copy some files into the test dir |
| 120 |
copy: |
| 121 |
src: "{{ item }}" |
| 122 |
dest: "{{ remote_tmp_dir_test }}/{{ item }}" |
| 123 |
mode: 0644 |
| 124 |
with_items: |
| 125 |
- a.txt |
| 126 |
- log.txt |
| 127 |
- hello_world.gbk |
| 128 |
|
| 129 |
- name: Ensure '$' only matches the true end of the file with read_whole_file, not a line |
| 130 |
find: |
| 131 |
paths: "{{ remote_tmp_dir_test }}" |
| 132 |
patterns: "*.txt" |
| 133 |
contains: "KO$" |
| 134 |
read_whole_file: true |
| 135 |
register: whole_no_match |
| 136 |
|
| 137 |
- debug: var=whole_no_match |
| 138 |
|
| 139 |
- assert: |
| 140 |
that: |
| 141 |
- whole_no_match.matched == 0 |
| 142 |
|
| 143 |
- name: Match the end of the file successfully |
| 144 |
find: |
| 145 |
paths: "{{ remote_tmp_dir_test }}" |
| 146 |
patterns: "*.txt" |
| 147 |
contains: "OK$" |
| 148 |
read_whole_file: true |
| 149 |
register: whole_match |
| 150 |
|
| 151 |
- debug: var=whole_match |
| 152 |
|
| 153 |
- assert: |
| 154 |
that: |
| 155 |
- whole_match.matched == 1 |
| 156 |
|
| 157 |
- name: When read_whole_file=False, $ should match an individual line |
| 158 |
find: |
| 159 |
paths: "{{ remote_tmp_dir_test }}" |
| 160 |
patterns: "*.txt" |
| 161 |
contains: ".*KO$" |
| 162 |
read_whole_file: false |
| 163 |
register: match_end_of_line |
| 164 |
|
| 165 |
- debug: var=match_end_of_line |
| 166 |
|
| 167 |
- assert: |
| 168 |
that: |
| 169 |
- match_end_of_line.matched == 1 |
| 170 |
|
| 171 |
- name: When read_whole_file=True, match across line boundaries |
| 172 |
find: |
| 173 |
paths: "{{ remote_tmp_dir_test }}" |
| 174 |
patterns: "*.txt" |
| 175 |
contains: "has\na few" |
| 176 |
read_whole_file: true |
| 177 |
register: match_line_boundaries |
| 178 |
|
| 179 |
- debug: var=match_line_boundaries |
| 180 |
|
| 181 |
- assert: |
| 182 |
that: |
| 183 |
- match_line_boundaries.matched == 1 |
| 184 |
|
| 185 |
- name: When read_whole_file=False, do not match across line boundaries |
| 186 |
find: |
| 187 |
paths: "{{ remote_tmp_dir_test }}" |
| 188 |
patterns: "*.txt" |
| 189 |
contains: "has\na few" |
| 190 |
read_whole_file: false |
| 191 |
register: no_match_line_boundaries |
| 192 |
|
| 193 |
- debug: var=no_match_line_boundaries |
| 194 |
|
| 195 |
- assert: |
| 196 |
that: |
| 197 |
- no_match_line_boundaries.matched == 0 |
| 198 |
|
| 199 |
- name: read a gbk file by utf-8 |
| 200 |
find: |
| 201 |
paths: "{{ remote_tmp_dir_test }}" |
| 202 |
patterns: "*.gbk" |
| 203 |
contains: "你好世界" |
| 204 |
encoding: "utf-8" |
| 205 |
register: fail_to_read_wrong_encoding_file |
| 206 |
|
| 207 |
- debug: var=fail_to_read_wrong_encoding_file |
| 208 |
|
| 209 |
- assert: |
| 210 |
that: |
| 211 |
- fail_to_read_wrong_encoding_file.msg == 'Not all paths examined, check warnings for details' |
| 212 |
- >- |
| 213 |
fail_to_read_wrong_encoding_file.skipped_paths[remote_tmp_dir_test] == |
| 214 |
("Failed to decode the file '%s/hello_world.gbk' with encoding: utf-8" % (remote_tmp_dir_test)) |
| 215 |
|
| 216 |
- name: read a gbk file with no encoding specified (uses locale default) |
| 217 |
find: |
| 218 |
paths: "{{ remote_tmp_dir_test }}" |
| 219 |
patterns: "*.gbk" |
| 220 |
contains: "你好世界" |
| 221 |
register: fail_to_read_default_encoding |
| 222 |
|
| 223 |
- name: Verify error message shows locale encoding name |
| 224 |
vars: |
| 225 |
error_pattern: 'Failed to decode the file .+ with encoding: .+ \(Python default\)' |
| 226 |
assert: |
| 227 |
that: |
| 228 |
- fail_to_read_default_encoding.msg == 'Not all paths examined, check warnings for details' |
| 229 |
- fail_to_read_default_encoding.skipped_paths[remote_tmp_dir_test] is search("^" + error_pattern + "$") |
| 230 |
- fail_to_read_default_encoding.warnings | select('search', error_pattern) | list | length >= 1 |
| 231 |
|
| 232 |
- name: read a gbk file by gbk |
| 233 |
find: |
| 234 |
paths: "{{ remote_tmp_dir_test }}" |
| 235 |
encoding: "gbk" |
| 236 |
patterns: "*.gbk" |
| 237 |
contains: "你好世界" |
| 238 |
register: success_to_read_right_encoding_file |
| 239 |
|
| 240 |
- debug: var=success_to_read_right_encoding_file |
| 241 |
|
| 242 |
- assert: |
| 243 |
that: |
| 244 |
- success_to_read_right_encoding_file.matched == 1 |
| 245 |
|
| 246 |
- name: specify the checksum_algo |
| 247 |
register: find_by_specific_algo_res |
| 248 |
find: |
| 249 |
paths: "{{ remote_tmp_dir_test }}" |
| 250 |
get_checksum: true |
| 251 |
checksum_algorithm: md5 |
| 252 |
|
| 253 |
- debug: var=find_by_specific_algo_res |
| 254 |
|
| 255 |
- name: get the md5 checksum by stat |
| 256 |
register: all_files_st |
| 257 |
stat: |
| 258 |
get_checksum: true |
| 259 |
checksum_algorithm: md5 |
| 260 |
path: "{{ item.path }}" |
| 261 |
loop: "{{ find_by_specific_algo_res.files }}" |
| 262 |
|
| 263 |
- assert: |
| 264 |
that: "all_files_st.results[index].stat.checksum == item.checksum" |
| 265 |
loop: "{{ find_by_specific_algo_res.files }}" |
| 266 |
loop_control: |
| 267 |
index_var: index |
| 268 |
|
| 269 |
- name: read a gbk file by non-exists encoding |
| 270 |
find: |
| 271 |
paths: "{{ remote_tmp_dir_test }}" |
| 272 |
encoding: "idontexist" |
| 273 |
patterns: "*.gbk" |
| 274 |
contains: "你好世界" |
| 275 |
register: fail_to_search_file_by_non_exists_encoding |
| 276 |
|
| 277 |
- debug: var=fail_to_search_file_by_non_exists_encoding |
| 278 |
|
| 279 |
- assert: |
| 280 |
that: |
| 281 |
- 'fail_to_search_file_by_non_exists_encoding.skipped_paths[remote_tmp_dir_test] == "unknown encoding: idontexist"' |
| 282 |
|
| 283 |
- block: |
| 284 |
- set_fact: |
| 285 |
mypath: /idontexist{{lookup('pipe', 'mktemp')}} |
| 286 |
|
| 287 |
- find: |
| 288 |
paths: '{{mypath}}' |
| 289 |
patterns: '*' |
| 290 |
register: failed_path |
| 291 |
|
| 292 |
- assert: |
| 293 |
that: |
| 294 |
- failed_path.files == [] |
| 295 |
- 'failed_path.msg == "Not all paths examined, check warnings for details"' |
| 296 |
- mypath in failed_path.skipped_paths |
| 297 |
|
| 298 |
- name: test number of examined directories/files |
| 299 |
block: |
| 300 |
- name: Get all files/directories in the path |
| 301 |
find: |
| 302 |
paths: "{{ remote_tmp_dir_test }}" |
| 303 |
recurse: yes |
| 304 |
file_type: any |
| 305 |
register: total_contents |
| 306 |
|
| 307 |
- assert: |
| 308 |
that: |
| 309 |
- total_contents.matched == 19 |
| 310 |
- total_contents.examined == 19 |
| 311 |
|
| 312 |
- name: Get files and directories with depth |
| 313 |
find: |
| 314 |
paths: "{{ remote_tmp_dir_test }}" |
| 315 |
recurse: yes |
| 316 |
file_type: any |
| 317 |
depth: 2 |
| 318 |
register: contents_with_depth |
| 319 |
|
| 320 |
- assert: |
| 321 |
that: |
| 322 |
- contents_with_depth.matched == 9 |
| 323 |
# dir contents are considered until the depth exceeds the requested depth |
| 324 |
# there are 8 files/directories in the requested depth and 4 that exceed it by 1 |
| 325 |
- contents_with_depth.examined == 13 |
| 326 |
|
| 327 |
- name: Find files with depth |
| 328 |
find: |
| 329 |
paths: "{{ remote_tmp_dir_test }}" |
| 330 |
depth: 2 |
| 331 |
recurse: yes |
| 332 |
register: files_with_depth |
| 333 |
|
| 334 |
- assert: |
| 335 |
that: |
| 336 |
- files_with_depth.matched == 5 |
| 337 |
# dir contents are considered until the depth exceeds the requested depth |
| 338 |
# there are 8 files/directories in the requested depth and 4 that exceed it by 1 |
| 339 |
- files_with_depth.examined == 13 |
| 340 |
|
| 341 |
- name: exclude with regex |
| 342 |
find: |
| 343 |
paths: "{{ remote_tmp_dir_test }}" |
| 344 |
recurse: yes |
| 345 |
use_regex: true |
| 346 |
exclude: .*\.ogg |
| 347 |
register: find_test3 |
| 348 |
|
| 349 |
- set_fact: |
| 350 |
find_test3_list: "{{ find_test3.files|map(attribute='path') }}" |
| 351 |
|
| 352 |
- name: assert we skipped the ogg file |
| 353 |
assert: |
| 354 |
that: |
| 355 |
- remote_tmp_dir_test + "/e/f/g/h/8.ogg" not in find_test3_list |
| 356 |
|
| 357 |
- name: patterns with regex |
| 358 |
find: |
| 359 |
paths: "{{ remote_tmp_dir_test }}" |
| 360 |
recurse: yes |
| 361 |
use_regex: true |
| 362 |
patterns: .*\.ogg |
| 363 |
register: find_test4 |
| 364 |
|
| 365 |
- name: assert we matched the ogg file |
| 366 |
assert: |
| 367 |
that: |
| 368 |
- remote_tmp_dir_test ~ "/e/f/g/h/8.ogg" in find_test4.files|map(attribute="path") |
| 369 |
|
| 370 |
- name: create our age/size testing sub-directory |
| 371 |
file: |
| 372 |
path: "{{ remote_tmp_dir_test }}/astest" |
| 373 |
state: directory |
| 374 |
|
| 375 |
- name: create test file with old timestamps |
| 376 |
file: |
| 377 |
path: "{{ remote_tmp_dir_test }}/astest/old.txt" |
| 378 |
state: touch |
| 379 |
modification_time: "202001011200.0" |
| 380 |
|
| 381 |
- name: create test file with current timestamps |
| 382 |
file: |
| 383 |
path: "{{ remote_tmp_dir_test }}/astest/new.txt" |
| 384 |
state: touch |
| 385 |
|
| 386 |
- name: create hidden test file with current timestamps |
| 387 |
file: |
| 388 |
path: "{{ remote_tmp_dir_test }}/astest/.hidden.txt" |
| 389 |
state: touch |
| 390 |
|
| 391 |
- name: find files older than 1 week |
| 392 |
find: |
| 393 |
path: "{{ remote_tmp_dir_test }}/astest" |
| 394 |
age: 1w |
| 395 |
hidden: true |
| 396 |
register: result |
| 397 |
|
| 398 |
- set_fact: |
| 399 |
astest_list: "{{ result.files|map(attribute='path') }}" |
| 400 |
|
| 401 |
- name: assert we only find the old file |
| 402 |
assert: |
| 403 |
that: |
| 404 |
- result.matched == 1 |
| 405 |
- remote_tmp_dir_test + "/astest/old.txt" in astest_list |
| 406 |
|
| 407 |
- name: find files newer than 1 week |
| 408 |
find: |
| 409 |
path: "{{ remote_tmp_dir_test }}/astest" |
| 410 |
age: -1w |
| 411 |
register: result |
| 412 |
|
| 413 |
- set_fact: |
| 414 |
astest_list: "{{ result.files|map(attribute='path') }}" |
| 415 |
|
| 416 |
- name: assert we only find the current file |
| 417 |
assert: |
| 418 |
that: |
| 419 |
- result.matched == 1 |
| 420 |
- remote_tmp_dir_test + "/astest/new.txt" in astest_list |
| 421 |
|
| 422 |
- name: add some content to the new file |
| 423 |
shell: "echo hello world > {{ remote_tmp_dir_test }}/astest/new.txt" |
| 424 |
|
| 425 |
- name: find files with MORE than 5 bytes, also get checksums |
| 426 |
find: |
| 427 |
path: "{{ remote_tmp_dir_test }}/astest" |
| 428 |
size: 5 |
| 429 |
hidden: true |
| 430 |
get_checksum: true |
| 431 |
register: result |
| 432 |
|
| 433 |
- set_fact: |
| 434 |
astest_list: "{{ result.files|map(attribute='path') }}" |
| 435 |
|
| 436 |
- name: assert we only find the hello world file |
| 437 |
assert: |
| 438 |
that: |
| 439 |
- result.matched == 1 |
| 440 |
- remote_tmp_dir_test + "/astest/new.txt" in astest_list |
| 441 |
- '"checksum" in result.files[0]' |
| 442 |
|
| 443 |
- name: find ANY item with LESS than 5 bytes, also get checksums |
| 444 |
find: |
| 445 |
path: "{{ remote_tmp_dir_test }}/astest" |
| 446 |
size: -5 |
| 447 |
hidden: true |
| 448 |
get_checksum: true |
| 449 |
file_type: any |
| 450 |
register: result |
| 451 |
|
| 452 |
- set_fact: |
| 453 |
astest_list: "{{ result.files|map(attribute='path') }}" |
| 454 |
|
| 455 |
- name: assert we do not find the hello world file and a checksum is present |
| 456 |
assert: |
| 457 |
that: |
| 458 |
- result.matched == 2 |
| 459 |
- remote_tmp_dir_test + "/astest/old.txt" in astest_list |
| 460 |
- remote_tmp_dir_test + "/astest/.hidden.txt" in astest_list |
| 461 |
- '"checksum" in result.files[0]' |
| 462 |
|
| 463 |
# Test permission error is correctly handled by find module |
| 464 |
- vars: |
| 465 |
test_dir: /tmp/permission_test |
| 466 |
block: |
| 467 |
- name: Set up content |
| 468 |
file: |
| 469 |
path: "{{ test_dir }}/{{ item.name }}" |
| 470 |
state: "{{ item.state }}" |
| 471 |
mode: "{{ item.mode }}" |
| 472 |
owner: "{{ item.owner | default(omit) }}" |
| 473 |
group: "{{ item.group | default(omit) }}" |
| 474 |
loop: |
| 475 |
- name: readable |
| 476 |
state: directory |
| 477 |
owner: "{{ test_user_name }}" |
| 478 |
mode: "1711" |
| 479 |
- name: readable/1-unreadable |
| 480 |
state: directory |
| 481 |
mode: "0700" |
| 482 |
- name: readable/2-readable |
| 483 |
state: touch |
| 484 |
owner: "{{ test_user_name }}" |
| 485 |
mode: "0777" |
| 486 |
|
| 487 |
- name: Find a file in readable directory |
| 488 |
find: |
| 489 |
paths: "{{ test_dir }}/readable/" |
| 490 |
patterns: "*" |
| 491 |
recurse: true |
| 492 |
register: permission_issue |
| 493 |
become_user: "{{ test_user_name }}" |
| 494 |
|
| 495 |
- name: Find a file in readable directory |
| 496 |
find: |
| 497 |
paths: "{{ test_dir }}/readable/" |
| 498 |
patterns: "*" |
| 499 |
recurse: true |
| 500 |
register: permission_issue |
| 501 |
become_user: "{{ test_user_name }}" |
| 502 |
become: yes |
| 503 |
|
| 504 |
- name: Check if the skipped_paths are populated correctly with permission error |
| 505 |
assert: |
| 506 |
that: |
| 507 |
- permission_issue is success |
| 508 |
- not permission_issue.changed |
| 509 |
- permission_issue.skipped_paths|length == 1 |
| 510 |
- (test_dir + '/readable/1-unreadable') in permission_issue.skipped_paths |
| 511 |
- "'Permission denied' in permission_issue.skipped_paths[test_dir + '/readable/1-unreadable']" |
| 512 |
- permission_issue.matched == 1 |
| 513 |
always: |
| 514 |
- name: cleanup test directory |
| 515 |
file: |
| 516 |
dest: "{{ test_dir }}" |
| 517 |
state: absent |
| 518 |
|
| 519 |
- name: Run mode tests |
| 520 |
import_tasks: mode.yml |
| 521 |
|
| 522 |
- name: User block |
| 523 |
become: true |
| 524 |
become_user: "{{ test_user_name }}" |
| 525 |
block: |
| 526 |
- name: Create file in home dir |
| 527 |
copy: |
| 528 |
content: "" |
| 529 |
dest: ~/wharevs.txt |
| 530 |
mode: '0644' |
| 531 |
|
| 532 |
- name: Find file in home dir with ~/ |
| 533 |
find: |
| 534 |
paths: ~/ |
| 535 |
patterns: 'whar*' |
| 536 |
register: homedir_search |
| 537 |
|
| 538 |
- set_fact: |
| 539 |
astest_list: "{{ homedir_search.files | map(attribute='path') }}" |
| 540 |
|
| 541 |
- name: Check if found |
| 542 |
assert: |
| 543 |
that: |
| 544 |
- homedir_search is success |
| 545 |
- homedir_search.matched == 1 |
| 546 |
- (homedir + '/wharevs.txt') in astest_list |
| 547 |
vars: |
| 548 |
homedir: "{{ test_user.home }}" |