-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfetch.go
More file actions
366 lines (334 loc) · 13.9 KB
/
Copy pathfetch.go
File metadata and controls
366 lines (334 loc) · 13.9 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package sawchain
import (
"context"
"github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/guidewire-oss/sawchain/internal/chainsaw"
"github.com/guidewire-oss/sawchain/internal/options"
"github.com/guidewire-oss/sawchain/internal/util"
)
// FetchSingle retrieves a single resource with an object, a manifest, or a Chainsaw template, saves its
// state to the object (if provided), and returns the object. This is especially useful for assertions
// on resource state when the resource is expected to already exist.
//
// # Arguments
//
// The following arguments may be provided in any order after the context:
//
// - Object (client.Object): Typed or unstructured object for reading/writing resource state. If provided
// without a template, resource state will be read from the object for identification and written back
// to the object. If provided with a template, resource state will be read from the template for
// identification and written to the object.
//
// - Template (string): File path or content of a static manifest or Chainsaw template containing a
// resource identifier to be read for retrieval. Must contain exactly one resource identifier
// matching the type of the object (if provided).
//
// - Bindings (map[string]any): Bindings to be applied to the Chainsaw template (if provided) in addition
// to (or overriding) Sawchain's global bindings. If multiple maps are provided, they will be merged in
// natural order.
//
// A template or an object must be provided.
//
// # Notes
//
// - Invalid input and client errors will result in immediate test failure.
//
// - When dealing with typed objects, the client scheme will be used for internal conversions.
//
// - Templates will be sanitized before use, including de-indenting (removing any common leading
// whitespace prefix from non-empty lines) and pruning empty documents.
//
// - When no input object is provided and an object must be returned, FetchSingle attempts to return a
// typed object. If a typed object cannot be created (i.e., if the client scheme does not support the
// necessary type), an unstructured object will be returned instead.
//
// - Use FetchSingleFunc if you need to create a FetchSingle function for polling.
//
// # Examples
//
// Fetch a resource with an object:
//
// fetched := sc.FetchSingle(ctx, obj)
//
// Fetch a resource with a manifest file:
//
// fetched := sc.FetchSingle(ctx, "path/to/configmap.yaml")
//
// Fetch a resource with a Chainsaw template and bindings:
//
// fetched := sc.FetchSingle(ctx, `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: ($name)
// namespace: ($namespace)
// `, map[string]any{"name": "test-cm", "namespace": "default"})
//
// Fetch a resource with a Chainsaw template and save the resource's state to an object:
//
// configMap := &corev1.ConfigMap{}
// fetched := sc.FetchSingle(ctx, configMap, `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: ($name)
// namespace: ($namespace)
// `, map[string]any{"name": "test-cm", "namespace": "default"})
func (s *Sawchain) FetchSingle(ctx context.Context, args ...any) client.Object {
s.t.Helper()
// Parse options
opts, err := options.ParseAndApplyDefaults(&s.opts, false, false, true, false, true, args...)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidArgs)
s.g.Expect(opts).NotTo(gomega.BeNil(), errNilOpts)
// Check required options
s.g.Expect(options.RequireTemplateObject(opts)).To(gomega.Succeed(), errInvalidArgs)
if len(opts.Template) > 0 {
// Render template
bindings, err := chainsaw.BindingsFromMap(opts.Bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidBindings)
unstructuredObj, err := chainsaw.RenderTemplateSingle(ctx, opts.Template, bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidTemplate)
// Get resource
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(&unstructuredObj), &unstructuredObj)).To(gomega.Succeed(), errFailedGetWithTemplate)
// Save/return object
if opts.Object != nil {
s.g.Expect(util.CopyUnstructuredToObject(s.c, unstructuredObj, opts.Object)).To(gomega.Succeed(), errFailedSave)
return opts.Object
} else {
return s.convertReturnObject(unstructuredObj)
}
} else {
// Get resource
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(opts.Object), opts.Object)).To(gomega.Succeed(), errFailedGetWithObject)
// Return object
return opts.Object
}
}
// FetchMultiple retrieves multiple resources with objects, a manifest, or a Chainsaw template, saves their
// states to the objects (if provided), and returns the objects. This is especially useful for assertions
// on resource states when the resources are expected to already exist.
//
// # Arguments
//
// The following arguments may be provided in any order after the context:
//
// - Objects ([]client.Object): Slice of typed or unstructured objects for reading/writing resource states.
// If provided without a template, resource states will be read from the objects for identification and
// written back to the objects. If provided with a template, resource states will be read from the
// template for identification and written to the objects.
//
// - Template (string): File path or content of a static manifest or Chainsaw template containing resource
// identifiers to be read for retrieval. Must contain resource identifiers exactly matching the count,
// order, and types of the objects (if provided).
//
// - Bindings (map[string]any): Bindings to be applied to the Chainsaw template (if provided) in addition
// to (or overriding) Sawchain's global bindings. If multiple maps are provided, they will be merged in
// natural order.
//
// A template or objects must be provided.
//
// # Notes
//
// - Invalid input and client errors will result in immediate test failure.
//
// - When dealing with typed objects, the client scheme will be used for internal conversions.
//
// - Templates will be sanitized before use, including de-indenting (removing any common leading
// whitespace prefix from non-empty lines) and pruning empty documents.
//
// - When no input objects are provided and objects must be returned, FetchMultiple attempts to return
// typed objects. If typed objects cannot be created (i.e., if the client scheme does not support the
// necessary types), unstructured objects will be returned instead.
//
// - Use FetchMultipleFunc if you need to create a FetchMultiple function for polling.
//
// # Examples
//
// Fetch resources with objects:
//
// fetchedObjs := sc.FetchMultiple(ctx, []client.Object{configMap, secret})
//
// Fetch resources with a manifest file:
//
// fetchedObjs := sc.FetchMultiple(ctx, "path/to/resources.yaml")
//
// Fetch resources with a Chainsaw template and bindings:
//
// fetchedObjs := sc.FetchMultiple(ctx, `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: (concat($prefix, '-cm'))
// namespace: ($namespace)
// ---
// apiVersion: v1
// kind: Secret
// metadata:
// name: (concat($prefix, '-secret'))
// namespace: ($namespace)
// `, map[string]any{"prefix": "test", "namespace": "default"})
//
// Fetch resources with a Chainsaw template and save their states to objects:
//
// configMap := &corev1.ConfigMap{}
// secret := &corev1.Secret{}
// fetchedObjs := sc.FetchMultiple(ctx, []client.Object{configMap, secret}, `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: (concat($prefix, '-cm'))
// namespace: ($namespace)
// ---
// apiVersion: v1
// kind: Secret
// metadata:
// name: (concat($prefix, '-secret'))
// namespace: ($namespace)
// `, map[string]any{"prefix": "test", "namespace": "default"})
func (s *Sawchain) FetchMultiple(ctx context.Context, args ...any) []client.Object {
s.t.Helper()
// Parse options
opts, err := options.ParseAndApplyDefaults(&s.opts, false, false, false, true, true, args...)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidArgs)
s.g.Expect(opts).NotTo(gomega.BeNil(), errNilOpts)
// Check required options
s.g.Expect(options.RequireTemplateObjects(opts)).To(gomega.Succeed(), errInvalidArgs)
if len(opts.Template) > 0 {
// Render template
bindings, err := chainsaw.BindingsFromMap(opts.Bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidBindings)
unstructuredObjs, err := chainsaw.RenderTemplate(ctx, opts.Template, bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidTemplate)
// Validate objects length
if opts.Objects != nil {
s.g.Expect(opts.Objects).To(gomega.HaveLen(len(unstructuredObjs)), errObjectsWrongLength)
}
// Get resources
for i := range unstructuredObjs {
// Pass pointer to slice element to save to outer scope
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(&unstructuredObjs[i]), &unstructuredObjs[i])).To(gomega.Succeed(), errFailedGetWithTemplate)
}
// Save/return objects
if opts.Objects != nil {
for i, unstructuredObj := range unstructuredObjs {
s.g.Expect(util.CopyUnstructuredToObject(s.c, unstructuredObj, opts.Objects[i])).To(gomega.Succeed(), errFailedSave)
}
return opts.Objects
} else {
objs := make([]client.Object, len(unstructuredObjs))
for i, unstructuredObj := range unstructuredObjs {
objs[i] = s.convertReturnObject(unstructuredObj)
}
return objs
}
} else {
// Get resources
for _, obj := range opts.Objects {
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(obj), obj)).To(gomega.Succeed(), errFailedGetWithObject)
}
// Return objects
return opts.Objects
}
}
// FetchSingleFunc returns a function that retrieves a single resource with an object, a manifest, or a
// Chainsaw template, saves its state to the object (if provided), and returns the object.
//
// The returned function performs the same operations as FetchSingle, but is particularly useful for
// polling scenarios where resources might not immediately reflect the desired state.
//
// For details on arguments, examples, and behavior, see the documentation for FetchSingle.
func (s *Sawchain) FetchSingleFunc(ctx context.Context, args ...any) func() client.Object {
s.t.Helper()
// Parse options
opts, err := options.ParseAndApplyDefaults(&s.opts, false, false, true, false, true, args...)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidArgs)
s.g.Expect(opts).NotTo(gomega.BeNil(), errNilOpts)
// Check required options
s.g.Expect(options.RequireTemplateObject(opts)).To(gomega.Succeed(), errInvalidArgs)
if len(opts.Template) > 0 {
// Render template
bindings, err := chainsaw.BindingsFromMap(opts.Bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidBindings)
unstructuredObj, err := chainsaw.RenderTemplateSingle(ctx, opts.Template, bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidTemplate)
return func() client.Object {
s.t.Helper()
// Get resource
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(&unstructuredObj), &unstructuredObj)).To(gomega.Succeed(), errFailedGetWithTemplate)
// Save/return object
if opts.Object != nil {
s.g.Expect(util.CopyUnstructuredToObject(s.c, unstructuredObj, opts.Object)).To(gomega.Succeed(), errFailedSave)
return opts.Object
} else {
return s.convertReturnObject(unstructuredObj)
}
}
} else {
return func() client.Object {
s.t.Helper()
// Get resource
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(opts.Object), opts.Object)).To(gomega.Succeed(), errFailedGetWithObject)
// Return object
return opts.Object
}
}
}
// FetchMultipleFunc returns a function that retrieves multiple resources with objects, a manifest, or a
// Chainsaw template, saves their states to the objects (if provided), and returns the objects.
//
// The returned function performs the same operations as FetchMultiple, but is particularly useful for
// polling scenarios where resources might not immediately reflect the desired state.
//
// For details on arguments, examples, and behavior, see the documentation for FetchMultiple.
func (s *Sawchain) FetchMultipleFunc(ctx context.Context, args ...any) func() []client.Object {
s.t.Helper()
// Parse options
opts, err := options.ParseAndApplyDefaults(&s.opts, false, false, false, true, true, args...)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidArgs)
s.g.Expect(opts).NotTo(gomega.BeNil(), errNilOpts)
// Check required options
s.g.Expect(options.RequireTemplateObjects(opts)).To(gomega.Succeed(), errInvalidArgs)
if len(opts.Template) > 0 {
// Render template
bindings, err := chainsaw.BindingsFromMap(opts.Bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidBindings)
unstructuredObjs, err := chainsaw.RenderTemplate(ctx, opts.Template, bindings)
s.g.Expect(err).NotTo(gomega.HaveOccurred(), errInvalidTemplate)
// Validate objects length
if opts.Objects != nil {
s.g.Expect(opts.Objects).To(gomega.HaveLen(len(unstructuredObjs)), errObjectsWrongLength)
}
return func() []client.Object {
s.t.Helper()
// Get resources
for i := range unstructuredObjs {
// Pass pointer to slice element to save to outer scope
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(&unstructuredObjs[i]), &unstructuredObjs[i])).To(gomega.Succeed(), errFailedGetWithTemplate)
}
// Save/return objects
if opts.Objects != nil {
for i, unstructuredObj := range unstructuredObjs {
s.g.Expect(util.CopyUnstructuredToObject(s.c, unstructuredObj, opts.Objects[i])).To(gomega.Succeed(), errFailedSave)
}
return opts.Objects
} else {
objs := make([]client.Object, len(unstructuredObjs))
for i, unstructuredObj := range unstructuredObjs {
objs[i] = s.convertReturnObject(unstructuredObj)
}
return objs
}
}
} else {
return func() []client.Object {
s.t.Helper()
// Get resources
for _, obj := range opts.Objects {
s.g.Expect(s.c.Get(ctx, client.ObjectKeyFromObject(obj), obj)).To(gomega.Succeed(), errFailedGetWithObject)
}
// Return objects
return opts.Objects
}
}
}