Skip to content

Commit

Permalink
Fix for #14915 (#34254)
Browse files Browse the repository at this point in the history
* mount.swaps now works on solarish like systems

* mount.swapon / mount.swapoff supported on solarish platforms

* mount.active (and internal consumers) now work on all solarish platforms

* unit-tests - mount_tests should set kernel grain
  • Loading branch information
sjorge authored and Nicole Thomas committed Jun 24, 2016
1 parent 39579ce commit a09055c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 38 deletions.
43 changes: 36 additions & 7 deletions salt/modules/mount.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def active(extended=False):
ret = {}
if __grains__['os'] == 'FreeBSD':
_active_mounts_freebsd(ret)
elif __grains__['os'] == 'Solaris':
elif __grains__['kernel'] == 'SunOS':
_active_mounts_solaris(ret)
elif __grains__['os'] == 'OpenBSD':
_active_mounts_openbsd(ret)
Expand Down Expand Up @@ -858,14 +858,25 @@ def swaps():
'''
Return a dict containing information on active swap
.. versionchanged:: 2016.3.2
CLI Example:
.. code-block:: bash
salt '*' mount.swaps
'''
ret = {}
if __grains__['os'] != 'OpenBSD':
if __grains__['kernel'] == 'SunOS':
for line in __salt__['cmd.run_stdout']('swap -l').splitlines():
if line.startswith('swapfile'):
continue
comps = line.split()
ret[comps[0]] = {'type': 'device' if comps[0].startswith(('/dev', 'swap')) else 'file',
'size': int(comps[3]),
'used': (int(comps[3]) - int(comps[4])),
'priority': '-'}
elif __grains__['os'] != 'OpenBSD':
with salt.utils.fopen('/proc/swaps') as fp_:
for line in fp_:
if line.startswith('Filename'):
Expand Down Expand Up @@ -894,6 +905,8 @@ def swapon(name, priority=None):
'''
Activate a swap disk
.. versionchanged:: 2016.3.2
CLI Example:
.. code-block:: bash
Expand All @@ -906,22 +919,33 @@ def swapon(name, priority=None):
ret['stats'] = on_[name]
ret['new'] = False
return ret
cmd = 'swapon {0}'.format(name)
if priority:
cmd += ' -p {0}'.format(priority)
__salt__['cmd.run'](cmd, python_shell=False)

if __grains__['kernel'] == 'SunOS':
if __grains__['virtual'] != 'zone':
__salt__['cmd.run']('swap -a {0}'.format(name), python_shell=False)
else:
return False
else:
cmd = 'swapon {0}'.format(name)
if priority:
cmd += ' -p {0}'.format(priority)
__salt__['cmd.run'](cmd, python_shell=False)

on_ = swaps()
if name in on_:
ret['stats'] = on_[name]
ret['new'] = True
return ret

return ret


def swapoff(name):
'''
Deactivate a named swap mount
.. versionchanged:: 2016.3.2
CLI Example:
.. code-block:: bash
Expand All @@ -930,7 +954,12 @@ def swapoff(name):
'''
on_ = swaps()
if name in on_:
if __grains__['os'] != 'OpenBSD':
if __grains__['kernel'] == 'SunOS':
if __grains__['virtual'] != 'zone':
__salt__['cmd.run']('swap -a {0}'.format(name), python_shell=False)
else:
return False
elif __grains__['os'] != 'OpenBSD':
__salt__['cmd.run']('swapoff {0}'.format(name), python_shell=False)
else:
__salt__['cmd.run']('swapctl -d {0}'.format(name),
Expand Down
68 changes: 37 additions & 31 deletions tests/unit/modules/mount_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_active(self):
'''
List the active mounts.
'''
with patch.dict(mount.__grains__, {'os': 'FreeBSD'}):
with patch.dict(mount.__grains__, {'os': 'FreeBSD', 'kernel': 'FreeBSD'}):
# uid=user1 tests the improbable case where a OS returns a name
# instead of a numeric id, for #25293
mock = MagicMock(return_value='A B C D,E,F,uid=user1,gid=grp1')
Expand All @@ -56,25 +56,25 @@ def test_active(self):
'gid=100'],
'fstype': 'C'}})

with patch.dict(mount.__grains__, {'os': 'Solaris'}):
with patch.dict(mount.__grains__, {'os': 'Solaris', 'kernel': 'SunOS'}):
mock = MagicMock(return_value='A * B * C D/E/F')
with patch.dict(mount.__salt__, {'cmd.run_stdout': mock}):
self.assertEqual(mount.active(), {'B':
{'device': 'A',
'opts': ['D', 'E', 'F'],
'fstype': 'C'}})

with patch.dict(mount.__grains__, {'os': 'OpenBSD'}):
with patch.dict(mount.__grains__, {'os': 'OpenBSD', 'kernel': 'OpenBSD'}):
mock = MagicMock(return_value={})
with patch.object(mount, '_active_mounts_openbsd', mock):
self.assertEqual(mount.active(), {})

with patch.dict(mount.__grains__, {'os': 'MacOS'}):
with patch.dict(mount.__grains__, {'os': 'MacOS', 'kernel': 'Darwin'}):
mock = MagicMock(return_value={})
with patch.object(mount, '_active_mounts_darwin', mock):
self.assertEqual(mount.active(), {})

with patch.dict(mount.__grains__, {'os': 'MacOS'}):
with patch.dict(mount.__grains__, {'os': 'MacOS', 'kernel': 'Darwin'}):
mock = MagicMock(return_value={})
with patch.object(mount, '_active_mountinfo', mock):
with patch.object(mount, '_active_mounts_darwin', mock):
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_swaps(self):

file_data = '\n'.join(['Filename Type Size Used Priority',
'/dev/sda1 partition 31249404 4100 -1'])
with patch.dict(mount.__grains__, {'os': ''}):
with patch.dict(mount.__grains__, {'os': '', 'kernel': ''}):
with patch('salt.utils.fopen',
mock_open(read_data=file_data),
create=True) as m:
Expand All @@ -263,7 +263,7 @@ def test_swaps(self):
file_data = '\n'.join(['Device Size Used Unknown Unknown Priority',
'/dev/sda1 31249404 4100 unknown unknown -1'])
mock = MagicMock(return_value=file_data)
with patch.dict(mount.__grains__, {'os': 'OpenBSD'}):
with patch.dict(mount.__grains__, {'os': 'OpenBSD', 'kernel': 'OpenBSD'}):
with patch.dict(mount.__salt__, {'cmd.run_stdout': mock}):
self.assertDictEqual(mount.swaps(), {'/dev/sda1':
{'priority': '-1',
Expand All @@ -276,44 +276,50 @@ def test_swapon(self):
Activate a swap disk
'''
mock = MagicMock(return_value={'name': 'name'})
with patch.object(mount, 'swaps', mock):
self.assertEqual(mount.swapon('name'),
{'stats': 'name', 'new': False})
with patch.dict(mount.__grains__, {'kernel': ''}):
with patch.object(mount, 'swaps', mock):
self.assertEqual(mount.swapon('name'),
{'stats': 'name', 'new': False})

mock = MagicMock(return_value={})
with patch.object(mount, 'swaps', mock):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertEqual(mount.swapon('name', False), {})
with patch.dict(mount.__grains__, {'kernel': ''}):
with patch.object(mount, 'swaps', mock):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertEqual(mount.swapon('name', False), {})

mock = MagicMock(side_effect=[{}, {'name': 'name'}])
with patch.object(mount, 'swaps', mock):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertEqual(mount.swapon('name'), {'stats': 'name',
'new': True})
with patch.dict(mount.__grains__, {'kernel': ''}):
with patch.object(mount, 'swaps', mock):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertEqual(mount.swapon('name'), {'stats': 'name',
'new': True})

def test_swapoff(self):
'''
Deactivate a named swap mount
'''
mock = MagicMock(return_value={})
with patch.object(mount, 'swaps', mock):
self.assertEqual(mount.swapoff('name'), None)
with patch.dict(mount.__grains__, {'kernel': ''}):
with patch.object(mount, 'swaps', mock):
self.assertEqual(mount.swapoff('name'), None)

mock = MagicMock(return_value={'name': 'name'})
with patch.object(mount, 'swaps', mock):
with patch.dict(mount.__grains__, {'os': 'test'}):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertFalse(mount.swapoff('name'))
with patch.dict(mount.__grains__, {'kernel': ''}):
with patch.object(mount, 'swaps', mock):
with patch.dict(mount.__grains__, {'os': 'test'}):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertFalse(mount.swapoff('name'))

mock = MagicMock(side_effect=[{'name': 'name'}, {}])
with patch.object(mount, 'swaps', mock):
with patch.dict(mount.__grains__, {'os': 'test'}):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertTrue(mount.swapoff('name'))
with patch.dict(mount.__grains__, {'kernel': ''}):
with patch.object(mount, 'swaps', mock):
with patch.dict(mount.__grains__, {'os': 'test'}):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertTrue(mount.swapoff('name'))

def test_is_mounted(self):
'''
Expand Down

0 comments on commit a09055c

Please sign in to comment.