Skip to content

Commit 0664393

Browse files
committed
Logging tests
1 parent 5542cf2 commit 0664393

File tree

3 files changed

+166
-2
lines changed

3 files changed

+166
-2
lines changed

reportportal_client/logs/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _log(self, level, msg, args,
7777
record = self.makeRecord(self.name, level, fn, lno, msg, args,
7878
exc_info, func, extra, sinfo)
7979

80-
if not hasattr(record, 'attachment'):
80+
if not getattr(record, 'attachment', None):
8181
record.attachment = attachment
8282
self.handle(record)
8383

@@ -156,6 +156,6 @@ def emit(self, record):
156156
timestamp(),
157157
msg,
158158
level=self._loglevel_map[level],
159-
attachment=record.__dict__.get('attachment', None),
159+
attachment=getattr(record, 'attachment'),
160160
item_id=rp_client.current_item()
161161
)

tests/logs/test_rp_log_handler.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright (c) 2022 https://reportportal.io .
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# https://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License
13+
import re
14+
15+
import pytest
16+
from six.moves import mock
17+
18+
from reportportal_client._local import set_current
19+
from reportportal_client.logs import RPLogHandler, RPLogger
20+
21+
22+
@pytest.mark.parametrize(
23+
'logger_name, filter_logs,expected_result',
24+
[
25+
('reportportal_client', False, True),
26+
('reportportal_client', True, False),
27+
('some_logger', False, True),
28+
('some_logger', True, True)
29+
]
30+
)
31+
@mock.patch('reportportal_client.logs.logging.Logger.handle')
32+
def test_filter_client_logs(mocked_handle, logger_name, filter_logs,
33+
expected_result):
34+
RPLogger(logger_name).info('test message')
35+
record = mocked_handle.call_args[0][0]
36+
37+
log_handler = RPLogHandler(filter_client_logs=filter_logs)
38+
assert log_handler.filter(record) == expected_result
39+
40+
41+
@pytest.mark.parametrize(
42+
'hostname, expected_result',
43+
[
44+
('localhost', True),
45+
('docker.local', False)
46+
]
47+
)
48+
@mock.patch('reportportal_client.logs.logging.Logger.handle')
49+
def test_filter_by_endpoint(mocked_handle, hostname, expected_result):
50+
RPLogger('urllib3.connectionpool').info(hostname + ': test message')
51+
record = mocked_handle.call_args[0][0]
52+
log_handler = RPLogHandler(
53+
filter_client_logs=True,
54+
endpoint='http://docker.local:8080'
55+
)
56+
assert log_handler.filter(record) == expected_result
57+
58+
59+
@mock.patch('reportportal_client.logs.logging.Logger.handle')
60+
def test_emit_simple(mocked_handle):
61+
test_message = 'test message'
62+
RPLogger('test_logger').info(test_message)
63+
record = mocked_handle.call_args[0][0]
64+
65+
item_id = 'item_id'
66+
mock_client = mock.Mock()
67+
mock_client.current_item.side_effect = lambda: item_id
68+
set_current(mock_client)
69+
70+
log_handler = RPLogHandler()
71+
log_handler.emit(record)
72+
73+
assert mock_client.log.call_count == 1
74+
call_args = mock_client.log.call_args[0]
75+
call_kwargs = mock_client.log.call_args[1]
76+
77+
assert re.match('^[0-9]+$', call_args[0])
78+
assert test_message == call_args[1]
79+
assert call_kwargs['level'] == 'INFO'
80+
assert not call_kwargs['attachment']
81+
assert call_kwargs['item_id'] == item_id
82+
83+
84+
@mock.patch('reportportal_client.logs.logging.Logger.handle')
85+
def test_emit_custom_level(mocked_handle):
86+
test_message = 'test message'
87+
RPLogger('test_logger').log(30, test_message)
88+
record = mocked_handle.call_args[0][0]
89+
90+
mock_client = mock.Mock()
91+
set_current(mock_client)
92+
93+
log_handler = RPLogHandler()
94+
log_handler.emit(record)
95+
assert mock_client.log.call_count == 1
96+
call_kwargs = mock_client.log.call_args[1]
97+
assert call_kwargs['level'] == 'WARN'

tests/logs/test_rp_logger.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright (c) 2022 https://reportportal.io .
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# https://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License
13+
import logging
14+
from logging import LogRecord
15+
16+
import pytest
17+
from six.moves import mock
18+
19+
from reportportal_client._local import set_current
20+
from reportportal_client.logs import RPLogger, RPLogHandler
21+
22+
23+
def verify_record(logger_handler):
24+
assert logger_handler.call_count == 1
25+
call_args = logger_handler.call_args[0]
26+
assert len(call_args) == 1
27+
record = call_args[0]
28+
assert isinstance(record, LogRecord)
29+
return record
30+
31+
32+
@mock.patch('reportportal_client.logs.logging.Logger.handle')
33+
def test_record_make(logger_handler):
34+
logger = RPLogger('test_logger')
35+
logger.info('test_log')
36+
record = verify_record(logger_handler)
37+
assert not getattr(record, 'attachment')
38+
39+
40+
@mock.patch('reportportal_client.logs.logging.Logger.handle')
41+
def test_record_attachment(logger_handler):
42+
logger = RPLogger('test_logger')
43+
attachment = {'name': 'test.txt', 'content': 'test',
44+
'content_type': 'text/plain'}
45+
logger.info('test_log', attachment=attachment)
46+
record = verify_record(logger_handler)
47+
result_attachment = getattr(record, 'attachment')
48+
assert result_attachment
49+
assert result_attachment == attachment
50+
51+
52+
@pytest.mark.parametrize(
53+
'handler_level, log_level, expected_calls',
54+
[
55+
(logging.WARN, 'info', 0),
56+
(logging.INFO, 'info', 1),
57+
]
58+
)
59+
def test_log_level_filter(handler_level, log_level, expected_calls):
60+
mock_client = mock.Mock()
61+
set_current(mock_client)
62+
63+
logger = RPLogger('test_logger')
64+
logger.addHandler(RPLogHandler(level=handler_level))
65+
getattr(logger, log_level)('test_log')
66+
67+
assert mock_client.log.call_count == expected_calls

0 commit comments

Comments
 (0)