Debian Codegraph @

config_test.go

raw symbols SCIP
1 // Copyright (c) 2016 Uber Technologies, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 package zapscip-go gomod go.uber.org/zap . `go.uber.org/zap`/
22
23 import (
24 "osscip-go gomod github.com/golang/go/src go1.19 os/"
25 "path/filepathscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/"
26 "sync/atomicscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/"
27 "testingscip-go gomod github.com/golang/go/src go1.19 testing/"
28
29 "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/"
30 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/"
31 "go.uber.org/zap/zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/"
32 )
33
34 func TestConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/TestConfig().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
35 tests := []struct {
36 desc string
37 cfg Configscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#
38 expectN int64
39 expectRe string
40 }{
41 {
42 desc: "production",
43 cfg: NewProductionConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/NewProductionConfig().(),
44 expectN: 2 + 100 + 1, // 2 from initial logs, 100 initial sampled logs, 1 from off-by-one in sampler
45 expectRe: `{"level":"info","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" +
46 `{"level":"warn","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n",
47 },
48 {
49 desc: "development",
50 cfg: NewDevelopmentConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/NewDevelopmentConfig().(),
51 expectN: 3 + 200, // 3 initial logs, all 200 subsequent logs
52 expectRe: "DEBUG\t[a-z0-9_-]+/config_test.go:" + `\d+` + "\tdebug\t" + `{"k": "v", "z": "zz"}` + "\n" +
53 "INFO\t[a-z0-9_-]+/config_test.go:" + `\d+` + "\tinfo\t" + `{"k": "v", "z": "zz"}` + "\n" +
54 "WARN\t[a-z0-9_-]+/config_test.go:" + `\d+` + "\twarn\t" + `{"k": "v", "z": "zz"}` + "\n" +
55 `go.uber.org/zap.TestConfig.\w+`,
56 },
57 }
58
59 for _, tt := range tests {
60 t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().(tt.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
61 logOut := filepathscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/Join().(t.TempDirscip-go gomod github.com/golang/go/src go1.19 testing/common#TempDir().(), "test.log")
62
63 tt.cfg.OutputPathsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#OutputPaths. = []string{logOut}
64 tt.cfg.EncoderConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#EncoderConfig..TimeKeyscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EncoderConfig#TimeKey. = "" // no timestamps in tests
65 tt.cfg.InitialFieldsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#InitialFields. = map[string]interface{}{"z": "zz", "k": "v"}
66
67 hook, count := makeCountingHookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/makeCountingHook().()
68 logger, err := tt.cfg.Buildscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Build().(Hooksscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Hooks().(hook))
69 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/NoError().(t, err, "Unexpected error constructing logger.")
70
71 logger.Debugscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Debug().("debug")
72 logger.Infoscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Info().("info")
73 logger.Warnscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Warn().("warn")
74
75 byteContents, err := osscip-go gomod github.com/golang/go/src go1.19 os/.ReadFilescip-go gomod github.com/golang/go/src go1.19 os/ReadFile().(logOut)
76 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/NoError().(t, err, "Couldn't read log contents from temp file.")
77 logs := string(byteContents)
78 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Regexpscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Regexp().(t, tt.expectRe, logs, "Unexpected log output.")
79
80 for i := 0; i < 200; i++ {
81 logger.Infoscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Info().("sampling")
82 }
83 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.expectN, count.Loadscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#Load().(), "Hook called an unexpected number of times.")
84 })
85 }
86 }
87
88 func TestConfigWithInvalidPathsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/TestConfigWithInvalidPaths().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
89 tests := []struct {
90 desc string
91 output string
92 errOutput string
93 }{
94 {"output directory doesn't exist", "/tmp/not-there/foo.log", "stderr"},
95 {"error output directory doesn't exist", "stdout", "/tmp/not-there/foo-errors.log"},
96 {"neither output directory exists", "/tmp/not-there/foo.log", "/tmp/not-there/foo-errors.log"},
97 }
98
99 for _, tt := range tests {
100 t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().(tt.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
101 cfg := NewProductionConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/NewProductionConfig().()
102 cfg.OutputPathsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#OutputPaths. = []string{tt.output}
103 cfg.ErrorOutputPathsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#ErrorOutputPaths. = []string{tt.errOutput}
104 _, err := cfg.Buildscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Build().()
105 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Errorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Error().(t, err, "Expected an error opening a non-existent directory.")
106 })
107 }
108 }
109
110 func TestConfigWithMissingAttributesscip-go gomod go.uber.org/zap . `go.uber.org/zap`/TestConfigWithMissingAttributes().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
111 tests := []struct {
112 desc string
113 cfg Configscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#
114 expectErr string
115 }{
116 {
117 desc: "missing level",
118 cfg: Configscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#{
119 Encodingscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Encoding.: "json",
120 },
121 expectErr: "missing Level",
122 },
123 {
124 desc: "missing encoder time in encoder config",
125 cfg: Configscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#{
126 Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Level.: NewAtomicLevelAtscip-go gomod go.uber.org/zap . `go.uber.org/zap`/NewAtomicLevelAt().(zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.),
127 Encodingscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Encoding.: "json",
128 EncoderConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#EncoderConfig.: zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.EncoderConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EncoderConfig#{
129 MessageKeyscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EncoderConfig#MessageKey.: "msg",
130 TimeKeyscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EncoderConfig#TimeKey.: "ts",
131 },
132 },
133 expectErr: "missing EncodeTime in EncoderConfig",
134 },
135 }
136
137 for _, tt := range tests {
138 t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().(tt.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
139 cfg := tt.cfg
140 _, err := cfg.Buildscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Build().()
141 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.EqualErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/EqualError().(t, err, tt.expectErr)
142 })
143 }
144 }
145
146 func makeSamplerCountingHookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/makeSamplerCountingHook().() (h func(zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#, zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.SamplingDecisionscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/SamplingDecision#),
147 dropped, sampled *atomicscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/.Int64scip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#,
148 ) {
149 dropped = new(atomicscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/.Int64scip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#)
150 sampled = new(atomicscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/.Int64scip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#)
151 h = func(_ zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#, dec zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.SamplingDecisionscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/SamplingDecision#) {
152 if dec&zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.LogDroppedscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LogDropped. > 0 {
153 dropped.Addscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#Add().(1)
154 } else if dec&zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.LogSampledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LogSampled. > 0 {
155 sampled.Addscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#Add().(1)
156 }
157 }
158 return h, dropped, sampled
159 }
160
161 func TestConfigWithSamplingHookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/TestConfigWithSamplingHook().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
162 shook, dcount, scount := makeSamplerCountingHookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/makeSamplerCountingHook().()
163 cfg := Configscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#{
164 Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Level.: NewAtomicLevelAtscip-go gomod go.uber.org/zap . `go.uber.org/zap`/NewAtomicLevelAt().(InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap`/InfoLevel.),
165 Developmentscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Development.: false,
166 Samplingscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Sampling.: &SamplingConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/SamplingConfig#{
167 Initialscip-go gomod go.uber.org/zap . `go.uber.org/zap`/SamplingConfig#Initial.: 100,
168 Thereafterscip-go gomod go.uber.org/zap . `go.uber.org/zap`/SamplingConfig#Thereafter.: 100,
169 Hookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/SamplingConfig#Hook.: shook,
170 },
171 Encodingscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Encoding.: "json",
172 EncoderConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#EncoderConfig.: NewProductionEncoderConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/NewProductionEncoderConfig().(),
173 OutputPathsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#OutputPaths.: []string{"stderr"},
174 ErrorOutputPathsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#ErrorOutputPaths.: []string{"stderr"},
175 }
176 expectRe := `{"level":"info","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" +
177 `{"level":"warn","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n"
178 expectDropped := 99 // 200 - 100 initial - 1 thereafter
179 expectSampled := 103 // 2 from initial + 100 + 1 thereafter
180
181 logOut := filepathscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/Join().(t.TempDirscip-go gomod github.com/golang/go/src go1.19 testing/common#TempDir().(), "test.log")
182 cfg.OutputPathsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#OutputPaths. = []string{logOut}
183 cfg.EncoderConfigscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#EncoderConfig..TimeKeyscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/EncoderConfig#TimeKey. = "" // no timestamps in tests
184 cfg.InitialFieldsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#InitialFields. = map[string]interface{}{"z": "zz", "k": "v"}
185
186 logger, err := cfg.Buildscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Config#Build().()
187 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/NoError().(t, err, "Unexpected error constructing logger.")
188
189 logger.Debugscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Debug().("debug")
190 logger.Infoscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Info().("info")
191 logger.Warnscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Warn().("warn")
192
193 byteContents, err := osscip-go gomod github.com/golang/go/src go1.19 os/.ReadFilescip-go gomod github.com/golang/go/src go1.19 os/ReadFile().(logOut)
194 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/NoError().(t, err, "Couldn't read log contents from temp file.")
195 logs := string(byteContents)
196 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Regexpscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Regexp().(t, expectRe, logs, "Unexpected log output.")
197
198 for i := 0; i < 200; i++ {
199 logger.Infoscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#Info().("sampling")
200 }
201 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, int64(expectDropped), dcount.Loadscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#Load().())
202 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, int64(expectSampled), scount.Loadscip-go gomod github.com/golang/go/src go1.19 `sync/atomic`/Int64#Load().())
203 }