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