-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete.go
More file actions
271 lines (254 loc) · 9.77 KB
/
Copy pathdelete.go
File metadata and controls
271 lines (254 loc) · 9.77 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
package sawchain
import (
"context"
"github.com/onsi/gomega"
"github.com/guidewire-oss/sawchain/internal/chainsaw"
"github.com/guidewire-oss/sawchain/internal/options"
)
// Delete deletes resources with objects, a manifest, or a Chainsaw template, and returns an error
// if any client Delete operations fail.
//
// # Arguments
//
// The following arguments may be provided in any order after the context:
//
// - Object (client.Object): Typed or unstructured object representing a single resource to be deleted.
// If provided with a template, the template will take precedence and the object will be ignored.
//
// - Objects ([]client.Object): Slice of typed or unstructured objects representing multiple resources to
// be deleted. If provided with a template, the template will take precedence and the objects will be
// ignored.
//
// - Template (string): File path or content of a static manifest or Chainsaw template containing the
// identifiers of the resources to be deleted. Takes precedence over objects.
//
// - 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, an object, or a slice of objects must be provided. However, an object and a slice of objects
// may not be provided together.
//
// # Notes
//
// - Invalid input will result in immediate test failure.
//
// - Templates will be sanitized before use, including de-indenting (removing any common leading
// whitespace prefix from non-empty lines) and pruning empty documents.
//
// - When running tests in parallel, ensure resource names or namespaces are unique per process to
// prevent collisions. See docs/parallel-tests.md for isolation strategies.
//
// - Use DeleteAndWait instead of Delete if you need to ensure deletion is successful and the client
// cache is synced.
//
// # Examples
//
// Delete a single resource with an object:
//
// err := sc.Delete(ctx, obj)
//
// Delete multiple resources with objects:
//
// err := sc.Delete(ctx, []client.Object{obj1, obj2, obj3})
//
// Delete resources with a manifest file:
//
// err := sc.Delete(ctx, "path/to/resources.yaml")
//
// Delete a single resource with a Chainsaw template and bindings:
//
// err := sc.Delete(ctx, `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: ($name)
// namespace: ($namespace)
// `, map[string]any{"name": "test-cm", "namespace": "default"})
//
// Delete multiple resources with a Chainsaw template and bindings:
//
// err := sc.Delete(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"})
func (s *Sawchain) Delete(ctx context.Context, args ...any) error {
s.t.Helper()
// Parse options
opts, err := options.ParseAndApplyDefaults(&s.opts, false, false, true, 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.RequireTemplateObjectObjects(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)
// Delete resources
for _, unstructuredObj := range unstructuredObjs {
if err := s.c.Delete(ctx, &unstructuredObj); err != nil {
return err
}
}
} else if opts.Object != nil {
// Delete resource
if err := s.c.Delete(ctx, opts.Object); err != nil {
return err
}
} else {
// Delete resources
for _, obj := range opts.Objects {
if err := s.c.Delete(ctx, obj); err != nil {
return err
}
}
}
return nil
}
// DeleteAndWait deletes resources with objects, a manifest, or a Chainsaw template, and ensures client Get
// operations for all resources reflect the deletion (resources not found) within a configurable duration
// before returning. If testing with a cached client, this ensures the client cache is synced and it is
// safe to make assertions on the resources' absence immediately after execution.
//
// # Arguments
//
// The following arguments may be provided in any order (unless noted otherwise) after the context:
//
// - Object (client.Object): Typed or unstructured object representing a single resource to be deleted.
// If provided with a template, the template will take precedence and the object will be ignored.
//
// - Objects ([]client.Object): Slice of typed or unstructured objects representing multiple resources to
// be deleted. If provided with a template, the template will take precedence and the objects will be
// ignored.
//
// - Template (string): File path or content of a static manifest or Chainsaw template containing the
// identifiers of the resources to be deleted. Takes precedence over objects.
//
// - 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.
//
// - Timeout (string or time.Duration): Duration within which client Get operations for all resources
// should reflect deletion. If provided, must be before interval. Defaults to Sawchain's global
// timeout value.
//
// - Interval (string or time.Duration): Polling interval for checking the resources after deletion.
// If provided, must be after timeout. Defaults to Sawchain's global interval value.
//
// A template, an object, or a slice of objects must be provided. However, an object and a slice of objects
// may not be provided together. All other arguments are optional.
//
// # Notes
//
// - Invalid input, client errors, and timeout errors will result in immediate test failure.
//
// - Templates will be sanitized before use, including de-indenting (removing any common leading
// whitespace prefix from non-empty lines) and pruning empty documents.
//
// - When running tests in parallel, ensure resource names or namespaces are unique per process to
// prevent collisions. See docs/parallel-tests.md for isolation strategies.
//
// - Use Delete instead of DeleteAndWait if you need to delete resources without ensuring success.
//
// # Examples
//
// Delete a single resource with an object:
//
// sc.DeleteAndWait(ctx, obj)
//
// Delete multiple resources with objects:
//
// sc.DeleteAndWait(ctx, []client.Object{obj1, obj2, obj3})
//
// Delete resources with a manifest file and override duration settings:
//
// sc.DeleteAndWait(ctx, "path/to/resources.yaml", "10s", "2s")
//
// Delete a single resource with a Chainsaw template and bindings:
//
// sc.DeleteAndWait(ctx, `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: ($name)
// namespace: ($namespace)
// `, map[string]any{"name": "test-cm", "namespace": "default"})
//
// Delete multiple resources with a Chainsaw template and bindings:
//
// sc.DeleteAndWait(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"})
func (s *Sawchain) DeleteAndWait(ctx context.Context, args ...any) {
s.t.Helper()
// Parse options
opts, err := options.ParseAndApplyDefaults(&s.opts, false, true, true, 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.RequireDurations(opts)).To(gomega.Succeed(), errInvalidArgs)
s.g.Expect(options.RequireTemplateObjectObjects(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)
// Delete resources
for _, unstructuredObj := range unstructuredObjs {
s.g.Expect(s.c.Delete(ctx, &unstructuredObj)).To(gomega.Succeed(), errFailedDeleteWithTemplate)
}
// Wait for delete to be reflected
checkAll := func() error {
for i := range unstructuredObjs {
// Use index to update object in outer scope
if err := s.checkNotFound(ctx, &unstructuredObjs[i]); err != nil {
return err
}
}
return nil
}
s.g.Eventually(checkAll, opts.Timeout, opts.Interval).Should(gomega.Succeed(), errDeleteNotReflected)
} else if opts.Object != nil {
// Delete resource
s.g.Expect(s.c.Delete(ctx, opts.Object)).To(gomega.Succeed(), errFailedDeleteWithObject)
// Wait for delete to be reflected
s.g.Eventually(s.checkNotFoundF(ctx, opts.Object), opts.Timeout, opts.Interval).Should(gomega.Succeed(), errDeleteNotReflected)
} else {
// Delete resources
for _, obj := range opts.Objects {
s.g.Expect(s.c.Delete(ctx, obj)).To(gomega.Succeed(), errFailedDeleteWithObject)
}
// Wait for delete to be reflected
checkAll := func() error {
for _, obj := range opts.Objects {
if err := s.checkNotFound(ctx, obj); err != nil {
return err
}
}
return nil
}
s.g.Eventually(checkAll, opts.Timeout, opts.Interval).Should(gomega.Succeed(), errDeleteNotReflected)
}
}