22
33"""Tests suite for session."""
44
5+ import logging
6+
57import pytest
68
79from pylibsshext .errors import LibsshSessionException
10+ from pylibsshext .logging import ANSIBLE_PYLIBSSH_TRACE
811from pylibsshext .session import Session
912
1013
14+ LOCALHOST = '127.0.0.1'
15+
1116def test_make_session ():
1217 """Smoke-test Session instance creation."""
1318 assert Session ()
@@ -18,4 +23,49 @@ def test_session_connection_refused(free_port_num):
1823 error_msg = '^ssh connect failed: Connection refused$'
1924 ssh_session = Session ()
2025 with pytest .raises (LibsshSessionException , match = error_msg ):
21- ssh_session .connect (host = '127.0.0.1' , port = free_port_num )
26+ ssh_session .connect (host = LOCALHOST , port = free_port_num )
27+
28+
29+ def test_session_log_level_debug (caplog , free_port_num ):
30+ """Test setting the log level to DEBUG should reveal copyright information."""
31+ ssh_session = Session ()
32+ ssh_session .set_log_level (logging .DEBUG )
33+
34+ # the connection will fail but first log lands before that
35+ with pytest .raises (LibsshSessionException ):
36+ ssh_session .connect (host = LOCALHOST , port = free_port_num )
37+
38+ found_copyright = False
39+ for record in caplog .records :
40+ # This log message is shown at different log levels in different libssh versions
41+ if record .levelname in {'DEBUG' , 'INFO' } and 'and libssh contributors.' in record .msg :
42+ found_copyright = True
43+ assert found_copyright
44+
45+
46+ def test_session_log_level_no_log (caplog , free_port_num ):
47+ """Test setting the log level to NONE should be quiet."""
48+ ssh_session = Session ()
49+ ssh_session .set_log_level (logging .NOTSET )
50+
51+ # the connection will fail but first log lands before that
52+ with pytest .raises (LibsshSessionException ):
53+ ssh_session .connect (host = LOCALHOST , port = free_port_num )
54+
55+ assert not caplog .records
56+
57+
58+ def test_session_log_level_trace (caplog , free_port_num ):
59+ """Test setting the log level to TRACE should provide even more logs."""
60+ ssh_session = Session ()
61+ ssh_session .set_log_level (ANSIBLE_PYLIBSSH_TRACE )
62+
63+ # the connection will fail but first log lands before that
64+ with pytest .raises (LibsshSessionException ):
65+ ssh_session .connect (host = LOCALHOST , port = free_port_num )
66+
67+ found_trace = False
68+ for record in caplog .records :
69+ if record .levelname == 'TRACE' and 'ssh_socket_pollcallback: Poll callback on socket' in record .msg :
70+ found_trace = True
71+ assert found_trace
0 commit comments