-
Notifications
You must be signed in to change notification settings - Fork 1
/
zaplog_test.go
183 lines (177 loc) · 4.89 KB
/
zaplog_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package log_test
import (
"bytes"
"fmt"
"strings"
"testing"
itn "github.com/1set/starlet/internal"
lg "github.com/1set/starlet/lib/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestLoadModule_Log_NoSet(t *testing.T) {
mm := lg.NewModule(nil)
script := "load('log', 'info')\ninfo('this is 1st info message only')"
res, err := itn.ExecModuleWithErrorTest(t, lg.ModuleName, mm.LoadModule, script, "", nil)
if err != nil {
t.Errorf("log.SetLog(nil) expects no error, actual error = '%v', result = %v", err, res)
return
}
}
func TestLoadModule_Log_SetLog_Nil(t *testing.T) {
mm := lg.NewModule(nil)
mm.SetLog(nil)
script := "load('log', 'info')\ninfo('this is 2nd info message only')"
res, err := itn.ExecModuleWithErrorTest(t, lg.ModuleName, mm.LoadModule, script, "", nil)
if err != nil {
t.Errorf("log.SetLog(nil) expects no error, actual error = '%v', result = %v", err, res)
return
}
}
func TestLoadModule_Log(t *testing.T) {
tests := []struct {
name string
script string
wantErr string
keywords []string
}{
{
name: `debug message`,
script: itn.HereDoc(`
load('log', 'debug')
debug('this is a debug message only')
`),
keywords: []string{"DEBUG", "this is a debug message only"},
},
{
name: `debug with no args`,
script: itn.HereDoc(`
load('log', 'debug')
debug()
`),
wantErr: "log.debug: expected at least 1 argument, got 0",
},
{
name: `debug with invalid arg type`,
script: itn.HereDoc(`
load('log', 'debug')
debug(520)
`),
wantErr: "log.debug: expected string as first argument, got int",
},
{
name: `debug with pending args`,
script: itn.HereDoc(`
load('log', 'debug')
debug('this is a broken message', "what", 123, True)
`),
keywords: []string{"DEBUG", `this is a broken message what 123 True`},
},
{
name: `debug with key values`,
script: itn.HereDoc(`
load('log', 'debug')
m = {"mm": "this is more"}
l = [2, "LIST", 3.14, True]
debug('this is a data message', map=m, list=l)
`),
keywords: []string{"DEBUG", "this is a data message", `{"map": {"mm":"this is more"}, "list": [2,"LIST",3.14,true]}`},
},
{
name: `info message`,
script: itn.HereDoc(`
load('log', 'info')
info('this is an info message', a1=2, hello="world")
`),
keywords: []string{"INFO", "this is an info message", `{"a1": 2, "hello": "world"}`},
},
{
name: `info self args`,
script: itn.HereDoc(`
load('log', 'info')
d = {"hello": "world"}
d["a"] = d
l = [1,2,3]
l.append(l)
s = set([4,5,6])
info('this is complex info message', self1=d, self2=l, self3=s)
`),
keywords: []string{"INFO", "this is complex info message", `{"self1": "{\"hello\": \"world\", \"a\": {...}}", "self2": "[1, 2, 3, [...]]", "self3": [4,5,6]}`},
},
{
name: `warn message`,
script: itn.HereDoc(`
load('log', 'warn')
warn('this is a warning message only')
`),
keywords: []string{"WARN", "this is a warning message only"},
},
{
name: `error message`,
script: itn.HereDoc(`
load('log', 'error')
error('this is an error message only', dsat=None)
`),
keywords: []string{"ERROR", "this is an error message only", `{"dsat": null}`},
},
{
name: `fatal message`,
script: itn.HereDoc(`
load('log', 'fatal')
fatal('this is a fatal message only')
`),
wantErr: `this is a fatal message only`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l, b := buildCustomLogger()
lg.SetLog(l)
res, err := itn.ExecModuleWithErrorTest(t, lg.ModuleName, lg.LoadModule, tt.script, tt.wantErr, nil)
if (err != nil) != (tt.wantErr != "") {
t.Errorf("log(%q) expects error = '%v', actual error = '%v', result = %v", tt.name, tt.wantErr, err, res)
return
}
if len(tt.wantErr) > 0 {
return
}
if len(tt.keywords) > 0 {
bs := b.String()
for _, k := range tt.keywords {
if !strings.Contains(bs, k) {
t.Errorf("log(%q) expects keyword = '%v', actual log = '%v'", tt.name, k, bs)
return
}
}
} else {
fmt.Println(b.String())
}
})
}
}
func buildCustomLogger() (*zap.SugaredLogger, *bytes.Buffer) {
buf := bytes.NewBufferString("")
var al zap.LevelEnablerFunc = func(lvl zapcore.Level) bool {
return true
}
ce := zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
})
cr := zapcore.NewCore(ce, zapcore.AddSync(buf), al)
op := []zap.Option{
zap.AddCaller(),
//zap.AddStacktrace(zap.ErrorLevel),
}
logger := zap.New(cr, op...)
return logger.Sugar(), buf
}