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:
Gareth J. Greenaway 2018-11-07 15:50:17 -08:00
parent fccf875077
commit 469c090330
No known key found for this signature in database
GPG key ID: 10B62F8A7CAD7A41
3 changed files with 14 additions and 2 deletions

View file

@ -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_:

View file

@ -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 '

View file

@ -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)