forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
176 lines (136 loc) · 3.51 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
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package logging
import (
"bytes"
"encoding/json"
"errors"
"os"
"testing"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/stretchr/testify/require"
)
func TestLogger_SetupBasic(t *testing.T) {
cfg := Config{LogLevel: "INFO"}
logger, err := Setup(cfg, nil)
require.NoError(t, err)
require.NotNil(t, logger)
}
func TestLogger_SetupInvalidLogLevel(t *testing.T) {
cfg := Config{}
_, err := Setup(cfg, nil)
testutil.RequireErrorContains(t, err, "Invalid log level")
}
func TestLogger_SetupLoggerErrorLevel(t *testing.T) {
cases := []struct {
desc string
before func(*Config)
}{
{
desc: "ERR log level",
before: func(cfg *Config) {
cfg.LogLevel = "ERR"
},
},
{
desc: "ERROR log level",
before: func(cfg *Config) {
cfg.LogLevel = "ERROR"
},
},
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
var cfg Config
c.before(&cfg)
var buf bytes.Buffer
logger, err := Setup(cfg, &buf)
require.NoError(t, err)
require.NotNil(t, logger)
logger.Error("test error msg")
logger.Info("test info msg")
output := buf.String()
require.Contains(t, output, "[ERROR] test error msg")
require.NotContains(t, output, "[INFO] test info msg")
})
}
}
func TestLogger_SetupLoggerDebugLevel(t *testing.T) {
cfg := Config{LogLevel: "DEBUG"}
var buf bytes.Buffer
logger, err := Setup(cfg, &buf)
require.NoError(t, err)
require.NotNil(t, logger)
logger.Info("test info msg")
logger.Debug("test debug msg")
output := buf.String()
require.Contains(t, output, "[INFO] test info msg")
require.Contains(t, output, "[DEBUG] test debug msg")
}
func TestLogger_SetupLoggerWithName(t *testing.T) {
cfg := Config{
LogLevel: "DEBUG",
Name: "test-system",
}
var buf bytes.Buffer
logger, err := Setup(cfg, &buf)
require.NoError(t, err)
require.NotNil(t, logger)
logger.Warn("test warn msg")
require.Contains(t, buf.String(), "[WARN] test-system: test warn msg")
}
func TestLogger_SetupLoggerWithJSON(t *testing.T) {
cfg := Config{
LogLevel: "DEBUG",
LogJSON: true,
Name: "test-system",
}
var buf bytes.Buffer
logger, err := Setup(cfg, &buf)
require.NoError(t, err)
require.NotNil(t, logger)
logger.Warn("test warn msg")
var jsonOutput map[string]string
err = json.Unmarshal(buf.Bytes(), &jsonOutput)
require.NoError(t, err)
require.Contains(t, jsonOutput, "@level")
require.Equal(t, jsonOutput["@level"], "warn")
require.Contains(t, jsonOutput, "@message")
require.Equal(t, jsonOutput["@message"], "test warn msg")
}
func TestLogger_SetupLoggerWithValidLogPath(t *testing.T) {
tmpDir := testutil.TempDir(t, t.Name())
cfg := Config{
LogLevel: "INFO",
LogFilePath: tmpDir + "/",
}
var buf bytes.Buffer
logger, err := Setup(cfg, &buf)
require.NoError(t, err)
require.NotNil(t, logger)
}
func TestLogger_SetupLoggerWithInValidLogPath(t *testing.T) {
cfg := Config{
LogLevel: "INFO",
LogFilePath: "nonexistentdir/",
}
var buf bytes.Buffer
logger, err := Setup(cfg, &buf)
require.Error(t, err)
require.True(t, errors.Is(err, os.ErrNotExist))
require.Nil(t, logger)
}
func TestLogger_SetupLoggerWithInValidLogPathPermission(t *testing.T) {
tmpDir := "/tmp/" + t.Name()
os.Mkdir(tmpDir, 0000)
defer os.RemoveAll(tmpDir)
cfg := Config{
LogLevel: "INFO",
LogFilePath: tmpDir + "/",
}
var buf bytes.Buffer
logger, err := Setup(cfg, &buf)
require.Error(t, err)
require.True(t, errors.Is(err, os.ErrPermission))
require.Nil(t, logger)
}