From e0596ab78f9b23f30c58b8eb12310daa3cbfc0f6 Mon Sep 17 00:00:00 2001 From: Alberto Planas Date: Fri, 26 Apr 2019 17:22:57 +0200 Subject: [PATCH] mount: fix remount when fstype is not set --- salt/modules/mount.py | 22 +++++----- tests/unit/modules/test_mount.py | 73 +++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 11 deletions(-) diff --git a/salt/modules/mount.py b/salt/modules/mount.py index 3ea5fa17bb70..541a2c9cc6ac 100644 --- a/salt/modules/mount.py +++ b/salt/modules/mount.py @@ -1219,10 +1219,10 @@ def mount(name, device, mkmnt=False, fstype='', opts='defaults', user=None, util if 'AIX' in __grains__['os']: args += ' -v {0}'.format(fstype) elif 'solaris' in __grains__['os'].lower(): - if fstype: - args += ' -F {0}'.format(fstype) + args += ' -F {0}'.format(fstype) else: args += ' -t {0}'.format(fstype) + cmd = 'mount {0} {1} {2} '.format(args, device, name) out = __salt__['cmd.run_all'](cmd, runas=user, python_shell=False) if out['retcode']: @@ -1250,7 +1250,7 @@ def remount(name, device, mkmnt=False, fstype='', opts='defaults', user=None): if 'AIX' in __grains__['os']: if opts == 'defaults': - opts = '' + opts = [] if isinstance(opts, six.string_types): opts = opts.split(',') @@ -1265,14 +1265,16 @@ def remount(name, device, mkmnt=False, fstype='', opts='defaults', user=None): lopts = ','.join(opts) args = '-o {0}'.format(lopts) - # use of fstype on AIX differs from typical Linux use of -t functionality - # AIX uses -v vfsname, -t fstype mounts all with fstype in /etc/filesystems - if 'AIX' in __grains__['os']: - if fstype: + if fstype: + # use of fstype on AIX differs from typical Linux use of + # -t functionality AIX uses -v vfsname, -t fstype mounts + # all with fstype in /etc/filesystems + if 'AIX' in __grains__['os']: args += ' -v {0}'.format(fstype) - args += ' -o remount' - else: - args += ' -t {0}'.format(fstype) + elif 'solaris' in __grains__['os'].lower(): + args += ' -F {0}'.format(fstype) + else: + args += ' -t {0}'.format(fstype) if __grains__['os'] not in ['OpenBSD', 'MacOS', 'Darwin'] or force_mount: cmd = 'mount {0} {1} {2} '.format(args, device, name) diff --git a/tests/unit/modules/test_mount.py b/tests/unit/modules/test_mount.py index 1a0d64f2f582..808cef90df46 100644 --- a/tests/unit/modules/test_mount.py +++ b/tests/unit/modules/test_mount.py @@ -362,7 +362,7 @@ def test_mount(self): with patch.dict(mount.__salt__, {'cmd.run_all': mock}): self.assertTrue(mount.mount('name', 'device')) - def test_remount(self): + def test_remount_non_mounted(self): ''' Attempt to remount a device, if the device is not already mounted, mount is called @@ -381,6 +381,77 @@ def test_remount(self): with patch.object(mount, 'mount', mock): self.assertTrue(mount.remount('name', 'device')) + with patch.dict(mount.__grains__, {'os': 'Linux'}): + mock = MagicMock(return_value=[]) + with patch.object(mount, 'active', mock): + mock = MagicMock(return_value=True) + with patch.object(mount, 'mount', mock): + self.assertTrue(mount.remount('name', 'device')) + + def test_remount_already_mounted_no_fstype(self): + ''' + Attempt to remount a device already mounted that do not provides + fstype + ''' + with patch.dict(mount.__grains__, {'os': 'MacOS'}): + mock = MagicMock(return_value=['name']) + with patch.object(mount, 'active', mock): + mock = MagicMock(return_value={'retcode': 0}) + with patch.dict(mount.__salt__, {'cmd.run_all': mock}): + self.assertTrue(mount.remount('name', 'device')) + mock.assert_called_with('mount -u -o noowners device name ', + python_shell=False, runas=None) + + with patch.dict(mount.__grains__, {'os': 'AIX'}): + mock = MagicMock(return_value=['name']) + with patch.object(mount, 'active', mock): + mock = MagicMock(return_value={'retcode': 0}) + with patch.dict(mount.__salt__, {'cmd.run_all': mock}): + self.assertTrue(mount.remount('name', 'device')) + mock.assert_called_with('mount -o remount device name ', + python_shell=False, runas=None) + + with patch.dict(mount.__grains__, {'os': 'Linux'}): + mock = MagicMock(return_value=['name']) + with patch.object(mount, 'active', mock): + mock = MagicMock(return_value={'retcode': 0}) + with patch.dict(mount.__salt__, {'cmd.run_all': mock}): + self.assertTrue(mount.remount('name', 'device')) + mock.assert_called_with('mount -o defaults,remount device name ', + python_shell=False, runas=None) + + def test_remount_already_mounted_with_fstype(self): + ''' + Attempt to remount a device already mounted that do not provides + fstype + ''' + with patch.dict(mount.__grains__, {'os': 'MacOS'}): + mock = MagicMock(return_value=['name']) + with patch.object(mount, 'active', mock): + mock = MagicMock(return_value={'retcode': 0}) + with patch.dict(mount.__salt__, {'cmd.run_all': mock}): + self.assertTrue(mount.remount('name', 'device', fstype='type')) + mock.assert_called_with('mount -u -o noowners -t type device name ', + python_shell=False, runas=None) + + with patch.dict(mount.__grains__, {'os': 'AIX'}): + mock = MagicMock(return_value=['name']) + with patch.object(mount, 'active', mock): + mock = MagicMock(return_value={'retcode': 0}) + with patch.dict(mount.__salt__, {'cmd.run_all': mock}): + self.assertTrue(mount.remount('name', 'device', fstype='type')) + mock.assert_called_with('mount -o remount -v type device name ', + python_shell=False, runas=None) + + with patch.dict(mount.__grains__, {'os': 'Linux'}): + mock = MagicMock(return_value=['name']) + with patch.object(mount, 'active', mock): + mock = MagicMock(return_value={'retcode': 0}) + with patch.dict(mount.__salt__, {'cmd.run_all': mock}): + self.assertTrue(mount.remount('name', 'device', fstype='type')) + mock.assert_called_with('mount -o defaults,remount -t type device name ', + python_shell=False, runas=None) + def test_umount(self): ''' Attempt to unmount a device by specifying the directory it is