Skip to content

Commit 05933f7

Browse files
authored
add logging tests (#42)
1 parent 8c47f52 commit 05933f7

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

pkg/utils/logging_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2015-2025 CEA/DAM/DIF
2+
// Author: Arnaud Guignard <[email protected]>
3+
// Contributor: Cyril Servant <[email protected]>
4+
//
5+
// This software is governed by the CeCILL-B license under French law and
6+
// abiding by the rules of distribution of free software. You can use,
7+
// modify and/ or redistribute the software under the terms of the CeCILL-B
8+
// license as circulated by CEA, CNRS and INRIA at the following URL
9+
// "http://www.cecill.info".
10+
11+
package utils
12+
13+
import (
14+
"bytes"
15+
"fmt"
16+
"io"
17+
"os"
18+
"testing"
19+
20+
"github.com/op/go-logging"
21+
)
22+
23+
var MustSetupLoggingTests = []struct {
24+
debug, inputDebug bool
25+
input, want string
26+
}{
27+
{true, true, "test", "DEBUG test\n"},
28+
{false, false, "test", "INFO test\n"},
29+
{false, true, "test", ""},
30+
}
31+
32+
func TestMustSetupLoggingStdout(t *testing.T) {
33+
logFormat := "%{level} %{message}"
34+
for _, tt := range MustSetupLoggingTests {
35+
// Save the original stdout
36+
originalStdout := os.Stdout
37+
38+
// Create a new buffer and redirect stdout
39+
var buf bytes.Buffer
40+
r, w, err := os.Pipe()
41+
if err != nil {
42+
t.Fatalf("Failed to create pipe: %v", err)
43+
}
44+
os.Stdout = w
45+
46+
var log = logging.MustGetLogger("sshproxy")
47+
MustSetupLogging("sshproxy", "", logFormat, logFormat, tt.debug)
48+
if tt.inputDebug {
49+
log.Debug(tt.input)
50+
} else {
51+
log.Info(tt.input)
52+
}
53+
54+
// Stop writing and restore stdout
55+
w.Close()
56+
os.Stdout = originalStdout
57+
io.Copy(&buf, r)
58+
59+
// Verify the output
60+
got := buf.String()
61+
if got != tt.want {
62+
t.Errorf("got: %s, want: %s", got, tt.want)
63+
}
64+
}
65+
}
66+
67+
func TestMustSetupLoggingFile(t *testing.T) {
68+
logFormat := "%{level} %{message}"
69+
logFile := "/tmp/sshproxytest.log"
70+
os.Remove(logFile)
71+
for _, tt := range MustSetupLoggingTests {
72+
var log = logging.MustGetLogger("sshproxy")
73+
MustSetupLogging("sshproxy", logFile, logFormat, logFormat, tt.debug)
74+
75+
// test first log
76+
if tt.inputDebug {
77+
log.Debug(tt.input)
78+
} else {
79+
log.Info(tt.input)
80+
}
81+
got, err := os.ReadFile(logFile)
82+
if err != nil {
83+
t.Fatalf("Failed to read file: %v", err)
84+
}
85+
if string(got) != tt.want {
86+
t.Errorf("got: %s, want: %s", string(got), tt.want)
87+
}
88+
89+
// test second log in same file
90+
if tt.inputDebug {
91+
log.Debug(tt.input)
92+
} else {
93+
log.Info(tt.input)
94+
}
95+
got, err = os.ReadFile(logFile)
96+
if err != nil {
97+
t.Fatalf("Failed to read file: %v", err)
98+
}
99+
if string(got) != tt.want+tt.want {
100+
t.Errorf("got: %s, want: %s", string(got), tt.want+tt.want)
101+
}
102+
103+
os.Remove(logFile)
104+
}
105+
}
106+
107+
func BenchmarkMustSetupLogging(b *testing.B) {
108+
logFormat := "%{level} %{message}"
109+
for _, logFile := range []string{"", "/tmp/sshproxytest.log"} {
110+
for _, debug := range []bool{true, false} {
111+
b.Run(fmt.Sprintf("%s_%v", logFile, debug), func(b *testing.B) {
112+
MustSetupLogging("sshproxy", logFile, logFormat, logFormat, debug)
113+
})
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)