Debian Codegraph @

test/integration/targets/file/tasks/state_link.yml

raw
1 # file module tests for dealing with symlinks (state=link)
2
3 - name: Initialize the test output dir
4 import_tasks: initialize.yml
5
6 #
7 # Basic absolute symlink to a file
8 #
9 - name: create soft link to file
10 file: src={{output_file}} dest={{remote_tmp_dir_test}}/soft.txt state=link
11 register: file1_result
12
13 - name: Get stat info for the link
14 stat:
15 path: '{{ remote_tmp_dir_test }}/soft.txt'
16 follow: False
17 register: file1_link_stat
18
19 - name: verify that the symlink was created correctly
20 assert:
21 that:
22 - 'file1_result is changed'
23 - 'file1_link_stat["stat"].islnk'
24 - 'file1_link_stat["stat"].lnk_target | expanduser == output_file | expanduser'
25
26 #
27 # Change an absolute soft link into a relative soft link
28 #
29 - name: change soft link to relative
30 file: src={{output_file|basename}} dest={{remote_tmp_dir_test}}/soft.txt state=link
31 register: file2_result
32 diff: true
33
34 - name: Get stat info for the link
35 stat:
36 path: '{{ remote_tmp_dir_test }}/soft.txt'
37 follow: False
38 register: file2_link_stat
39
40 - name: verify that the file was marked as changed
41 assert:
42 that:
43 - "file2_result is changed"
44 - "file2_result.diff.before.src == remote_file_expanded"
45 - "file2_result.diff.after.src == remote_file_expanded|basename"
46 - "file2_link_stat['stat'].islnk"
47 - "file2_link_stat['stat'].lnk_target == remote_file_expanded | basename"
48
49 #
50 # Check that creating the soft link a second time was idempotent
51 #
52 - name: soft link idempotency check
53 file: src={{output_file|basename}} dest={{remote_tmp_dir_test}}/soft.txt state=link
54 register: file3_result
55
56 - name: Get stat info for the link
57 stat:
58 path: '{{ remote_tmp_dir_test }}/soft.txt'
59 follow: False
60 register: file3_link_stat
61
62 - name: verify that the file was not marked as changed
63 assert:
64 that:
65 - "not file3_result is changed"
66 - "file3_link_stat['stat'].islnk"
67 - "file3_link_stat['stat'].lnk_target == remote_file_expanded | basename"
68
69 #
70 # Test symlink to nonexistent files
71 #
72 - name: fail to create soft link to non existent file
73 file:
74 src: '/nonexistent'
75 dest: '{{remote_tmp_dir_test}}/soft2.txt'
76 state: 'link'
77 force: False
78 register: file4_result
79 ignore_errors: true
80
81 - name: verify that link was not created
82 assert:
83 that:
84 - "file4_result is failed"
85
86 - name: force creation soft link to non existent
87 file:
88 src: '/nonexistent'
89 dest: '{{ remote_tmp_dir_test}}/soft2.txt'
90 state: 'link'
91 force: True
92 register: file5_result
93
94 - name: Get stat info for the link
95 stat:
96 path: '{{ remote_tmp_dir_test }}/soft2.txt'
97 follow: False
98 register: file5_link_stat
99
100 - name: verify that link was created
101 assert:
102 that:
103 - "file5_result is changed"
104 - "file5_link_stat['stat'].islnk"
105 - "file5_link_stat['stat'].lnk_target == '/nonexistent'"
106
107 - name: Prove idempotence of force creation soft link to non existent
108 file:
109 src: '/nonexistent'
110 dest: '{{ remote_tmp_dir_test }}/soft2.txt'
111 state: 'link'
112 force: True
113 register: file6a_result
114
115 - name: verify that the link to nonexistent is idempotent
116 assert:
117 that:
118 - "file6a_result.changed == false"
119
120 # In order for a symlink in a sticky world writable directory to be followed, it must
121 # either be owned by the follower,
122 # or the directory and symlink must have the same owner.
123 - name: symlink in sticky directory
124 block:
125 - name: Create remote unprivileged remote user
126 user:
127 name: '{{ remote_unprivileged_user }}'
128 register: user
129 notify: remove users
130
131 - name: Create a local temporary directory
132 tempfile:
133 state: directory
134 register: tempdir
135
136 - name: Set sticky bit
137 file:
138 path: '{{ tempdir.path }}'
139 mode: o=rwXt
140
141 - name: 'Check mode: force creation soft link in sticky directory owned by another user (mode is used)'
142 file:
143 src: '{{ user.home }}/nonexistent'
144 dest: '{{ tempdir.path }}/soft3.txt'
145 mode: 0640
146 state: 'link'
147 owner: '{{ remote_unprivileged_user }}'
148 force: true
149 follow: false
150 check_mode: true
151 register: missing_dst_no_follow_enable_force_use_mode1
152
153 - name: force creation soft link in sticky directory owned by another user (mode is used)
154 file:
155 src: '{{ user.home }}/nonexistent'
156 dest: '{{ tempdir.path }}/soft3.txt'
157 mode: 0640
158 state: 'link'
159 owner: '{{ remote_unprivileged_user }}'
160 force: true
161 follow: false
162 register: missing_dst_no_follow_enable_force_use_mode2
163
164 - name: Get stat info for the link
165 stat:
166 path: '{{ tempdir.path }}/soft3.txt'
167 follow: false
168 register: soft3_result
169
170 - name: 'Idempotence: force creation soft link in sticky directory owned by another user (mode is used)'
171 file:
172 src: '{{ user.home }}/nonexistent'
173 dest: '{{ tempdir.path }}/soft3.txt'
174 mode: 0640
175 state: 'link'
176 owner: '{{ remote_unprivileged_user }}'
177 force: yes
178 follow: false
179 register: missing_dst_no_follow_enable_force_use_mode3
180 always:
181 - name: Delete remote unprivileged remote user
182 user:
183 name: '{{ remote_unprivileged_user }}'
184 state: absent
185 force: yes
186 remove: yes
187
188 - name: Delete unprivileged user home and tempdir
189 file:
190 path: "{{ item }}"
191 state: absent
192 loop:
193 - '{{ tempdir.path }}'
194 - '{{ user.home }}'
195
196 - name: verify that link was created
197 assert:
198 that:
199 - "missing_dst_no_follow_enable_force_use_mode1 is changed"
200 - "missing_dst_no_follow_enable_force_use_mode2 is changed"
201 - "missing_dst_no_follow_enable_force_use_mode3 is not changed"
202 - "soft3_result['stat'].islnk"
203 - "soft3_result['stat'].lnk_target == user.home + '/nonexistent'"
204
205 #
206 # Test creating a link to a directory https://github.com/ansible/ansible/issues/1369
207 #
208 - name: create soft link to directory using absolute path
209 file:
210 src: '/'
211 dest: '{{ remote_tmp_dir_test }}/root'
212 state: 'link'
213 register: file6_result
214
215 - name: Get stat info for the link
216 stat:
217 path: '{{ remote_tmp_dir_test }}/root'
218 follow: False
219 register: file6_link_stat
220
221 - name: Get stat info for the pointed to file
222 stat:
223 path: '{{ remote_tmp_dir_test }}/root'
224 follow: True
225 register: file6_links_dest_stat
226
227 - name: Get stat info for the file we intend to point to
228 stat:
229 path: '/'
230 follow: False
231 register: file6_dest_stat
232
233 - name: verify that the link was created correctly
234 assert:
235 that:
236 # file command reports it created something
237 - "file6_result.changed == true"
238 # file command created a link
239 - 'file6_link_stat["stat"]["islnk"]'
240 # Link points to the right path
241 - 'file6_link_stat["stat"]["lnk_target"] == "/"'
242 # The link target and the file we intended to link to have the same inode
243 - 'file6_links_dest_stat["stat"]["inode"] == file6_dest_stat["stat"]["inode"]'
244
245 #
246 # Test creating a relative link
247 #
248
249 # Relative link to file
250 - name: create a test sub-directory to link to
251 file:
252 dest: '{{ remote_tmp_dir_test }}/sub1'
253 state: 'directory'
254
255 - name: create a file to link to in the test sub-directory
256 file:
257 dest: '{{ remote_tmp_dir_test }}/sub1/file1'
258 state: 'touch'
259
260 - name: create another test sub-directory to place links within
261 file:
262 dest: '{{remote_tmp_dir_test}}/sub2'
263 state: 'directory'
264
265 - name: create soft link to relative file
266 file:
267 src: '../sub1/file1'
268 dest: '{{ remote_tmp_dir_test }}/sub2/link1'
269 state: 'link'
270 register: file7_result
271
272 - name: Get stat info for the link
273 stat:
274 path: '{{ remote_tmp_dir_test }}/sub2/link1'
275 follow: False
276 register: file7_link_stat
277
278 - name: Get stat info for the pointed to file
279 stat:
280 path: '{{ remote_tmp_dir_test }}/sub2/link1'
281 follow: True
282 register: file7_links_dest_stat
283
284 - name: Get stat info for the file we intend to point to
285 stat:
286 path: '{{ remote_tmp_dir_test }}/sub1/file1'
287 follow: False
288 register: file7_dest_stat
289
290 - name: verify that the link was created correctly
291 assert:
292 that:
293 # file command reports it created something
294 - "file7_result.changed == true"
295 # file command created a link
296 - 'file7_link_stat["stat"]["islnk"]'
297 # Link points to the right path
298 - 'file7_link_stat["stat"]["lnk_target"] == "../sub1/file1"'
299 # The link target and the file we intended to link to have the same inode
300 - 'file7_links_dest_stat["stat"]["inode"] == file7_dest_stat["stat"]["inode"]'
301
302 # Relative link to directory
303 - name: create soft link to relative directory
304 file:
305 src: sub1
306 dest: '{{ remote_tmp_dir_test }}/sub1-link'
307 state: 'link'
308 register: file8_result
309
310 - name: Get stat info for the link
311 stat:
312 path: '{{ remote_tmp_dir_test }}/sub1-link'
313 follow: False
314 register: file8_link_stat
315
316 - name: Get stat info for the pointed to file
317 stat:
318 path: '{{ remote_tmp_dir_test }}/sub1-link'
319 follow: True
320 register: file8_links_dest_stat
321
322 - name: Get stat info for the file we intend to point to
323 stat:
324 path: '{{ remote_tmp_dir_test }}/sub1'
325 follow: False
326 register: file8_dest_stat
327
328 - name: verify that the link was created correctly
329 assert:
330 that:
331 # file command reports it created something
332 - "file8_result.changed == true"
333 # file command created a link
334 - 'file8_link_stat["stat"]["islnk"]'
335 # Link points to the right path
336 - 'file8_link_stat["stat"]["lnk_target"] == "sub1"'
337 # The link target and the file we intended to link to have the same inode
338 - 'file8_links_dest_stat["stat"]["inode"] == file8_dest_stat["stat"]["inode"]'
339
340 # test the file module using follow=yes, so that the target of a
341 # symlink is modified, rather than the link itself
342
343 - name: create a test file
344 copy:
345 dest: '{{remote_tmp_dir_test}}/test_follow'
346 content: 'this is a test file\n'
347 mode: 0666
348
349 - name: create a symlink to the test file
350 file:
351 path: '{{remote_tmp_dir_test}}/test_follow_link'
352 src: './test_follow'
353 state: 'link'
354
355 - name: modify the permissions on the link using follow=yes
356 file:
357 path: '{{remote_tmp_dir_test}}/test_follow_link'
358 mode: 0644
359 follow: yes
360 register: file9_result
361
362 - name: stat the link target
363 stat:
364 path: '{{remote_tmp_dir_test}}/test_follow'
365 register: file9_stat
366
367 - name: assert that the chmod worked
368 assert:
369 that:
370 - 'file9_result is changed'
371 - 'file9_stat["stat"]["mode"] == "0644"'
372
373 #
374 # Test modifying the permissions of a link itself
375 #
376 - name: attempt to modify the permissions of the link itself
377 file:
378 path: '{{remote_tmp_dir_test}}/test_follow_link'
379 state: 'link'
380 mode: 0600
381 follow: False
382 register: file10_result
383
384 # Whether the link itself changed is platform dependent! (BSD vs Linux?)
385 # Just check that the underlying file was not changed
386 - name: stat the link target
387 stat:
388 path: '{{remote_tmp_dir_test}}/test_follow'
389 register: file10_target_stat
390
391 - name: assert that the link target was unmodified
392 assert:
393 that:
394 - 'file10_target_stat["stat"]["mode"] == "0644"'
395
396
397 # https://github.com/ansible/ansible/issues/56928
398 - block:
399
400 - name: Create a testing file
401 file:
402 path: "{{ remote_tmp_dir_test }}/test_follow1"
403 state: touch
404
405 - name: Create a symlink and change mode of the original file, since follow == yes by default
406 file:
407 src: "{{ remote_tmp_dir_test }}/test_follow1"
408 dest: "{{ remote_tmp_dir_test }}/test_follow1_link"
409 state: link
410 mode: 0700
411
412 - name: stat the original file
413 stat:
414 path: "{{ remote_tmp_dir_test }}/test_follow1"
415 register: stat_out
416
417 - name: Check if the mode of the original file was set
418 assert:
419 that:
420 - 'stat_out.stat.mode == "0700"'
421
422 always:
423 - name: Clean up
424 file:
425 path: "{{ item }}"
426 state: absent
427 loop:
428 - "{{ remote_tmp_dir_test }}/test_follow1"
429 - "{{ remote_tmp_dir_test }}/test_follow1_link"
430
431 # END #56928
432
433
434 # Test failure with src and no state parameter
435 - name: Specify src without state
436 file:
437 src: "{{ output_file }}"
438 dest: "{{ remote_tmp_dir_test }}/link.txt"
439 ignore_errors: yes
440 register: src_state
441
442 - name: Ensure src without state failed
443 assert:
444 that:
445 - src_state is failed
446 - "'src option requires state to be' in src_state.msg"
447
448 - name: Create without src
449 file:
450 state: link
451 dest: "{{ output_dir }}/link.txt"
452 ignore_errors: yes
453 register: create_without_src
454
455 - name: Ensure create without src failed
456 assert:
457 that:
458 - create_without_src is failed
459 - "'src is required' in create_without_src.msg"
460
461 # Test creating a symlink when the destination exists and is a file
462 - name: create a test file
463 copy:
464 dest: '{{ remote_tmp_dir_test }}/file.txt'
465 content: 'this is a test file\n'
466 mode: 0666
467
468 - name: Create a symlink with dest already a file
469 file:
470 src: '{{ output_file }}'
471 dest: '{{ remote_tmp_dir_test }}/file.txt'
472 state: link
473 ignore_errors: true
474 register: dest_is_existing_file_fail
475
476 - name: Stat to make sure the symlink was not created
477 stat:
478 path: '{{ remote_tmp_dir_test }}/file.txt'
479 follow: false
480 register: dest_is_existing_file_fail_stat
481
482 - name: Forcefully a symlink with dest already a file
483 file:
484 src: '{{ output_file }}'
485 dest: '{{ remote_tmp_dir_test }}/file.txt'
486 state: link
487 force: true
488 register: dest_is_existing_file_force
489
490 - name: Stat to make sure the symlink was created
491 stat:
492 path: '{{ remote_tmp_dir_test }}/file.txt'
493 follow: false
494 register: dest_is_existing_file_force_stat
495
496 - assert:
497 that:
498 - dest_is_existing_file_fail is failed
499 - not dest_is_existing_file_fail_stat.stat.islnk
500 - dest_is_existing_file_force is changed
501 - dest_is_existing_file_force_stat.stat.exists
502 - dest_is_existing_file_force_stat.stat.islnk