1 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
25 "ruderich.org/simon/safcm"
26 "ruderich.org/simon/safcm/rpc"
27 "ruderich.org/simon/safcm/testutil"
30 type TestHost struct {
34 func (th *TestHost) Name() string {
37 func (th *TestHost) Dial(*rpc.Conn) error {
38 return fmt.Errorf("not implemented")
41 func TestLogEvent(t *testing.T) {
42 // Restore default logger
43 defer log.SetFlags(log.Flags())
44 defer log.SetOutput(os.Stderr)
58 Error: fmt.Errorf("fake error"),
62 "[error] [fake-host] fake error\n",
68 Error: fmt.Errorf("fake error"),
72 "[error] [\x1b[31mfake-host\x1b[0m] fake error\n",
78 Error: fmt.Errorf("\x00"),
82 "[error] [fake-host] \\x00\n",
86 "Error: escape (tty)",
88 Error: fmt.Errorf("\x00"),
92 "[error] [\x1b[31mfake-host\x1b[0m] \x1b[35m\\x00\x1b[0m\n",
100 Level: safcm.LogInfo,
106 "[info] [fake-host] info log\n",
113 Level: safcm.LogInfo,
119 "[info] [fake-host] info log\n",
126 Level: safcm.LogVerbose,
132 "[verbose] [fake-host] verbose log\n",
139 Level: safcm.LogDebug,
145 "[debug] [fake-host] debug log\n",
152 Level: safcm.LogDebug2,
158 "[debug2] [fake-host] debug2 log\n",
165 Level: safcm.LogDebug3,
171 fmt.Sprintf("[INVALID=%d] [fake-host] debug3 log\n",
179 Level: safcm.LogDebug3,
185 fmt.Sprintf("[INVALID=%d] [\x1b[31mfake-host\x1b[0m] debug3 log\n",
193 Level: safcm.LogInfo,
199 "[info] [fake-host] \\x00\n",
206 Level: safcm.LogInfo,
212 "[info] [fake-host] \x1b[35m\\x00\x1b[0m\n",
219 ConnEvent: rpc.ConnEvent{
220 Type: rpc.ConnEventStderr,
226 "[stderr] [fake-host] fake stderr\n",
230 "ConnEvent: stderr (tty)",
232 ConnEvent: rpc.ConnEvent{
233 Type: rpc.ConnEventStderr,
239 "[stderr] [fake-host] fake stderr\n",
245 ConnEvent: rpc.ConnEvent{
246 Type: rpc.ConnEventDebug,
252 "[debug3] [fake-host] conn debug\n",
258 ConnEvent: rpc.ConnEvent{
259 Type: rpc.ConnEventUpload,
264 "[info] [fake-host] remote helper upload in progress\n",
268 "ConnEvent: upload (ignored)",
270 ConnEvent: rpc.ConnEvent{
271 Type: rpc.ConnEventUpload,
280 "ConnEvent: invalid",
282 ConnEvent: rpc.ConnEvent{
289 "[INVALID=42] [fake-host] invalid\n",
293 "ConnEvent: invalid (tty)",
295 ConnEvent: rpc.ConnEvent{
302 "[INVALID=42] [\x1b[31mfake-host\x1b[0m] invalid\n",
308 ConnEvent: rpc.ConnEvent{
309 Type: rpc.ConnEventStderr,
315 "[stderr] [fake-host] \\x00\n",
319 "ConnEvent: escape (tty)",
321 ConnEvent: rpc.ConnEvent{
322 Type: rpc.ConnEventDebug,
328 "[debug3] [fake-host] \x1b[35m\\x01\x1b[0m\n",
336 Level: safcm.LogInfo,
343 "[info] [fake-host] \x00\n",
350 Level: safcm.LogInfo,
357 "[info] [fake-host] \x00\n",
366 "[INVALID=0] [fake-host] \n",
371 for _, tc := range tests {
372 t.Run(tc.name, func(t *testing.T) {
373 tc.event.Host = &TestHost{
382 LogEvent(tc.event, tc.level, tc.isTTY, &failed)
384 testutil.AssertEqual(t, "log",
385 buf.String(), tc.exp)
386 testutil.AssertEqual(t, "failed",
387 failed, tc.expFailed)