From 031ba51726bad640afdba16fe938d5f25dc401bf Mon Sep 17 00:00:00 2001 From: Kevin Carter Date: Fri, 23 Mar 2018 14:56:27 -0500 Subject: [PATCH] Add basic tests This change adds basic tests when creating a mount unit file. This test will ensure system-mount is functional with a local mounts, NFS mounts, and swap mounts. Change-Id: I00dbf7f6ff3b687e622b29808197dbd7427d24fe Signed-off-by: Kevin Carter --- defaults/main.yml | 30 ++---------- tasks/main.yml | 74 +++++++++--------------------- tasks/systemd_mounts.yml | 66 +++++++++++++++++++++++++++ templates/systemd-mount.j2 | 9 ++-- tests/test-create-btrfs-dev.yml | 48 +++++++++++++++++++ tests/test-create-nfs-dev.yml | 81 +++++++++++++++++++++++++++++++++ tests/test-create-swap-dev.yml | 32 +++++++++++++ tests/test.yml | 51 +++++++++++++++++++++ 8 files changed, 308 insertions(+), 83 deletions(-) create mode 100644 tasks/systemd_mounts.yml create mode 100644 tests/test-create-btrfs-dev.yml create mode 100644 tests/test-create-nfs-dev.yml create mode 100644 tests/test-create-swap-dev.yml diff --git a/defaults/main.yml b/defaults/main.yml index 7cf6671..2b501ef 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -18,33 +18,16 @@ # https://www.freedesktop.org/software/systemd/man/systemd.mount.html#Options= systemd_default_mount_options: 'defaults' - -# The `systemd_default_unit` option can be used on all mounts to start it once -# all listed services are ready. This option is a case sensitive free from -# disctionary containing list values. See the following link for all available -# options: https://www.freedesktop.org/software/systemd/man/systemd.unit.html#%5BUnit%5D%20Section%20Options -# systemd_default_unit: -# After: -# - service.one -# - service.two -# ConditionPathExists: -# - /var/path/exists1 -# - /var/path/exists2 -systemd_default_unit: {} - - # The systemd mounts dictionary is a set of mounts that will be created. The # dictionary can contain the following options: # `config_overrides` -- (optional) used to inject extra configuration options into the mount file. # `what` -- (required) Define what will be mounted. This can be a network target. -# `where` -- (required) Where will the "WHAT" be mounted. +# `where` -- (required) Where will the "what" be mounted. Required when type is not swap. # `type` -- (required) The type of file system that will be mounted. # `options` -- (optional) Any optioned required to make the mount point work. # If no options are provided the default will be used. # This list is comma separted. See # `systemd_default_mount_options` for more details. -# `unit` -- (optional) Dictionary of unit overrides commonly used in a mount. -# See `systemd_default_unit` for more details. # `state` -- (optional) system state of the mount point. The default will omit # the state so that it is not started or stopped # unessisarily. If it is desirable for this role to @@ -60,9 +43,7 @@ systemd_default_unit: {} # where: '/var/lib/machines' # type: 'btrfs' # options: 'loop' -# unit: -# ConditionPathExists: -# - '/var/lib/machines.raw' + # state: 'started' # enabled: true # - config_overrides: {} @@ -70,13 +51,12 @@ systemd_default_unit: {} # Where: "/var/lib/glance/images" # type: "nfs" # options: "_netdev,auto" -# unit: -# After: -# - network.target + # - what: "/openstack/swap.img" # priority: "0" # options: "%%" # type: "swap" # state: 'started' # enabled: true -systemd_mounts: {} + +systemd_mounts: [] diff --git a/tasks/main.yml b/tasks/main.yml index 13ad544..40a5a5f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,5 +1,5 @@ --- -# Copyright 2017, Rackspace US, Inc. +# Copyright 2018, Rackspace US, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,62 +13,32 @@ # See the License for the specific language governing permissions and # limitations under the License. -- name: Fail if mount is missing parameters +- name: Fail if mount is missing what parameters fail: - msg: "The mount params are missing [ where ] to mount {{ item }}" + msg: "Mount parameters [ type ] is missing for mount {{ item }}" + with_items: "{{ systemd_mounts }}" + when: + - item.type is undefined + tags: + - always + +- name: Fail if mount is missing what parameters + fail: + msg: "Mount parameters [ what ] is missing for mount {{ item }}" + with_items: "{{ systemd_mounts }}" + when: + - item.what is undefined + tags: + - always + +- name: Fail if mount is missing where parameters + fail: + msg: "Mount parameters [ where ] is missing for mount {{ item }}" with_items: "{{ systemd_mounts }}" when: - item.where is undefined and item.type != 'swap' tags: - always -- name: Create mount target(s) - file: - path: "{{ item.where }}" - state: directory - owner: "{{ item.owner | default(omit) }}" - group: "{{ item.group | default(omit) }}" - mode: "0755" - with_items: "{{ systemd_mounts }}" - when: - - item.where is defined - - item.state | default('unknown') != 'absent' - - item.type != 'swap' - tags: - - systemd-mount - -- name: Create systemd mount point(s) - config_template: - src: "systemd-mount.j2" - dest: "/etc/systemd/system/{{ (item.where | default(item.what)).strip('/') | replace('/', '-') }}.{{ (item.type == 'swap') | ternary('swap', 'mount') }}" - owner: "root" - group: "root" - mode: "0640" - config_overrides: "{{ item.config_overrides | default({}) }}" - config_type: "ini" - with_items: "{{ systemd_mounts }}" - when: - - item.state | default('unknown') != 'absent' - tags: - - systemd-mount - -- name: Load mount(s) - systemd: - daemon_reload: yes - name: "{{ (item.where | default(item.what)).strip('/') | replace('/', '-') }}.{{ (item.type == 'swap') | ternary('swap', 'mount') }}" - enabled: "{{ item.enabled | default(true) }}" - state: "{{ item.state | default(omit) }}" - when: - - item.state | default('unknown') != 'absent' - with_items: "{{ systemd_mounts }}" - -- name: Unload mount(s) - systemd: - daemon_reload: yes - name: "{{ (item.where | default(item.what)).strip('/') | replace('/', '-') }}.{{ (item.type == 'swap') | ternary('swap', 'mount') }}" - enabled: "stopped" - no_block: yes - when: - - item.state | default('unknown') == 'absent' - notify: Remove mount +- include_tasks: systemd_mounts.yml with_items: "{{ systemd_mounts }}" diff --git a/tasks/systemd_mounts.yml b/tasks/systemd_mounts.yml new file mode 100644 index 0000000..563c7c0 --- /dev/null +++ b/tasks/systemd_mounts.yml @@ -0,0 +1,66 @@ +--- +# Copyright 2018, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Escape mount service file name + command: systemd-escape -p --suffix="{{ (item.type == 'swap') | ternary('swap', 'mount') }}" "{{ item.where | default(item.what) }}" + changed_when: false + register: mount_service_name + +- name: Create mount target(s) + file: + path: "{{ item.where }}" + state: directory + owner: "{{ item.owner | default(omit) }}" + group: "{{ item.group | default(omit) }}" + mode: "0755" + when: + - item.where is defined + - item.state | default('unknown') != 'absent' + - item.type != 'swap' + tags: + - systemd-mount + +- name: Create systemd mount services(s) + config_template: + src: "systemd-mount.j2" + dest: "/etc/systemd/system/{{ mount_service_name.stdout }}" + owner: "root" + group: "root" + mode: "0640" + config_overrides: "{{ item.config_overrides | default({}) }}" + config_type: "ini" + when: + - item.state | default('unknown') != 'absent' + tags: + - systemd-mount + +- name: Load mount(s) + systemd: + daemon_reload: yes + name: "{{ mount_service_name.stdout }}" + enabled: "{{ item.enabled | default(true) }}" + state: "{{ item.state | default(omit) }}" + when: + - item.state | default('unknown') != 'absent' + +- name: Unload mount(s) + systemd: + daemon_reload: yes + name: "{{ mount_service_name.stdout }}" + enabled: "stopped" + no_block: yes + when: + - item.state | default('unknown') == 'absent' + notify: Remove mount diff --git a/templates/systemd-mount.j2 b/templates/systemd-mount.j2 index 2902fac..9773ab4 100644 --- a/templates/systemd-mount.j2 +++ b/templates/systemd-mount.j2 @@ -1,10 +1,7 @@ +# {{ ansible_managed }} + [Unit] -Description=Auto mount for {{ (item.where | default(item.what)).strip('/') | replace('/', '-') }}.{{ (item.type == 'swap') | ternary('swap', 'mount') }} -{% for key, value in (item.unit | default(systemd_default_unit)).items() %} -{% for unit_value in value %} -{{ key }}={{ unit_value }} -{% endfor %} -{% endfor %} +Description=Auto mount for {{ item.where | default(item.what) }} [{{ (item.type == 'swap') | ternary('Swap', 'Mount') }}] What={{ item.what }} diff --git a/tests/test-create-btrfs-dev.yml b/tests/test-create-btrfs-dev.yml new file mode 100644 index 0000000..aa02dfc --- /dev/null +++ b/tests/test-create-btrfs-dev.yml @@ -0,0 +1,48 @@ +--- +# Copyright 2017, BBC R&D +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Configure BTRFS sparse file + hosts: localhost + user: root + become: true + connection: local + tasks: + - name: Install BTRFS packages + package: + name: "{{ btrfs_package[ansible_pkg_mgr | lower] }}" + state: present + + - name: Create base directories + file: + path: "/var/lib" + state: "directory" + + - name: Create sparse file + command: "truncate -s 1024G /var/lib/sparse-file.img" + args: + creates: /var/lib/sparse-file.img + register: sparse_file + + - name: Format the sparse file + filesystem: + fstype: btrfs + dev: /var/lib/sparse-file.img + when: + - sparse_file | changed + vars: + btrfs_package: + apt: "btrfs-tools" + yum: "btrfs-progs" + zypper: "btrfsprogs" diff --git a/tests/test-create-nfs-dev.yml b/tests/test-create-nfs-dev.yml new file mode 100644 index 0000000..27932cf --- /dev/null +++ b/tests/test-create-nfs-dev.yml @@ -0,0 +1,81 @@ +--- +# Copyright 2017, BBC R&D +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Create an NFS backing store + hosts: localhost + user: root + become: true + connection: local + tasks: + - name: Install NFS packages + package: + name: "{{ nfs_package[ansible_distribution.split()[0] | lower] }}" + state: present + + - name: create the system group for nfs + group: + name: "nfs-user" + gid: "10000" + state: "present" + system: "yes" + + - name: Create the system user for nfs + user: + name: "nfs-user" + uid: "10000" + group: "nfs-user" + comment: "nfs-user" + shell: "/bin/false" + system: "yes" + createhome: "yes" + home: "/srv/nfs" + + - name: Create base directories + file: + path: "{{ item }}" + state: "directory" + owner: "nfs-user" + group: "nfs-user" + with_items: + - "/srv/nfs/test" + + - name: Create exports file + lineinfile: + path: /etc/exports + line: '{{ item }} 127.0.0.1/255.0.0.0(rw,sync,no_subtree_check,insecure,all_squash,anonuid=10000,anongid=10000)' + owner: root + group: root + mode: 0644 + create: yes + with_items: + - "/srv/nfs/test" + register: nfs_exportfs + + - name: Restart nfs-server + systemd: + daemon_reload: yes + name: "nfs-server" + enabled: "yes" + state: "restarted" + when: + - nfs_exportfs | changed + + - name: Export NFS + command: exportfs -rav + vars: + nfs_package: + ubuntu: "nfs-kernel-server" + centos: "nfs-utils" + opensuse: "nfs-kernel-server" diff --git a/tests/test-create-swap-dev.yml b/tests/test-create-swap-dev.yml new file mode 100644 index 0000000..46704b3 --- /dev/null +++ b/tests/test-create-swap-dev.yml @@ -0,0 +1,32 @@ +--- +# Copyright 2017, BBC R&D +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Configure swap sparse file + hosts: localhost + user: root + become: true + connection: local + tasks: + - name: Create swap file + command: "dd if=/dev/zero of=/var/lib/test-swap.img bs=1M count=128" + args: + creates: /var/lib/test-swap.img + register: create_swap + + - name: Format the swap file + command: mkswap /var/lib/test-swap.img + failed_when: false + when: + - create_swap | changed diff --git a/tests/test.yml b/tests/test.yml index c019bc6..eddbb94 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -14,6 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +- import_playbook: test-create-swap-dev.yml + +- import_playbook: test-create-nfs-dev.yml + +- import_playbook: test-create-btrfs-dev.yml + - name: Playbook for role testing hosts: localhost connection: local @@ -22,3 +28,48 @@ roles: - role: "systemd_mount" + post_tasks: + - name: Ensure mount are mounted + command: grep -w '{{ item }}' /proc/mounts + with_items: + - /var/lib/sparse-file + - /var/lib/test + tags: + - skip_ansible_lint + + - name: Ensure swap is enabled + shell: swapon | grep -w '/var/lib/test-swap.img' + tags: + - skip_ansible_lint + + vars: + systemd_mounts: + - what: '/var/lib/sparse-file.img' + where: '/var/lib/sparse-file' + type: 'btrfs' + options: 'loop' + state: 'started' + enabled: true + config_overrides: + Unit: + ConditionPathExists: '/var/lib/sparse-file.img' + + - what: "/var/lib/test-swap.img" + priority: "0" + options: "%%" + type: "swap" + state: 'started' + enabled: true + + - what: "127.0.0.1:/srv/nfs/test" + where: "/var/lib/test" + type: "nfs" + options: "_netdev,auto" + state: 'started' + enabled: true + config_overrides: + Unit: + After: + ? network.target + ? network-online.target + Wants: network-online.target