-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper_test.go
More file actions
87 lines (72 loc) · 1.87 KB
/
helper_test.go
File metadata and controls
87 lines (72 loc) · 1.87 KB
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
package logging
import (
"bytes"
"context"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/snabble/go-logging/v2/tracex"
)
func Test_LifecycleStart_AcceptsNil(t *testing.T) {
assert.NotPanics(t, func() {
LifecycleStart("app", nil)
})
}
func Test_Call_UsesTrace(t *testing.T) {
capture := capturingLogger(t)
defer func() { _ = Set("info", true) }()
provider := tracex.NewGlobalNoopTraceProvider("sampleApp", "v1.0.0")
ctx, span := startSpan()
defer span.End()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/", nil)
Call(req, &http.Response{}, time.Time{}, nil)
require.NoError(t, provider.Shutdown(context.Background()))
assert.NotContains(t, capture.String(), "00000000000000000000000000000000")
assert.Contains(t, capture.String(), "trace")
assert.Contains(t, capture.String(), "span")
}
func Test_SetGoogle(t *testing.T) {
require.NoError(t, SetGoogle("debug"))
defer SetWithConfig("info", &DefaultLogConfig) // Reset to default
tt := []struct {
level string
message string
logFn func()
}{
{
level: "debug",
message: "__debug__",
logFn: func() { Log.Debug("__debug__") },
},
{
level: "info",
message: "__info__",
logFn: func() { Log.Info("__info__") },
},
{
level: "warning",
message: "__warn__",
logFn: func() { Log.Warn("__warn__") },
},
{
level: "error",
message: "__error__",
logFn: func() { Log.Error("__error__") },
},
}
for _, tc := range tt {
t.Run(tc.level, func(t *testing.T) {
b := bytes.NewBuffer(nil)
Log.Out = b
tc.logFn()
result := map[string]string{}
require.NoError(t, json.Unmarshal(b.Bytes(), &result))
assert.Equal(t, tc.message, result["message"])
assert.Equal(t, tc.level, result["severity"])
assert.NotEmpty(t, result["timestamp"])
})
}
}