|
1
|
|
|
package logger |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"bytes" |
|
5
|
|
|
"errors" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"os" |
|
8
|
|
|
"os/exec" |
|
9
|
|
|
"testing" |
|
10
|
|
|
|
|
11
|
|
|
"github.com/stretchr/testify/require" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
func TestLogger(t *testing.T) { |
|
15
|
|
|
tests := []struct { |
|
16
|
|
|
name string |
|
17
|
|
|
level string |
|
18
|
|
|
funcName string |
|
19
|
|
|
message string |
|
20
|
|
|
expectedMsg string |
|
21
|
|
|
}{ |
|
22
|
|
|
{ |
|
23
|
|
|
name: "error msg", |
|
24
|
|
|
level: "ERROR", |
|
25
|
|
|
funcName: "Error", |
|
26
|
|
|
message: "This is error message", |
|
27
|
|
|
expectedMsg: "ERROR:This is error message", |
|
28
|
|
|
}, |
|
29
|
|
|
{ |
|
30
|
|
|
name: "skipp_warn", |
|
31
|
|
|
level: "ERROR", |
|
32
|
|
|
funcName: "Warn", |
|
33
|
|
|
message: "This is error message", |
|
34
|
|
|
expectedMsg: "", |
|
35
|
|
|
}, |
|
36
|
|
|
{ |
|
37
|
|
|
name: "skipp_info", |
|
38
|
|
|
level: "ERROR", |
|
39
|
|
|
funcName: "Info", |
|
40
|
|
|
message: "This is error message", |
|
41
|
|
|
expectedMsg: "", |
|
42
|
|
|
}, |
|
43
|
|
|
{ |
|
44
|
|
|
name: "skipp_debug", |
|
45
|
|
|
level: "ERROR", |
|
46
|
|
|
funcName: "Debug", |
|
47
|
|
|
message: "This is error message", |
|
48
|
|
|
expectedMsg: "", |
|
49
|
|
|
}, |
|
50
|
|
|
{ |
|
51
|
|
|
name: "debug", |
|
52
|
|
|
level: "DEBUG", |
|
53
|
|
|
funcName: "Debug", |
|
54
|
|
|
message: "This is error message", |
|
55
|
|
|
expectedMsg: "DEBUG:This is error message", |
|
56
|
|
|
}, |
|
57
|
|
|
{ |
|
58
|
|
|
name: "skipp_debug2", |
|
59
|
|
|
level: "INFO", |
|
60
|
|
|
funcName: "Debug", |
|
61
|
|
|
message: "This is error message", |
|
62
|
|
|
expectedMsg: "", |
|
63
|
|
|
}, |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
t.Parallel() |
|
67
|
|
|
for _, tc := range tests { |
|
68
|
|
|
t.Run(fmt.Sprintf("case %s", tc.name), func(t *testing.T) { |
|
69
|
|
|
var b bytes.Buffer |
|
70
|
|
|
tc := tc |
|
71
|
|
|
l := NewLogger(tc.level, &b) |
|
72
|
|
|
|
|
73
|
|
|
switch tc.funcName { |
|
74
|
|
|
case "Error": |
|
75
|
|
|
l.Errorf(tc.message) |
|
|
|
|
|
|
76
|
|
|
case "Warn": |
|
77
|
|
|
l.Warningf(tc.message) |
|
78
|
|
|
case "Info": |
|
79
|
|
|
l.Infof(tc.message) |
|
80
|
|
|
case "Debug": |
|
81
|
|
|
l.Debugf(tc.message) |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
require.Equal(t, tc.expectedMsg, b.String(), "error output message") |
|
85
|
|
|
}) |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
func TestFatalf(t *testing.T) { |
|
90
|
|
|
if os.Getenv("BE_CRASHER") == "1" { |
|
91
|
|
|
var b bytes.Buffer |
|
92
|
|
|
_ = NewLogger("WRONG DB TYPE", &b) |
|
93
|
|
|
return |
|
94
|
|
|
} |
|
95
|
|
|
cmd := exec.Command(os.Args[0], "-test.run=TestFatalf") |
|
96
|
|
|
cmd.Env = append(os.Environ(), "BE_CRASHER=1") |
|
97
|
|
|
err := cmd.Run() |
|
98
|
|
|
|
|
99
|
|
|
var e *exec.ExitError |
|
100
|
|
|
require.True(t, err != nil && errors.As(err, &e), "process ran with err %v, want exit status 1", err) |
|
101
|
|
|
} |
|
102
|
|
|
|