File: test_logging.py

package info (click to toggle)
python-scrapli 2023.7.30-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,536 kB
  • sloc: python: 14,459; makefile: 72
file content (104 lines) | stat: -rw-r--r-- 3,350 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import logging
import re
import sys
from pathlib import Path

import pytest

from scrapli.exceptions import ScrapliException
from scrapli.logging import (
    ScrapliFileHandler,
    ScrapliFormatter,
    ScrapliLogRecord,
    enable_basic_logging,
    logger,
)


def test_scrapli_formatter():
    formatter = ScrapliFormatter(log_header=True, caller_info=True)
    record = ScrapliLogRecord(
        name="test_log",
        level=20,
        pathname="somepath",
        lineno=999,
        msg="thisisalogmessage!",
        args=None,
        exc_info=None,
        func="coolfunc",
        message_id=1,
    )
    record.uid = "UID"
    record.host = "scrapli"
    record.port = "22"
    formatted_record = formatter.format(record=record)
    assert (
        re.sub(
            string=formatted_record,
            pattern=r"\d{4}-\d{2}\-\d{2} \d{2}:\d{2}:\d{2},\d{3}",
            repl="_TIMESTMAMP__TIMESTAMP_",
        )
        == "ID    | TIMESTAMP               | LEVEL    | (UID:)HOST:PORT           | MODULE               | FUNCNAME  "
        "           | LINE  | MESSAGE\n1     | _TIMESTMAMP__TIMESTAMP_ | INFO     | UID:scrapli:22            | "
        "somepath             | coolfunc             | 999   | thisisalogmessage!"
    )

    # validate format for messages w/out uid/host
    del record.host
    del record.uid
    formatted_record = formatter.format(record=record)
    assert (
        re.sub(
            string=formatted_record,
            pattern=r"\d{4}-\d{2}\-\d{2} \d{2}:\d{2}:\d{2},\d{3}",
            repl="_TIMESTMAMP__TIMESTAMP_",
        )
        == "2     | _TIMESTMAMP__TIMESTAMP_ | INFO     |                           | somepath             | coolfunc             | 999   | thisisalogmessage!"
    )


def test_scrapli_filehandler():
    pass


def test_enable_basic_logging(fs_):
    assert Path("scrapli.log").is_file() is False
    enable_basic_logging(file=True, level="debug")
    scrapli_logger = logging.getLogger("scrapli")

    assert scrapli_logger.level == 10
    assert isinstance(scrapli_logger.handlers[1], ScrapliFileHandler)
    assert isinstance(scrapli_logger.handlers[1].formatter, ScrapliFormatter)
    assert scrapli_logger.propagate is False

    assert Path("scrapli.log").is_file() is True

    # reset the main logger to propagate and delete the file handler so caplog works!
    logger.propagate = True
    del logger.handlers[1]


def test_enable_basic_logging_no_buffer(fs_):
    assert Path("mylog.log").is_file() is False

    enable_basic_logging(file="mylog.log", level="debug", buffer_log=False, caller_info=True)
    scrapli_logger = logging.getLogger("scrapli")

    assert scrapli_logger.level == 10
    assert isinstance(scrapli_logger.handlers[1], logging.FileHandler)
    assert isinstance(scrapli_logger.handlers[1].formatter, ScrapliFormatter)
    assert scrapli_logger.propagate is False

    assert Path("mylog.log").is_file() is True

    # reset the main logger to propagate and delete the file handler so caplog works!
    logger.propagate = True
    del logger.handlers[1]


def test_enable_basic_logging_bad_mode():
    with pytest.raises(ScrapliException):
        enable_basic_logging(file="mylog.log", level="debug", mode="tacocat")

    # reset the main logger to propagate and delete the file handler so caplog works!
    logger.propagate = True