1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/test/integration/targets/file/tasks/state_link.yml

348 lines
8.9 KiB
YAML
Raw Normal View History

# file module tests for dealing with symlinks (state=link)
- name: Initialize the test output dir
include: initialize.yml
#
# Basic absolute symlink to a file
#
- name: create soft link to file
file: src={{output_file}} dest={{output_dir}}/soft.txt state=link
register: file1_result
- name: Get stat info for the link
stat:
path: '{{ output_dir }}/soft.txt'
follow: False
register: file1_link_stat
- name: verify that the symlink was created correctly
assert:
that:
- 'file1_result is changed'
- 'file1_link_stat["stat"].islnk'
- 'file1_link_stat["stat"].lnk_target | expanduser == output_file | expanduser'
#
# Change an absolute soft link into a relative soft link
#
- name: change soft link to relative
file: src={{output_file|basename}} dest={{output_dir}}/soft.txt state=link
register: file2_result
- name: Get stat info for the link
stat:
path: '{{ output_dir }}/soft.txt'
follow: False
register: file2_link_stat
- name: verify that the file was marked as changed
assert:
that:
- "file2_result is changed"
- "file2_result.diff.before.src == remote_file_expanded"
- "file2_result.diff.after.src == remote_file_expanded|basename"
- "file2_link_stat['stat'].islnk"
- "file2_link_stat['stat'].lnk_target == remote_file_expanded | basename"
#
# Check that creating the soft link a second time was idempotent
#
- name: soft link idempotency check
file: src={{output_file|basename}} dest={{output_dir}}/soft.txt state=link
register: file3_result
- name: Get stat info for the link
stat:
path: '{{ output_dir }}/soft.txt'
follow: False
register: file3_link_stat
- name: verify that the file was not marked as changed
assert:
that:
- "not file3_result is changed"
- "file3_link_stat['stat'].islnk"
- "file3_link_stat['stat'].lnk_target == remote_file_expanded | basename"
#
# Test symlink to nonexistent files
#
- name: fail to create soft link to non existent file
file:
src: '/nonexistent'
dest: '{{output_dir}}/soft2.txt'
state: 'link'
force: False
register: file4_result
ignore_errors: true
- name: verify that link was not created
assert:
that:
- "file4_result is failed"
- name: force creation soft link to non existent
file:
src: '/nonexistent'
dest: '{{ output_dir}}/soft2.txt'
state: 'link'
force: True
register: file5_result
- name: Get stat info for the link
stat:
path: '{{ output_dir }}/soft2.txt'
follow: False
register: file5_link_stat
- name: verify that link was created
assert:
that:
- "file5_result is changed"
- "file5_link_stat['stat'].islnk"
- "file5_link_stat['stat'].lnk_target == '/nonexistent'"
- name: Prove idempotence of force creation soft link to non existent
file:
src: '/nonexistent'
dest: '{{ output_dir }}/soft2.txt'
state: 'link'
force: True
register: file6a_result
- name: verify that the link to nonexistent is idempotent
assert:
that:
- "file6a_result.changed == false"
#
# Test creating a link to a directory https://github.com/ansible/ansible/issues/1369
#
- name: create soft link to directory using absolute path
file:
src: '/'
dest: '{{ output_dir }}/root'
state: 'link'
register: file6_result
- name: Get stat info for the link
stat:
path: '{{ output_dir }}/root'
follow: False
register: file6_link_stat
- name: Get stat info for the pointed to file
stat:
path: '{{ output_dir }}/root'
follow: True
register: file6_links_dest_stat
- name: Get stat info for the file we intend to point to
stat:
path: '/'
follow: False
register: file6_dest_stat
- name: verify that the link was created correctly
assert:
that:
# file command reports it created something
- "file6_result.changed == true"
# file command created a link
- 'file6_link_stat["stat"]["islnk"]'
# Link points to the right path
- 'file6_link_stat["stat"]["lnk_target"] == "/"'
# The link target and the file we intended to link to have the same inode
- 'file6_links_dest_stat["stat"]["inode"] == file6_dest_stat["stat"]["inode"]'
#
# Test creating a relative link
#
# Relative link to file
- name: create a test sub-directory to link to
file:
dest: '{{ output_dir }}/sub1'
state: 'directory'
- name: create a file to link to in the test sub-directory
file:
dest: '{{ output_dir }}/sub1/file1'
state: 'touch'
- name: create another test sub-directory to place links within
file:
dest: '{{output_dir}}/sub2'
state: 'directory'
- name: create soft link to relative file
file:
src: '../sub1/file1'
dest: '{{ output_dir }}/sub2/link1'
state: 'link'
register: file7_result
- name: Get stat info for the link
stat:
path: '{{ output_dir }}/sub2/link1'
follow: False
register: file7_link_stat
- name: Get stat info for the pointed to file
stat:
path: '{{ output_dir }}/sub2/link1'
follow: True
register: file7_links_dest_stat
- name: Get stat info for the file we intend to point to
stat:
path: '{{ output_dir }}/sub1/file1'
follow: False
register: file7_dest_stat
- name: verify that the link was created correctly
assert:
that:
# file command reports it created something
- "file7_result.changed == true"
# file command created a link
- 'file7_link_stat["stat"]["islnk"]'
# Link points to the right path
- 'file7_link_stat["stat"]["lnk_target"] == "../sub1/file1"'
# The link target and the file we intended to link to have the same inode
- 'file7_links_dest_stat["stat"]["inode"] == file7_dest_stat["stat"]["inode"]'
# Relative link to directory
- name: create soft link to relative directory
file:
src: sub1
dest: '{{ output_dir }}/sub1-link'
state: 'link'
register: file8_result
- name: Get stat info for the link
stat:
path: '{{ output_dir }}/sub1-link'
follow: False
register: file8_link_stat
- name: Get stat info for the pointed to file
stat:
path: '{{ output_dir }}/sub1-link'
follow: True
register: file8_links_dest_stat
- name: Get stat info for the file we intend to point to
stat:
path: '{{ output_dir }}/sub1'
follow: False
register: file8_dest_stat
- name: verify that the link was created correctly
assert:
that:
# file command reports it created something
- "file8_result.changed == true"
# file command created a link
- 'file8_link_stat["stat"]["islnk"]'
# Link points to the right path
- 'file8_link_stat["stat"]["lnk_target"] == "sub1"'
# The link target and the file we intended to link to have the same inode
- 'file8_links_dest_stat["stat"]["inode"] == file8_dest_stat["stat"]["inode"]'
# test the file module using follow=yes, so that the target of a
# symlink is modified, rather than the link itself
- name: create a test file
copy:
dest: '{{output_dir}}/test_follow'
content: 'this is a test file\n'
mode: 0666
- name: create a symlink to the test file
file:
path: '{{output_dir}}/test_follow_link'
src: './test_follow'
state: 'link'
- name: modify the permissions on the link using follow=yes
file:
path: '{{output_dir}}/test_follow_link'
mode: 0644
follow: yes
register: file9_result
- name: stat the link target
stat:
path: '{{output_dir}}/test_follow'
register: file9_stat
- name: assert that the chmod worked
assert:
that:
- 'file9_result is changed'
- 'file9_stat["stat"]["mode"] == "0644"'
#
# Test modifying the permissions of a link itself
#
- name: attempt to modify the permissions of the link itself
file:
path: '{{output_dir}}/test_follow_link'
src: './test_follow'
state: 'link'
mode: 0600
follow: False
register: file10_result
# Whether the link itself changed is platform dependent! (BSD vs Linux?)
# Just check that the underlying file was not changed
- name: stat the link target
stat:
path: '{{output_dir}}/test_follow'
register: file10_target_stat
- name: assert that the link target was unmodified
assert:
that:
- 'file10_result is changed'
- 'file10_target_stat["stat"]["mode"] == "0644"'
# https://github.com/ansible/ansible/issues/56928
- block:
- name: Create a testing file
file:
path: "{{ output_dir }}/test_follow1"
state: touch
- name: Create a symlink and change mode of the original file, since follow == yes by default
file:
src: "{{ output_dir }}/test_follow1"
dest: "{{ output_dir }}/test_follow1_link"
state: link
mode: 0700
- name: stat the original file
stat:
path: "{{ output_dir }}/test_follow1"
register: stat_out
- name: Check if the mode of the original file was set
assert:
that:
- 'stat_out.stat.mode == "0700"'
always:
- name: Clean up
file:
path: "{{ item }}"
state: absent
loop:
- "{{ output_dir }}/test_follow1"
- "{{ output_dir }}/test_follow1_link"
# END #56928