2015-10-02 18:11:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
2017-12-05 20:43:13 +00:00
|
|
|
# (c) 2017, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2015-10-02 18:11:48 +00:00
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import syslog
|
2017-12-05 20:43:13 +00:00
|
|
|
from itertools import product
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
import pytest
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2016-05-10 15:13:48 +00:00
|
|
|
import ansible.module_utils.basic
|
2017-12-05 20:43:13 +00:00
|
|
|
from ansible.module_utils.six import PY3
|
2016-04-14 16:06:38 +00:00
|
|
|
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
class TestAnsibleModuleLogSmokeTest:
|
|
|
|
DATA = [u'Text string', u'Toshio くらとみ non-ascii test']
|
|
|
|
DATA = DATA + [d.encode('utf-8') for d in DATA]
|
|
|
|
DATA += [b'non-utf8 :\xff: test']
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
|
|
|
@pytest.mark.parametrize('msg, stdin', ((m, {}) for m in DATA), indirect=['stdin']) # pylint: disable=undefined-variable
|
|
|
|
def test_smoketest_syslog(self, am, mocker, msg):
|
2015-10-02 18:11:48 +00:00
|
|
|
# These talk to the live daemons on the system. Need to do this to
|
|
|
|
# show that what we send doesn't cause an issue once it gets to the
|
|
|
|
# daemon. These are just smoketests to test that we don't fail.
|
2017-12-05 20:43:13 +00:00
|
|
|
mocker.patch('ansible.module_utils.basic.has_journal', False)
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
am.log(u'Text string')
|
|
|
|
am.log(u'Toshio くらとみ non-ascii test')
|
2016-04-12 14:10:45 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
am.log(b'Byte string')
|
|
|
|
am.log(u'Toshio くらとみ non-ascii test'.encode('utf-8'))
|
|
|
|
am.log(b'non-utf8 :\xff: test')
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
@pytest.mark.skipif(not ansible.module_utils.basic.has_journal, reason='python systemd bindings not installed')
|
|
|
|
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
|
|
|
@pytest.mark.parametrize('msg, stdin', ((m, {}) for m in DATA), indirect=['stdin']) # pylint: disable=undefined-variable
|
|
|
|
def test_smoketest_journal(self, am, mocker, msg):
|
2015-10-02 18:11:48 +00:00
|
|
|
# These talk to the live daemons on the system. Need to do this to
|
|
|
|
# show that what we send doesn't cause an issue once it gets to the
|
|
|
|
# daemon. These are just smoketests to test that we don't fail.
|
2017-12-05 20:43:13 +00:00
|
|
|
mocker.patch('ansible.module_utils.basic.has_journal', True)
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
am.log(u'Text string')
|
|
|
|
am.log(u'Toshio くらとみ non-ascii test')
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
am.log(b'Byte string')
|
|
|
|
am.log(u'Toshio くらとみ non-ascii test'.encode('utf-8'))
|
|
|
|
am.log(b'non-utf8 :\xff: test')
|
2015-10-02 18:11:48 +00:00
|
|
|
|
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
class TestAnsibleModuleLogSyslog:
|
2015-10-02 18:11:48 +00:00
|
|
|
"""Test the AnsibleModule Log Method"""
|
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
PY2_OUTPUT_DATA = {
|
2017-01-29 07:28:53 +00:00
|
|
|
u'Text string': b'Text string',
|
|
|
|
u'Toshio くらとみ non-ascii test': u'Toshio くらとみ non-ascii test'.encode('utf-8'),
|
|
|
|
b'Byte string': b'Byte string',
|
|
|
|
u'Toshio くらとみ non-ascii test'.encode('utf-8'): u'Toshio くらとみ non-ascii test'.encode('utf-8'),
|
|
|
|
b'non-utf8 :\xff: test': b'non-utf8 :\xff: test'.decode('utf-8', 'replace').encode('utf-8'),
|
2017-05-30 17:05:19 +00:00
|
|
|
}
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
PY3_OUTPUT_DATA = {
|
2017-01-29 07:28:53 +00:00
|
|
|
u'Text string': u'Text string',
|
|
|
|
u'Toshio くらとみ non-ascii test': u'Toshio くらとみ non-ascii test',
|
|
|
|
b'Byte string': u'Byte string',
|
|
|
|
u'Toshio くらとみ non-ascii test'.encode('utf-8'): u'Toshio くらとみ non-ascii test',
|
|
|
|
b'non-utf8 :\xff: test': b'non-utf8 :\xff: test'.decode('utf-8', 'replace')
|
2017-05-30 17:05:19 +00:00
|
|
|
}
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
@pytest.mark.parametrize('no_log, stdin', (product((True, False), [{}])), indirect=['stdin'])
|
|
|
|
def test_no_log(self, am, mocker, no_log):
|
|
|
|
"""Test that when no_log is set, logging does not occur"""
|
|
|
|
mock_syslog = mocker.patch('syslog.syslog', autospec=True)
|
|
|
|
mocker.patch('ansible.module_utils.basic.has_journal', False)
|
|
|
|
am.no_log = no_log
|
|
|
|
am.log('unittest no_log')
|
|
|
|
if no_log:
|
|
|
|
assert not mock_syslog.called
|
2015-10-02 18:11:48 +00:00
|
|
|
else:
|
2017-12-05 20:43:13 +00:00
|
|
|
mock_syslog.assert_called_once_with(syslog.LOG_INFO, 'unittest no_log')
|
|
|
|
|
|
|
|
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
|
|
|
@pytest.mark.parametrize('msg, param, stdin',
|
|
|
|
((m, p, {}) for m, p in
|
|
|
|
(PY3_OUTPUT_DATA.items() if PY3 else PY2_OUTPUT_DATA.items())), # pylint: disable=undefined-variable
|
|
|
|
indirect=['stdin'])
|
|
|
|
def test_output_matches(self, am, mocker, msg, param):
|
|
|
|
"""Check that log messages are sent correctly"""
|
|
|
|
mocker.patch('ansible.module_utils.basic.has_journal', False)
|
|
|
|
mock_syslog = mocker.patch('syslog.syslog', autospec=True)
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
am.log(msg)
|
|
|
|
mock_syslog.assert_called_once_with(syslog.LOG_INFO, param)
|
2015-10-02 18:11:48 +00:00
|
|
|
|
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
@pytest.mark.skipif(not ansible.module_utils.basic.has_journal, reason='python systemd bindings not installed')
|
|
|
|
class TestAnsibleModuleLogJournal:
|
2015-10-02 18:11:48 +00:00
|
|
|
"""Test the AnsibleModule Log Method"""
|
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
OUTPUT_DATA = {
|
2017-01-29 07:28:53 +00:00
|
|
|
u'Text string': u'Text string',
|
|
|
|
u'Toshio くらとみ non-ascii test': u'Toshio くらとみ non-ascii test',
|
|
|
|
b'Byte string': u'Byte string',
|
|
|
|
u'Toshio くらとみ non-ascii test'.encode('utf-8'): u'Toshio くらとみ non-ascii test',
|
|
|
|
b'non-utf8 :\xff: test': b'non-utf8 :\xff: test'.decode('utf-8', 'replace')
|
2017-05-30 17:05:19 +00:00
|
|
|
}
|
2015-10-02 18:11:48 +00:00
|
|
|
|
2017-12-05 20:43:13 +00:00
|
|
|
@pytest.mark.parametrize('no_log, stdin', (product((True, False), [{}])), indirect=['stdin'])
|
|
|
|
def test_no_log(self, am, mocker, no_log):
|
|
|
|
journal_send = mocker.patch('systemd.journal.send')
|
|
|
|
am.no_log = no_log
|
|
|
|
am.log('unittest no_log')
|
|
|
|
if no_log:
|
|
|
|
assert not journal_send.called
|
|
|
|
else:
|
|
|
|
assert journal_send.called == 1
|
|
|
|
# Message
|
|
|
|
# call_args is a 2-tuple of (arg_list, kwarg_dict)
|
2018-07-25 17:45:51 +00:00
|
|
|
assert journal_send.call_args[1]['MESSAGE'].endswith('unittest no_log'), 'Message was not sent to log'
|
2017-12-05 20:43:13 +00:00
|
|
|
# log adds this journal field
|
|
|
|
assert 'MODULE' in journal_send.call_args[1]
|
|
|
|
assert 'basic.py' in journal_send.call_args[1]['MODULE']
|
|
|
|
|
|
|
|
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
|
|
|
@pytest.mark.parametrize('msg, param, stdin',
|
|
|
|
((m, p, {}) for m, p in OUTPUT_DATA.items()), # pylint: disable=undefined-variable
|
|
|
|
indirect=['stdin'])
|
|
|
|
def test_output_matches(self, am, mocker, msg, param):
|
|
|
|
journal_send = mocker.patch('systemd.journal.send')
|
|
|
|
am.log(msg)
|
|
|
|
assert journal_send.call_count == 1, 'journal.send not called exactly once'
|
2018-07-25 17:45:51 +00:00
|
|
|
assert journal_send.call_args[1]['MESSAGE'].endswith(param)
|
2017-12-05 20:43:13 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('stdin', ({},), indirect=['stdin'])
|
|
|
|
def test_log_args(self, am, mocker):
|
|
|
|
journal_send = mocker.patch('systemd.journal.send')
|
|
|
|
am.log('unittest log_args', log_args=dict(TEST='log unittest'))
|
|
|
|
assert journal_send.called == 1
|
2018-07-25 17:45:51 +00:00
|
|
|
assert journal_send.call_args[1]['MESSAGE'].endswith('unittest log_args'), 'Message was not sent to log'
|
2015-10-02 18:11:48 +00:00
|
|
|
|
|
|
|
# log adds this journal field
|
2017-12-05 20:43:13 +00:00
|
|
|
assert 'MODULE' in journal_send.call_args[1]
|
|
|
|
assert 'basic.py' in journal_send.call_args[1]['MODULE']
|
2015-10-02 18:11:48 +00:00
|
|
|
|
|
|
|
# We added this journal field
|
2017-12-05 20:43:13 +00:00
|
|
|
assert 'TEST' in journal_send.call_args[1]
|
|
|
|
assert 'log unittest' in journal_send.call_args[1]['TEST']
|