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
8
10
from pylibsshext .session import Session
11
+ from pylibsshext .logging import ANSIBLE_PYLIBSSH_TRACE
9
12
10
13
11
14
def test_make_session ():
@@ -19,3 +22,47 @@ def test_session_connection_refused(free_port_num):
19
22
ssh_session = Session ()
20
23
with pytest .raises (LibsshSessionException , match = error_msg ):
21
24
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