Save off work after laptop issue

This commit is contained in:
David Murphy 2018-07-27 10:40:17 -06:00
parent e5c2741fe7
commit b28f427432
4 changed files with 334 additions and 44 deletions

View file

@ -1209,8 +1209,9 @@ def mount(name, device, mkmnt=False, fstype='', opts='defaults', user=None, util
# 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 fstype and 'AIX' in __grains__['os']:
args += ' -v {0}'.format(fstype)
if 'AIX' in __grains__['os']:
if fstype:
args += ' -v {0}'.format(fstype)
else:
args += ' -t {0}'.format(fstype)
cmd = 'mount {0} {1} {2} '.format(args, device, name)
@ -1257,8 +1258,10 @@ def remount(name, device, mkmnt=False, fstype='', opts='defaults', user=None):
# 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 fstype and 'AIX' in __grains__['os']:
args += ' -v {0}'.format(fstype)
if 'AIX' in __grains__['os']:
if fstype:
args += ' -v {0}'.format(fstype)
args += ' -o remount'
else:
args += ' -t {0}'.format(fstype)
@ -1643,7 +1646,12 @@ def filesystems(config='/etc/filesystems'):
if 'AIX' not in __grains__['kernel']:
return ret
return _filesystems(config)
ret_dict = _filesystems(config)
if ret_dict:
ret_key = ret_dict.keys()[0]
ret = {ret_key: dict(ret_dict[ret_key])}
return ret
def set_filesystems(

View file

@ -207,8 +207,9 @@ def mounted(name,
opts = 'noowners'
# Defaults is not a valid option on AIX
if __grains__['os'] in ['AIX'] and opts == 'defaults':
opts = ''
if __grains__['os'] in ['AIX']:
if opts == 'defaults':
opts = ''
# Make sure that opts is correct, it can be a list or a comma delimited
# string
@ -738,7 +739,9 @@ def swap(name, persist=True, config='/etc/fstab'):
ret['result'] = False
if persist:
device_key_name = 'device'
if 'AIX' in __grains__['os']:
device_key_name = 'dev'
if '/etc/fstab' == config:
# Override default for AIX
config = "/etc/filesystems"
@ -754,7 +757,7 @@ def swap(name, persist=True, config='/etc/fstab'):
return ret
if 'none' in fstab_data:
if fstab_data['none']['device'] == name and \
if fstab_data['none'][device_key_name] == name and \
fstab_data['none']['fstype'] != 'swap':
return ret
@ -861,11 +864,13 @@ def unmounted(name,
cache_result = __salt__['mount.delete_mount_cache'](name)
if persist:
device_key_name = 'device'
# Override default for Mac OS
if __grains__['os'] in ['MacOS', 'Darwin'] and config == '/etc/fstab':
config = "/etc/auto_salt"
fstab_data = __salt__['mount.automaster'](config)
elif 'AIX' in __grains__['os']:
device_key_name = 'dev'
if config == '/etc/fstab':
config = "/etc/filesystems"
fstab_data = __salt__['mount.filesystems'](config)
@ -876,7 +881,7 @@ def unmounted(name,
ret['comment'] += '. fstab entry not found'
else:
if device:
if fstab_data[name]['device'] != device:
if fstab_data[name][device_key_name] != device:
ret['comment'] += '. fstab entry for device {0} not found'.format(device)
return ret
if __opts__['test']:

View file

@ -79,6 +79,12 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
with patch.object(mount, '_active_mounts_darwin', mock):
self.assertEqual(mount.active(extended=True), {})
with patch.dict(mount.__grains__, {'os': 'AIX', 'kernel': 'AIX'}):
mock = MagicMock(return_value={})
with patch.object(mount, '_active_mounts_aix', mock):
self.assertEqual(mount.active(), {})
def test_fstab(self):
'''
List the content of the fstab
@ -127,6 +133,48 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
'pass_fsck': '-'}
}, vfstab
def test_filesystems(self):
'''
List the content of the filesystems
'''
file_data = textwrap.dedent('''\
#
''')
mock = MagicMock(return_value=True)
with patch.dict(mount.__grains__, {'os': 'AIX', 'kernel': 'AIX'}), \
patch.object(os.path, 'isfile', mock), \
patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
self.assertEqual(mount.filesystems(), {})
file_data = textwrap.dedent('''\
#
/home:
dev = /dev/hd1
vfs = jfs2
log = /dev/hd8
mount = true
check = true
vol = /home
free = false
quota = no
''')
mock = MagicMock(return_value=True)
with patch.dict(mount.__grains__, {'os': 'AIX', 'kernel': 'AIX'}), \
patch.object(os.path, 'isfile', mock), \
patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
fsyst = mount.filesystems()
test_fsyst = { '/home': {'dev': '/dev/hd1',
'vfs': 'jfs2',
'log': '/dev/hd8',
'mount': 'true',
'check': 'true',
'vol': '/home',
'free': 'false',
'quota': 'no'} }
self.assertEqual( test_fsyst, fsyst)
def test_rm_fstab(self):
'''
Remove the mount point from the fstab
@ -190,6 +238,59 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
'''
self.assertDictEqual(mount.automaster(), {})
def test_rm_filesystems(self):
'''
Remove the mount point from the filesystems
'''
file_data = textwrap.dedent('''\
#
''')
mock = MagicMock(return_value=True)
with patch.dict(mount.__grains__, {'os': 'AIX', 'kernel': 'AIX'}), \
patch.object(os.path, 'isfile', mock), \
patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
self.assertFalse(mount.rm_filesystems('name', 'device'))
file_data = textwrap.dedent('''\
#
/name:
dev = device
vol = /name
''')
mock = MagicMock(return_value=True)
mock_fsyst = MagicMock(return_value=True)
with patch.dict(mount.__grains__, {'os': 'AIX', 'kernel': 'AIX'}), \
patch.object(os.path, 'isfile', mock), \
patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
self.assertTrue(mount.rm_filesystems('/name', 'device'))
def test_set_filesystems(self):
'''
Tests to verify that this mount is represented in the filesystems,
change the mount to match the data passed, or add the mount
if it is not present.
'''
mock = MagicMock(return_value=True)
with patch.dict(mount.__grains__, {'os': 'AIX', 'kernel': 'AIX'}) :
with patch.object(os.path, 'isfile', mock):
self.assertRaises(CommandExecutionError,
mount.set_filesystems, 'A', 'B', 'C')
mock_read = MagicMock(side_effect=OSError)
with patch.object(os.path, 'isfile', mock):
with patch.object(salt.utils.files, 'fopen', mock_read):
self.assertRaises(CommandExecutionError,
mount.set_filesystems, 'A', 'B', 'C')
with patch.object(os.path, 'isfile', mock):
with patch('salt.utils.files.fopen',
mock_open(read_data=MOCK_SHELL_FILE)):
self.assertEqual(mount.set_filesystems('A', 'B', 'C'), 'new')
def test_mount(self):
'''
Mount a device
@ -209,6 +310,21 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
with patch.dict(mount.__salt__, {'cmd.run_all': mock}):
self.assertTrue(mount.mount('name', 'device'))
with patch.dict(mount.__grains__, {'os': 'AIX'}):
mock = MagicMock(return_value=True)
with patch.object(os.path, 'exists', mock):
mock = MagicMock(return_value=None)
with patch.dict(mount.__salt__, {'file.mkdir': None}):
mock = MagicMock(return_value={'retcode': True,
'stderr': True})
with patch.dict(mount.__salt__, {'cmd.run_all': mock}):
self.assertTrue(mount.mount('name', 'device'))
mock = MagicMock(return_value={'retcode': False,
'stderr': False})
with patch.dict(mount.__salt__, {'cmd.run_all': mock}):
self.assertTrue(mount.mount('name', 'device'))
def test_remount(self):
'''
Attempt to remount a device, if the device is not already mounted, mount
@ -221,6 +337,13 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
with patch.object(mount, 'mount', mock):
self.assertTrue(mount.remount('name', 'device'))
with patch.dict(mount.__grains__, {'os': 'AIX'}):
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_umount(self):
'''
Attempt to unmount a device by specifying the directory it is
@ -274,7 +397,6 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
'''
Return a dict containing information on active swap
'''
file_data = textwrap.dedent('''\
Filename Type Size Used Priority
/dev/sda1 partition 31249404 4100 -1
@ -306,6 +428,23 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
'used': '4100'}
}, swaps
file_data = textwrap.dedent('''\
device maj,min total free
/dev/hd6 10, 2 11776MB 11765MB
''')
mock = MagicMock(return_value=file_data)
with patch.dict(mount.__grains__, {'os': 'AIX', 'kernel': 'AIX'}), \
patch.dict(mount.__salt__, {'cmd.run_stdout': mock}):
swaps = mount.swaps()
assert swaps == {
'/dev/hd6': {
'priority': '-',
'size': 12058624,
'type': 'device',
'used': 11264}
}, swaps
def test_swapon(self):
'''
Activate a swap disk
@ -330,6 +469,27 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertEqual(mount.swapon('name'), {'stats': 'name',
'new': True})
## effects of AIX
mock = MagicMock(return_value={'name': 'name'})
with patch.dict(mount.__grains__, {'kernel': 'AIX'}):
with patch.object(mount, 'swaps', mock):
self.assertEqual(mount.swapon('name'),
{'stats': 'name', 'new': False})
mock = MagicMock(return_value={})
with patch.dict(mount.__grains__, {'kernel': 'AIX'}):
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.dict(mount.__grains__, {'kernel': 'AIX'}):
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):
'''
@ -356,14 +516,38 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
with patch.dict(mount.__salt__, {'cmd.run': mock}):
self.assertTrue(mount.swapoff('name'))
# check on AIX
mock = MagicMock(return_value={})
with patch.dict(mount.__grains__, {'kernel': 'AIX'}):
with patch.object(mount, 'swaps', mock):
self.assertEqual(mount.swapoff('name'), None)
mock = MagicMock(return_value={'name': 'name'})
with patch.dict(mount.__grains__, {'kernel': 'AIX'}):
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.dict(mount.__grains__, {'kernel': 'AIX'}):
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):
'''
Provide information if the path is mounted
'''
mock = MagicMock(return_value={})
with patch.object(mount, 'active', mock):
self.assertFalse(mount.is_mounted('name'))
with patch.object(mount, 'active', mock), \
patch.dict(mount.__grains__, {'kernel': ''}):
self.assertFalse(mount.is_mounted('name'))
mock = MagicMock(return_value={'name': 'name'})
with patch.object(mount, 'active', mock):
self.assertTrue(mount.is_mounted('name'))
with patch.object(mount, 'active', mock), \
patch.dict(mount.__grains__, {'kernel': ''}):
self.assertTrue(mount.is_mounted('name'))

View file

@ -44,6 +44,13 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
superopts2 = ['uid=510', 'gid=100', 'username=cifsuser',
'domain=cifsdomain']
name3 = os.path.realpath('/mnt/jfs2')
device3 = '/dev/hd1'
fstype3 = 'jfs2'
opts3 = ['']
superopts3 = ['uid=510', 'gid=100', 'username=jfs2user',
'domain=jfs2sdomain']
ret = {'name': name,
'result': False,
'comment': '',
@ -57,7 +64,11 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
mock_mnt = MagicMock(return_value={name: {'device': device, 'opts': [],
'superopts': []},
name2: {'device': device2, 'opts': opts2,
'superopts': superopts2}})
'superopts': superopts2},
name3: {'device': device3, 'opts': opts3,
'superopts': superopts3}})
mock_aixfs_retn = MagicMock(return_value='present')
mock_emt = MagicMock(return_value={})
mock_str = MagicMock(return_value='salt')
mock_user = MagicMock(return_value={'uid': 510})
@ -185,6 +196,26 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
'gid=group1']),
ret)
with patch.dict(mount.__grains__, {'os': 'AIX'}):
with patch.dict(mount.__salt__, {'mount.active': mock_mnt,
'mount.mount': mock_str,
'mount.umount': mock_f,
'mount.read_mount_cache': mock_read_cache,
'mount.write_mount_cache': mock_write_cache,
'mount.set_filesystems': mock_aixfs_retn,
'user.info': mock_user,
'group.info': mock_group}):
with patch.dict(mount.__opts__, {'test': True}):
with patch.object(os.path, 'exists', mock_t):
comt = 'Target was already mounted. Entry already exists in the fstab.'
ret.update({'name': name3, 'result': True})
ret.update({'comment': comt, 'changes': {}})
self.assertDictEqual(mount.mounted(name3, device3,
fstype3,
opts=['uid=user1',
'gid=group1']),
ret)
# 'swap' function tests: 1
def test_swap(self):
@ -203,44 +234,67 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
mock_swp = MagicMock(return_value=[name])
mock_fs = MagicMock(return_value={'none': {'device': name,
'fstype': 'xfs'}})
mock_aixfs = MagicMock(return_value={name: {'dev': name,
'fstype': 'jfs2'}})
mock_emt = MagicMock(return_value={})
with patch.dict(mount.__salt__, {'mount.swaps': mock_swp,
'mount.fstab': mock_fs,
'file.is_link': mock_f}):
with patch.dict(mount.__opts__, {'test': True}):
comt = ('Swap {0} is set to be added to the '
'fstab and to be activated'.format(name))
ret.update({'comment': comt})
self.assertDictEqual(mount.swap(name), ret)
with patch.dict(mount.__grains__, {'os': 'test'}):
with patch.dict(mount.__salt__, {'mount.swaps': mock_swp,
'mount.fstab': mock_fs,
'file.is_link': mock_f}):
with patch.dict(mount.__opts__, {'test': True}):
comt = ('Swap {0} is set to be added to the '
'fstab and to be activated'.format(name))
ret.update({'comment': comt})
self.assertDictEqual(mount.swap(name), ret)
with patch.dict(mount.__opts__, {'test': False}):
comt = ('Swap {0} already active'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.swap(name), ret)
with patch.dict(mount.__salt__, {'mount.fstab': mock_emt,
'mount.set_fstab': mock}):
with patch.dict(mount.__opts__, {'test': False}):
comt = ('Swap {0} already active'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. '
'Added new entry to the fstab.')
ret.update({'comment': comt, 'result': True,
'changes': {'persist': 'new'}})
with patch.dict(mount.__salt__, {'mount.fstab': mock_emt,
'mount.set_fstab': mock}):
comt = ('Swap {0} already active'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. '
'Added new entry to the fstab.')
ret.update({'comment': comt, 'result': True,
'changes': {'persist': 'new'}})
self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. '
'Updated the entry in the fstab.')
ret.update({'comment': comt, 'result': True,
'changes': {'persist': 'update'}})
self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. '
'However, the fstab was not found.')
ret.update({'comment': comt, 'result': False,
'changes': {}})
self.assertDictEqual(mount.swap(name), ret)
with patch.dict(mount.__grains__, {'os': 'AIX'}):
with patch.dict(mount.__salt__, {'mount.swaps': mock_swp,
'mount.filesystems': mock_aixfs,
'file.is_link': mock_f}):
with patch.dict(mount.__opts__, {'test': True}):
comt = ('Swap {0} already active'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. '
'Updated the entry in the fstab.')
ret.update({'comment': comt, 'result': True,
'changes': {'persist': 'update'}})
with patch.dict(mount.__opts__, {'test': False}):
comt = ('Swap {0} already active. swap not present in /etc/filesystems on AIX.'.format(name))
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. '
'However, the fstab was not found.')
ret.update({'comment': comt, 'result': False,
'changes': {}})
self.assertDictEqual(mount.swap(name), ret)
with patch.dict(mount.__salt__, {'mount.filesystems': mock_emt,
'mount.set_filesystems': mock}):
comt = ('Swap {0} already active. swap not present in /etc/filesystems on AIX.'.format(name))
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(mount.swap(name), ret)
# 'unmounted' function tests: 1
@ -257,11 +311,21 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
'changes': {}}
mock_f = MagicMock(return_value=False)
mock_t = MagicMock(return_value=True)
mock_dev = MagicMock(return_value={name: {'device': device}})
mock_fs = MagicMock(return_value={name: {'device': name}})
mock_mnt = MagicMock(side_effect=[{name: {}}, {}, {}, {}])
name3 = os.path.realpath('/mnt/jfs2')
device3 = '/dev/hd1'
fstype3 = 'jfs2'
opts3 = ['']
mock_mnta = MagicMock(return_value={name3: {'device': device3, 'opts': opts3}})
mock_aixfs = MagicMock(return_value={name: {'dev': name3, 'fstype': fstype3}})
comt3 = ('Mount point /mnt/sdb is unmounted but needs to be purged '
'from /etc/auto_salt to be made persistent')
with patch.dict(mount.__grains__, {'os': 'Darwin'}):
with patch.dict(mount.__salt__, {'mount.active': mock_mnt,
'mount.automaster': mock_fs,
@ -273,7 +337,7 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
self.assertDictEqual(mount.unmounted(name, device), ret)
comt = ('Target was already unmounted. '
'fstab entry for device /dev/sdb5 not found')
'fstab entry for device {0} not found'.format(device))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.unmounted(name, device,
persist=True), ret)
@ -288,6 +352,35 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.unmounted(name, device), ret)
with patch.dict(mount.__grains__, {'os': 'AIX'}):
with patch.dict(mount.__salt__, {'mount.active': mock_mnta,
'mount.filesystems': mock_aixfs,
'file.is_link': mock_f}):
with patch.dict(mount.__opts__, {'test': True}):
comt = ('Target was already unmounted')
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.unmounted(name, device), ret)
comt = ('Target was already unmounted. '
'fstab entry for device /dev/sdb5 not found')
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.unmounted(name, device,
persist=True), ret)
with patch.dict(mount.__salt__,
{'mount.filesystems': mock_dev}):
comt = ('Mount point {0} is mounted but should not '
'be'.format(name3))
ret.update({'comment': comt, 'result': None, 'name':name3})
self.assertDictEqual(mount.unmounted(name3, device3,
persist=True), ret)
###### HERE DGM
with patch.dict(mount.__opts__, {'test': False}), \
patch.dict(mount.__salt__, { 'mount.umount': mock_t}):
comt = ('Target was already unmounted')
ret.update({'comment': comt, 'result': True, 'name': name3})
self.assertDictEqual(mount.unmounted(name3, device3), ret)
# 'mod_watch' function tests: 1
def test_mod_watch(self):