]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - frontend/log_test.go
Use SPDX license identifiers
[safcm/safcm.git] / frontend / log_test.go
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024  Simon Ruderich
3
4 package frontend
5
6 import (
7         "bytes"
8         "fmt"
9         "log"
10         "os"
11         "testing"
12
13         "ruderich.org/simon/safcm"
14         "ruderich.org/simon/safcm/rpc"
15         "ruderich.org/simon/safcm/testutil"
16 )
17
18 type TestHost struct {
19         name string
20 }
21
22 func (th *TestHost) Name() string {
23         return th.name
24 }
25 func (th *TestHost) Dial(*rpc.Conn) error {
26         return fmt.Errorf("not implemented")
27 }
28
29 func TestLogEvent(t *testing.T) {
30         // Restore default logger
31         defer log.SetFlags(log.Flags())
32         defer log.SetOutput(os.Stderr)
33
34         tests := []struct {
35                 name      string
36                 event     Event
37                 level     safcm.LogLevel
38                 isTTY     bool
39                 exp       string
40                 expFailed bool
41         }{
42
43                 {
44                         "Error",
45                         Event{
46                                 Error: fmt.Errorf("fake error"),
47                         },
48                         safcm.LogDebug3,
49                         false,
50                         "[error]   [fake-host] fake error\n",
51                         true,
52                 },
53                 {
54                         "Error (tty)",
55                         Event{
56                                 Error: fmt.Errorf("fake error"),
57                         },
58                         safcm.LogDebug3,
59                         true,
60                         "[error]   [\x1b[31mfake-host\x1b[0m] fake error\n",
61                         true,
62                 },
63                 {
64                         "Error: escape",
65                         Event{
66                                 Error: fmt.Errorf("\x00"),
67                         },
68                         safcm.LogDebug3,
69                         false,
70                         "[error]   [fake-host] \\x00\n",
71                         true,
72                 },
73                 {
74                         "Error: escape (tty)",
75                         Event{
76                                 Error: fmt.Errorf("\x00"),
77                         },
78                         safcm.LogDebug3,
79                         true,
80                         "[error]   [\x1b[31mfake-host\x1b[0m] \x1b[35m\\x00\x1b[0m\n",
81                         true,
82                 },
83
84                 {
85                         "Log: info",
86                         Event{
87                                 Log: Log{
88                                         Level: safcm.LogInfo,
89                                         Text:  "info log",
90                                 },
91                         },
92                         safcm.LogDebug3,
93                         false,
94                         "[info]    [fake-host] info log\n",
95                         false,
96                 },
97                 {
98                         "Log: info (tty)",
99                         Event{
100                                 Log: Log{
101                                         Level: safcm.LogInfo,
102                                         Text:  "info log",
103                                 },
104                         },
105                         safcm.LogDebug3,
106                         true,
107                         "[info]    [fake-host] info log\n",
108                         false,
109                 },
110                 {
111                         "Log: verbose",
112                         Event{
113                                 Log: Log{
114                                         Level: safcm.LogVerbose,
115                                         Text:  "verbose log",
116                                 },
117                         },
118                         safcm.LogDebug3,
119                         false,
120                         "[verbose] [fake-host] verbose log\n",
121                         false,
122                 },
123                 {
124                         "Log: debug",
125                         Event{
126                                 Log: Log{
127                                         Level: safcm.LogDebug,
128                                         Text:  "debug log",
129                                 },
130                         },
131                         safcm.LogDebug3,
132                         false,
133                         "[debug]   [fake-host] debug log\n",
134                         false,
135                 },
136                 {
137                         "Log: debug2",
138                         Event{
139                                 Log: Log{
140                                         Level: safcm.LogDebug2,
141                                         Text:  "debug2 log",
142                                 },
143                         },
144                         safcm.LogDebug3,
145                         false,
146                         "[debug2]  [fake-host] debug2 log\n",
147                         false,
148                 },
149                 {
150                         "Log: debug3",
151                         Event{
152                                 Log: Log{
153                                         Level: safcm.LogDebug3,
154                                         Text:  "debug3 log",
155                                 },
156                         },
157                         safcm.LogDebug3,
158                         false,
159                         fmt.Sprintf("[INVALID=%d] [fake-host] debug3 log\n",
160                                 safcm.LogDebug3),
161                         false,
162                 },
163                 {
164                         "Log: debug3 (tty)",
165                         Event{
166                                 Log: Log{
167                                         Level: safcm.LogDebug3,
168                                         Text:  "debug3 log",
169                                 },
170                         },
171                         safcm.LogDebug3,
172                         true,
173                         fmt.Sprintf("[INVALID=%d] [\x1b[31mfake-host\x1b[0m] debug3 log\n",
174                                 safcm.LogDebug3),
175                         false,
176                 },
177                 {
178                         "Log: escape",
179                         Event{
180                                 Log: Log{
181                                         Level: safcm.LogInfo,
182                                         Text:  "\x00",
183                                 },
184                         },
185                         safcm.LogDebug3,
186                         false,
187                         "[info]    [fake-host] \\x00\n",
188                         false,
189                 },
190                 {
191                         "Log: escape (tty)",
192                         Event{
193                                 Log: Log{
194                                         Level: safcm.LogInfo,
195                                         Text:  "\x00",
196                                 },
197                         },
198                         safcm.LogDebug3,
199                         true,
200                         "[info]    [fake-host] \x1b[35m\\x00\x1b[0m\n",
201                         false,
202                 },
203
204                 {
205                         "ConnEvent: stderr",
206                         Event{
207                                 ConnEvent: rpc.ConnEvent{
208                                         Type: rpc.ConnEventStderr,
209                                         Data: "fake stderr",
210                                 },
211                         },
212                         safcm.LogDebug3,
213                         false,
214                         "[stderr]  [fake-host] fake stderr\n",
215                         false,
216                 },
217                 {
218                         "ConnEvent: stderr (tty)",
219                         Event{
220                                 ConnEvent: rpc.ConnEvent{
221                                         Type: rpc.ConnEventStderr,
222                                         Data: "fake stderr",
223                                 },
224                         },
225                         safcm.LogDebug3,
226                         true,
227                         "[stderr]  [fake-host] fake stderr\n",
228                         false,
229                 },
230                 {
231                         "ConnEvent: debug",
232                         Event{
233                                 ConnEvent: rpc.ConnEvent{
234                                         Type: rpc.ConnEventDebug,
235                                         Data: "conn debug",
236                                 },
237                         },
238                         safcm.LogDebug3,
239                         false,
240                         "[debug3]  [fake-host] conn debug\n",
241                         false,
242                 },
243                 {
244                         "ConnEvent: upload",
245                         Event{
246                                 ConnEvent: rpc.ConnEvent{
247                                         Type: rpc.ConnEventUpload,
248                                 },
249                         },
250                         safcm.LogDebug3,
251                         false,
252                         "[info]    [fake-host] remote helper upload in progress\n",
253                         false,
254                 },
255                 {
256                         "ConnEvent: upload (ignored)",
257                         Event{
258                                 ConnEvent: rpc.ConnEvent{
259                                         Type: rpc.ConnEventUpload,
260                                 },
261                         },
262                         safcm.LogError,
263                         false,
264                         "",
265                         false,
266                 },
267                 {
268                         "ConnEvent: invalid",
269                         Event{
270                                 ConnEvent: rpc.ConnEvent{
271                                         Type: 42,
272                                         Data: "invalid",
273                                 },
274                         },
275                         safcm.LogError,
276                         false,
277                         "[INVALID=42] [fake-host] invalid\n",
278                         false,
279                 },
280                 {
281                         "ConnEvent: invalid (tty)",
282                         Event{
283                                 ConnEvent: rpc.ConnEvent{
284                                         Type: 42,
285                                         Data: "invalid",
286                                 },
287                         },
288                         safcm.LogError,
289                         true,
290                         "[INVALID=42] [\x1b[31mfake-host\x1b[0m] invalid\n",
291                         false,
292                 },
293                 {
294                         "ConnEvent: escape",
295                         Event{
296                                 ConnEvent: rpc.ConnEvent{
297                                         Type: rpc.ConnEventStderr,
298                                         Data: "\x00",
299                                 },
300                         },
301                         safcm.LogDebug3,
302                         false,
303                         "[stderr]  [fake-host] \\x00\n",
304                         false,
305                 },
306                 {
307                         "ConnEvent: escape (tty)",
308                         Event{
309                                 ConnEvent: rpc.ConnEvent{
310                                         Type: rpc.ConnEventDebug,
311                                         Data: "\x01",
312                                 },
313                         },
314                         safcm.LogDebug3,
315                         true,
316                         "[debug3]  [fake-host] \x1b[35m\\x01\x1b[0m\n",
317                         false,
318                 },
319
320                 {
321                         "Escaped",
322                         Event{
323                                 Log: Log{
324                                         Level: safcm.LogInfo,
325                                         Text:  "\x00",
326                                 },
327                                 Escaped: true,
328                         },
329                         safcm.LogDebug3,
330                         false,
331                         "[info]    [fake-host] \x00\n",
332                         false,
333                 },
334                 {
335                         "Escaped (tty)",
336                         Event{
337                                 Log: Log{
338                                         Level: safcm.LogInfo,
339                                         Text:  "\x00",
340                                 },
341                                 Escaped: true,
342                         },
343                         safcm.LogDebug3,
344                         true,
345                         "[info]    [fake-host] \x00\n",
346                         false,
347                 },
348
349                 {
350                         "empty (invalid)",
351                         Event{},
352                         safcm.LogDebug3,
353                         false,
354                         "[INVALID=0] [fake-host] \n",
355                         false,
356                 },
357         }
358
359         for _, tc := range tests {
360                 t.Run(tc.name, func(t *testing.T) {
361                         tc.event.Host = &TestHost{
362                                 name: "fake-host",
363                         }
364
365                         var buf bytes.Buffer
366                         log.SetFlags(0)
367                         log.SetOutput(&buf)
368
369                         var failed bool
370                         LogEvent(tc.event, tc.level, tc.isTTY, &failed)
371
372                         testutil.AssertEqual(t, "log",
373                                 buf.String(), tc.exp)
374                         testutil.AssertEqual(t, "failed",
375                                 failed, tc.expFailed)
376                 })
377         }
378 }