Merge pull request #50423 from garethgreenaway/50292_no_error_log_test_true

[2018.3] Fixes to sysctl modules and state
This commit is contained in:
Nicole Thomas 2018-11-08 09:12:59 -05:00 committed by GitHub
commit e9464c947e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View file

@ -6,6 +6,7 @@ Module for viewing and modifying sysctl parameters
# Import Python libs
from __future__ import absolute_import, unicode_literals, print_function
import logging
import os
# Import salt libs
import salt.utils.files
@ -66,6 +67,10 @@ def show(config_file=False):
comps = ['']
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, 'r') as f:
for line in f.readlines():

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)