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_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 ()
@@ -18,4 +24,55 @@ def test_session_connection_refused(free_port_num):
18
24
error_msg = '^ssh connect failed: Connection refused$'
19
25
ssh_session = Session ()
20
26
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