Skip to content

Commit 57644bf

Browse files
committed
test for setting log level
Signed-off-by: Jakub Jelen <[email protected]>
1 parent 5a03278 commit 57644bf

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

tests/unit/session_test.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
"""Tests suite for session."""
44

5+
import logging
6+
57
import pytest
68

79
from pylibsshext.errors import LibsshSessionException
10+
from pylibsshext.logging import ANSIBLE_PYLIBSSH_NOLOG, ANSIBLE_PYLIBSSH_TRACE
811
from pylibsshext.session import Session
912

1013

14+
LOCALHOST = '127.0.0.1'
15+
16+
1117
def test_make_session():
1218
"""Smoke-test Session instance creation."""
1319
assert Session()
@@ -27,4 +33,59 @@ def test_session_connection_refused(free_port_num):
2733
error_msg = '^ssh connect failed: Connection refused$'
2834
ssh_session = Session()
2935
with pytest.raises(LibsshSessionException, match=error_msg):
30-
ssh_session.connect(host='127.0.0.1', port=free_port_num)
36+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
37+
38+
39+
def test_session_log_level_debug(caplog, free_port_num):
40+
"""Test setting the log level to DEBUG should reveal copyright information. But no trace messages."""
41+
ssh_session = Session()
42+
ssh_session.set_log_level(logging.DEBUG)
43+
44+
# the connection will fail but first log lands before that
45+
with pytest.raises(LibsshSessionException, match='ssh connect failed: Connection refused'):
46+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
47+
48+
expected_copyright_substring = 'and libssh contributors.'
49+
# This log message is shown at different log levels
50+
# in different libssh versions:
51+
expected_copyright_log_levels = {'DEBUG', 'INFO'}
52+
informational_log_messages = (
53+
record.msg
54+
for record in caplog.records
55+
if record.levelname in expected_copyright_log_levels
56+
)
57+
assert any(expected_copyright_substring in message for message in informational_log_messages)
58+
59+
for record in caplog.records:
60+
if record.levelname == 'TRACE':
61+
assert not 'Found unexpected TRACE message {msg}'.format(msg=record.msg)
62+
63+
64+
def test_session_log_level_no_log(caplog, free_port_num):
65+
"""Test setting the log level to NOLOG should be quiet."""
66+
ssh_session = Session()
67+
ssh_session.set_log_level(ANSIBLE_PYLIBSSH_NOLOG)
68+
69+
# the connection will fail but first log lands before that
70+
with pytest.raises(LibsshSessionException, match='ssh connect failed: Connection refused'):
71+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
72+
73+
assert not caplog.records
74+
75+
76+
def test_session_log_level_trace(caplog, free_port_num):
77+
"""Test setting the log level to TRACE should provide all of the logs."""
78+
ssh_session = Session()
79+
ssh_session.set_log_level(ANSIBLE_PYLIBSSH_TRACE)
80+
81+
# the connection will fail but first log lands before that
82+
with pytest.raises(LibsshSessionException, match='ssh connect failed: Connection refused'):
83+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
84+
85+
expected_poll_message = 'ssh_socket_pollcallback: Poll callback on socket'
86+
informational_log_messages = (
87+
record.msg
88+
for record in caplog.records
89+
if record.levelname == 'TRACE'
90+
)
91+
assert any(expected_poll_message in message for message in informational_log_messages)

0 commit comments

Comments
 (0)