-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_test.go
100 lines (83 loc) · 2.8 KB
/
config_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
package logger_test
import (
"os"
"strconv"
"testing"
"github.com/secureworks/logger/internal/testutils"
"github.com/secureworks/logger/log"
"github.com/secureworks/logger/testlogger"
)
var defaultConfig = &log.Config{
Level: log.INFO,
LocalDevel: false,
Format: log.JSONFormat,
EnableErrStack: false,
Output: os.Stderr,
}
var loadedConfig = &log.Config{
Level: log.DEBUG,
LocalDevel: true,
Format: log.ImplementationDefaultFormat,
EnableErrStack: true,
Output: os.Stderr,
}
func TestDefaultConfig(t *testing.T) {
t.Run("with an empty environment", func(t *testing.T) {
config := log.DefaultConfig(func(string) string { return "" }) // Ignore env.
testutils.AssertEqual(t, defaultConfig, config)
})
t.Run("with environment variables", func(t *testing.T) {
fakeenv := map[string]string{
log.Environment.String(): "prod",
log.LogLevel.String(): "DEBUG",
log.LocalDevel.String(): "true",
log.Format.String(): strconv.Itoa(int(log.ImplementationDefaultFormat)),
log.EnableErrStack.String(): "true",
}
config := log.DefaultConfig(func(varname string) string { return fakeenv[varname] })
testutils.AssertEqual(t, loadedConfig, config)
})
t.Run("with Sentry config but missing Sentry DSN", func(t *testing.T) {
fakeenv := map[string]string{
"SENTRY_LEVELS": "FATAL,PANIC,ERROR,WARN",
"SENTRY_RELEASE": "app-1-a",
"ENVIRONMENT": "prod",
"SENTRY_SERVER": "app-main",
"SENTRY_DEBUG": "true",
}
config := log.DefaultConfig(func(varname string) string {
return fakeenv[varname]
})
testutils.AssertEqual(t, defaultConfig, config)
})
}
func TestOpenRegister(t *testing.T) {
t.Run("Open before Register fails", func(t *testing.T) {
logger, err := log.Open("newlogger", nil)
testutils.AssertNil(t, logger)
testutils.AssertEqual(t, "log: No logger by name (newlogger)", err.Error())
})
t.Run("Open after Register succeeds", func(t *testing.T) {
defer log.Unregister("newlogger")
log.Register("newlogger", func(c *log.Config, opts ...log.Option) (log.Logger, error) {
return testlogger.New(c, opts...)
})
logger, err := log.Open("newlogger", nil)
testutils.AssertNil(t, err)
testutils.AssertNotNil(t, logger)
})
t.Run("Open with config sets config", func(t *testing.T) {
logger, err := log.Open("test", nil)
testutils.AssertNil(t, err)
// Test logger uses a bytes.Buffer for Output by default instead of
// os.Stderr, so let's just reset that.
config := logger.(*testlogger.Logger).Config
config.Output = os.Stderr
testutils.AssertEqual(t, defaultConfig, config)
logger, err = log.Open("test", loadedConfig)
testutils.AssertNil(t, err)
config = logger.(*testlogger.Logger).Config
config.Output = os.Stderr
testutils.AssertEqual(t, loadedConfig, config)
})
}