summaryrefslogtreecommitdiff
path: root/test/integration/targets/file/tasks/main.yml
blob: 34ae4ba4b7bb3b097762c0fbad7dcdb1cb9e6d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# Test code for the file module.
# (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.com>

# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

- set_fact: output_file={{output_dir}}/foo.txt

# same as expanduser & expandvars called on managed host
- command: 'echo {{ output_file }}'
  register: echo

- set_fact:
    remote_file_expanded: '{{ echo.stdout }}'

# Import the test tasks
- name: Run tests for state=link
  import_tasks: state_link.yml

- name: Run tests for directory as dest
  import_tasks: directory_as_dest.yml

- name: Run tests for unicode
  import_tasks: unicode_path.yml
  environment:
    LC_ALL: C
    LANG: C

- name: decide to include or not include selinux tests
  include_tasks: selinux_tests.yml
  when: selinux_installed is defined and selinux_installed.stdout != "" and selinux_enabled.stdout != "Disabled"

- name: Initialize the test output dir
  import_tasks: initialize.yml

- name: Test _diff_peek
  import_tasks: diff_peek.yml


# These tests need to be organized by state parameter into separate files later

- name: verify that we are checking a file and it is present
  file: path={{output_file}} state=file
  register: file_result

- name: verify that the file was marked as changed
  assert:
    that:
      - "file_result.changed == false"
      - "file_result.state == 'file'"

- name: Make sure file does not exist
  file:
    path: /tmp/ghost
    state: absent

- name: Target a file that does not exist
  file:
    path: /tmp/ghost
  ignore_errors: yes
  register: ghost_file_result

- name: Validate ghost file results
  assert:
    that:
      - ghost_file_result is failed
      - ghost_file_result is not changed
      - ghost_file_result.state == 'absent'
      - "'cannot continue' in ghost_file_result.msg"

- name: verify that we are checking an absent file
  file: path={{output_dir}}/bar.txt state=absent
  register: file2_result

- name: verify that the file was marked as changed
  assert:
    that:
      - "file2_result.changed == false"
      - "file2_result.state == 'absent'"

- name: verify we can touch a file
  file: path={{output_dir}}/baz.txt state=touch
  register: file3_result

- name: verify that the file was marked as changed
  assert:
    that:
      - "file3_result.changed == true"
      - "file3_result.state == 'file'"
      - "file3_result.mode == '0644'"

- name: change file mode
  file: path={{output_dir}}/baz.txt mode=0600
  register: file4_result

- name: verify that the file was marked as changed
  assert:
    that:
      - "file4_result.changed == true"
      - "file4_result.mode == '0600'"

- name: define file to verify chattr/lsattr with
  set_fact:
    attributes_file: "{{ output_dir }}/attributes.txt"
    attributes_supported: no

- name: create file to verify chattr/lsattr with
  command: touch "{{ attributes_file }}"

- name: add "A" attribute to file
  command: chattr +A "{{ attributes_file }}"
  ignore_errors: yes

- name: get attributes from file
  # Use of `-v` is important, as that is what the module does (through `set_attributes_if_different` and then `get_file_attributes` in basic.py).
  # On some systems, such as in containers, attributes work, but file versions may not.
  # It should be possible to update `set_attributes_if_different` in the future to not use `-v` since the file version is unrelated to the attributes.
  command: lsattr -vd "{{ attributes_file }}"
  register: attribute_A_set
  ignore_errors: yes

- name: remove "A" attribute from file
  command: chattr -A "{{ attributes_file }}"
  ignore_errors: yes

- name: get attributes from file
  # See the note above on use of the `-v` option.
  command: lsattr -vd "{{ attributes_file }}"
  register: attribute_A_unset
  ignore_errors: yes

- name: determine if chattr/lsattr is supported
  set_fact:
    attributes_supported: yes
  when:
    - attribute_A_set is success
    - "'A' in attribute_A_set.stdout_lines[0].split()[1]"
    - attribute_A_unset is success
    - "'A' not in attribute_A_unset.stdout_lines[0].split()[1]"

- name: explicitly set file attribute "A"
  file: path={{output_dir}}/baz.txt attributes=A
  register: file_attributes_result
  ignore_errors: True
  when: attributes_supported

- name: add file attribute "A"
  file: path={{output_dir}}/baz.txt attributes=+A
  register: file_attributes_result_2
  when: file_attributes_result is changed

- name: verify that the file was not marked as changed
  assert:
    that:
      - "file_attributes_result_2 is not changed"
  when: file_attributes_result is changed

- name: remove file attribute "A"
  file: path={{output_dir}}/baz.txt attributes=-A
  register: file_attributes_result_3
  ignore_errors: True

- name: explicitly remove file attributes
  file: path={{output_dir}}/baz.txt attributes=""
  register: file_attributes_result_4
  when: file_attributes_result_3 is changed

- name: verify that the file was not marked as changed
  assert:
    that:
      - "file_attributes_result_4 is not changed"
  when: file_attributes_result_4 is changed

- name: change ownership and group
  file: path={{output_dir}}/baz.txt owner=1234 group=1234

- name: Get stat info to check atime later
  stat: path={{output_dir}}/baz.txt
  register: file_attributes_result_5_before

- name: updates access time
  file: path={{output_dir}}/baz.txt access_time=now
  register: file_attributes_result_5

- name: Get stat info to check atime later
  stat: path={{output_dir}}/baz.txt
  register: file_attributes_result_5_after

- name: verify that the file was marked as changed and atime changed
  assert:
    that:
      - "file_attributes_result_5 is changed"
      - "file_attributes_result_5_after['stat']['atime'] != file_attributes_result_5_before['stat']['atime']"

- name: setup a tmp-like directory for ownership test
  file: path=/tmp/worldwritable mode=1777 state=directory

- name: Ask to create a file without enough perms to change ownership
  file: path=/tmp/worldwritable/baz.txt state=touch owner=root
  become: yes
  become_user: nobody
  register: chown_result
  ignore_errors: True

- name: Ask whether the new file exists
  stat: path=/tmp/worldwritable/baz.txt
  register: file_exists_result

- name: Verify that the file doesn't exist on failure
  assert:
    that:
      - "chown_result.failed == True"
      - "file_exists_result.stat.exists == False"

- name: clean up
  file: path=/tmp/worldwritable state=absent

- name: create hard link to file
  file: src={{output_file}} dest={{output_dir}}/hard.txt state=hard
  register: file6_result

- name: verify that the file was marked as changed
  assert:
    that:
      - "file6_result.changed == true"

- name: touch a hard link
  file:
    dest: '{{ output_dir }}/hard.txt'
    state: 'touch'
  register: file6_touch_result

- name: verify that the hard link was touched
  assert:
    that:
      - "file6_touch_result.changed == true"

- name: stat1
  stat: path={{output_file}}
  register: hlstat1

- name: stat2
  stat: path={{output_dir}}/hard.txt
  register: hlstat2

- name: verify that hard link is still the same after timestamp updated
  assert:
    that:
      - "hlstat1.stat.inode == hlstat2.stat.inode"

- name: create hard link to file 2
  file: src={{output_file}} dest={{output_dir}}/hard.txt state=hard
  register: hlink_result

- name: verify that hard link creation is idempotent
  assert:
    that:
      - "hlink_result.changed == False"

- name: Change mode on a hard link
  file: src={{output_file}} dest={{output_dir}}/hard.txt mode=0701
  register: file6_mode_change

- name: verify that the hard link was touched
  assert:
    that:
      - "file6_touch_result.changed == true"

- name: stat1
  stat: path={{output_file}}
  register: hlstat1

- name: stat2
  stat: path={{output_dir}}/hard.txt
  register: hlstat2

- name: verify that hard link is still the same after timestamp updated
  assert:
    that:
      - "hlstat1.stat.inode == hlstat2.stat.inode"
      - "hlstat1.stat.mode == '0701'"

- name: create a directory
  file: path={{output_dir}}/foobar state=directory
  register: file7_result

- name: verify that the file was marked as changed
  assert:
    that:
      - "file7_result.changed == true"
      - "file7_result.state == 'directory'"

- name: determine if selinux is installed
  shell: which getenforce || exit 0
  register: selinux_installed

- name: determine if selinux is enabled
  shell: getenforce
  register: selinux_enabled
  when: selinux_installed.stdout != ""
  ignore_errors: true

- name: remove directory foobar
  file: path={{output_dir}}/foobar state=absent

- name: remove file foo.txt
  file: path={{output_dir}}/foo.txt state=absent

- name: remove file bar.txt
  file: path={{output_dir}}/foo.txt state=absent

- name: remove file baz.txt
  file: path={{output_dir}}/foo.txt state=absent

- name: copy directory structure over
  copy: src=foobar dest={{output_dir}}

- name: check what would be removed if folder state was absent and diff is enabled
  file:
    path: "{{ item }}"
    state: absent
  check_mode: yes
  diff: yes
  with_items:
    - "{{ output_dir }}"
    - "{{ output_dir }}/foobar/fileA"
  register: folder_absent_result

- name: 'assert that the "absent" state lists expected files and folders for only directories'
  assert:
    that:
      - folder_absent_result.results[0].diff.before.path_content is defined
      - folder_absent_result.results[1].diff.before.path_content is not defined
      - test_folder in folder_absent_result.results[0].diff.before.path_content.directories
      - test_file in folder_absent_result.results[0].diff.before.path_content.files
  vars:
    test_folder: "{{ folder_absent_result.results[0].path }}/foobar"
    test_file: "{{ folder_absent_result.results[0].path }}/foobar/fileA"

- name: Change ownership of a directory with recurse=no(default)
  file: path={{output_dir}}/foobar owner=1234

- name: verify that the permission of the directory was set
  file: path={{output_dir}}/foobar state=directory
  register: file8_result

- name: assert that the directory has changed to have owner 1234
  assert:
    that:
      - "file8_result.uid == 1234"

- name: verify that the permission of a file under the directory was not set
  file: path={{output_dir}}/foobar/fileA state=file
  register: file9_result

- name: assert the file owner has not changed to 1234
  assert:
    that:
      - "file9_result.uid != 1234"

- name: change the ownership of a directory with recurse=yes
  file: path={{output_dir}}/foobar owner=1235 recurse=yes

- name: verify that the permission of the directory was set
  file: path={{output_dir}}/foobar state=directory
  register: file10_result

- name: assert that the directory has changed to have owner 1235
  assert:
    that:
      - "file10_result.uid == 1235"

- name: verify that the permission of a file under the directory was not set
  file: path={{output_dir}}/foobar/fileA state=file
  register: file11_result

- name: assert that the file has changed to have owner 1235
  assert:
    that:
      - "file11_result.uid == 1235"

- name: remove directory foobar
  file: path={{output_dir}}/foobar state=absent
  register: file14_result

- name: verify that the directory was removed
  assert:
    that:
      - 'file14_result.changed == true'
      - 'file14_result.state == "absent"'

- name: create a test sub-directory
  file: dest={{output_dir}}/sub1 state=directory
  register: file15_result

- name: verify that the new directory was created
  assert:
    that:
      - 'file15_result.changed == true'
      - 'file15_result.state == "directory"'

- name: create test files in the sub-directory
  file: dest={{output_dir}}/sub1/{{item}} state=touch
  with_items:
  - file1
  - file2
  - file3
  register: file16_result

- name: verify the files were created
  assert:
    that:
      - 'item.changed == true'
      - 'item.state == "file"'
  with_items: "{{file16_result.results}}"

- name: test file creation with symbolic mode
  file: dest={{output_dir}}/test_symbolic state=touch mode=u=rwx,g=rwx,o=rwx
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0777'

- name: modify symbolic mode for all
  file: dest={{output_dir}}/test_symbolic state=touch mode=a=r
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0444'

- name: modify symbolic mode for owner
  file: dest={{output_dir}}/test_symbolic state=touch mode=u+w
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0644'

- name: modify symbolic mode for group
  file: dest={{output_dir}}/test_symbolic state=touch mode=g+w
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0664'

- name: modify symbolic mode for world
  file: dest={{output_dir}}/test_symbolic state=touch mode=o+w
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0666'

- name: modify symbolic mode for owner
  file: dest={{output_dir}}/test_symbolic state=touch mode=u+x
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0766'

- name: modify symbolic mode for group
  file: dest={{output_dir}}/test_symbolic state=touch mode=g+x
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0776'

- name: modify symbolic mode for world
  file: dest={{output_dir}}/test_symbolic state=touch mode=o+x
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0777'

- name: remove symbolic mode for world
  file: dest={{output_dir}}/test_symbolic state=touch mode=o-wx
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0774'

- name: remove symbolic mode for group
  file: dest={{output_dir}}/test_symbolic state=touch mode=g-wx
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0744'

- name: remove symbolic mode for owner
  file: dest={{output_dir}}/test_symbolic state=touch mode=u-wx
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0444'

- name: set sticky bit with symbolic mode
  file: dest={{output_dir}}/test_symbolic state=touch mode=o+t
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '01444'

- name: remove sticky bit with symbolic mode
  file: dest={{output_dir}}/test_symbolic state=touch mode=o-t
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0444'

- name: add setgid with symbolic mode
  file: dest={{output_dir}}/test_symbolic state=touch mode=g+s
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '02444'

- name: remove setgid with symbolic mode
  file: dest={{output_dir}}/test_symbolic state=touch mode=g-s
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0444'

- name: add setuid with symbolic mode
  file: dest={{output_dir}}/test_symbolic state=touch mode=u+s
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '04444'

- name: remove setuid with symbolic mode
  file: dest={{output_dir}}/test_symbolic state=touch mode=u-s
  register: result

- name: assert file mode
  assert:
    that:
    - result.mode == '0444'

# https://github.com/ansible/ansible/issues/50943
# Need to use /tmp as nobody can't access output_dir at all
- name: create file as root with all write permissions
  file: dest=/tmp/write_utime state=touch mode=0666 owner={{ansible_user_id}}

- name: Pause to ensure stat times are not the exact same
  pause:
    seconds: 1

- block:
  - name: get previous time
    stat: path=/tmp/write_utime
    register: previous_time

  - name: pause for 1 second to ensure the next touch is newer
    pause: seconds=1

  - name: touch file as nobody
    file: dest=/tmp/write_utime state=touch
    become: True
    become_user: nobody
    register: result

  - name: get new time
    stat: path=/tmp/write_utime
    register: current_time

  always:
  - name: remove test utime file
    file: path=/tmp/write_utime state=absent

- name: assert touch file as nobody
  assert:
    that:
    - result is changed
    - current_time.stat.atime > previous_time.stat.atime
    - current_time.stat.mtime > previous_time.stat.mtime

# Follow + recursive tests
- name: create a toplevel directory
  file: path={{output_dir}}/test_follow_rec state=directory mode=0755

- name: create a file outside of the toplevel
  file: path={{output_dir}}/test_follow_rec_target_file state=touch mode=0700

- name: create a directory outside of the toplevel
  file: path={{output_dir}}/test_follow_rec_target_dir state=directory mode=0700

- name: create a file inside of the link target directory
  file: path={{output_dir}}/test_follow_rec_target_dir/foo state=touch mode=0700

- name: create a symlink to the file
  file: path={{output_dir}}/test_follow_rec/test_link state=link src="../test_follow_rec_target_file"

- name: create a symlink to the directory
  file: path={{output_dir}}/test_follow_rec/test_link_dir state=link src="../test_follow_rec_target_dir"

- name: create a symlink to a nonexistent file
  file: path={{output_dir}}/test_follow_rec/nonexistent state=link src=does_not_exist force=True

- name: try to change permissions without following symlinks
  file: path={{output_dir}}/test_follow_rec follow=False mode="a-x" recurse=True

- name: stat the link file target
  stat: path={{output_dir}}/test_follow_rec_target_file
  register: file_result

- name: stat the link dir target
  stat: path={{output_dir}}/test_follow_rec_target_dir
  register: dir_result

- name: stat the file inside the link dir target
  stat: path={{output_dir}}/test_follow_rec_target_dir/foo
  register: file_in_dir_result

- name: assert that the link targets were unmodified
  assert:
    that:
    - file_result.stat.mode == '0700'
    - dir_result.stat.mode == '0700'
    - file_in_dir_result.stat.mode == '0700'

- name: try to change permissions with following symlinks
  file: path={{output_dir}}/test_follow_rec follow=True mode="a-x" recurse=True

- name: stat the link file target
  stat: path={{output_dir}}/test_follow_rec_target_file
  register: file_result

- name: stat the link dir target
  stat: path={{output_dir}}/test_follow_rec_target_dir
  register: dir_result

- name: stat the file inside the link dir target
  stat: path={{output_dir}}/test_follow_rec_target_dir/foo
  register: file_in_dir_result

- name: assert that the link targets were modified
  assert:
    that:
    - file_result.stat.mode == '0600'
    - dir_result.stat.mode == '0600'
    - file_in_dir_result.stat.mode == '0600'

# https://github.com/ansible/ansible/issues/55971
- name: Test missing src and path
  file:
    state: hard
  register: file_error1
  ignore_errors: yes

- assert:
    that:
      - "file_error1 is failed"
      - "file_error1.msg == 'missing required arguments: path'"

- name: Test missing src
  file:
    dest: "{{ output_dir }}/hard.txt"
    state: hard
  register: file_error2
  ignore_errors: yes

- assert:
    that:
      - "file_error2 is failed"
      - "file_error2.msg == 'src is required for creating new hardlinks'"

- name: Test non-existing src
  file:
    src: non-existing-file-that-does-not-exist.txt
    dest: "{{ output_dir }}/hard.txt"
    state: hard
  register: file_error3
  ignore_errors: yes

- assert:
    that:
      - "file_error3 is failed"
      - "file_error3.msg == 'src does not exist'"
      - "file_error3.dest == '{{ output_dir }}/hard.txt' | expanduser"
      - "file_error3.src == 'non-existing-file-that-does-not-exist.txt'"

- block:
  - name: Create a testing file
    file:
      dest: original_file.txt
      state: touch

  - name: Test relative path with state=hard
    file:
      src: original_file.txt
      dest: hard_link_file.txt
      state: hard
    register: hard_link_relpath

  - name: Just check if it was successful, we don't care about the actual hard link in this test
    assert:
      that:
        - "hard_link_relpath is success"

  always:
    - name: Clean up
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - original_file.txt
        - hard_link_file.txt

# END #55971