Skip to content

Commit d430b6b

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

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/unit/session_test.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

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

5+
import logging
6+
57
import pytest
68

79
from pylibsshext.errors import LibsshSessionException
810
from pylibsshext.session import Session
11+
from pylibsshext.logging import ANSIBLE_PYLIBSSH_TRACE
912

1013

1114
def test_make_session():
@@ -19,3 +22,47 @@ def test_session_connection_refused(free_port_num):
1922
ssh_session = Session()
2023
with pytest.raises(LibsshSessionException, match=error_msg):
2124
ssh_session.connect(host='127.0.0.1', port=free_port_num)
25+
26+
27+
def test_session_log_level_debug(caplog, free_port_num):
28+
"""Test setting the log level to DEBUG should reveal copyright information."""
29+
ssh_session = Session()
30+
ssh_session.set_log_level(logging.DEBUG)
31+
32+
# the connection will fail but first log lands before that
33+
with pytest.raises(LibsshSessionException):
34+
ssh_session.connect(host='127.0.0.1', port=free_port_num)
35+
36+
found_copyright = False
37+
for record in caplog.records:
38+
if record.levelname == 'DEBUG' and 'and libssh contributors.' in record.msg:
39+
found_copyright = True
40+
assert found_copyright
41+
42+
43+
def test_session_log_level_no_log(caplog, free_port_num):
44+
"""Test setting the log level to NONE should be quiet."""
45+
ssh_session = Session()
46+
ssh_session.set_log_level(logging.NOTSET)
47+
48+
# the connection will fail but first log lands before that
49+
with pytest.raises(LibsshSessionException):
50+
ssh_session.connect(host='127.0.0.1', port=free_port_num)
51+
52+
assert len(caplog.records) == 0
53+
54+
55+
def test_session_log_level_trace(caplog, free_port_num):
56+
"""Test setting the log level to TRACE should provide even more logs."""
57+
ssh_session = Session()
58+
ssh_session.set_log_level(ANSIBLE_PYLIBSSH_TRACE)
59+
60+
# the connection will fail but first log lands before that
61+
with pytest.raises(LibsshSessionException):
62+
ssh_session.connect(host='127.0.0.1', port=free_port_num)
63+
64+
found_trace = False
65+
for record in caplog.records:
66+
if record.levelname == 'TRACE' and 'socket_callback_connected: Connection refused' in record.msg:
67+
found_trace = True
68+
assert found_trace

0 commit comments

Comments
 (0)