1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024 Simon Ruderich
13 "ruderich.org/simon/safcm"
14 "ruderich.org/simon/safcm/rpc"
15 "ruderich.org/simon/safcm/testutil"
18 type TestHost struct {
22 func (th *TestHost) Name() string {
25 func (th *TestHost) Dial(*rpc.Conn) error {
26 return fmt.Errorf("not implemented")
29 func TestLogEvent(t *testing.T) {
30 // Restore default logger
31 defer log.SetFlags(log.Flags())
32 defer log.SetOutput(os.Stderr)
46 Error: fmt.Errorf("fake error"),
50 "[error] [fake-host] fake error\n",
56 Error: fmt.Errorf("fake error"),
60 "[error] [\x1b[31mfake-host\x1b[0m] fake error\n",
66 Error: fmt.Errorf("\x00"),
70 "[error] [fake-host] \\x00\n",
74 "Error: escape (tty)",
76 Error: fmt.Errorf("\x00"),
80 "[error] [\x1b[31mfake-host\x1b[0m] \x1b[35m\\x00\x1b[0m\n",
94 "[info] [fake-host] info log\n",
101 Level: safcm.LogInfo,
107 "[info] [fake-host] info log\n",
114 Level: safcm.LogVerbose,
120 "[verbose] [fake-host] verbose log\n",
127 Level: safcm.LogDebug,
133 "[debug] [fake-host] debug log\n",
140 Level: safcm.LogDebug2,
146 "[debug2] [fake-host] debug2 log\n",
153 Level: safcm.LogDebug3,
159 fmt.Sprintf("[INVALID=%d] [fake-host] debug3 log\n",
167 Level: safcm.LogDebug3,
173 fmt.Sprintf("[INVALID=%d] [\x1b[31mfake-host\x1b[0m] debug3 log\n",
181 Level: safcm.LogInfo,
187 "[info] [fake-host] \\x00\n",
194 Level: safcm.LogInfo,
200 "[info] [fake-host] \x1b[35m\\x00\x1b[0m\n",
207 ConnEvent: rpc.ConnEvent{
208 Type: rpc.ConnEventStderr,
214 "[stderr] [fake-host] fake stderr\n",
218 "ConnEvent: stderr (tty)",
220 ConnEvent: rpc.ConnEvent{
221 Type: rpc.ConnEventStderr,
227 "[stderr] [fake-host] fake stderr\n",
233 ConnEvent: rpc.ConnEvent{
234 Type: rpc.ConnEventDebug,
240 "[debug3] [fake-host] conn debug\n",
246 ConnEvent: rpc.ConnEvent{
247 Type: rpc.ConnEventUpload,
252 "[info] [fake-host] remote helper upload in progress\n",
256 "ConnEvent: upload (ignored)",
258 ConnEvent: rpc.ConnEvent{
259 Type: rpc.ConnEventUpload,
268 "ConnEvent: invalid",
270 ConnEvent: rpc.ConnEvent{
277 "[INVALID=42] [fake-host] invalid\n",
281 "ConnEvent: invalid (tty)",
283 ConnEvent: rpc.ConnEvent{
290 "[INVALID=42] [\x1b[31mfake-host\x1b[0m] invalid\n",
296 ConnEvent: rpc.ConnEvent{
297 Type: rpc.ConnEventStderr,
303 "[stderr] [fake-host] \\x00\n",
307 "ConnEvent: escape (tty)",
309 ConnEvent: rpc.ConnEvent{
310 Type: rpc.ConnEventDebug,
316 "[debug3] [fake-host] \x1b[35m\\x01\x1b[0m\n",
324 Level: safcm.LogInfo,
331 "[info] [fake-host] \x00\n",
338 Level: safcm.LogInfo,
345 "[info] [fake-host] \x00\n",
354 "[INVALID=0] [fake-host] \n",
359 for _, tc := range tests {
360 t.Run(tc.name, func(t *testing.T) {
361 tc.event.Host = &TestHost{
370 LogEvent(tc.event, tc.level, tc.isTTY, &failed)
372 testutil.AssertEqual(t, "log",
373 buf.String(), tc.exp)
374 testutil.AssertEqual(t, "failed",
375 failed, tc.expFailed)