-
Notifications
You must be signed in to change notification settings - Fork 149
/
testClient.go
190 lines (151 loc) · 4.9 KB
/
testClient.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
package commontestutils
import (
"context"
"time"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
type FakeWriteErrorGenerator func(obj client.Object) error
type FakeReadErrorGenerator func(key client.ObjectKey) error
// implements the client.Client interface (proxy pattern)
type HcoTestClient struct {
client client.Client
sw *HcoTestStatusWriter
getError FakeReadErrorGenerator
createError FakeWriteErrorGenerator
updateError FakeWriteErrorGenerator
deleteError FakeWriteErrorGenerator
}
func (c *HcoTestClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
return c.client.GroupVersionKindFor(obj)
}
func (c *HcoTestClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
return c.client.IsObjectNamespaced(obj)
}
func (c *HcoTestClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
if c.getError != nil {
if err := c.getError(key); err != nil {
return err
}
}
return c.client.Get(ctx, key, obj, opts...)
}
func (c *HcoTestClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
return c.client.List(ctx, list, opts...)
}
func (c *HcoTestClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
if c.createError != nil {
if err := c.createError(obj); err != nil {
return err
}
}
if err := checkDeadline(ctx); err != nil {
return err
}
return c.client.Create(ctx, obj, opts...)
}
func (c *HcoTestClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
if c.deleteError != nil {
if err := c.deleteError(obj); err != nil {
return err
}
}
if err := checkDeadline(ctx); err != nil {
return err
}
return c.client.Delete(ctx, obj, opts...)
}
func (c *HcoTestClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
if c.updateError != nil {
if err := c.updateError(obj); err != nil {
return err
}
}
if err := checkDeadline(ctx); err != nil {
return err
}
return c.client.Update(ctx, obj, opts...)
}
func checkDeadline(ctx context.Context) error {
if timeout, ok := ctx.Deadline(); ok && timeout.Before(time.Now()) {
<-ctx.Done()
return ctx.Err()
}
return nil
}
func (c *HcoTestClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return c.client.Patch(ctx, obj, patch, opts...)
}
func (c *HcoTestClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
return c.client.DeleteAllOf(ctx, obj, opts...)
}
func (c *HcoTestClient) Status() client.StatusWriter {
return c.sw
}
func (c *HcoTestClient) SubResource(subResource string) client.SubResourceClient {
return c.client.SubResource(subResource)
}
func (c *HcoTestClient) InitiateDeleteErrors(f FakeWriteErrorGenerator) {
c.deleteError = f
}
func (c *HcoTestClient) InitiateUpdateErrors(f FakeWriteErrorGenerator) {
c.updateError = f
}
func (c *HcoTestClient) InitiateGetErrors(f FakeReadErrorGenerator) {
c.getError = f
}
func (c *HcoTestClient) InitiateCreateErrors(f FakeWriteErrorGenerator) {
c.createError = f
}
func (c *HcoTestClient) Scheme() *runtime.Scheme {
return c.client.Scheme()
}
func (c *HcoTestClient) RESTMapper() meta.RESTMapper {
return c.client.RESTMapper()
}
type HcoTestStatusWriter struct {
client client.SubResourceWriter
errors TestErrors
}
func (sw *HcoTestStatusWriter) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
if ok, err := sw.errors.GetNextError(); ok {
return err
}
return sw.client.Create(ctx, obj, subResource, opts...)
}
func (sw *HcoTestStatusWriter) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
if ok, err := sw.errors.GetNextError(); ok {
return err
}
return sw.client.Update(ctx, obj, opts...)
}
func (sw *HcoTestStatusWriter) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
if ok, err := sw.errors.GetNextError(); ok {
return err
}
return sw.client.Patch(ctx, obj, patch, opts...)
}
func (sw *HcoTestStatusWriter) InitiateErrors(errs ...error) {
sw.errors = errs
}
type TestErrors []error
func (errs *TestErrors) GetNextError() (bool, error) {
if len(*errs) == 0 {
return false, nil
}
err := (*errs)[0]
*errs = (*errs)[1:]
return true, err
}
func InitClient(clientObjects []client.Object) *HcoTestClient {
// Create a fake client to mock API calls
cl := fake.NewClientBuilder().
WithObjects(clientObjects...).
WithScheme(GetScheme()).
WithStatusSubresource(clientObjects...).
Build()
return &HcoTestClient{client: cl, sw: &HcoTestStatusWriter{client: cl.Status()}}
}