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,49 @@ 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 ):
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