Backport #50358 to 2017.7

This commit is contained in:
Gareth J. Greenaway 2019-01-18 12:16:03 -08:00
parent 4a12c5fa91
commit c33c5589ca
No known key found for this signature in database
GPG Key ID: 10B62F8A7CAD7A41
2 changed files with 77 additions and 29 deletions

View File

@ -663,7 +663,7 @@ def swap(name, persist=True, config='/etc/fstab'):
if persist: if persist:
fstab_data = __salt__['mount.fstab'](config) fstab_data = __salt__['mount.fstab'](config)
if __opts__['test']: if __opts__['test']:
if name not in fstab_data: if name not in fstab_data and name not in [fstab_data[item]['device'] for item in fstab_data]:
ret['result'] = None ret['result'] = None
if name in on_: if name in on_:
ret['comment'] = ('Swap {0} is set to be added to the ' ret['comment'] = ('Swap {0} is set to be added to the '

View File

@ -200,44 +200,92 @@ class MountTestCase(TestCase, LoaderModuleMockMixin):
mock_swp = MagicMock(return_value=[name]) mock_swp = MagicMock(return_value=[name])
mock_fs = MagicMock(return_value={'none': {'device': name, mock_fs = MagicMock(return_value={'none': {'device': name,
'fstype': 'xfs'}}) 'fstype': 'xfs'}})
mock_fs_diff = MagicMock(return_value={'none': {'device': 'something_else',
'fstype': 'xfs'}})
mock_emt = MagicMock(return_value={}) mock_emt = MagicMock(return_value={})
with patch.dict(mount.__salt__, {'mount.swaps': mock_swp, with patch.dict(mount.__grains__, {'os': 'test'}):
'mount.fstab': mock_fs, with patch.dict(mount.__salt__, {'mount.swaps': mock_swp,
'file.is_link': mock_f}): 'mount.fstab': mock_fs_diff,
with patch.dict(mount.__opts__, {'test': True}): 'file.is_link': mock_f}):
comt = ('Swap {0} is set to be added to the ' with patch.dict(mount.__opts__, {'test': True}):
'fstab and to be activated'.format(name)) comt = ('Swap {0} is set to be added to the '
ret.update({'comment': comt}) 'fstab and to be activated'.format(name))
self.assertDictEqual(mount.swap(name), ret) ret.update({'comment': comt})
self.assertDictEqual(mount.swap(name), ret)
with patch.dict(mount.__opts__, {'test': False}): with patch.dict(mount.__opts__, {'test': False}):
comt = ('Swap {0} already active'.format(name)) comt = ('Swap {0} already active'.format(name))
ret.update({'comment': comt, 'result': True}) ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.swap(name), ret) self.assertDictEqual(mount.swap(name, persist=False), ret)
with patch.dict(mount.__salt__, {'mount.fstab': mock_emt, with patch.dict(mount.__salt__, {'mount.fstab': mock_emt,
'mount.set_fstab': mock}): '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)
ret = {'name': name,
'result': None,
'comment': '',
'changes': {}}
mock = MagicMock(side_effect=['present', 'new', 'change', 'bad config'])
mock_emt = MagicMock(return_value={})
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} already active'.format(name)) comt = ('Swap {0} already active'.format(name))
ret.update({'comment': comt, 'result': True}) ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.swap(name), ret) self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. ' with patch.dict(mount.__opts__, {'test': False}):
'Added new entry to the fstab.') comt = ('Swap {0} already active'.format(name))
ret.update({'comment': comt, 'result': True, ret.update({'comment': comt, 'result': True})
'changes': {'persist': 'new'}})
self.assertDictEqual(mount.swap(name), ret) self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. ' with patch.dict(mount.__salt__, {'mount.fstab': mock_emt,
'Updated the entry in the fstab.') 'mount.set_fstab': mock}):
ret.update({'comment': comt, 'result': True, comt = ('Swap {0} already active'.format(name))
'changes': {'persist': 'update'}}) ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mount.swap(name), ret) self.assertDictEqual(mount.swap(name), ret)
comt = ('Swap /mnt/sdb already active. ' comt = ('Swap /mnt/sdb already active. '
'However, the fstab was not found.') 'Added new entry to the fstab.')
ret.update({'comment': comt, 'result': False, ret.update({'comment': comt, 'result': True,
'changes': {}}) 'changes': {'persist': 'new'}})
self.assertDictEqual(mount.swap(name), ret) 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)
# 'unmounted' function tests: 1 # 'unmounted' function tests: 1