forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 214
/
keyvalues_test.go
151 lines (141 loc) · 4.44 KB
/
keyvalues_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package serialize_test
import (
"bytes"
"fmt"
"testing"
"time"
"k8s.io/klog/v2"
"k8s.io/klog/v2/internal/serialize"
"k8s.io/klog/v2/internal/test"
)
// point conforms to fmt.Stringer interface as it implements the String() method
type point struct {
x int
y int
}
// we now have a value receiver
func (p point) String() string {
return fmt.Sprintf("x=%d, y=%d", p.x, p.y)
}
// Test that kvListFormat works as advertised.
func TestKvListFormat(t *testing.T) {
var emptyPoint *point
var testKVList = []struct {
keysValues []interface{}
want string
}{
{
keysValues: []interface{}{"pod", "kubedns"},
want: " pod=\"kubedns\"",
},
{
keysValues: []interface{}{"pod", "kubedns", "update", true},
want: " pod=\"kubedns\" update=true",
},
{
keysValues: []interface{}{"pod", "kubedns", "spec", struct {
X int
Y string
N time.Time
}{X: 76, Y: "strval", N: time.Date(2006, 1, 2, 15, 4, 5, .067890e9, time.UTC)}},
want: " pod=\"kubedns\" spec={X:76 Y:strval N:2006-01-02 15:04:05.06789 +0000 UTC}",
},
{
keysValues: []interface{}{"pod", "kubedns", "values", []int{8, 6, 7, 5, 3, 0, 9}},
want: " pod=\"kubedns\" values=[8 6 7 5 3 0 9]",
},
{
keysValues: []interface{}{"pod", "kubedns", "values", []string{"deployment", "svc", "configmap"}},
want: " pod=\"kubedns\" values=[deployment svc configmap]",
},
{
keysValues: []interface{}{"pod", "kubedns", "bytes", []byte("test case for byte array")},
want: " pod=\"kubedns\" bytes=\"test case for byte array\"",
},
{
keysValues: []interface{}{"pod", "kubedns", "bytes", []byte("��=� ⌘")},
want: " pod=\"kubedns\" bytes=\"\\ufffd\\ufffd=\\ufffd \\u2318\"",
},
{
keysValues: []interface{}{"multiLineString", `Hello world!
Starts with tab.
Starts with spaces.
No whitespace.`,
"pod", "kubedns",
},
want: ` multiLineString=<
Hello world!
Starts with tab.
Starts with spaces.
No whitespace.
> pod="kubedns"`,
},
{
keysValues: []interface{}{"pod", "kubedns", "maps", map[string]int{"three": 4}},
want: " pod=\"kubedns\" maps=map[three:4]",
},
{
keysValues: []interface{}{"pod", klog.KRef("kube-system", "kubedns"), "status", "ready"},
want: " pod=\"kube-system/kubedns\" status=\"ready\"",
},
{
keysValues: []interface{}{"pod", klog.KRef("", "kubedns"), "status", "ready"},
want: " pod=\"kubedns\" status=\"ready\"",
},
{
keysValues: []interface{}{"pod", klog.KObj(test.KMetadataMock{Name: "test-name", NS: "test-ns"}), "status", "ready"},
want: " pod=\"test-ns/test-name\" status=\"ready\"",
},
{
keysValues: []interface{}{"pod", klog.KObj(test.KMetadataMock{Name: "test-name", NS: ""}), "status", "ready"},
want: " pod=\"test-name\" status=\"ready\"",
},
{
keysValues: []interface{}{"pod", klog.KObj(nil), "status", "ready"},
want: " pod=\"\" status=\"ready\"",
},
{
keysValues: []interface{}{"pod", klog.KObj((*test.PtrKMetadataMock)(nil)), "status", "ready"},
want: " pod=\"\" status=\"ready\"",
},
{
keysValues: []interface{}{"pod", klog.KObj((*test.KMetadataMock)(nil)), "status", "ready"},
want: " pod=\"\" status=\"ready\"",
},
{
keysValues: []interface{}{"pods", klog.KObjs([]test.KMetadataMock{
{
Name: "kube-dns",
NS: "kube-system",
},
{
Name: "mi-conf",
},
})},
want: " pods=[kube-system/kube-dns mi-conf]",
},
{
keysValues: []interface{}{"point-1", point{100, 200}, "point-2", emptyPoint},
want: " point-1=\"x=100, y=200\" point-2=\"<panic: value method k8s.io/klog/v2/internal/serialize_test.point.String called using nil *point pointer>\"",
},
}
for _, d := range testKVList {
b := &bytes.Buffer{}
serialize.KVListFormat(b, d.keysValues...)
if b.String() != d.want {
t.Errorf("KVListFormat error:\n got:\n\t%s\nwant:\t%s", b.String(), d.want)
}
}
}