This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
91 lines (79 loc) · 2.03 KB
/
logger_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
package logs
import (
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func logConfig(withSenrty bool) *Config {
s := &SentryConfig{
Enable: true,
Stage: "test",
DSN: "https://xxx@sentry.io/yyy",
ResponseTimeout: 0,
MinlLogLevel: "error",
StackTrace: StackTraceConfig{
Enable: true,
},
}
cfg := &Config{
Level: "info",
Format: "text",
}
if withSenrty {
cfg.Sentry = s
}
return cfg
}
// I just want to test it before pushing. Don't know how to test it in right way, sorry )
func TestNewLogger(t *testing.T) {
t.Run("Construct new logger", func(t *testing.T) {
cfg := logConfig(false)
l, err := NewLogger(cfg)
assert.NoError(t, err)
assert.Equal(t, logrus.InfoLevel, l.GetLevel())
})
t.Run("Logger format", func(t *testing.T) {
cfg := logConfig(false)
cfg.Format = "du'soley"
l, err := NewLogger(cfg)
assert.NoError(t, err)
assert.Equal(t, &logrus.JSONFormatter{TimestampFormat: time.RFC3339}, l.Formatter)
})
t.Run("good sentry config", func(t *testing.T) {
cfg := logConfig(true)
l, err := NewLogger(cfg)
assert.NoError(t, err)
assert.Equal(t, len(l.Hooks), len(sentryLevels[:getLoggerLevel(cfg)]))
})
t.Run("bad sentry config", func(t *testing.T) {
cfg := logConfig(true)
cfg.Sentry.DSN = "bad dsn"
_, err := NewLogger(cfg)
assert.Error(t, err)
})
}
func TestGetLoggerLeve(t *testing.T) {
t.Run("existing Logger level", func(t *testing.T) {
cfg := logConfig(false)
ll := getLoggerLevel(cfg)
assert.Equal(t, logrus.InfoLevel, ll)
})
t.Run("not existing Logger level", func(t *testing.T) {
cfg := logConfig(false)
cfg.Level = "paranoia"
ll := getLoggerLevel(cfg)
assert.Equal(t, logrus.WarnLevel, ll)
})
}
func TestSentryStage(t *testing.T) {
t.Run("stage is set", func(t *testing.T) {
cfg := logConfig(true)
assert.Equal(t, "test", getSTAGE(cfg))
})
t.Run("stage is not set", func(t *testing.T) {
cfg := logConfig(true)
cfg.Sentry.Stage = ""
assert.Equal(t, defaultSTAGE, getSTAGE(cfg))
})
}