Skip to content

🐛 pkg/log/zap: use enabled in UseDevMode, add tests #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Opts func(*Options)
// See Options.Development
func UseDevMode(enabled bool) Opts {
return func(o *Options) {
o.Development = true
o.Development = enabled
}
}

Expand Down
217 changes: 130 additions & 87 deletions pkg/log/zap/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,120 +112,163 @@ func (f *fakeLogger) Info(msg string, vals ...interface{}) {
func (f *fakeLogger) Enabled() bool { return true }
func (f *fakeLogger) V(lvl int) logr.InfoLogger { return f }

var _ = Describe("Zap options setup", func() {
var opts *Options

BeforeEach(func() {
opts = &Options{}
})

It("should enable development mode", func() {
UseDevMode(true)(opts)
Expect(opts.Development).To(BeTrue())
})

It("should disable development mode", func() {
UseDevMode(false)(opts)
Expect(opts.Development).To(BeFalse())
})

It("should set a custom writer", func() {
var w fakeSyncWriter
WriteTo(&w)(opts)
Expect(opts.DestWritter).To(Equal(&w))
})
})

var _ = Describe("Zap logger setup", func() {
Context("with the default output", func() {
It("shouldn't fail when setting up production", func() {
Expect(Logger(false)).NotTo(BeNil())
Expect(New(UseDevMode(false))).NotTo(BeNil())
})

It("shouldn't fail when setting up development", func() {
Expect(Logger(true)).NotTo(BeNil())
Expect(New(UseDevMode(true))).NotTo(BeNil())
})
})

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

It("shouldn't fail when setting up development", func() {
Expect(LoggerTo(ioutil.Discard, true)).NotTo(BeNil())
Expect(New(WriteTo(ioutil.Discard), UseDevMode(true))).NotTo(BeNil())
})
})

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

BeforeEach(func() {
logOut = new(bytes.Buffer)
By("setting up the logger")
// use production settings (false) to get just json output
logger = LoggerTo(logOut, false)
})

It("should log a standard namespaced Kubernetes object name and namespace", func() {
pod := &kapi.Pod{}
pod.Name = "some-pod"
pod.Namespace = "some-ns"
logger.Info("here's a kubernetes object", "thing", pod)
defineTests := func() {
It("should log a standard namespaced Kubernetes object name and namespace", func() {
pod := &kapi.Pod{}
pod.Name = "some-pod"
pod.Namespace = "some-ns"
logger.Info("here's a kubernetes object", "thing", pod)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": pod.Name,
"namespace": pod.Namespace,
}))
})

It("should work fine with normal stringers", func() {
logger.Info("here's a non-kubernetes stringer", "thing", testStringer{})
outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", "value"))
})

It("should log a standard non-namespaced Kubernetes object name", func() {
node := &kapi.Node{}
node.Name = "some-node"
logger.Info("here's a kubernetes object", "thing", node)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": node.Name,
}))
})

It("should log a standard Kubernetes object's kind, if set", func() {
node := &kapi.Node{}
node.Name = "some-node"
node.APIVersion = "v1"
node.Kind = "Node"
logger.Info("here's a kubernetes object", "thing", node)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": node.Name,
"apiVersion": "v1",
"kind": "Node",
}))
})

It("should log a standard non-namespaced NamespacedName name", func() {
name := types.NamespacedName{Name: "some-node"}
logger.Info("here's a kubernetes object", "thing", name)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": name.Name,
}))
})

It("should log a standard namespaced NamespacedName name and namespace", func() {
name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"}
logger.Info("here's a kubernetes object", "thing", name)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": name.Name,
"namespace": name.Namespace,
}))
})
}

Context("with logger created using New", func() {
BeforeEach(func() {
logOut = new(bytes.Buffer)
By("setting up the logger")
// use production settings (false) to get just json output
logger = New(WriteTo(logOut), UseDevMode(false))
})
defineTests()

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": pod.Name,
"namespace": pod.Namespace,
}))
})

It("should work fine with normal stringers", func() {
logger.Info("here's a non-kubernetes stringer", "thing", testStringer{})
outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", "value"))
})

It("should log a standard non-namespaced Kubernetes object name", func() {
node := &kapi.Node{}
node.Name = "some-node"
logger.Info("here's a kubernetes object", "thing", node)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": node.Name,
}))
})

It("should log a standard Kubernetes object's kind, if set", func() {
node := &kapi.Node{}
node.Name = "some-node"
node.APIVersion = "v1"
node.Kind = "Node"
logger.Info("here's a kubernetes object", "thing", node)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": node.Name,
"apiVersion": "v1",
"kind": "Node",
}))
})

It("should log a standard non-namespaced NamespacedName name", func() {
name := types.NamespacedName{Name: "some-node"}
logger.Info("here's a kubernetes object", "thing", name)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": name.Name,
}))
})

It("should log a standard namespaced NamespacedName name and namespace", func() {
name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"}
logger.Info("here's a kubernetes object", "thing", name)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": name.Name,
"namespace": name.Namespace,
}))
Context("with logger created using LoggerTo", func() {
BeforeEach(func() {
logOut = new(bytes.Buffer)
By("setting up the logger")
// use production settings (false) to get just json output
logger = LoggerTo(logOut, false)
})
defineTests()
})
})
})