2
2
3
3
"""Tests suite for session."""
4
4
5
+ import logging
6
+
5
7
import pytest
6
8
7
9
from pylibsshext .errors import LibsshSessionException
10
+ from pylibsshext .logging import ANSIBLE_PYLIBSSH_NOLOG , ANSIBLE_PYLIBSSH_TRACE
8
11
from pylibsshext .session import Session
9
12
10
13
14
+ LOCALHOST = '127.0.0.1'
15
+
16
+
11
17
def test_make_session ():
12
18
"""Smoke-test Session instance creation."""
13
19
assert Session ()
@@ -27,4 +33,59 @@ def test_session_connection_refused(free_port_num):
27
33
error_msg = '^ssh connect failed: Connection refused$'
28
34
ssh_session = Session ()
29
35
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