Fix problem with sysrc on FreeBSD, YAML overeager to coerce to bool and int.

Fix problem with sysrc on FreeBSD, YAML overeager to coerce to bool
This commit is contained in:
C. R. Oldham 2017-05-20 19:36:07 -06:00
parent 29bd7f48b7
commit 6db31ce52a
2 changed files with 66 additions and 3 deletions

View File

@ -93,6 +93,20 @@ def set_(name, value, **kwargs):
if 'jail' in kwargs:
cmd += ' -j '+kwargs['jail']
# This is here because the YAML parser likes to convert the string literals
# YES, NO, Yes, No, True, False, etc. to boolean types. However, in this case,
# we will check to see if that happened and replace it with "YES" or "NO" because
# those items are accepted in sysrc.
if type(value) == bool:
if value:
value = "YES"
else:
value = "NO"
# This is here for the same reason, except for numbers
if type(value) == int:
value = str(value)
cmd += ' '+name+"=\""+value+"\""
sysrcs = __salt__['cmd.run'](cmd)
@ -105,9 +119,6 @@ def set_(name, value, **kwargs):
newval = sysrc.split(': ')[2].split(" -> ")[1]
if rcfile not in ret:
ret[rcfile] = {}
#ret[rcfile][var] = {}
#ret[rcfile][var]['old'] = oldval
#ret[rcfile][var]['new'] = newval
ret[rcfile][var] = newval
return ret

View File

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import
import sys
# Import Salt Testing libs
from salttesting import skipIf
from salttesting.helpers import ensure_in_syspath, destructiveTest
ensure_in_syspath('../../')
# Import salt libs
import integration
class SysrcModuleTest(integration.ModuleCase):
def setUp(self):
super(SysrcModuleTest, self).setUp()
ret = self.run_function('cmd.has_exec', ['sysrc'])
if not ret:
self.skipTest('sysrc not found')
@skipIf(not sys.platform.startswith('freebsd'), 'FreeBSD specific')
def test_show(self):
ret = self.run_function('sysrc.get')
self.assertIsInstance(ret, dict, 'sysrc.get returned wrong type, expecting dictionary')
self.assertIn('/etc/rc.conf', ret, 'sysrc.get should have an rc.conf key in it.')
@skipIf(not sys.platform.startswith('freebsd'), 'FreeBSD specific')
@destructiveTest
def test_set(self):
ret = self.run_function('sysrc.set', ['test_var', '1'])
self.assertIsInstance(ret, dict, 'sysrc.get returned wrong type, expecting dictionary')
self.assertIn('/etc/rc.conf', ret, 'sysrc.set should have an rc.conf key in it.')
self.assertIn('1', ret['/etc/rc.conf']['test_var'], 'sysrc.set should return the value it set.')
ret = self.run_function('sysrc.remove', ['test_var'])
self.assertEqual('test_var removed', ret)
@skipIf(not sys.platform.startswith('freebsd'), 'FreeBSD specific')
@destructiveTest
def test_set_bool(self):
ret = self.run_function('sysrc.set', ['test_var', True])
self.assertIsInstance(ret, dict, 'sysrc.get returned wrong type, expecting dictionary')
self.assertIn('/etc/rc.conf', ret, 'sysrc.set should have an rc.conf key in it.')
self.assertIn('YES', ret['/etc/rc.conf']['test_var'], 'sysrc.set should return the value it set.')
ret = self.run_function('sysrc.remove', ['test_var'])
self.assertEqual('test_var removed', ret)
if __name__ == '__main__':
from integration import run_tests
run_tests(SysrcModuleTest)