Skip to content

Commit 6912306

Browse files
committed
test for setting log level
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 6faea1a commit 6912306

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

tests/unit/session_test.py

Lines changed: 58 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_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()
@@ -18,4 +24,55 @@ def test_session_connection_refused(free_port_num):
1824
error_msg = '^ssh connect failed: Connection refused$'
1925
ssh_session = Session()
2026
with pytest.raises(LibsshSessionException, match=error_msg):
21-
ssh_session.connect(host='127.0.0.1', port=free_port_num)
27+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
28+
29+
30+
def test_session_log_level_debug(caplog, free_port_num):
31+
"""Test setting the log level to DEBUG should reveal copyright information."""
32+
ssh_session = Session()
33+
ssh_session.set_log_level(logging.DEBUG)
34+
35+
# the connection will fail but first log lands before that
36+
with pytest.raises(LibsshSessionException, match='ssh connect failed: Connection refused'):
37+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
38+
39+
expected_copyright_substring = 'and libssh contributors.'
40+
# This log message is shown at different log levels
41+
# in different libssh versions:
42+
expected_copyright_log_levels = {'DEBUG', 'INFO'}
43+
informational_log_messages = (
44+
record.msg
45+
for record in caplog.records
46+
if record.levelname in expected_copyright_log_levels
47+
)
48+
assert any([expected_copyright_substring in message for message in informational_log_messages])
49+
50+
51+
def test_session_log_level_no_log(caplog, free_port_num):
52+
"""Test setting the log level to CRITICAL should be quiet (unless there is something critical)."""
53+
ssh_session = Session()
54+
ssh_session.set_log_level(logging.CRITICAL)
55+
56+
# the connection will fail but first log lands before that
57+
with pytest.raises(LibsshSessionException, match='ssh connect failed: Connection refused'):
58+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
59+
60+
assert not caplog.records
61+
62+
63+
def test_session_log_level_trace(caplog, free_port_num):
64+
"""Test setting the log level to TRACE should provide all of the logs."""
65+
ssh_session = Session()
66+
ssh_session.set_log_level(ANSIBLE_PYLIBSSH_TRACE)
67+
68+
# the connection will fail but first log lands before that
69+
with pytest.raises(LibsshSessionException, match='ssh connect failed: Connection refused'):
70+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
71+
72+
expected_poll_message = 'ssh_socket_pollcallback: Poll callback on socket'
73+
informational_log_messages = (
74+
record.msg
75+
for record in caplog.records
76+
if record.levelname == 'TRACE'
77+
)
78+
assert any([expected_poll_message in message for message in informational_log_messages])

0 commit comments

Comments
 (0)