Debian Codegraph @

test/units/module_utils/basic/test_log.py

raw
1 # -*- coding: utf-8 -*-
2 # (c) 2012-2014, Michael DeHaan <[email protected]>
3 # (c) 2017, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8 import syslog
9 from itertools import product
10
11 import pytest
12
13
14 class TestAnsibleModuleLogSmokeTest:
15 DATA = [u'Text string', u'Toshio くらとみ non-ascii test']
16 DATA = DATA + [d.encode('utf-8') for d in DATA]
17 DATA += [b'non-utf8 :\xff: test']
18
19 @pytest.mark.parametrize('msg, stdin', [(m, {}) for m in DATA], indirect=['stdin'])
20 def test_smoketest_syslog(self, am, mocker, msg):
21 # These talk to the live daemons on the system. Need to do this to
22 # show that what we send doesn't cause an issue once it gets to the
23 # daemon. These are just smoketests to test that we don't fail.
24 mocker.patch('ansible.module_utils._internal._logging.has_journal', False)
25
26 am.log(u'Text string')
27 am.log(u'Toshio くらとみ non-ascii test')
28
29 am.log(b'Byte string')
30 am.log(u'Toshio くらとみ non-ascii test'.encode('utf-8'))
31 am.log(b'non-utf8 :\xff: test')
32
33
34 class TestAnsibleModuleLogSyslog:
35 """Test the AnsibleModule Log Method"""
36
37 OUTPUT_DATA = [
38 (u'Text string', u'Text string'),
39 (u'Toshio くらとみ non-ascii test', u'Toshio くらとみ non-ascii test'),
40 (b'Byte string', u'Byte string'),
41 (u'Toshio くらとみ non-ascii test'.encode('utf-8'), u'Toshio くらとみ non-ascii test'),
42 (b'non-utf8 :\xff: test', b'non-utf8 :\xff: test'.decode('utf-8', 'replace')),
43 ]
44
45 @pytest.mark.parametrize('no_log, stdin', list(product((True, False), [{}])), indirect=['stdin'])
46 def test_no_log(self, am, mocker, no_log):
47 """Test that when no_log is set, logging does not occur"""
48 mock_syslog = mocker.patch('syslog.syslog', autospec=True)
49 mocker.patch('ansible.module_utils._internal._logging.has_journal', False)
50 am.no_log = no_log
51 am.log('unittest no_log')
52 if no_log:
53 assert not mock_syslog.called
54 else:
55 mock_syslog.assert_called_once_with(syslog.LOG_INFO, 'unittest no_log')
56
57 @pytest.mark.parametrize('msg, param, stdin',
58 [(m, p, {}) for m, p in OUTPUT_DATA],
59 indirect=['stdin'])
60 def test_output_matches(self, am, mocker, msg, param):
61 """Check that log messages are sent correctly"""
62 mocker.patch('ansible.module_utils._internal._logging.has_journal', False)
63 mock_syslog = mocker.patch('syslog.syslog', autospec=True)
64
65 am.log(msg)
66 mock_syslog.assert_called_once_with(syslog.LOG_INFO, param)