Skip to content

Commit 24a31cb

Browse files
committed
test for setting log level
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 3dfb6f7 commit 24a31cb

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

tests/unit/session_test.py

Lines changed: 52 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,49 @@ 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):
37+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
38+
39+
found_copyright = False
40+
for record in caplog.records:
41+
# This log message is shown at different log levels in different libssh versions
42+
if record.levelname in {'DEBUG', 'INFO'} and 'and libssh contributors.' in record.msg:
43+
found_copyright = True
44+
assert found_copyright
45+
46+
47+
def test_session_log_level_no_log(caplog, free_port_num):
48+
"""Test setting the log level to NONE should be quiet."""
49+
ssh_session = Session()
50+
ssh_session.set_log_level(logging.NOTSET)
51+
52+
# the connection will fail but first log lands before that
53+
with pytest.raises(LibsshSessionException):
54+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
55+
56+
assert not caplog.records
57+
58+
59+
def test_session_log_level_trace(caplog, free_port_num):
60+
"""Test setting the log level to TRACE should provide even more logs."""
61+
ssh_session = Session()
62+
ssh_session.set_log_level(ANSIBLE_PYLIBSSH_TRACE)
63+
64+
# the connection will fail but first log lands before that
65+
with pytest.raises(LibsshSessionException):
66+
ssh_session.connect(host=LOCALHOST, port=free_port_num)
67+
68+
found_trace = False
69+
for record in caplog.records:
70+
if record.levelname == 'TRACE' and 'ssh_socket_pollcallback: Poll callback on socket' in record.msg:
71+
found_trace = True
72+
assert found_trace

0 commit comments

Comments
 (0)