-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathadapt_test.go
199 lines (166 loc) · 5.7 KB
/
adapt_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package log
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/aws/smithy-go/logging"
"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
)
func TestAsGoaMiddlwareLogger(t *testing.T) {
restore := timeNow
timeNow = func() time.Time { return time.Date(2022, time.January, 9, 20, 29, 45, 0, time.UTC) }
defer func() { timeNow = restore }()
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf))
logger := AsGoaMiddlewareLogger(ctx)
assert.NoError(t, logger.Log("msg", "hello world"))
want := "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
}
func TestAsStdLogger(t *testing.T) {
restore := timeNow
timeNow = func() time.Time { return time.Date(2022, time.January, 9, 20, 29, 45, 0, time.UTC) }
defer func() { timeNow = restore }()
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf))
logger := AsStdLogger(ctx)
logger.Print("hello world")
want := "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
buf.Reset()
logger.Println("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\n\"\n"
assert.Equal(t, want, buf.String())
buf.Reset()
logger.Printf("hello %s", "world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
func() {
buf.Reset()
var msg string
defer func() { msg = recover().(string) }()
logger.Panic("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
assert.Equal(t, msg, "hello world")
}()
func() {
buf.Reset()
var msg string
defer func() { msg = recover().(string) }()
logger.Panicf("hello %s", "world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
assert.Equal(t, msg, "hello world")
}()
func() {
buf.Reset()
var msg string
defer func() { msg = recover().(string) }()
logger.Panicln("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\\n\"\n"
assert.Equal(t, want, buf.String())
assert.Equal(t, msg, "hello world")
}()
osExitFunc := osExit
var exited int
osExit = func(code int) {
exited = code
}
defer func() { osExit = osExitFunc }()
buf.Reset()
logger.Fatal("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
assert.Equal(t, exited, 1)
exited = 0
buf.Reset()
logger.Fatalf("hello %s", "world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
assert.Equal(t, exited, 1)
exited = 0
buf.Reset()
logger.Fatalln("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\n\"\n"
assert.Equal(t, want, buf.String())
assert.Equal(t, exited, 1)
}
type ctxkey string
func TestAsAWSLogger(t *testing.T) {
restore := timeNow
timeNow = func() time.Time { return time.Date(2022, time.January, 9, 20, 29, 45, 0, time.UTC) }
defer func() { timeNow = restore }()
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithDebug())
var logger logging.Logger = AsAWSLogger(ctx)
logger.Logf(logging.Classification("INFO"), "hello %s", "world")
want := "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
buf.Reset()
logger.Logf(logging.Classification("DEBUG"), "hello world")
want = "time=2022-01-09T20:29:45Z level=debug msg=\"hello world\"\n"
assert.Equal(t, want, buf.String())
buf.Reset()
key := ctxkey("key")
pctx := context.WithValue(context.Background(), key, "small")
logger = logger.(logging.ContextLogger).WithContext(pctx)
logger.Logf(logging.Classification("INFO"), "hello %v world", pctx.Value(key))
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello small world\"\n"
assert.Equal(t, want, buf.String())
}
func TestToLogrSink(t *testing.T) {
restore := timeNow
timeNow = func() time.Time { return time.Date(2022, time.January, 9, 20, 29, 45, 0, time.UTC) }
defer func() { timeNow = restore }()
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithDebug())
var sink logr.LogSink = ToLogrSink(ctx)
sink.Init(logr.RuntimeInfo{})
assert.True(t, sink.Enabled(0))
assert.True(t, sink.Enabled(1))
assert.True(t, sink.Enabled(2))
assert.True(t, sink.Enabled(3))
msg := "hello world"
expected := "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
expecteddebug := "time=2022-01-09T20:29:45Z level=debug msg=\"hello world\"\n"
expectederr := "time=2022-01-09T20:29:45Z level=error err=error msg=\"hello world\"\n"
empty := ""
logger := logr.New(sink)
logger.Info(msg)
assert.Equal(t, expected, buf.String())
buf.Reset()
logger.V(1).Info(msg)
assert.Equal(t, expecteddebug, buf.String())
buf.Reset()
logger.Error(errors.New("error"), msg)
assert.Equal(t, expectederr, buf.String())
ctx = Context(context.Background(), WithOutput(&buf))
sink = ToLogrSink(ctx)
logger = logr.New(sink)
buf.Reset()
logger.Info(msg)
assert.Equal(t, empty, buf.String())
FlushAndDisableBuffering(ctx)
buf.Reset()
logger.Info(msg)
assert.Equal(t, expected, buf.String())
buf.Reset()
logger.V(1).Info(msg)
assert.Equal(t, empty, buf.String())
sink = sink.WithValues("key", "value")
expectedWithValues := "time=2022-01-09T20:29:45Z level=info key=value msg=\"hello world\"\n"
logger = logr.New(sink)
buf.Reset()
logger.Info(msg)
assert.Equal(t, expectedWithValues, buf.String())
sink = sink.WithName("name")
expectedWithName := "time=2022-01-09T20:29:45Z level=info key=value log=name msg=\"hello world\"\n"
logger = logr.New(sink)
buf.Reset()
logger.Info(msg)
assert.Equal(t, expectedWithName, buf.String())
}