ansible.posix/tests/unit/modules/system/test_mount.py
Abhijeet Kasurde 75a5f83602
Partially revert "mount: Check if src exists before mounted (ansible/ansible#61752)" (#33)
This reverts part of ansible commit 72023d7462e78635264fd12bfdb23894b4163cba.

The immediate reason is that it breaks mounts where src is not a path.
Examples of such mounts are network-based filesystems such as nfs, cifs,
glusterfs, ceph, virtual filesystems such as tmpfs or overlayfs, and
also UUID-based mounts. It is too hard to come with an exhaustive list,
especially if we take non-Linux systems into account, so don't even try.

Additionally, it did not really fix the issue (ansible/ansible#59183) that
it intended to fix, because the mount could fail but leave a non-working
fstab entry for reasons other than non-existing src path.

Restore fstab and remove the mount point after a failed mount

Add a reminder that not only devices can be mounted

Fixes: ansible/ansible#65855
Fixes: ansible/ansible#67588
Fixes: ansible/ansible#67966

Signed-off-by: Alexander E. Patrakov <patrakov@gmail.com>
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

Co-authored-by: Alexander E. Patrakov <patrakov@gmail.com>
2020-05-29 15:34:53 -05:00

59 lines
1.9 KiB
Python

import os
import tempfile
from ansible_collections.ansible.posix.tests.unit.compat import unittest
from ansible_collections.ansible.posix.tests.unit.compat.mock import MagicMock
from ansible.module_utils._text import to_bytes
from ansible_collections.ansible.posix.plugins.modules.mount import (
get_linux_mounts,
_set_mount_save_old,
set_mount,
)
class LinuxMountsTestCase(unittest.TestCase):
def _create_file(self, content):
tmp_file = tempfile.NamedTemporaryFile(prefix='ansible-test-', delete=False)
tmp_file.write(to_bytes(content))
tmp_file.close()
self.addCleanup(os.unlink, tmp_file.name)
return tmp_file.name
def test_code_comment(self):
path = self._create_file(
'140 136 253:2 /rootfs / rw - ext4 /dev/sdb2 rw\n'
'141 140 253:2 /rootfs/tmp/aaa /tmp/bbb rw - ext4 /dev/sdb2 rw\n'
)
mounts = get_linux_mounts(None, path)
self.assertEqual(mounts['/tmp/bbb']['src'], '/tmp/aaa')
def test_set_mount_save_old(self):
module = MagicMock(name='AnsibleModule')
module.check_mode = True
module.params = {'backup': False}
fstab_data = [
'UUID=8ac075e3-1124-4bb6-bef7-a6811bf8b870 / xfs defaults 0 0\n',
'/swapfile none swap defaults 0 0\n'
]
path = self._create_file("".join(fstab_data))
args = {
'fstab': path,
'name': '/data',
'src': '/dev/sdb1',
'fstype': 'ext4',
'opts': 'defaults',
'dump': '0',
'passno': '0',
}
name, changed = set_mount(module, args)
self.assertEqual(name, '/data')
self.assertTrue(changed)
name, backup_lines, changed = _set_mount_save_old(module, args)
self.assertEqual(backup_lines, fstab_data)
self.assertEqual(name, '/data')
self.assertTrue(changed)