GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — code42cli/tests/logger/test_init.py at main · code42/code42cli

Expand file tree

/

test_init.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
172 lines (136 loc) · 6.01 KB
/

test_init.py

Copy path

File metadata and controls

172 lines (136 loc) · 6.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import logging
import os
from logging.handlers import RotatingFileHandler
import pytest
from requests import Request
from code42cli.enums import OutputFormat
from code42cli.enums import SendToFileEventsOutputFormat
from code42cli.logger import add_handler_to_logger
from code42cli.logger import CliLogger
from code42cli.logger import get_logger_for_server
from code42cli.logger import get_view_error_details_message
from code42cli.logger import logger_has_handlers
from code42cli.logger.enums import ServerProtocol
from code42cli.logger.formatters import FileEventDictToCEFFormatter
from code42cli.logger.formatters import FileEventDictToJSONFormatter
from code42cli.logger.formatters import FileEventDictToRawJSONFormatter
from code42cli.logger.handlers import NoPrioritySysLogHandler
from code42cli.util import get_user_project_path
@pytest.fixture(autouse=True)
def init_socket_mock(mocker):
return mocker.patch("code42cli.logger.NoPrioritySysLogHandler.connect_socket")
@pytest.fixture(autouse=True)
def fresh_syslog_handler(init_socket_mock):
# Set handlers to empty list so it gets initialized each test
get_logger_for_server(
"example.com",
ServerProtocol.TCP,
SendToFileEventsOutputFormat.CEF,
None,
).handlers = []
init_socket_mock.call_count = 0
def test_add_handler_to_logger_does_as_expected():
logger = logging.getLogger("TEST_CODE42_CLI")
formatter = logging.Formatter()
handler = logging.Handler()
add_handler_to_logger(logger, handler, formatter)
assert handler in logger.handlers
assert handler.formatter == formatter
def test_logger_has_handlers_when_logger_has_handlers_returns_true():
logger = logging.getLogger("TEST_CODE42_CLI")
handler = logging.Handler()
logger.addHandler(handler)
assert logger_has_handlers(logger)
def test_logger_has_handlers_when_logger_does_not_have_handlers_returns_false():
logger = logging.getLogger("TEST_CODE42_CLI")
logger.handlers = []
assert not logger_has_handlers(logger)
def test_get_view_exceptions_location_message_returns_expected_message():
actual = get_view_error_details_message()
path = os.path.join(get_user_project_path("log"), "code42_errors.log")
expected = f"View details in {path}"
assert actual == expected
def test_get_logger_for_server_has_info_level():
logger = get_logger_for_server(
"example.com", ServerProtocol.TCP, SendToFileEventsOutputFormat.CEF, None
)
assert logger.level == logging.INFO
def test_get_logger_for_server_when_given_cef_format_uses_cef_formatter():
logger = get_logger_for_server(
"example.com", ServerProtocol.TCP, SendToFileEventsOutputFormat.CEF, None
)
assert isinstance(logger.handlers[0].formatter, FileEventDictToCEFFormatter)
def test_get_logger_for_server_when_given_json_format_uses_json_formatter():
logger = get_logger_for_server(
"example.com", ServerProtocol.TCP, OutputFormat.JSON, None
)
assert isinstance(logger.handlers[0].formatter, FileEventDictToJSONFormatter)
def test_get_logger_for_server_when_given_raw_json_format_uses_raw_json_formatter():
logger = get_logger_for_server(
"example.com", ServerProtocol.TCP, OutputFormat.RAW, None
)
assert isinstance(logger.handlers[0].formatter, FileEventDictToRawJSONFormatter)
def test_get_logger_for_server_when_called_twice_only_has_one_handler():
get_logger_for_server("example.com", ServerProtocol.TCP, OutputFormat.JSON, None)
logger = get_logger_for_server(
"example.com", ServerProtocol.TCP, SendToFileEventsOutputFormat.CEF, None
)
assert len(logger.handlers) == 1
def test_get_logger_for_server_uses_no_priority_syslog_handler():
logger = get_logger_for_server(
"example.com", ServerProtocol.TCP, SendToFileEventsOutputFormat.CEF, None
)
assert isinstance(logger.handlers[0], NoPrioritySysLogHandler)
def test_get_logger_for_server_constructs_handler_with_expected_args(
mocker, monkeypatch
):
no_priority_syslog_handler = mocker.patch(
"code42cli.logger.handlers.NoPrioritySysLogHandler.__init__"
)
no_priority_syslog_handler.return_value = None
get_logger_for_server(
"example.com", ServerProtocol.TCP, SendToFileEventsOutputFormat.CEF, "cert"
)
no_priority_syslog_handler.assert_called_once_with(
"example.com", 514, ServerProtocol.TCP, "cert"
)
def test_get_logger_for_server_when_hostname_includes_port_constructs_handler_with_expected_args(
mocker,
):
no_priority_syslog_handler = mocker.patch(
"code42cli.logger.handlers.NoPrioritySysLogHandler.__init__"
)
no_priority_syslog_handler.return_value = None
get_logger_for_server(
"example.com:999", ServerProtocol.TCP, SendToFileEventsOutputFormat.CEF, None
)
no_priority_syslog_handler.assert_called_once_with(
"example.com",
999,
ServerProtocol.TCP,
None,
)
def test_get_logger_for_server_inits_socket(init_socket_mock):
get_logger_for_server(
"example.com", ServerProtocol.TCP, SendToFileEventsOutputFormat.CEF, None
)
assert init_socket_mock.call_count == 1
class TestCliLogger:
def test_init_creates_user_error_logger_with_expected_handlers(self):
logger = CliLogger()
handler_types = [type(h) for h in logger._logger.handlers]
assert RotatingFileHandler in handler_types
def test_log_error_logs_expected_text_at_expected_level(self, caplog):
with caplog.at_level(logging.ERROR):
ex = Exception("TEST")
CliLogger().log_error(ex)
assert str(ex) in caplog.text
def test_log_verbose_error_logs_expected_text_at_expected_level(
self, mocker, caplog
):
with caplog.at_level(logging.ERROR):
request = mocker.MagicMock(sepc=Request)
request.body = {"foo": "bar"}
CliLogger().log_verbose_error("code42 dothing --flag YES", request)
assert "'code42 dothing --flag YES'" in caplog.text
assert "Request parameters: {'foo': 'bar'}" in caplog.text
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.