-
Notifications
You must be signed in to change notification settings - Fork 535
/
cacheable_object.go
220 lines (188 loc) · 6.12 KB
/
cacheable_object.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright 2019 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 testing
import (
"bytes"
"fmt"
"io"
"testing"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// nonCacheableTestObject implements json.Marshaler and proto.Marshaler interfaces
// for mocking purpose.
// +k8s:deepcopy-gen=false
type noncacheableTestObject struct {
gvk schema.GroupVersionKind
}
// MarshalJSON implements json.Marshaler interface.
func (*noncacheableTestObject) MarshalJSON() ([]byte, error) {
return []byte("\"json-result\""), nil
}
// Marshal implements proto.Marshaler interface.
func (*noncacheableTestObject) Marshal() ([]byte, error) {
return []byte("\"proto-result\""), nil
}
// DeepCopyObject implements runtime.Object interface.
func (*noncacheableTestObject) DeepCopyObject() runtime.Object {
panic("DeepCopy unimplemented for noncacheableTestObject")
}
// GetObjectKind implements runtime.Object interface.
func (o *noncacheableTestObject) GetObjectKind() schema.ObjectKind {
return o
}
// GroupVersionKind implements schema.ObjectKind interface.
func (o *noncacheableTestObject) GroupVersionKind() schema.GroupVersionKind {
return o.gvk
}
// SetGroupVersionKind implements schema.ObjectKind interface.
func (o *noncacheableTestObject) SetGroupVersionKind(gvk schema.GroupVersionKind) {
o.gvk = gvk
}
var _ runtime.CacheableObject = &MockCacheableObject{}
// MochCacheableObject is used to test CacheableObject interface.
// +k8s:deepcopy-gen=false
type MockCacheableObject struct {
gvk schema.GroupVersionKind
t *testing.T
runEncode bool
returnSelf bool
expectedResult string
expectedError error
intercepted []runtime.Identifier
}
// DeepCopyObject implements runtime.Object interface.
func (m *MockCacheableObject) DeepCopyObject() runtime.Object {
panic("DeepCopy unimplemented for MockCacheableObject")
}
// GetObjectKind implements runtime.Object interface.
func (m *MockCacheableObject) GetObjectKind() schema.ObjectKind {
return m
}
// GroupVersionKind implements schema.ObjectKind interface.
func (m *MockCacheableObject) GroupVersionKind() schema.GroupVersionKind {
return m.gvk
}
// SetGroupVersionKind implements schema.ObjectKind interface.
func (m *MockCacheableObject) SetGroupVersionKind(gvk schema.GroupVersionKind) {
m.gvk = gvk
}
// Marshal implements proto.Marshaler interface.
// This is implemented to avoid errors from protobuf serializer.
func (*MockCacheableObject) Marshal() ([]byte, error) {
return []byte("\"proto-result\""), nil
}
// CacheEncode implements runtime.CacheableObject interface.
func (m *MockCacheableObject) CacheEncode(id runtime.Identifier, encode func(runtime.Object, io.Writer) error, w io.Writer) error {
m.intercepted = append(m.intercepted, id)
if m.runEncode {
return encode(m.GetObject(), w)
}
if _, err := w.Write([]byte(m.expectedResult)); err != nil {
m.t.Errorf("couldn't write to io.Writer: %v", err)
}
return m.expectedError
}
// GetObject implements runtime.CacheableObject interface.
func (m *MockCacheableObject) GetObject() runtime.Object {
if m.returnSelf {
return m
}
gvk := schema.GroupVersionKind{Group: "group", Version: "version", Kind: "noncacheableTestObject"}
return &noncacheableTestObject{gvk: gvk}
}
func (m *MockCacheableObject) interceptedCalls() []runtime.Identifier {
return m.intercepted
}
type testBuffer struct {
writer io.Writer
t *testing.T
object *MockCacheableObject
}
// Write implements io.Writer interface.
func (b *testBuffer) Write(p []byte) (int, error) {
// Before writing any byte, check if <object> has already
// intercepted any CacheEncode operation.
if len(b.object.interceptedCalls()) == 0 {
b.t.Errorf("writing to buffer without handling MockCacheableObject")
}
return b.writer.Write(p)
}
// CacheableObjectTest implements a test that should be run for every
// runtime.Encoder interface implementation.
// It checks whether CacheableObject is properly supported by it.
func CacheableObjectTest(t *testing.T, e runtime.Encoder) {
gvk1 := schema.GroupVersionKind{Group: "group", Version: "version1", Kind: "MockCacheableObject"}
testCases := []struct {
desc string
runEncode bool
returnSelf bool
expectedResult string
expectedError error
}{
{
desc: "delegate",
runEncode: true,
},
{
desc: "delegate return self",
runEncode: true,
returnSelf: true,
},
{
desc: "cached success",
runEncode: false,
expectedResult: "result",
expectedError: nil,
},
{
desc: "cached failure",
runEncode: false,
expectedResult: "",
expectedError: fmt.Errorf("encoding error"),
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
obj := &MockCacheableObject{
gvk: gvk1,
t: t,
runEncode: test.runEncode,
returnSelf: test.returnSelf,
expectedResult: test.expectedResult,
expectedError: test.expectedError,
}
buffer := bytes.NewBuffer(nil)
w := &testBuffer{
writer: buffer,
t: t,
object: obj,
}
if err := e.Encode(obj, w); err != test.expectedError {
t.Errorf("unexpected error: %v, expected: %v", err, test.expectedError)
}
if !test.runEncode {
if result := buffer.String(); result != test.expectedResult {
t.Errorf("unexpected result: %s, expected: %s", result, test.expectedResult)
}
}
intercepted := obj.interceptedCalls()
if len(intercepted) != 1 {
t.Fatalf("unexpected number of intercepted calls: %v", intercepted)
}
if intercepted[0] != e.Identifier() {
t.Errorf("unexpected intercepted call: %v, expected: %v", intercepted, e.Identifier())
}
})
}
}