Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2018.3] fixes to function mount.swap in mount state #50358

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion salt/states/mount.py
Expand Up @@ -764,7 +764,7 @@ def swap(name, persist=True, config='/etc/fstab'):
else:
fstab_data = __salt__['mount.fstab'](config)
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
if name in on_:
ret['comment'] = ('Swap {0} is set to be added to the '
Expand Down
49 changes: 48 additions & 1 deletion tests/unit/states/test_mount.py
Expand Up @@ -236,19 +236,66 @@ def test_swap(self):
mock_swp = MagicMock(return_value=[name])
mock_fs = MagicMock(return_value={'none': {'device': name,
'fstype': 'xfs'}})
mock_fs_diff = MagicMock(return_value={'none': {'device': 'something_else',
'fstype': 'xfs'}})
mock_aixfs = MagicMock(return_value={name: {'dev': name,
'fstype': 'jfs2'}})
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,
'mount.fstab': mock_fs_diff,
'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, persist=False), ret)

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)

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))
ret.update({'comment': comt, 'result': True})
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})
Expand Down