diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index e913637..4c93b9e 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -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() }} diff --git a/plugins/modules/multipass_mount.py b/plugins/modules/multipass_mount.py index ee9c196..7add7d3 100644 --- a/plugins/modules/multipass_mount.py +++ b/plugins/modules/multipass_mount.py @@ -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 @@ -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: @@ -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" + ] + } ''' \ No newline at end of file diff --git a/tests/integration/targets/multipass_mount/tasks/main.yml b/tests/integration/targets/multipass_mount/tasks/main.yml index 6ec8794..aeb40c9 100644 --- a/tests/integration/targets/multipass_mount/tasks/main.yml +++ b/tests/integration/targets/multipass_mount/tasks/main.yml @@ -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" diff --git a/tests/integration/targets/multipass_mount/vars/main.yml b/tests/integration/targets/multipass_mount/vars/main.yml new file mode 100644 index 0000000..2692643 --- /dev/null +++ b/tests/integration/targets/multipass_mount/vars/main.yml @@ -0,0 +1,2 @@ +vm_name: "{{ 'ansible-multipass--mount-test-%0x' % ((2**32) | random) }}" +mount_source_for_test: "{{ansible_env.HOME}}/foo_bar" \ No newline at end of file