mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Adding a check to see if the config file exists, if not then return an empty list so we get the would be changes. Adding a test for this functionality.
This commit is contained in:
parent
fccf875077
commit
469c090330
3 changed files with 14 additions and 2 deletions
|
@ -71,6 +71,10 @@ def show(config_file=False):
|
|||
'''
|
||||
ret = {}
|
||||
if config_file:
|
||||
# If the file doesn't exist, return an empty list
|
||||
if not os.path.exists(config_file):
|
||||
return []
|
||||
|
||||
try:
|
||||
with salt.utils.files.fopen(config_file) as fp_:
|
||||
for line in fp_:
|
||||
|
|
|
@ -62,7 +62,7 @@ def present(name, value, config=None):
|
|||
if __opts__['test']:
|
||||
current = __salt__['sysctl.show']()
|
||||
configured = __salt__['sysctl.show'](config_file=config)
|
||||
if not configured:
|
||||
if configured is None:
|
||||
ret['result'] = None
|
||||
ret['comment'] = (
|
||||
'Sysctl option {0} might be changed, we failed to check '
|
||||
|
|
|
@ -46,6 +46,9 @@ class SysctlTestCase(TestCase, LoaderModuleMockMixin):
|
|||
|
||||
ret = {'name': name, 'result': None, 'changes': {}, 'comment': comment}
|
||||
|
||||
comment_empty = ('Sysctl option {0} would be changed to {1}'
|
||||
''.format(name, value))
|
||||
|
||||
comment1 = ('Sysctl option {0} set to be changed to {1}'
|
||||
.format(name, value))
|
||||
|
||||
|
@ -91,10 +94,15 @@ class SysctlTestCase(TestCase, LoaderModuleMockMixin):
|
|||
return [name]
|
||||
|
||||
with patch.dict(sysctl.__opts__, {'test': True}):
|
||||
mock = MagicMock(return_value=False)
|
||||
mock = MagicMock(return_value=None)
|
||||
with patch.dict(sysctl.__salt__, {'sysctl.show': mock}):
|
||||
self.assertDictEqual(sysctl.present(name, value), ret)
|
||||
|
||||
mock = MagicMock(return_value=[])
|
||||
with patch.dict(sysctl.__salt__, {'sysctl.show': mock}):
|
||||
ret.update({'comment': comment_empty})
|
||||
self.assertDictEqual(sysctl.present(name, value), ret)
|
||||
|
||||
with patch.dict(sysctl.__salt__, {'sysctl.show': mock_current}):
|
||||
ret.update({'comment': comment1})
|
||||
self.assertDictEqual(sysctl.present(name, value), ret)
|
||||
|
|
Loading…
Add table
Reference in a new issue