From 8399ca83f7e48590a86825ed368e861a2a93ee76 Mon Sep 17 00:00:00 2001 From: quidame Date: Sun, 14 Feb 2021 18:14:30 +0100 Subject: [PATCH] refactor integration tests * for better readability/understanding of what is tested, and what OS is targetted * extend tests to FreeBSD when possible and: * convert swap source if it is an UUID or a LABEL (linux), or a memory disk (freebsd) * 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) --- plugins/modules/mount.py | 101 +++++-- .../targets/mount/tasks/bind_mount.yml | 94 ++++++ .../targets/mount/tasks/fstab_last_fields.yml | 30 ++ .../targets/mount/tasks/fstab_multi_swap.yml | 93 ++++++ .../integration/targets/mount/tasks/main.yml | 267 ++---------------- .../targets/mount/tasks/remount.yml | 61 ++++ .../integration/targets/mount/tasks/swap.yml | 202 ------------- .../targets/mount/tasks/swapfile_freebsd.yml | 103 +++++++ .../targets/mount/tasks/swapfile_linux.yml | 159 +++++++++++ 9 files changed, 631 insertions(+), 479 deletions(-) create mode 100644 tests/integration/targets/mount/tasks/bind_mount.yml create mode 100644 tests/integration/targets/mount/tasks/fstab_last_fields.yml create mode 100644 tests/integration/targets/mount/tasks/fstab_multi_swap.yml create mode 100644 tests/integration/targets/mount/tasks/remount.yml delete mode 100644 tests/integration/targets/mount/tasks/swap.yml create mode 100644 tests/integration/targets/mount/tasks/swapfile_freebsd.yml create mode 100644 tests/integration/targets/mount/tasks/swapfile_linux.yml diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index ddad4afdf72..2c857fa24d9 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -26,7 +26,8 @@ options: path: description: - - Path to the mount point (e.g. C(/mnt/files)). Must be C(none) for swap spaces. + - Path to the mount point (e.g. C(/mnt/files)). Must be C(none) (or C(-) + on Solaris) for swap spaces. - Before Ansible 2.3 this option was only usable as I(dest), I(destfile) and I(name). type: path required: true @@ -69,8 +70,7 @@ description: - If C(mounted), the device will be actively mounted and appropriately configured in I(fstab). If the mount point is not present, the mount - point will be created (unless I(path=none), as expected for C(swap) - filesystems). + point will be created (unless I(fstype=swap)). - If C(unmounted), the device will be unmounted without changing I(fstab). - C(present) only specifies that the device is to be configured in I(fstab) and does not trigger or require a mount. @@ -102,7 +102,7 @@ boot: description: - Determines if the filesystem should be mounted on boot. - - Only applies to Solaris systems. + - Only applies to Solaris systems. Defaults to C(true) unless I(fstype=swap). type: bool default: yes backup: @@ -184,7 +184,7 @@ state: mounted fstype: nfs -- name: Enable swap device with priority=1 +- name: Enable swap device with priority=1 (Linux) ansible.posix.mount: src: /dev/mapper/vg0-swap fstype: swap @@ -192,12 +192,20 @@ opts: pri=1 state: mounted -- name: Disable swap file and keep its record in fstab +- name: Enable a swapfile (Linux, Solaris) ansible.posix.mount: src: /var/swapfile fstype: swap path: none - state: unmounted + state: mounted + +- name: Enable a swapfile (FreeBSD) + ansible.posix.mount: + src: md99 + fstype: swap + path: none + opts: file=/var/swapfile + state: mounted ''' @@ -317,8 +325,6 @@ def _set_mount_save_old(module, args): if ( ld['name'] != escaped_args['name'] or ( # In the case of swap, check the src instead - 'src' in args and - ld['name'] == 'none' and ld['fstype'] == 'swap' and ld['src'] != args['src'])): to_write.append(line) @@ -404,8 +410,6 @@ def unset_mount(module, args): if ( ld['name'] != escaped_name or ( # In the case of swap, check the src instead - 'src' in args and - ld['name'] == 'none' and ld['fstype'] == 'swap' and ld['src'] != args['src'])): to_write.append(line) @@ -674,7 +678,7 @@ def get_linux_mounts(module, mntinfo_file="/proc/self/mountinfo"): def is_swap(module, args): """Return True if the device/file is an active swap space, False otherwise.""" - if module.params['fstype'] != 'swap' or args['name'] != 'none': + if module.params['fstype'] != 'swap': return False if SYSTEM == 'sunos': @@ -688,20 +692,26 @@ def is_swap(module, args): swapon_bin = module.get_bin_path('swapon', required=True) cmd = [swapon_bin] - if SYSTEM == 'linux': - cmd += ['--noheadings', '--show=name'] - rc, out, err = module.run_command(cmd) - if rc != 0: + if rc: module.fail_json(msg="Error while querying active swaps: %s" % err) + # Get the first field of each line but the header + devices = [x.split()[0] for x in out.splitlines()[1:]] + dev = os.path.realpath(args['src']) + if SYSTEM == 'linux': - devices = out.splitlines() - else: - devices = [x.split()[0] for x in out.splitlines()] + if args['src'].startswith('UUID='): + uuid_path = os.path.join('/dev/disk/by-uuid', args['src'].split('=')[1]) + dev = os.path.realpath(uuid_path) + elif args['src'].startswith('LABEL='): + label_path = os.path.join('/dev/disk/by-label', args['src'].split('=')[1]) + dev = os.path.realpath(label_path) + elif SYSTEM == 'freebsd': + if args['src'].startswith('md'): + dev = os.path.join('/dev', args['src']) - dev = os.path.realpath(args['src']) return bool(dev in devices) @@ -723,15 +733,17 @@ def swapon(module, args): # unless provided on command line with '-o opts'. But not all versions of # swapon accept -o or --options. So we don't use it here, but at least we # keep the 'priority' and 'discard' flags available on Linux. - if SYSTEM == 'linux' and args['opts'] is not None: + if SYSTEM == 'linux': for opt in args['opts'].split(','): if opt.startswith('pri='): cmd += ['-p', opt.split('=')[1]] elif opt.startswith('discard'): cmd += ['--%s' % opt] - # src such as UUID=some_uuid and LABEL=some_label work as is (Linux). - cmd += [args['src']] + if SYSTEM == 'freebsd' and args['src'].startswith('md'): + cmd += [os.path.join('/dev', args['src'])] + else: + cmd += [args['src']] rc, out, err = module.run_command(cmd) @@ -754,7 +766,10 @@ def swapoff(module, args): swapoff_bin = module.get_bin_path('swapoff', required=True) cmd = [swapoff_bin] - cmd += [args['src']] + if SYSTEM == 'freebsd' and args['src'].startswith('md'): + cmd += [os.path.join('/dev', args['src'])] + else: + cmd += [args['src']] rc, out, err = module.run_command(cmd) @@ -800,6 +815,25 @@ def main(): ), ) + fstype = module.params['fstype'] + + # swapon/swapoff (and the likes) don't honor alternative fstab locations + # the same way the mount command does, that could make things very, very + # complicated... + if fstype == 'swap': + if SYSTEM == 'sunos': + swap_fstab_file = '/etc/vfstab' + swap_mountpoint = '-' + else: + swap_fstab_file = '/etc/fstab' + swap_mountpoint = 'none' + + if module.params['fstab'] not in (None, swap_fstab_file): + module.fail_json(msg="option 'fstype=swap' does not support alternative fstab locations") + if module.params['path'] != swap_mountpoint: + module.fail_json(msg="swap filesystems can't be mounted, please set path to '%s'" % + swap_mountpoint) + # solaris args: # name, src, fstype, opts, boot, passno, state, fstab=/etc/vfstab # linux args: @@ -816,6 +850,10 @@ def main(): ) if args['fstab'] is None: args['fstab'] = '/etc/vfstab' + + # swap spaces are used internally by kernels and have no mountpoint + if fstype == 'swap': + args['boot'] = 'no' else: args = dict( name=module.params['path'], @@ -827,8 +865,11 @@ def main(): if args['fstab'] is None: args['fstab'] = '/etc/fstab' + # Override default value of options field for swap filesystems + if fstype == 'swap': + args['opts'] = 'sw' # FreeBSD doesn't have any 'default' so set 'rw' instead - if SYSTEM == 'freebsd': + elif SYSTEM == 'freebsd': args['opts'] = 'rw' linux_mounts = [] @@ -871,7 +912,7 @@ def main(): # changed in fstab then remount it. state = module.params['state'] - name = module.params['path'] + name = args['name'] changed = False if state == 'absent': @@ -917,7 +958,7 @@ def main(): changed = True elif state == 'mounted': dirs_created = [] - if name != 'none' and not os.path.exists(name) and not module.check_mode: + if fstype != 'swap' and not os.path.exists(name) and not module.check_mode: try: # Something like mkdir -p but with the possibility to undo. # Based on some copy-paste from the "file" module. @@ -961,7 +1002,7 @@ def main(): changed = True if not module.check_mode: - if args['fstype'] == 'swap' and name == 'none': + if fstype == 'swap': res, msg = swapon(module, args) else: res, msg = mount(module, args) @@ -982,7 +1023,7 @@ def main(): except Exception: pass - if args['fstype'] == 'swap' and name == 'none': + if fstype == 'swap': error_msg = "Error enabling swap space %s: %s" % (args['src'], msg) else: error_msg = "Error mounting %s: %s" % (name, msg) @@ -992,7 +1033,7 @@ def main(): name, changed = set_mount(module, args) elif state == 'remounted': if not module.check_mode: - if module.params['fstype'] == 'swap' and name == 'none': + if fstype == 'swap': res, msg = reswap(module, args) if res: diff --git a/tests/integration/targets/mount/tasks/bind_mount.yml b/tests/integration/targets/mount/tasks/bind_mount.yml new file mode 100644 index 00000000000..54ebb8309db --- /dev/null +++ b/tests/integration/targets/mount/tasks/bind_mount.yml @@ -0,0 +1,94 @@ +--- +# Tasks to validate bind mounts (i.e. mount of a directory to another one). +# Linux and FreeBSD only. + +- name: Create the mount point (the target of the mount) + file: + state: directory + path: '{{ output_dir }}/mount_dest' + +- name: Create a directory to bind mount (the source of the mount) + file: + state: directory + path: '{{ output_dir }}/mount_source' + +- name: Put something in the directory so we see that it worked + copy: + content: 'Testing + + ' + dest: '{{ output_dir }}/mount_source/test_file' + register: orig_info + +- name: Bind mount a directory + mount: + src: '{{ output_dir }}/mount_source' + name: '{{ output_dir }}/mount_dest' + state: mounted + fstype: "{{ 'none' if ansible_system == 'Linux' else 'nullfs' }}" + opts: "{{ 'bind' if ansible_system == 'Linux' else omit }}" + register: bind_result + +- name: get checksum for bind mounted file + stat: + path: '{{ output_dir }}/mount_dest/test_file' + when: ansible_system in ('FreeBSD', 'Linux') + register: dest_stat + +- name: assert the bind mount was successful + assert: + that: + - bind_result is changed + - dest_stat.stat.exists + - orig_info.checksum == dest_stat.stat.checksum + +- name: Bind mount a directory again + mount: + src: '{{ output_dir }}/mount_source' + name: '{{ output_dir }}/mount_dest' + state: mounted + fstype: "{{ 'none' if ansible_system == 'Linux' else 'nullfs' }}" + opts: "{{ 'bind' if ansible_system == 'Linux' else omit }}" + register: bind_result + +- name: Make sure we didn't mount a second time + assert: + that: + - bind_result is not changed + +- name: Remount directory with different options + mount: + src: '{{ output_dir }}/mount_source' + name: '{{ output_dir }}/mount_dest' + state: mounted + fstype: "{{ 'none' if ansible_system == 'Linux' else 'nullfs' }}" + opts: "{{ 'bind,' if ansible_system == 'Linux' else '' }}ro" + register: bind_result + +- name: Get mount options + shell: mount | grep mount_dest | grep -E -w '(ro|read-only)' | wc -l + register: remount_options + +- name: Make sure the filesystem now has the new opts + assert: + that: + - bind_result is changed + - '''1'' in remount_options.stdout' + - 1 == remount_options.stdout_lines | length + +- name: Unmount the bind mount + mount: + name: '{{ output_dir }}/mount_dest' + state: absent + register: unmount_result + +- name: Check if the file still exists in dest + stat: + path: '{{ output_dir }}/mount_dest/test_file' + register: dest_stat + +- name: Assert that we unmounted + assert: + that: + - unmount_result is changed + - not dest_stat.stat.exists diff --git a/tests/integration/targets/mount/tasks/fstab_last_fields.yml b/tests/integration/targets/mount/tasks/fstab_last_fields.yml new file mode 100644 index 00000000000..a0e7b016f8e --- /dev/null +++ b/tests/integration/targets/mount/tasks/fstab_last_fields.yml @@ -0,0 +1,30 @@ +--- +# Tasks to validate the two last fields in fstab, that are optional on Linux, +# are added if missing. + +- name: Create fstab record with missing last two fields + copy: + dest: /etc/fstab + content: '//nas/photo /home/jik/pictures cifs defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev + + ' + +- name: Try to change the fstab record with the missing last two fields + mount: + src: //nas/photo + path: /home/jik/pictures + fstype: cifs + opts: defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev,x-systemd.mount-timeout=0 + state: present + register: optional_fields_update + +- name: Get the content of the fstab file + shell: cat /etc/fstab + register: optional_fields_content + +- name: Check if the line containing the missing last two fields was changed + assert: + that: + - optional_fields_update is changed + - ''' 0 0'' in optional_fields_content.stdout' + - 1 == optional_fields_content.stdout_lines | length diff --git a/tests/integration/targets/mount/tasks/fstab_multi_swap.yml b/tests/integration/targets/mount/tasks/fstab_multi_swap.yml new file mode 100644 index 00000000000..c6a3668c5ef --- /dev/null +++ b/tests/integration/targets/mount/tasks/fstab_multi_swap.yml @@ -0,0 +1,93 @@ +--- +# Tasks to validate that adding a swap record in fstab doesn't replace another +# one, unless the 'src' (not the 'path') is the same. For Linux and FreeBSD. + +- name: Create fstab record for the first swap device + mount: + name: none + src: /dev/swap1 + opts: sw + fstype: swap + state: present + register: swap1_created + +- name: Try to create fstab record for the first swap device again + mount: + name: none + src: /dev/swap1 + opts: sw + fstype: swap + state: present + register: swap1_created_again + +- name: Check that we created the swap1 record + assert: + that: + - swap1_created is changed + - swap1_created_again is not changed + +- name: Create fstab record for the second swap device + mount: + name: none + src: /dev/swap2 + opts: sw + fstype: swap + state: present + register: swap2_created + +- name: Try to create fstab record for the second swap device again + mount: + name: none + src: /dev/swap1 + opts: sw + fstype: swap + state: present + register: swap2_created_again + +- name: Check that we created the swap2 record + assert: + that: + - swap2_created is changed + - swap2_created_again is not changed + + + +- name: Remove the fstab record for the first swap device + mount: + name: none + src: /dev/swap1 + state: absent + register: swap1_removed + +- name: Try to remove the fstab record for the first swap device again + mount: + name: none + src: /dev/swap1 + state: absent + register: swap1_removed_again + +- name: Check that we removed the swap1 record + assert: + that: + - swap1_removed is changed + - swap1_removed_again is not changed + +- name: Remove the fstab record for the second swap device + mount: + name: none + src: /dev/swap2 + state: absent + register: swap2_removed + +- name: Try to remove the fstab record for the second swap device again + mount: + name: none + src: /dev/swap2 + state: absent + register: swap2_removed_again + +- name: Check that we removed the swap2 record + assert: + that: + - swap2_removed is changed + - swap2_removed_again is not changed diff --git a/tests/integration/targets/mount/tasks/main.yml b/tests/integration/targets/mount/tasks/main.yml index 4ac7e855f23..4c570691702 100644 --- a/tests/integration/targets/mount/tasks/main.yml +++ b/tests/integration/targets/mount/tasks/main.yml @@ -1,256 +1,29 @@ --- -- name: Create the mount point - file: - state: directory - path: '{{ output_dir }}/mount_dest' +- name: Include tasks to validate bind mount management (Linux, FreeBSD) + include_tasks: bind_mount.yml + when: + - ansible_system in ['Linux', 'FreeBSD'] -- name: Create a directory to bind mount - file: - state: directory - path: '{{ output_dir }}/mount_source' -- name: Put something in the directory so we see that it worked - copy: - content: 'Testing +- name: Include tasks to validate optional fields management in fstab (Linux) + include_tasks: fstab_last_fields.yml + when: + - ansible_system in ['Linux'] - ' - dest: '{{ output_dir }}/mount_source/test_file' - register: orig_info -- name: Bind mount a filesystem (Linux) - mount: - src: '{{ output_dir }}/mount_source' - name: '{{ output_dir }}/mount_dest' - state: mounted - fstype: None - opts: bind - when: ansible_system == 'Linux' - register: bind_result_linux +- name: Include tasks to validate state=remounted behaviour (Linux) + include_tasks: remount.yml + when: + - ansible_system in ['Linux'] -- name: Bind mount a filesystem (FreeBSD) - mount: - src: '{{ output_dir }}/mount_source' - name: '{{ output_dir }}/mount_dest' - state: mounted - fstype: nullfs - when: ansible_system == 'FreeBSD' - register: bind_result_freebsd -- name: get checksum for bind mounted file - stat: - path: '{{ output_dir }}/mount_dest/test_file' - when: ansible_system in ('FreeBSD', 'Linux') - register: dest_stat +- name: Include tasks to validate multi swap management in fstab (Linux) + include_tasks: fstab_multi_swap.yml + when: + - ansible_system in ['Linux', 'FreeBSD'] -- name: assert the bind mount was successful - assert: - that: - - (ansible_system == 'Linux' and bind_result_linux['changed']) or - (ansible_system == 'FreeBSD' and bind_result_freebsd['changed']) - - dest_stat['stat']['exists'] - - orig_info['checksum'] == dest_stat['stat']['checksum'] - when: ansible_system in ('FreeBSD', 'Linux') -- name: Bind mount a filesystem (Linux) - mount: - src: '{{ output_dir }}/mount_source' - name: '{{ output_dir }}/mount_dest' - state: mounted - fstype: None - opts: bind - when: ansible_system == 'Linux' - register: bind_result_linux - -- name: Bind mount a filesystem (FreeBSD) - mount: - src: '{{ output_dir }}/mount_source' - name: '{{ output_dir }}/mount_dest' - state: mounted - fstype: nullfs - when: ansible_system == 'FreeBSD' - register: bind_result_freebsd - -- name: Make sure we didn't mount a second time - assert: - that: - - (ansible_system == 'Linux' and not bind_result_linux['changed']) or - (ansible_system == 'FreeBSD' and not bind_result_freebsd['changed']) - when: ansible_system in ('FreeBSD', 'Linux') - -- name: Remount filesystem with different opts (Linux) - mount: - src: '{{ output_dir }}/mount_source' - name: '{{ output_dir }}/mount_dest' - state: mounted - fstype: None - opts: bind,ro - when: ansible_system == 'Linux' - register: bind_result_linux - -- name: Remount filesystem with different opts (FreeBSD) - mount: - src: '{{ output_dir }}/mount_source' - name: '{{ output_dir }}/mount_dest' - state: mounted - fstype: nullfs - opts: ro - when: ansible_system == 'FreeBSD' - register: bind_result_freebsd - -- name: Get mount options - shell: mount | grep mount_dest | grep -E -w '(ro|read-only)' | wc -l - register: remount_options - -- name: Make sure the filesystem now has the new opts - assert: - that: - - (ansible_system == 'Linux' and bind_result_linux['changed']) or - (ansible_system == 'FreeBSD' and bind_result_freebsd['changed']) - - '''1'' in remount_options.stdout' - - 1 == remount_options.stdout_lines | length - when: ansible_system in ('FreeBSD', 'Linux') - -- name: Unmount the bind mount - mount: - name: '{{ output_dir }}/mount_dest' - state: absent - when: ansible_system in ('Linux', 'FreeBSD') - register: unmount_result - -- name: Make sure the file no longer exists in dest - stat: - path: '{{ output_dir }}/mount_dest/test_file' - when: ansible_system in ('FreeBSD', 'Linux') - register: dest_stat - -- name: Check that we unmounted - assert: - that: - - unmount_result['changed'] - - not dest_stat['stat']['exists'] - when: ansible_system in ('FreeBSD', 'Linux') - - -- name: Create fstab record with missing last two fields - copy: - dest: /etc/fstab - content: '//nas/photo /home/jik/pictures cifs defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev - - ' - when: ansible_system in ('Linux') - -- name: Try to change the fstab record with the missing last two fields - mount: - src: //nas/photo - path: /home/jik/pictures - fstype: cifs - opts: defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev,x-systemd.mount-timeout=0 - state: present - register: optional_fields_update - when: ansible_system in ('Linux') - -- name: Get the content of the fstab file - shell: cat /etc/fstab - register: optional_fields_content - when: ansible_system in ('Linux') - -- name: Check if the line containing the missing last two fields was changed - assert: - that: - - optional_fields_update['changed'] - - ''' 0 0'' in optional_fields_content.stdout' - - 1 == optional_fields_content.stdout_lines | length - when: ansible_system in ('Linux') - - -- name: Block to test remounted option - block: - - name: Create empty file - command: dd if=/dev/zero of=/tmp/myfs.img bs=1048576 count=20 - when: ansible_system in ('Linux') - - - name: Format FS - when: ansible_system in ('Linux') - community.general.system.filesystem: - fstype: ext3 - dev: /tmp/myfs.img - - - name: Mount the FS for the first time - mount: - path: /tmp/myfs - src: /tmp/myfs.img - fstype: ext2 - state: mounted - when: ansible_system in ('Linux') - - - name: Get the last write time - shell: 'dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-' - register: last_write_time - when: ansible_system in ('Linux') - - - name: Wait 2 second - pause: - seconds: 2 - when: ansible_system in ('Linux') - - - name: Test if the FS is remounted - mount: - path: /tmp/myfs - state: remounted - when: ansible_system in ('Linux') - - - name: Get again the last write time - shell: 'dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-' - register: last_write_time2 - when: ansible_system in ('Linux') - - - name: Fail if they are the same - fail: - msg: Filesytem was not remounted, testing of the module failed! - when: - - last_write is defined - - last_write_time2 is defined - - last_write_time.stdout == last_write_time2.stdout - - ansible_system in ('Linux') - - - name: Remount filesystem with different opts using remounted option (Linux only) - mount: - path: /tmp/myfs - state: remounted - opts: rw,noexec - when: ansible_system == 'Linux' - - - name: Get remounted options (Linux only) - shell: mount | grep myfs | grep -E -w 'noexec' | wc -l - register: remounted_options - when: ansible_system == 'Linux' - - - name: Make sure the filesystem now has the new opts after using remounted (Linux only) - assert: - that: - - "'1' in remounted_options.stdout" - - "1 == remounted_options.stdout_lines | length" - when: ansible_system == 'Linux' - - always: - - name: Umount the test FS - mount: - path: /tmp/myfs - src: /tmp/myfs.img - opts: loop - state: absent - when: ansible_system in ('Linux') - - - name: Remove the test FS - file: - path: '{{ item }}' - state: absent - loop: - - /tmp/myfs.img - - /tmp/myfs - when: ansible_system in ('Linux') - - -- name: Include tests against swap filesystem - when: ansible_system in ('Linux') - include_tasks: swap.yml +- name: Include tasks to validate enabling/disabling of a swapfile (Linux, FreeBSD) + include_tasks: "swapfile_{{ ansible_system | lower }}.yml" + when: + - ansible_system in ['Linux', 'FreeBSD'] diff --git a/tests/integration/targets/mount/tasks/remount.yml b/tests/integration/targets/mount/tasks/remount.yml new file mode 100644 index 00000000000..b4b4689e505 --- /dev/null +++ b/tests/integration/targets/mount/tasks/remount.yml @@ -0,0 +1,61 @@ +--- +# Tasks to validate state=remounted behaves as expected. +# Linux only. + +- name: Create empty file + command: dd if=/dev/zero of=/tmp/myfs.img bs=1048576 count=20 + +- name: Format FS + community.general.filesystem: + fstype: ext3 + dev: /tmp/myfs.img + +- name: Mount the FS for the first time + mount: + path: /tmp/myfs + src: /tmp/myfs.img + fstype: ext3 + state: mounted + +- name: Get the mount counter value + shell: + cmd: "dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i '^mount count:'" + register: mount_count1 + +- name: Get the current mount as exposed in /proc/mounts + command: + cmd: "grep /tmp/myfs /proc/mounts" + changed_when: false + register: proc_mounts1 + +- name: Remount the filesystem + mount: + path: /tmp/myfs + opts: nosuid + state: remounted + +- name: Get again the mount counter value + shell: "dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i '^mount count:'" + register: mount_count2 + +- name: Get again the current mount as exposed in /proc/mounts + command: + cmd: "grep /tmp/myfs /proc/mounts" + changed_when: false + register: proc_mounts2 + +- name: Assert that mount has changed despite its counter has not + assert: + that: + - mount_count1.stdout == mount_count2.stdout + - proc_mounts1.stdout != proc_mounts2.stdout + +- name: Unmount the FS + mount: + path: /tmp/myfs + state: absent + +- name: Remove disk image + file: + path: /tmp/myfs.img + state: absent diff --git a/tests/integration/targets/mount/tasks/swap.yml b/tests/integration/targets/mount/tasks/swap.yml deleted file mode 100644 index 625bccb547b..00000000000 --- a/tests/integration/targets/mount/tasks/swap.yml +++ /dev/null @@ -1,202 +0,0 @@ ---- -- name: Create fstab record for the first swap file - mount: - name: none - src: /tmp/swap1 - opts: sw - fstype: swap - state: present - register: swap1_created - -- name: Try to create fstab record for the first swap file again - mount: - name: none - src: /tmp/swap1 - opts: sw - fstype: swap - state: present - register: swap1_created_again - -- name: Check that we created the swap1 record - assert: - that: - - swap1_created['changed'] - - not swap1_created_again['changed'] - -- name: Create fstab record for the second swap file - mount: - name: none - src: /tmp/swap2 - opts: sw - fstype: swap - state: present - register: swap2_created - -- name: Try to create fstab record for the second swap file again - mount: - name: none - src: /tmp/swap1 - opts: sw - fstype: swap - state: present - register: swap2_created_again - -- name: Check that we created the swap2 record - assert: - that: - - swap2_created['changed'] - - not swap2_created_again['changed'] - -- name: Remove the fstab record for the first swap file - mount: - name: none - src: /tmp/swap1 - state: absent - register: swap1_removed - -- name: Try to remove the fstab record for the first swap file again - mount: - name: none - src: /tmp/swap1 - state: absent - register: swap1_removed_again - -- name: Check that we removed the swap1 record - assert: - that: - - swap1_removed['changed'] - - not swap1_removed_again['changed'] - -- name: Remove the fstab record for the second swap file - mount: - name: none - src: /tmp/swap2 - state: absent - register: swap2_removed - -- name: Try to remove the fstab record for the second swap file again - mount: - name: none - src: /tmp/swap2 - state: absent - register: swap2_removed_again - -- name: Check that we removed the swap2 record - assert: - that: - - swap2_removed['changed'] - - not swap2_removed_again['changed'] - - -- name: Try to activate swap spaces - block: - - name: create a file to be used for memory swapping - command: - cmd: dd if=/dev/zero of=/tmp/swap1 bs=1M count=64 - creates: /tmp/swap1 - - - name: create a swap filesystem into the dedicated file - community.general.filesystem: - dev: /tmp/swap1 - fstype: swap - - - name: setup permissions suitable for swap usage - file: - path: /tmp/swap1 - mode: u=rw,go= - state: file - - - name: Get swap info (0) - command: swapon --noheadings --show=name,prio - register: swap_info_0 - changed_when: false - - - name: Enable swap file with priority 1234 - mount: - path: none - src: /tmp/swap1 - fstype: swap - opts: pri=1234 - state: mounted - register: do_swap_1 - - - name: Get swap info (1) - command: swapon --noheadings --show=name,prio - register: swap_info_1 - changed_when: false - - - name: Update swap priority to 10 - mount: - path: none - src: /tmp/swap1 - fstype: swap - opts: pri=10 - state: mounted - register: do_swap_2 - - - name: Get swap info (2) - command: swapon --noheadings --show=name,prio - register: swap_info_2 - changed_when: false - - - name: Do it again (test idempotency) - mount: - path: none - src: /tmp/swap1 - fstype: swap - opts: pri=10 - state: mounted - register: do_swap_3 - - - name: Get swap info (3) - command: swapon --noheadings --show=name,prio - register: swap_info_3 - changed_when: false - - - always: - - name: Disable swap file totally - mount: - path: none - src: /tmp/swap1 - fstype: swap - state: absent - register: do_swap_4 - - - name: Remove swap file - file: - path: /tmp/swap1 - state: absent - - - name: Get swap info (4) - command: swapon --noheadings --show=name,prio - register: swap_info_4 - changed_when: false - - -- name: format results - set_fact: - list_prio: "{{ list_prio | d([]) + [swap_prio] }}" - loop: - - "{{ swap_info_0 }}" - - "{{ swap_info_1 }}" - - "{{ swap_info_2 }}" - - "{{ swap_info_3 }}" - - "{{ swap_info_4 }}" - vars: - swap_line: "{{ item.stdout_lines | select('match', '/tmp/swap1') | list | join }}" - swap_prio: "{{ swap_line.split()[1] if swap_line | length > 0 else '' }}" - -- name: check that results are as expected - assert: - that: - - do_swap_1 is changed - - do_swap_2 is changed - - do_swap_3 is not changed - - do_swap_4 is changed - - - list_prio[0] | length == 0 - - list_prio[1] | int == 1234 - - list_prio[2] | int == 10 - - list_prio[3] | int == 10 - - list_prio[4] | length == 0 diff --git a/tests/integration/targets/mount/tasks/swapfile_freebsd.yml b/tests/integration/targets/mount/tasks/swapfile_freebsd.yml new file mode 100644 index 00000000000..fb9f967d1cc --- /dev/null +++ b/tests/integration/targets/mount/tasks/swapfile_freebsd.yml @@ -0,0 +1,103 @@ +--- +# Tasks to validate swapfile management (enabling/disabling its use) on FreeBSD. +# The swapfile MUST be associated to a memory disk, that becomes the 'src'. + +- name: Try to activate swapfile + vars: + swapfile: /var/swapfile0 + md_unit: 99 + md_name: "md{{ md_unit }}" + md_path: "/dev/{{ md_name }}" + + block: + - name: Create a file to be used for memory swapping + command: + cmd: "dd if=/dev/zero of={{ swapfile }} bs=1M count=64" + creates: "{{ swapfile }}" + + - name: Create memory disk to attach to the swapfile + command: + cmd: "mdconfig -a -t vnode -u {{ md_unit }} -f {{ swapfile }}" + creates: "{{ md_path }}" + + - name: Setup permissions suitable for swap usage + file: + path: "{{ swapfile }}" + mode: u=rw,go= + state: file + + - name: Get swap info (0) + command: swapctl -l + register: swap_info_0 + changed_when: false + + - name: Assert that memory disk {{ md_path }} is not used by swap + assert: + that: + - swap_info_0.stdout is not search(md_path) + + - name: Enable swap file + mount: + path: none + src: "{{ md_name }}" + fstype: swap + opts: "sw,file={{ swapfile }}" + state: mounted + register: do_swap_1 + + - name: Get swap info (1) + command: swapctl -l + register: swap_info_1 + changed_when: false + + - name: Do it again (test idempotency) + mount: + path: none + src: "{{ md_name }}" + fstype: swap + opts: "sw,file={{ swapfile }}" + state: mounted + register: do_swap_2 + + - name: Get swap info (2) + command: swapctl -l + register: swap_info_2 + changed_when: false + + - name: Assert that results are as expected + assert: + that: + - do_swap_1 is changed + - do_swap_2 is not changed + - swap_info_1.stdout is search(md_path) + - swap_info_2.stdout == swap_info_1.stdout + + always: + - name: Disable swap file totally + mount: + path: none + src: "{{ md_name }}" + fstype: swap + state: absent + register: do_swap_3 + + - name: Get swap info (3) + command: swapctl -l + register: swap_info_3 + changed_when: false + + - name: Assert that results are as expected + assert: + that: + - do_swap_3 is changed + - swap_info_3.stdout == swap_info_0.stdout + + - name: Detach memory disk + command: + cmd: "mdconfig -d -u {{ md_unit }}" + removes: "{{ md_path }}" + + - name: Remove swap file + file: + path: "{{ swapfile }}" + state: absent diff --git a/tests/integration/targets/mount/tasks/swapfile_linux.yml b/tests/integration/targets/mount/tasks/swapfile_linux.yml new file mode 100644 index 00000000000..0d959316f0b --- /dev/null +++ b/tests/integration/targets/mount/tasks/swapfile_linux.yml @@ -0,0 +1,159 @@ +--- +# Tasks to validate swapfile management (enabling/disabling its use) on Linux. +# The swapfile COULD be associated to a loop device, that would become the 'src'. + +- name: Try to activate swapfile + vars: + swapfile: /var/swapfile0 + + block: + - name: Create a file to be used for memory swapping + command: + cmd: "dd if=/dev/zero of={{ swapfile }} bs=1M count=64" + creates: "{{ swapfile }}" + + - name: Setup permissions suitable for swap usage + file: + path: "{{ swapfile }}" + mode: u=rw,go= + state: file + + - name: Create a swap filesystem into the dedicated file + community.general.filesystem: + dev: "{{ swapfile }}" + fstype: swap + + - name: Get swap info (0) + command: swapon --noheadings --show=name,prio + register: swap_info_0 + changed_when: false + + - name: Assert that swapfile is not already set + assert: + that: + - swap_relevant_line | length == 0 + vars: + swap_relevant_line: swap_info_0.stdout_lines | select('match', swapfile) | list | join + + + - name: Enable swap file with priority 1234 + mount: + path: none + src: "{{ swapfile }}" + fstype: swap + opts: pri=1234 + state: mounted + register: swap_enabled_1 + + - name: Get swap info (1) + command: swapon --noheadings --show=name,prio + register: swap_info_1 + changed_when: false + + - name: Assert that swap file is enabled with priority 1234 + assert: + that: + - swap_enabled_1 is changed + - swap_relevant_line | length > 0 + - swap_relevant_line.split()[1] | int == 1234 + vars: + swap_relevant_line: swap_info_1.stdout_lines | select('match', swapfile) | list | join + + + - name: Update swap priority to 10 + mount: + path: none + src: "{{ swapfile }}" + fstype: swap + opts: pri=10 + state: mounted + register: swap_enabled_2 + + - name: Get swap info (2) + command: swapon --noheadings --show=name,prio + register: swap_info_2 + changed_when: false + + - name: Assert that swap file is enabled with priority 10 + assert: + that: + - swap_enabled_2 is changed + - swap_relevant_line | length > 0 + - swap_relevant_line.split()[1] | int == 10 + vars: + swap_relevant_line: swap_info_2.stdout_lines | select('match', swapfile) | list | join + + + - name: Update swap priority to 10, again + mount: + path: none + src: "{{ swapfile }}" + fstype: swap + opts: pri=10 + state: mounted + register: swap_enabled_3 + + - name: Get swap info (3) + command: swapon --noheadings --show=name,prio + register: swap_info_3 + changed_when: false + + - name: Assert that nothing changed + assert: + that: + - swap_enabled_3 is not changed + - swap_relevant_line | length > 0 + - swap_relevant_line.split()[1] | int == 10 + - swap_info_3.stdout_lines == swap_info_2.stdout_lines + vars: + swap_relevant_line: swap_info_3.stdout_lines | select('match', swapfile) | list | join + + always: + - name: Disable swap file + mount: + path: none + src: "{{ swapfile }}" + fstype: swap + state: absent + register: swap_disabled_1 + + - name: Get swap info (4) + command: swapon --noheadings --show=name,prio + register: swap_info_4 + changed_when: false + + - name: Assert that swap file is disabled + assert: + that: + - swap_disabled_1 is changed + - swap_relevant_line | length == 0 + - swap_info_4.stdout_lines == swap_info_0.stdout_lines + vars: + swap_relevant_line: swap_info_4.stdout_lines | select('match', swapfile) | list | join + + - name: Disable swap file, again + mount: + path: none + src: "{{ swapfile }}" + fstype: swap + state: absent + register: swap_disabled_2 + + - name: Get swap info (4) + command: swapon --noheadings --show=name,prio + register: swap_info_5 + changed_when: false + + - name: Assert that swap file is disabled and nothing changed + assert: + that: + - swap_disabled_2 is not changed + - swap_relevant_line | length == 0 + - swap_info_5.stdout_lines == swap_info_0.stdout_lines + vars: + swap_relevant_line: swap_info_5.stdout_lines | select('match', swapfile) | list | join + + - name: Remove swap file + file: + path: "{{ swapfile }}" + state: absent