Skip to content

Commit eafd8f8

Browse files
committed
feature: mount - implement swapon/swapoff (#106)
* Declare functions swapon() & swapoff(), is_swap() & reswap(). * Apply swapon/swapoff for states mounted, unmounted, remounted and absent. * Override default opts and boot when fstype=swap. * Do not honor 'fstab' when fstype=swap (fail instead). * Also fail when fstype=swap and 'path' is not 'none' ('-' for Solaris). * Update module documentation accordingly. + Replace all platform.system() calls by a variable. refactor integration tests * Improve readability/understanding of what is tested, and what OS is targeted. * Move 'swap' related test cases into dedicated file (swap.yml). * Add new test cases about swap enabling/disabling. * Extend tests to FreeBSD when possible.
1 parent ecd5ad5 commit eafd8f8

File tree

8 files changed

+860
-335
lines changed

8 files changed

+860
-335
lines changed

plugins/modules/mount.py

Lines changed: 255 additions & 41 deletions
Large diffs are not rendered by default.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
# Tasks to validate bind mounts (i.e. mount of a directory to another one).
3+
# Linux and FreeBSD only.
4+
5+
- name: Create the mount point (the target of the mount)
6+
file:
7+
state: directory
8+
path: '{{ output_dir }}/mount_dest'
9+
10+
- name: Create a directory to bind mount (the source of the mount)
11+
file:
12+
state: directory
13+
path: '{{ output_dir }}/mount_source'
14+
15+
- name: Put something in the directory so we see that it worked
16+
copy:
17+
content: 'Testing
18+
19+
'
20+
dest: '{{ output_dir }}/mount_source/test_file'
21+
register: orig_info
22+
23+
- name: Bind mount a directory
24+
mount:
25+
src: '{{ output_dir }}/mount_source'
26+
name: '{{ output_dir }}/mount_dest'
27+
state: mounted
28+
fstype: "{{ 'none' if ansible_system == 'Linux' else 'nullfs' }}"
29+
opts: "{{ 'bind' if ansible_system == 'Linux' else omit }}"
30+
register: bind_result
31+
32+
- name: get checksum for bind mounted file
33+
stat:
34+
path: '{{ output_dir }}/mount_dest/test_file'
35+
when: ansible_system in ('FreeBSD', 'Linux')
36+
register: dest_stat
37+
38+
- name: assert the bind mount was successful
39+
assert:
40+
that:
41+
- bind_result is changed
42+
- dest_stat.stat.exists
43+
- orig_info.checksum == dest_stat.stat.checksum
44+
45+
- name: Bind mount a directory again
46+
mount:
47+
src: '{{ output_dir }}/mount_source'
48+
name: '{{ output_dir }}/mount_dest'
49+
state: mounted
50+
fstype: "{{ 'none' if ansible_system == 'Linux' else 'nullfs' }}"
51+
opts: "{{ 'bind' if ansible_system == 'Linux' else omit }}"
52+
register: bind_result
53+
54+
- name: Make sure we didn't mount a second time
55+
assert:
56+
that:
57+
- bind_result is not changed
58+
59+
- name: Remount directory with different options
60+
mount:
61+
src: '{{ output_dir }}/mount_source'
62+
name: '{{ output_dir }}/mount_dest'
63+
state: mounted
64+
fstype: "{{ 'none' if ansible_system == 'Linux' else 'nullfs' }}"
65+
opts: "{{ 'bind,' if ansible_system == 'Linux' else '' }}ro"
66+
register: bind_result
67+
68+
- name: Get mount options
69+
shell:
70+
cmd: "mount | grep mount_dest | grep -E -w '(ro|read-only)' | wc -l"
71+
register: remount_options
72+
73+
- name: Make sure the filesystem now has the new opts
74+
assert:
75+
that:
76+
- bind_result is changed
77+
- '''1'' in remount_options.stdout'
78+
- 1 == remount_options.stdout_lines | length
79+
80+
- name: Unmount the bind mount
81+
mount:
82+
name: '{{ output_dir }}/mount_dest'
83+
state: absent
84+
register: unmount_result
85+
86+
- name: Check if the file still exists in dest
87+
stat:
88+
path: '{{ output_dir }}/mount_dest/test_file'
89+
register: dest_stat
90+
91+
- name: Assert that we unmounted
92+
assert:
93+
that:
94+
- unmount_result is changed
95+
- not dest_stat.stat.exists
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
# Tasks to validate the two last fields in fstab, that are optional on Linux,
3+
# are added if missing.
4+
5+
- name: Create fstab record with missing last two fields
6+
copy:
7+
dest: /etc/fstab
8+
content: '//nas/photo /home/jik/pictures cifs defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev
9+
10+
'
11+
12+
- name: Try to change the fstab record with the missing last two fields
13+
mount:
14+
src: //nas/photo
15+
path: /home/jik/pictures
16+
fstype: cifs
17+
opts: defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev,x-systemd.mount-timeout=0
18+
state: present
19+
register: optional_fields_update
20+
21+
- name: Get the content of the fstab file
22+
command:
23+
cmd: cat /etc/fstab
24+
changed_when: false
25+
register: optional_fields_content
26+
27+
- name: Check if the line containing the missing last two fields was changed
28+
assert:
29+
that:
30+
- optional_fields_update is changed
31+
- ''' 0 0'' in optional_fields_content.stdout'
32+
- 1 == optional_fields_content.stdout_lines | length
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
# Tasks to validate that adding a swap record in fstab doesn't replace another
3+
# one, unless the 'src' (not the 'path') is the same. For Linux and FreeBSD.
4+
5+
- name: Create fstab record for the first swap device
6+
mount:
7+
name: none
8+
src: /dev/swap1
9+
opts: sw
10+
fstype: swap
11+
state: present
12+
register: swap1_created
13+
14+
- name: Try to create fstab record for the first swap device again
15+
mount:
16+
name: none
17+
src: /dev/swap1
18+
opts: sw
19+
fstype: swap
20+
state: present
21+
register: swap1_created_again
22+
23+
- name: Check that we created the swap1 record
24+
assert:
25+
that:
26+
- swap1_created is changed
27+
- swap1_created_again is not changed
28+
29+
- name: Create fstab record for the second swap device
30+
mount:
31+
name: none
32+
src: /dev/swap2
33+
opts: sw
34+
fstype: swap
35+
state: present
36+
register: swap2_created
37+
38+
- name: Try to create fstab record for the second swap device again
39+
mount:
40+
name: none
41+
src: /dev/swap1
42+
opts: sw
43+
fstype: swap
44+
state: present
45+
register: swap2_created_again
46+
47+
- name: Check that we created the swap2 record
48+
assert:
49+
that:
50+
- swap2_created is changed
51+
- swap2_created_again is not changed
52+
53+
54+
55+
- name: Remove the fstab record for the first swap device
56+
mount:
57+
name: none
58+
src: /dev/swap1
59+
state: absent
60+
register: swap1_removed
61+
62+
- name: Try to remove the fstab record for the first swap device again
63+
mount:
64+
name: none
65+
src: /dev/swap1
66+
state: absent
67+
register: swap1_removed_again
68+
69+
- name: Check that we removed the swap1 record
70+
assert:
71+
that:
72+
- swap1_removed is changed
73+
- swap1_removed_again is not changed
74+
75+
- name: Remove the fstab record for the second swap device
76+
mount:
77+
name: none
78+
src: /dev/swap2
79+
state: absent
80+
register: swap2_removed
81+
82+
- name: Try to remove the fstab record for the second swap device again
83+
mount:
84+
name: none
85+
src: /dev/swap2
86+
state: absent
87+
register: swap2_removed_again
88+
89+
- name: Check that we removed the swap2 record
90+
assert:
91+
that:
92+
- swap2_removed is changed
93+
- swap2_removed_again is not changed

0 commit comments

Comments
 (0)