Skip to content

Commit

Permalink
added integration test for multipass_mount module
Browse files Browse the repository at this point in the history
  • Loading branch information
theko2fi committed Jan 20, 2024
1 parent 3811646 commit 5836517
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Run ansible-test
run: |
cd ~/.ansible/collections/ansible_collections/theko2fi/multipass
ansible-test integration --target ssh:${{ secrets.SSH_USERNAME }}@${{ env.DROPLET_IP }},python=3.10 --exclude connection_multipass --exclude multipass_mount
ansible-test integration --target ssh:${{ secrets.SSH_USERNAME }}@${{ env.DROPLET_IP }},python=3.10 --exclude connection_multipass
- name: Delete droplet
if: ${{ always() }}
Expand Down
28 changes: 12 additions & 16 deletions plugins/modules/multipass_mount.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright 2023 Kenneth KOFFI <@theko2fi>
# Copyright 2023 Kenneth KOFFI (@theko2fi)
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -47,13 +47,9 @@ def main():
except Exception as e:
module.fail_json(msg=str(e))
else:
target = vm_name
if dest:
target = f"{target}:{dest}"
target = f"{vm_name}:{dest}" if dest else vm_name
try:
changed = True
if not get_existing_mounts(vm_name=vm_name):
changed = False
changed = False if not get_existing_mounts(vm_name=vm_name) else True
multipassclient.umount(mount=target)
module.exit_json(changed=changed)
except MountNonExistentError:
Expand Down Expand Up @@ -177,13 +173,13 @@ def main():
- Empty if O(state=absent).
returned: when O(state=present)
type: dict
sample: "/root/tmp": {
"gid_mappings": [
"0:default"
],
"source_path": "/root/tmp",
"uid_mappings": [
"0:default"
]
}
sample: {
"gid_mappings": [
"0:default"
],
"source_path": "/root/tmp",
"uid_mappings": [
"0:default"
]
}
'''
113 changes: 80 additions & 33 deletions tests/integration/targets/multipass_mount/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,74 +1,121 @@
---
- name: Delete the VM
- name: Delete any existing VM
theko2fi.multipass.multipass_vm:
name: "healthy-cankerworm"
name: "{{vm_name}}"
state: absent
purge: true

- name: Ensure that the VM exist for the test
theko2fi.multipass.multipass_vm:
name: "healthy-cankerworm"
name: "{{vm_name}}"
state: present

# - name: Unmount all the existing mount points
# theko2fi.multipass.multipass_mount:
# name: "healthy-cankerworm"
# state: absent

- name: Create '/tmp' directory if it does not exist
- name: Create '{{ mount_source_for_test }}' directory if it does not exist
ansible.builtin.file:
path: /tmp
path: "{{ mount_source_for_test }}"
state: directory

# - name: Debug
# ansible.builtin.debug:
# var: create_vm

- name: Mount '/tmp' directory from the host to '/tmp' inside the VM named 'healthy-cankerworm'
- name: Mount '{{ mount_source_for_test }}' directory from the host to '{{ mount_source_for_test }}' inside the VM named '{{vm_name}}'
theko2fi.multipass.multipass_mount:
name: "healthy-cankerworm"
source: /tmp

- name: Mount '/tmp' to '/tmp' (check idempotency)
name: "{{vm_name}}"
source: "{{ mount_source_for_test }}"
register: mount_output

- name: Assert that {{ mount_source_for_test }} has been mounted to the VM
ansible.builtin.assert:
that:
- mount_output.changed
- mount_output.result is defined
- mount_output.result.source_path == mount_source_for_test

- name: Mount '{{ mount_source_for_test }}' to '{{ mount_source_for_test }}' (check idempotency)
theko2fi.multipass.multipass_mount:
name: "healthy-cankerworm"
source: /tmp
name: "{{vm_name}}"
source: "{{ mount_source_for_test }}"
register: mount_idempotency_output

- name: Mount '/tmp' to '/data' inside the VM, set ownership
- name: Check idempotency
ansible.builtin.assert:
that:
- mount_idempotency_output.changed == False

- name: Mount '{{ mount_source_for_test }}' to '/data' inside the VM, set ownership
theko2fi.multipass.multipass_mount:
name: healthy-cankerworm
source: /tmp
name: "{{vm_name}}"
source: "{{ mount_source_for_test }}"
target: /data
state: present
type: classic
uid_map:
- "50:50"
- "1000:1000"
gid_map:
- "50:50"

- name: Unmount '/tmp' directory from the VM
register: mount_to_data

- name: Assert that {{ mount_source_for_test }} has been mounted to /data
ansible.builtin.assert:
that:
- mount_to_data.changed
- mount_to_data.result is defined
- mount_to_data.result.source_path == mount_source_for_test
- mount_to_data.result.uid_mappings == ["50:50", "1000:1000"]
- mount_to_data.result.gid_mappings == ["50:50"]

- name: Unmount '{{ mount_source_for_test }}' directory from the VM
theko2fi.multipass.multipass_mount:
name: healthy-cankerworm
target: /tmp
name: "{{vm_name}}"
target: "{{ mount_source_for_test }}"
state: absent
register: unmount

- name: Get infos on virtual machine
theko2fi.multipass.multipass_vm_info:
name: "{{vm_name}}"
register: unmount_info

- name: Assert that {{ mount_source_for_test }} has been unmounted
ansible.builtin.assert:
that:
- unmount.changed
- unmount_info.result.info[vm_name].mounts[mount_source_for_test] is not defined
- unmount_info.result.info[vm_name].mounts['/data'] is defined

- name: Unmount all mount points from the 'healthy-cankerworm' VM
- name: Unmount all mount points from the '{{vm_name}}' VM
theko2fi.multipass.multipass_mount:
name: healthy-cankerworm
name: "{{vm_name}}"
state: absent
register: unmount_all

- name: Get infos on virtual machine
theko2fi.multipass.multipass_vm_info:
name: "{{vm_name}}"
register: unmount_all_info

- name: Assert that all mount points have been removed from the VM
ansible.builtin.assert:
that:
- unmount_all.changed
- unmount_all_info.result.info[vm_name].mounts is not defined

- name: Unmount all directories (idempotency check)
theko2fi.multipass.multipass_mount:
name: "healthy-cankerworm"
name: "{{vm_name}}"
state: absent
register: unmount_all_idempotency

- name: Assert Unmount all directories (idempotency check)
ansible.builtin.assert:
that:
- unmount_all_idempotency.changed == False

- name: Delete the VM
theko2fi.multipass.multipass_vm:
name: "healthy-cankerworm"
name: "{{vm_name}}"
state: absent
purge: true

###### For windows devices #############

# - name: Mount a nonexistent source directory
# theko2fi.multipass.multipass_mount:
# name: "healthy-cankerworm"
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/multipass_mount/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vm_name: "{{ 'ansible-multipass--mount-test-%0x' % ((2**32) | random) }}"
mount_source_for_test: "{{ansible_env.HOME}}/foo_bar"

0 comments on commit 5836517

Please sign in to comment.