mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
component-base: test json output for KObj and KObjs
Thanks to the new klog, logr and zapr releases, ObjectRef is now logged as struct in JSON output.
This commit is contained in:
parent
169e8b65a0
commit
e36d54ce62
@ -31,6 +31,20 @@ import (
|
|||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type kmeta struct {
|
||||||
|
Name, Namespace string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k kmeta) GetName() string {
|
||||||
|
return k.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k kmeta) GetNamespace() string {
|
||||||
|
return k.Namespace
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ klog.KMetadata = kmeta{}
|
||||||
|
|
||||||
func TestKlogIntegration(t *testing.T) {
|
func TestKlogIntegration(t *testing.T) {
|
||||||
timeNow = func() time.Time {
|
timeNow = func() time.Time {
|
||||||
return time.Date(1970, time.January, 1, 0, 0, 0, 123, time.UTC)
|
return time.Date(1970, time.January, 1, 0, 0, 0, 123, time.UTC)
|
||||||
@ -130,6 +144,24 @@ func TestKlogIntegration(t *testing.T) {
|
|||||||
},
|
},
|
||||||
format: `{"ts":%f,"caller":"json/klog_test.go:%d","msg":"test","v":0,"count":1}`,
|
format: `{"ts":%f,"caller":"json/klog_test.go:%d","msg":"test","v":0,"count":1}`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "KObj",
|
||||||
|
fun: func() {
|
||||||
|
klog.InfoS("some", "pod", klog.KObj(&kmeta{Name: "pod-1", Namespace: "kube-system"}))
|
||||||
|
},
|
||||||
|
format: `{"ts":%f,"caller":"json/klog_test.go:%d","msg":"some","v":0,"pod":{"name":"pod-1","namespace":"kube-system"}}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "KObjs",
|
||||||
|
fun: func() {
|
||||||
|
klog.InfoS("several", "pods",
|
||||||
|
klog.KObjs([]interface{}{
|
||||||
|
&kmeta{Name: "pod-1", Namespace: "kube-system"},
|
||||||
|
&kmeta{Name: "pod-2", Namespace: "kube-system"},
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
format: `{"ts":%f,"caller":"json/klog_test.go:%d","msg":"several","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]}`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "Warning",
|
name: "Warning",
|
||||||
fun: func() {
|
fun: func() {
|
||||||
|
Loading…
Reference in New Issue
Block a user