Merge pull request #48579 from Ch3LL/fix_syslog

Fix python2 syslog returner expecting string bytes not unicode
This commit is contained in:
Nicole Thomas 2018-07-13 15:50:15 -04:00 committed by GitHub
commit 4b16537a29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 8 deletions

View File

@ -192,14 +192,10 @@ def returner(ret):
logoption = logoption | getattr(syslog, opt)
# Open syslog correctly based on options and tag
try:
if 'tag' in _options:
syslog.openlog(ident=_options['tag'], logoption=logoption)
else:
syslog.openlog(logoption=logoption)
except TypeError:
# Python 2.6 syslog.openlog does not accept keyword args
syslog.openlog(_options.get('tag', 'salt-minion'), logoption)
if 'tag' in _options:
syslog.openlog(ident=salt.utils.stringutils.to_str(_options['tag']), logoption=logoption)
else:
syslog.openlog(logoption=logoption)
# Send log of given level and facility
syslog.syslog(facility | level, salt.utils.json.dumps(ret))

View File

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Megan Wilhite (mwilhite@saltstack.com)`
tests.unit.returners.test_syslog_return
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'''
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
# Import salt libs
import salt.returners.syslog_return as syslog
@skipIf(NO_MOCK, NO_MOCK_REASON)
class SyslogReturnerTestCase(TestCase, LoaderModuleMockMixin):
'''
Test Syslog returner
'''
def setup_loader_modules(self):
return {syslog: {}}
def test_syslog_returner_unicode(self):
'''
test syslog returner with unicode
'''
ret = {'fun_args': [], 'jid': '20180713160901624786', 'return': True,
'retcode': 0, 'success': True, 'fun': 'test.ping', 'id': '02e10e971a30'}
opts = {u'level': u'LOG_INFO', u'options': [],
u'facility': u'LOG_USER', u'tag': u'salt-minion'}
with patch('salt.returners.syslog_return._get_options',
MagicMock(return_value=opts)):
try:
syslog.returner(ret)
except Exception as e:
self.fail('syslog.returner() failed with exception: {0}'.format(e))