Skip to content

Commit adee879

Browse files
authored
Merge pull request kubernetes-sigs#653 from joelanford/use-dev-mode-enabled
🐛 pkg/log/zap: use enabled in UseDevMode, add tests
2 parents b3fee0d + d2122ab commit adee879

File tree

2 files changed

+131
-88
lines changed

2 files changed

+131
-88
lines changed

pkg/log/zap/zap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type Opts func(*Options)
8282
// See Options.Development
8383
func UseDevMode(enabled bool) Opts {
8484
return func(o *Options) {
85-
o.Development = true
85+
o.Development = enabled
8686
}
8787
}
8888

pkg/log/zap/zap_test.go

Lines changed: 130 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -112,120 +112,163 @@ func (f *fakeLogger) Info(msg string, vals ...interface{}) {
112112
func (f *fakeLogger) Enabled() bool { return true }
113113
func (f *fakeLogger) V(lvl int) logr.InfoLogger { return f }
114114

115+
var _ = Describe("Zap options setup", func() {
116+
var opts *Options
117+
118+
BeforeEach(func() {
119+
opts = &Options{}
120+
})
121+
122+
It("should enable development mode", func() {
123+
UseDevMode(true)(opts)
124+
Expect(opts.Development).To(BeTrue())
125+
})
126+
127+
It("should disable development mode", func() {
128+
UseDevMode(false)(opts)
129+
Expect(opts.Development).To(BeFalse())
130+
})
131+
132+
It("should set a custom writer", func() {
133+
var w fakeSyncWriter
134+
WriteTo(&w)(opts)
135+
Expect(opts.DestWritter).To(Equal(&w))
136+
})
137+
})
138+
115139
var _ = Describe("Zap logger setup", func() {
116140
Context("with the default output", func() {
117141
It("shouldn't fail when setting up production", func() {
118142
Expect(Logger(false)).NotTo(BeNil())
143+
Expect(New(UseDevMode(false))).NotTo(BeNil())
119144
})
120145

121146
It("shouldn't fail when setting up development", func() {
122147
Expect(Logger(true)).NotTo(BeNil())
148+
Expect(New(UseDevMode(true))).NotTo(BeNil())
123149
})
124150
})
125151

126152
Context("with custom non-sync output", func() {
127153
It("shouldn't fail when setting up production", func() {
128154
Expect(LoggerTo(ioutil.Discard, false)).NotTo(BeNil())
155+
Expect(New(WriteTo(ioutil.Discard), UseDevMode(false))).NotTo(BeNil())
129156
})
130157

131158
It("shouldn't fail when setting up development", func() {
132159
Expect(LoggerTo(ioutil.Discard, true)).NotTo(BeNil())
160+
Expect(New(WriteTo(ioutil.Discard), UseDevMode(true))).NotTo(BeNil())
133161
})
134162
})
135163

136164
Context("when logging kubernetes objects", func() {
137165
var logOut *bytes.Buffer
138166
var logger logr.Logger
139167

140-
BeforeEach(func() {
141-
logOut = new(bytes.Buffer)
142-
By("setting up the logger")
143-
// use production settings (false) to get just json output
144-
logger = LoggerTo(logOut, false)
145-
})
146-
147-
It("should log a standard namespaced Kubernetes object name and namespace", func() {
148-
pod := &kapi.Pod{}
149-
pod.Name = "some-pod"
150-
pod.Namespace = "some-ns"
151-
logger.Info("here's a kubernetes object", "thing", pod)
168+
defineTests := func() {
169+
It("should log a standard namespaced Kubernetes object name and namespace", func() {
170+
pod := &kapi.Pod{}
171+
pod.Name = "some-pod"
172+
pod.Namespace = "some-ns"
173+
logger.Info("here's a kubernetes object", "thing", pod)
174+
175+
outRaw := logOut.Bytes()
176+
res := map[string]interface{}{}
177+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
178+
179+
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
180+
"name": pod.Name,
181+
"namespace": pod.Namespace,
182+
}))
183+
})
184+
185+
It("should work fine with normal stringers", func() {
186+
logger.Info("here's a non-kubernetes stringer", "thing", testStringer{})
187+
outRaw := logOut.Bytes()
188+
res := map[string]interface{}{}
189+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
190+
191+
Expect(res).To(HaveKeyWithValue("thing", "value"))
192+
})
193+
194+
It("should log a standard non-namespaced Kubernetes object name", func() {
195+
node := &kapi.Node{}
196+
node.Name = "some-node"
197+
logger.Info("here's a kubernetes object", "thing", node)
198+
199+
outRaw := logOut.Bytes()
200+
res := map[string]interface{}{}
201+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
202+
203+
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
204+
"name": node.Name,
205+
}))
206+
})
207+
208+
It("should log a standard Kubernetes object's kind, if set", func() {
209+
node := &kapi.Node{}
210+
node.Name = "some-node"
211+
node.APIVersion = "v1"
212+
node.Kind = "Node"
213+
logger.Info("here's a kubernetes object", "thing", node)
214+
215+
outRaw := logOut.Bytes()
216+
res := map[string]interface{}{}
217+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
218+
219+
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
220+
"name": node.Name,
221+
"apiVersion": "v1",
222+
"kind": "Node",
223+
}))
224+
})
225+
226+
It("should log a standard non-namespaced NamespacedName name", func() {
227+
name := types.NamespacedName{Name: "some-node"}
228+
logger.Info("here's a kubernetes object", "thing", name)
229+
230+
outRaw := logOut.Bytes()
231+
res := map[string]interface{}{}
232+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
233+
234+
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
235+
"name": name.Name,
236+
}))
237+
})
238+
239+
It("should log a standard namespaced NamespacedName name and namespace", func() {
240+
name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"}
241+
logger.Info("here's a kubernetes object", "thing", name)
242+
243+
outRaw := logOut.Bytes()
244+
res := map[string]interface{}{}
245+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
246+
247+
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
248+
"name": name.Name,
249+
"namespace": name.Namespace,
250+
}))
251+
})
252+
}
253+
254+
Context("with logger created using New", func() {
255+
BeforeEach(func() {
256+
logOut = new(bytes.Buffer)
257+
By("setting up the logger")
258+
// use production settings (false) to get just json output
259+
logger = New(WriteTo(logOut), UseDevMode(false))
260+
})
261+
defineTests()
152262

153-
outRaw := logOut.Bytes()
154-
res := map[string]interface{}{}
155-
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
156-
157-
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
158-
"name": pod.Name,
159-
"namespace": pod.Namespace,
160-
}))
161263
})
162-
163-
It("should work fine with normal stringers", func() {
164-
logger.Info("here's a non-kubernetes stringer", "thing", testStringer{})
165-
outRaw := logOut.Bytes()
166-
res := map[string]interface{}{}
167-
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
168-
169-
Expect(res).To(HaveKeyWithValue("thing", "value"))
170-
})
171-
172-
It("should log a standard non-namespaced Kubernetes object name", func() {
173-
node := &kapi.Node{}
174-
node.Name = "some-node"
175-
logger.Info("here's a kubernetes object", "thing", node)
176-
177-
outRaw := logOut.Bytes()
178-
res := map[string]interface{}{}
179-
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
180-
181-
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
182-
"name": node.Name,
183-
}))
184-
})
185-
186-
It("should log a standard Kubernetes object's kind, if set", func() {
187-
node := &kapi.Node{}
188-
node.Name = "some-node"
189-
node.APIVersion = "v1"
190-
node.Kind = "Node"
191-
logger.Info("here's a kubernetes object", "thing", node)
192-
193-
outRaw := logOut.Bytes()
194-
res := map[string]interface{}{}
195-
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
196-
197-
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
198-
"name": node.Name,
199-
"apiVersion": "v1",
200-
"kind": "Node",
201-
}))
202-
})
203-
204-
It("should log a standard non-namespaced NamespacedName name", func() {
205-
name := types.NamespacedName{Name: "some-node"}
206-
logger.Info("here's a kubernetes object", "thing", name)
207-
208-
outRaw := logOut.Bytes()
209-
res := map[string]interface{}{}
210-
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
211-
212-
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
213-
"name": name.Name,
214-
}))
215-
})
216-
217-
It("should log a standard namespaced NamespacedName name and namespace", func() {
218-
name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"}
219-
logger.Info("here's a kubernetes object", "thing", name)
220-
221-
outRaw := logOut.Bytes()
222-
res := map[string]interface{}{}
223-
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
224-
225-
Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
226-
"name": name.Name,
227-
"namespace": name.Namespace,
228-
}))
264+
Context("with logger created using LoggerTo", func() {
265+
BeforeEach(func() {
266+
logOut = new(bytes.Buffer)
267+
By("setting up the logger")
268+
// use production settings (false) to get just json output
269+
logger = LoggerTo(logOut, false)
270+
})
271+
defineTests()
229272
})
230273
})
231274
})

0 commit comments

Comments
 (0)