Skip to content

Commit 61bde47

Browse files
committed
test: testcases for zap logger with WithValues()
to introduce the expected behavior with KubeAwareEncoder Signed-off-by: STRRL <[email protected]>
1 parent 3cb6722 commit 61bde47

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

pkg/log/zap/zap_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,65 @@ var _ = Describe("Zap logger setup", func() {
263263
outRaw := logOut.Bytes()
264264
Expect(string(outRaw)).Should(ContainSubstring("got nil for runtime.Object"))
265265
})
266+
267+
// see https://github.com/kubernetes-sigs/controller-runtime/issues/1290
268+
It("should log NamespacedName as origin stringer when using logrLogger.WithValues", func() {
269+
name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"}
270+
logger.WithValues("thing", name).Info("here's a kubernetes object")
271+
272+
outRaw := logOut.Bytes()
273+
res := map[string]interface{}{}
274+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
275+
276+
Expect(res).NotTo(HaveKeyWithValue("thing", map[string]interface{}{
277+
"name": name.Name,
278+
"namespace": name.Namespace,
279+
}))
280+
Expect(res).To(HaveKeyWithValue("thing", "some-ns/some-pod"))
281+
})
282+
283+
// see https://github.com/kubernetes-sigs/controller-runtime/issues/1290
284+
It("should log Kubernetes objects as origin stringer when using logrLogger.WithValues", func() {
285+
node := &corev1.Node{}
286+
node.Name = "some-node"
287+
node.APIVersion = "v1"
288+
node.Kind = "Node"
289+
logger.WithValues("thing", node).Info("here's a kubernetes object")
290+
291+
outRaw := logOut.Bytes()
292+
res := map[string]interface{}{}
293+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
294+
295+
Expect(res).NotTo(HaveKeyWithValue("thing", map[string]interface{}{
296+
"name": node.Name,
297+
"apiVersion": "v1",
298+
"kind": "Node",
299+
}))
300+
Expect(res).To(HaveKeyWithValue("thing", "&Node{ObjectMeta:{some-node 0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[] map[] [] [] []},Spec:NodeSpec{PodCIDR:,DoNotUseExternalID:,ProviderID:,Unschedulable:false,Taints:[]Taint{},ConfigSource:nil,PodCIDRs:[],},Status:NodeStatus{Capacity:ResourceList{},Allocatable:ResourceList{},Phase:,Conditions:[]NodeCondition{},Addresses:[]NodeAddress{},DaemonEndpoints:NodeDaemonEndpoints{KubeletEndpoint:DaemonEndpoint{Port:0,},},NodeInfo:NodeSystemInfo{MachineID:,SystemUUID:,BootID:,KernelVersion:,OSImage:,ContainerRuntimeVersion:,KubeletVersion:,KubeProxyVersion:,OperatingSystem:,Architecture:,},Images:[]ContainerImage{},VolumesInUse:[],VolumesAttached:[]AttachedVolume{},Config:nil,},}"))
301+
})
302+
303+
// see https://github.com/kubernetes-sigs/controller-runtime/issues/1290
304+
It("should log an unstructured Kubernetes object as origin stringer when using logrLogger.WithValues", func() {
305+
pod := &unstructured.Unstructured{
306+
Object: map[string]interface{}{
307+
"metadata": map[string]interface{}{
308+
"name": "some-pod",
309+
"namespace": "some-ns",
310+
},
311+
},
312+
}
313+
logger.WithValues("thing", pod).Info("here's a kubernetes object")
314+
315+
outRaw := logOut.Bytes()
316+
res := map[string]interface{}{}
317+
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())
318+
319+
Expect(res).NotTo(HaveKeyWithValue("thing", map[string]interface{}{
320+
"name": "some-pod",
321+
"namespace": "some-ns",
322+
}))
323+
Expect(res).To(HaveKeyWithValue("thing", pod.Object))
324+
})
266325
}
267326

268327
Context("with logger created using New", func() {

0 commit comments

Comments
 (0)