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 <kevin.carter@rackspace.com>
This commit is contained in:
Kevin Carter 2018-03-23 14:56:27 -05:00
parent cdfca6ff26
commit 031ba51726
No known key found for this signature in database
GPG Key ID: 9443251A787B9FB3
8 changed files with 308 additions and 83 deletions

View File

@ -18,33 +18,16 @@
# https://www.freedesktop.org/software/systemd/man/systemd.mount.html#Options= # https://www.freedesktop.org/software/systemd/man/systemd.mount.html#Options=
systemd_default_mount_options: 'defaults' 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 # The systemd mounts dictionary is a set of mounts that will be created. The
# dictionary can contain the following options: # dictionary can contain the following options:
# `config_overrides` -- (optional) used to inject extra configuration options into the mount file. # `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. # `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. # `type` -- (required) The type of file system that will be mounted.
# `options` -- (optional) Any optioned required to make the mount point work. # `options` -- (optional) Any optioned required to make the mount point work.
# If no options are provided the default will be used. # If no options are provided the default will be used.
# This list is comma separted. See # This list is comma separted. See
# `systemd_default_mount_options` for more details. # `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 # `state` -- (optional) system state of the mount point. The default will omit
# the state so that it is not started or stopped # the state so that it is not started or stopped
# unessisarily. If it is desirable for this role to # unessisarily. If it is desirable for this role to
@ -60,9 +43,7 @@ systemd_default_unit: {}
# where: '/var/lib/machines' # where: '/var/lib/machines'
# type: 'btrfs' # type: 'btrfs'
# options: 'loop' # options: 'loop'
# unit:
# ConditionPathExists:
# - '/var/lib/machines.raw'
# state: 'started' # state: 'started'
# enabled: true # enabled: true
# - config_overrides: {} # - config_overrides: {}
@ -70,13 +51,12 @@ systemd_default_unit: {}
# Where: "/var/lib/glance/images" # Where: "/var/lib/glance/images"
# type: "nfs" # type: "nfs"
# options: "_netdev,auto" # options: "_netdev,auto"
# unit:
# After:
# - network.target
# - what: "/openstack/swap.img" # - what: "/openstack/swap.img"
# priority: "0" # priority: "0"
# options: "%%" # options: "%%"
# type: "swap" # type: "swap"
# state: 'started' # state: 'started'
# enabled: true # enabled: true
systemd_mounts: {}
systemd_mounts: []

View File

@ -1,5 +1,5 @@
--- ---
# Copyright 2017, Rackspace US, Inc. # Copyright 2018, Rackspace US, Inc.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with 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 # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
- name: Fail if mount is missing parameters - name: Fail if mount is missing what parameters
fail: 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 }}" with_items: "{{ systemd_mounts }}"
when: when:
- item.where is undefined and item.type != 'swap' - item.where is undefined and item.type != 'swap'
tags: tags:
- always - always
- name: Create mount target(s) - include_tasks: systemd_mounts.yml
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
with_items: "{{ systemd_mounts }}" with_items: "{{ systemd_mounts }}"

66
tasks/systemd_mounts.yml Normal file
View File

@ -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

View File

@ -1,10 +1,7 @@
# {{ ansible_managed }}
[Unit] [Unit]
Description=Auto mount for {{ (item.where | default(item.what)).strip('/') | replace('/', '-') }}.{{ (item.type == 'swap') | ternary('swap', 'mount') }} Description=Auto mount for {{ item.where | default(item.what) }}
{% for key, value in (item.unit | default(systemd_default_unit)).items() %}
{% for unit_value in value %}
{{ key }}={{ unit_value }}
{% endfor %}
{% endfor %}
[{{ (item.type == 'swap') | ternary('Swap', 'Mount') }}] [{{ (item.type == 'swap') | ternary('Swap', 'Mount') }}]
What={{ item.what }} What={{ item.what }}

View File

@ -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"

View File

@ -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"

View File

@ -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

View File

@ -14,6 +14,12 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # 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 - name: Playbook for role testing
hosts: localhost hosts: localhost
connection: local connection: local
@ -22,3 +28,48 @@
roles: roles:
- role: "systemd_mount" - 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