forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger_cb_test.go
51 lines (40 loc) · 914 Bytes
/
logger_cb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package libbpfgo
import (
"bytes"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLogFallback(t *testing.T) {
tt := []struct {
message string
}{
{
message: "This is a warning message",
},
{
message: "This is a information message",
},
{
message: "This is a debug message",
},
}
for _, tc := range tt {
var buf bytes.Buffer
r, w, err := os.Pipe()
require.NoError(t, err, "failed to create pipe")
writeEnd := os.NewFile(uintptr(w.Fd()), "pipe")
oldStderr := os.Stderr
os.Stderr = writeEnd
// level is ignored
logFallback(LibbpfInfoLevel, tc.message)
os.Stderr = oldStderr
err = writeEnd.Close()
require.NoError(t, err, "failed to close writeEnd")
_, err = io.Copy(&buf, r)
require.NoError(t, err, "failed to copy from read end to buffer")
assert.Equal(t, tc.message, buf.String())
}
}