--- # The refusals below stop in validation, before any task that changes a host, so they need no # container: they run once, on the controller, in check mode so that a regressed guard cannot # change it. # # Runs first, before any play gathers facts: the role refuses a host it cannot identify as # Debian-family, before any change. - name: Refuse a host without facts hosts: localhost connection: local check_mode: true gather_facts: false tasks: - name: Run the role without gathered facts ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_test_case: name: no-facts expect: nfs_client supports Debian-family hosts only # Negative tests: each validation stops the role before any change, with its own message. The # inputs replace the converge's mounts for one include only. - name: Refuse invalid input hosts: localhost connection: local check_mode: true gather_facts: true tasks: - name: Refuse a state the role does not have ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: /mnt/data, state: mounted} nfs_client_test_case: name: state-mounted expect: "value of state must be one of: present, absent, got: mounted" - name: Refuse remounted ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: /mnt/data, state: remounted} nfs_client_test_case: name: state-remounted expect: "value of state must be one of: present, absent, got: remounted" - name: Refuse an entry without a src ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {path: /mnt/data} nfs_client_test_case: name: src-missing expect: "missing required arguments: src" - name: Refuse an empty src ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "", path: /mnt/data} nfs_client_test_case: name: src-empty expect: nfs_client_mounts src must be one word of the form server:/export - name: Refuse a src without an export ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: nas.example, path: /mnt/data} nfs_client_test_case: name: src-no-export expect: nfs_client_mounts src must be one word of the form server:/export - name: Refuse a src with a space after the export ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/my data", path: /mnt/data} nfs_client_test_case: name: src-space expect: nfs_client_mounts src must be one word of the form server:/export - name: Refuse a mount point with a space ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: "/mnt/my data"} nfs_client_test_case: name: path-space expect: nfs_client_mounts path must be an absolute path - name: Refuse a relative mount point ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: mnt/data} nfs_client_test_case: name: path-relative expect: nfs_client_mounts path must be an absolute path - name: Refuse the root as a mount point ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: /} nfs_client_test_case: name: path-root expect: nfs_client_mounts path must be an absolute path - name: Refuse a trailing slash ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: /mnt/data/} nfs_client_test_case: name: path-trailing-slash expect: nfs_client_mounts path must be an absolute path - name: Refuse options with a space ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: /mnt/data, opts: "ro, nofail"} nfs_client_test_case: name: opts-space expect: nfs_client_mounts options must be a non-empty comma-separated word - name: Refuse an entry with no options when the default is empty ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_default_opts: "" nfs_client_mounts: - {src: "nas.example:/export/data", path: /mnt/data} nfs_client_test_case: name: opts-empty expect: nfs_client_mounts options must be a non-empty comma-separated word - name: Refuse the same mount point twice ansible.builtin.include_tasks: tasks/failed_run.yml vars: nfs_client_mounts: - {src: "nas.example:/export/data", path: /mnt/data} - {src: "nas.example:/export/other", path: /mnt/data} nfs_client_test_case: name: duplicate-path expect: nfs_client_mounts lists the same path twice - name: Verify the host hosts: all gather_facts: true tasks: - name: Read the installed packages ansible.builtin.package_facts: manager: apt - name: The client package is installed ansible.builtin.assert: that: - "'nfs-common' in ansible_facts.packages" quiet: true - name: Read fstab ansible.builtin.slurp: src: /etc/fstab register: nfs_client_test_fstab # Each line split into its six fields, so the assertions do not depend on the spacing the # module writes. - name: Split the fstab lines into fields ansible.builtin.set_fact: nfs_client_test_rows: >- {{ (nfs_client_test_fstab.content | b64decode).splitlines() | reject('match', '^#') | map('split') | reject('equalto', []) | list }} # The seeded line for /mnt/data named another server and the default options: the role # replaces it, so exactly one line is left for the path. - name: The default-options entry replaced the seeded line ansible.builtin.assert: that: - "['nas.example:/export/data', '/mnt/data', 'nfs', '_netdev,nofail', '0', '0'] in nfs_client_test_rows" - nfs_client_test_rows | selectattr('1', 'eq', '/mnt/data') | list | length == 1 quiet: true - name: The entry with its own options keeps them ansible.builtin.assert: that: - "['nas.example:/export/media', '/mnt/media', 'nfs', '_netdev,nofail,ro,vers=4.2', '0', '0'] in nfs_client_test_rows" quiet: true - name: The absent entry lost its line ansible.builtin.assert: that: - nfs_client_test_rows | selectattr('1', 'eq', '/mnt/old') | list | length == 0 quiet: true - name: Stat the mount points ansible.builtin.stat: path: "{{ item }}" loop: [/mnt/data, /mnt/media, /mnt/old, /mnt/old/keep] register: nfs_client_test_stat # prepare.yml gave /mnt/media an owner and a mode and /mnt/old a mode of its own. The role # created /mnt/data (a new directory is root's) and changed neither of the others, and the # removed entry's directory and the file in it are untouched. - name: The mount points exist, keep their owner and mode, and the removed entry's directory is intact ansible.builtin.assert: that: - nfs_client_test_stat.results | map(attribute='stat.exists') | min - nfs_client_test_stat.results[0].stat.isdir - nfs_client_test_stat.results[0].stat.pw_name == 'root' - nfs_client_test_stat.results[1].stat.pw_name == 'daemon' - nfs_client_test_stat.results[1].stat.mode == '0710' - nfs_client_test_stat.results[2].stat.mode == '0750' quiet: true - name: Read what is mounted ansible.builtin.slurp: src: /proc/mounts register: nfs_client_test_mounts - name: Nothing was mounted ansible.builtin.assert: that: - "'/mnt/data' not in (nfs_client_test_mounts.content | b64decode)" - "'/mnt/media' not in (nfs_client_test_mounts.content | b64decode)" quiet: true # The daemon-reload is what turns an fstab line into a unit; the generator ran again, so the # new lines have a unit and the removed one has none. systemctl and not the systemd module, # which fails on a unit that is not loaded, the very state asserted for the removed entry. - name: Ask systemd about the mount units # noqa: command-instead-of-module ansible.builtin.command: "systemctl show --property=LoadState --value {{ item }}" loop: [mnt-data.mount, mnt-media.mount, mnt-old.mount] register: nfs_client_test_units changed_when: false check_mode: false - name: Systemd knows the new entries and not the removed one ansible.builtin.assert: that: - nfs_client_test_units.results[0].stdout == 'loaded' - nfs_client_test_units.results[1].stdout == 'loaded' - nfs_client_test_units.results[2].stdout == 'not-found' quiet: true