Skip to content

Commit

Permalink
test for setting log level
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Jelen <[email protected]>
  • Loading branch information
Jakuje committed Aug 29, 2024
1 parent 63381f8 commit e27c5ec
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/unit/session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

"""Tests suite for session."""

import logging

import pytest

from pylibsshext.errors import LibsshSessionException
from pylibsshext.session import Session
from pylibsshext.logging import ANSIBLE_PYLIBSSH_TRACE


def test_make_session():
Expand All @@ -19,3 +22,45 @@ def test_session_connection_refused(free_port_num):
ssh_session = Session()
with pytest.raises(LibsshSessionException, match=error_msg):
ssh_session.connect(host='127.0.0.1', port=free_port_num)


def test_session_log_level_debug(caplog, free_port_num):
"""Test setting the log level to DEBUG should reveal copyright information."""
ssh_session = Session()
ssh_session.set_log_level(logging.DEBUG)

# the connection will fail but first log lands before that
with pytest.raises(LibsshSessionException):
ssh_session.connect(host='127.0.0.1', port=free_port_num)

found_copyright = False
for record in caplog.records:
if record.levelname == 'DEBUG' and 'and libssh contributors.' in record.msg:
found_copyright = True
assert found_copyright

def test_session_log_level_no_log(caplog, free_port_num):
"""Test setting the log level to NONE should be quiet."""
ssh_session = Session()
ssh_session.set_log_level(logging.NOTSET)

# the connection will fail but first log lands before that
with pytest.raises(LibsshSessionException):
ssh_session.connect(host='127.0.0.1', port=free_port_num)

assert len(caplog.records) == 0

def test_session_log_level_trace(caplog, free_port_num):
"""Test setting the log level to TRACE should provide even more logs."""
ssh_session = Session()
ssh_session.set_log_level(ANSIBLE_PYLIBSSH_TRACE)

# the connection will fail but first log lands before that
with pytest.raises(LibsshSessionException):
ssh_session.connect(host='127.0.0.1', port=free_port_num)

found_trace = False
for record in caplog.records:
if record.levelname == 'TRACE' and 'socket_callback_connected: Connection refused' in record.msg:
found_trace = True
assert found_trace

0 comments on commit e27c5ec

Please sign in to comment.