Merge pull request #46613 from myinitialsarepm/fix_puppet.fact_and_puppet.facts

Fix puppet.fact and puppet.facts to use stdout.
This commit is contained in:
Nicole Thomas 2018-04-10 11:18:07 -04:00 committed by GitHub
commit 39d65a39cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 17 deletions

View File

@ -372,7 +372,12 @@ def facts(puppet=False):
'''
ret = {}
opt_puppet = '--puppet' if puppet else ''
output = __salt__['cmd.run']('facter {0}'.format(opt_puppet))
cmd_ret = __salt__['cmd.run_all']('facter {0}'.format(opt_puppet))
if cmd_ret['retcode'] != 0:
raise CommandExecutionError(cmd_ret['stderr'])
output = cmd_ret['stdout']
# Loop over the facter output and properly
# parse it into a nice dictionary for using
@ -398,9 +403,13 @@ def fact(name, puppet=False):
salt '*' puppet.fact kernel
'''
opt_puppet = '--puppet' if puppet else ''
ret = __salt__['cmd.run'](
ret = __salt__['cmd.run_all'](
'facter {0} {1}'.format(opt_puppet, name),
python_shell=False)
if not ret:
if ret['retcode'] != 0:
raise CommandExecutionError(ret['stderr'])
if not ret['stdout']:
return ''
return ret
return ret['stdout']

View File

@ -164,22 +164,27 @@ class PuppetTestCase(TestCase, LoaderModuleMockMixin):
'''
Test to run facter and return the results
'''
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
mock_lst = MagicMock(return_value="True")
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
mock = MagicMock(return_value=["a", "b"])
with patch.object(puppet, '_format_fact', mock):
self.assertDictEqual(puppet.facts(), {'a': 'b'})
mock = MagicMock(return_value={
'retcode': 0,
'stdout': "1\n2"
})
with patch.dict(puppet.__salt__, {'cmd.run_all': mock}):
mock = MagicMock(side_effect=[
['a', 'b'],
['c', 'd'],
])
with patch.object(puppet, '_format_fact', mock):
self.assertDictEqual(puppet.facts(), {'a': 'b', 'c': 'd'})
def test_fact(self):
'''
Test to run facter for a specific fact
'''
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
mock_lst = MagicMock(side_effect=[False, True])
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
self.assertEqual(puppet.fact("salt"), "")
mock = MagicMock(side_effect=[
{'retcode': 0, 'stdout': False},
{'retcode': 0, 'stdout': True},
])
with patch.dict(puppet.__salt__, {'cmd.run_all': mock}):
self.assertEqual(puppet.fact('salt'), '')
self.assertTrue(puppet.fact("salt"))
self.assertTrue(puppet.fact('salt'))