/
generator.go
455 lines (364 loc) · 14.6 KB
/
generator.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package tests
import (
"fmt"
"os"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/ghodss/yaml"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/komega"
)
// LoadTestSuiteSpecs recursively walks the given paths looking for any file with the suffix `.testsuite.yaml`.
// It then loads these files in SuiteSpec structs ready for the generator to generate the test cases.
func LoadTestSuiteSpecs(paths ...string) ([]SuiteSpec, error) {
suiteFiles := make(map[string]struct{})
for _, path := range paths {
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dirPath := filepath.Base(filepath.Dir(filepath.Dir(path)))
if !info.IsDir() && strings.HasSuffix(path, ".yaml") && dirPath == "tests" {
suiteFiles[path] = struct{}{}
}
return nil
}); err != nil {
return nil, fmt.Errorf("could not load files from path %q: %w", path, err)
}
}
out := []SuiteSpec{}
for path := range suiteFiles {
suite, err := loadSuiteFile(path)
if err != nil {
return nil, fmt.Errorf("could not set up test suite: %w", err)
}
out = append(out, suite)
}
return out, nil
}
// loadSuiteFile loads an individual SuiteSpec from the given file name.
func loadSuiteFile(path string) (SuiteSpec, error) {
raw, err := os.ReadFile(path)
if err != nil {
return SuiteSpec{}, fmt.Errorf("could not read file %q: %w", path, err)
}
s := SuiteSpec{}
if err := yaml.Unmarshal(raw, &s); err != nil {
return SuiteSpec{}, fmt.Errorf("could not unmarshal YAML file %q: %w", path, err)
}
if len(s.CRDName) == 0 {
return SuiteSpec{}, fmt.Errorf("test suite spec %q is invalid: missing required field `crdName`", path)
}
s.PerTestRuntimeInfo, err = perTestRuntimeInfo(filepath.Dir(path), s.CRDName, s.FeatureGate)
if err != nil {
return SuiteSpec{}, fmt.Errorf("unable to determine which CRD files to use: %w", err)
}
if len(s.PerTestRuntimeInfo.CRDFilenames) == 0 {
return SuiteSpec{}, fmt.Errorf("missing CRD files to use for test %v", path)
}
if s.Version == "" {
version, err := getSuiteSpecTestVersion(s)
if err != nil {
return SuiteSpec{}, fmt.Errorf("could not determine test suite CRD version for %q: %w", path, err)
}
s.Version = version
}
return s, nil
}
// GenerateTestSuite generates a Ginkgo test suite from the provided SuiteSpec.
func GenerateTestSuite(suiteSpec SuiteSpec) {
for i := range suiteSpec.PerTestRuntimeInfo.CRDFilenames {
crdFilename := suiteSpec.PerTestRuntimeInfo.CRDFilenames[i]
baseCRD, err := loadVersionedCRD(suiteSpec, crdFilename)
Expect(err).ToNot(HaveOccurred())
suiteName, err := generateSuiteName(suiteSpec, crdFilename)
Expect(err).ToNot(HaveOccurred())
Describe(suiteName, Ordered, func() {
var crdOptions envtest.CRDInstallOptions
var crd *apiextensionsv1.CustomResourceDefinition
BeforeEach(OncePerOrdered, func() {
Expect(k8sClient).ToNot(BeNil(), "Kubernetes client is not initialised")
crdOptions = envtest.CRDInstallOptions{
CRDs: []*apiextensionsv1.CustomResourceDefinition{
baseCRD.DeepCopy(),
},
}
crds, err := envtest.InstallCRDs(cfg, crdOptions)
Expect(err).ToNot(HaveOccurred())
Expect(crds).To(HaveLen(1), "Only one CRD should have been installed")
crd = crds[0]
Expect(envtest.WaitForCRDs(cfg, crds, crdOptions)).To(Succeed())
})
AfterEach(func() {
// Remove all of the resources we created during the test.
for _, u := range newUnstructuredsFor(crd) {
Expect(k8sClient.DeleteAllOf(ctx, u, client.InNamespace("default")))
}
})
AfterEach(OncePerOrdered, func() {
// Remove the CRD and wait for it to be removed from the API.
// If we don't wait then subsequent tests may fail.
Expect(envtest.UninstallCRDs(cfg, crdOptions)).ToNot(HaveOccurred())
Eventually(komega.Get(crd)).Should(Not(Succeed()))
})
generateOnCreateTable(suiteSpec.Tests.OnCreate)
generateOnUpdateTable(suiteSpec.Tests.OnUpdate)
})
}
}
// generateOnCreateTable generates a table of tests from the defined OnCreate tests
// within the test suite test spec.
func generateOnCreateTable(onCreateTests []OnCreateTestSpec) {
type onCreateTableInput struct {
featureGate string
initial []byte
expected []byte
expectedError string
}
// assertOnCreate runs the actual test for each table entry
var assertOnCreate interface{} = func(in onCreateTableInput) {
initialObj, err := newUnstructuredFrom(in.initial)
Expect(err).ToNot(HaveOccurred(), "initial data should be a valid Kubernetes YAML resource")
err = k8sClient.Create(ctx, initialObj)
if in.expectedError != "" {
Expect(err).To(MatchError(ContainSubstring(in.expectedError)))
return
}
Expect(err).ToNot(HaveOccurred())
// Fetch the object we just created from the API.
gotObj := newEmptyUnstructuredFrom(initialObj)
Expect(k8sClient.Get(ctx, objectKey(initialObj), gotObj))
expectedObj, err := newUnstructuredFrom(in.expected)
Expect(err).ToNot(HaveOccurred(), "expected data should be a valid Kubernetes YAML resource when no expected error is provided")
// Ensure the name and namespace match.
// The IgnoreAutogeneratedMetadata will ignore any additional meta set in the API.
expectedObj.SetName(gotObj.GetName())
expectedObj.SetNamespace(gotObj.GetNamespace())
Expect(gotObj).To(komega.EqualObject(expectedObj, komega.IgnoreAutogeneratedMetadata))
}
// First argument to the table is the test function.
tableEntries := []interface{}{assertOnCreate}
// Convert the test specs into table entries
for _, testEntry := range onCreateTests {
tableEntries = append(tableEntries, Entry(testEntry.Name, onCreateTableInput{
initial: []byte(testEntry.Initial),
expected: []byte(testEntry.Expected),
expectedError: testEntry.ExpectedError,
}))
}
if len(tableEntries) > 1 {
DescribeTable("On Create", tableEntries...)
}
}
// generateOnUpdateTable generates a table of tests from the defined OnUpdate tests
// within the test suite test spec.
func generateOnUpdateTable(onUpdateTests []OnUpdateTestSpec) {
type onUpdateTableInput struct {
featureGate string
initial []byte
updated []byte
expected []byte
expectedError string
expectedStatusError string
}
var assertOnUpdate interface{} = func(in onUpdateTableInput) {
initialObj, err := newUnstructuredFrom(in.initial)
Expect(err).ToNot(HaveOccurred(), "initial data should be a valid Kubernetes YAML resource")
initialStatus, ok, err := unstructured.NestedFieldNoCopy(initialObj.Object, "status")
Expect(err).ToNot(HaveOccurred())
if ok {
Expect(initialStatus).ToNot(BeNil())
}
Expect(k8sClient.Create(ctx, initialObj)).ToNot(HaveOccurred(), "initial object should create successfully")
if initialStatus != nil {
Expect(unstructured.SetNestedField(initialObj.Object, initialStatus, "status")).To(Succeed(), "should be able to restore initial status")
Expect(k8sClient.Status().Update(ctx, initialObj)).ToNot(HaveOccurred(), "initial object status should update successfully")
}
// Fetch the object we just created from the API.
gotObj := newEmptyUnstructuredFrom(initialObj)
Expect(k8sClient.Get(ctx, objectKey(initialObj), gotObj))
updatedObj, err := newUnstructuredFrom(in.updated)
Expect(err).ToNot(HaveOccurred(), "updated data should be a valid Kubernetes YAML resource")
updatedObjStatus, ok, err := unstructured.NestedFieldNoCopy(updatedObj.Object, "status")
Expect(err).ToNot(HaveOccurred())
if ok {
Expect(updatedObjStatus).ToNot(BeNil())
}
// The updated object needs the following fields copied over.
updatedObj.SetName(gotObj.GetName())
updatedObj.SetNamespace(gotObj.GetNamespace())
updatedObj.SetResourceVersion(gotObj.GetResourceVersion())
err = k8sClient.Update(ctx, updatedObj)
if in.expectedError != "" {
Expect(err).To(MatchError(ContainSubstring(in.expectedError)))
return
}
Expect(err).ToNot(HaveOccurred())
if updatedObjStatus != nil {
Expect(unstructured.SetNestedField(updatedObj.Object, updatedObjStatus, "status")).To(Succeed(), "should be able to restore updated status")
err := k8sClient.Status().Update(ctx, updatedObj)
if in.expectedStatusError != "" {
Expect(err).To(MatchError(ContainSubstring(in.expectedStatusError)))
return
}
Expect(err).ToNot(HaveOccurred())
}
Expect(k8sClient.Get(ctx, objectKey(initialObj), gotObj))
expectedObj, err := newUnstructuredFrom(in.expected)
Expect(err).ToNot(HaveOccurred(), "expected data should be a valid Kubernetes YAML resource when no expected error is provided")
// Ensure the name and namespace match.
// The IgnoreAutogeneratedMetadata will ignore any additional meta set in the API.
expectedObj.SetName(gotObj.GetName())
expectedObj.SetNamespace(gotObj.GetNamespace())
Expect(gotObj).To(komega.EqualObject(expectedObj, komega.IgnoreAutogeneratedMetadata))
}
// First argument to the table is the test function.
tableEntries := []interface{}{assertOnUpdate}
// Convert the test specs into table entries
for _, testEntry := range onUpdateTests {
tableEntries = append(tableEntries, Entry(testEntry.Name, onUpdateTableInput{
initial: []byte(testEntry.Initial),
updated: []byte(testEntry.Updated),
expected: []byte(testEntry.Expected),
expectedError: testEntry.ExpectedError,
expectedStatusError: testEntry.ExpectedStatusError,
}))
}
if len(tableEntries) > 1 {
DescribeTable("On Update", tableEntries...)
}
}
// newUnstructuredsFor creates a set of unstructured resources for each version of the CRD.
// This allows us to ensure all CR instances are deleted after each test.
func newUnstructuredsFor(crd *apiextensionsv1.CustomResourceDefinition) []*unstructured.Unstructured {
out := []*unstructured.Unstructured{}
for _, version := range crd.Spec.Versions {
out = append(out, newUnstructuredsForVersion(crd, version.Name))
}
return out
}
// newUnstructuredsForVersion creates an unstructured resource for the CRD at a given version.
func newUnstructuredsForVersion(crd *apiextensionsv1.CustomResourceDefinition, version string) *unstructured.Unstructured {
u := &unstructured.Unstructured{}
u.SetAPIVersion(fmt.Sprintf("%s/%s", crd.Spec.Group, version))
u.SetKind(crd.Spec.Names.Kind)
return u
}
// newUnstructuredFrom unmarshals the raw YAML data into an unstructured,
// and then sets the namespace and generateName ahead of the test.
func newUnstructuredFrom(raw []byte) (*unstructured.Unstructured, error) {
u := &unstructured.Unstructured{}
if err := k8syaml.Unmarshal(raw, &u.Object); err != nil {
return nil, fmt.Errorf("could not unmarshal raw YAML: %w", err)
}
// Names should be unique for each test so ensure we generate a name
u.SetGenerateName("test-")
// We need to have a namespace, use the default.
u.SetNamespace("default")
return u, nil
}
// newEmptyUnstructuredFrom creates a new unstructured with the same GVK as the input object,
// all other fields are cleared.
func newEmptyUnstructuredFrom(initial *unstructured.Unstructured) *unstructured.Unstructured {
u := &unstructured.Unstructured{}
if initial != nil {
u.GetObjectKind().SetGroupVersionKind(initial.GetObjectKind().GroupVersionKind())
}
return u
}
// objectKey extracts a client.ObjectKey from the given object.
func objectKey(obj client.Object) client.ObjectKey {
return client.ObjectKey{Namespace: obj.GetNamespace(), Name: obj.GetName()}
}
func loadCRDFromFile(filename string) (*apiextensionsv1.CustomResourceDefinition, error) {
raw, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("could not load CRD: %w", err)
}
crd := &apiextensionsv1.CustomResourceDefinition{}
if err := yaml.Unmarshal(raw, crd); err != nil {
return nil, fmt.Errorf("could not unmarshal CRD: %w", err)
}
return crd, nil
}
// loadVersionedCRD loads the CRD and removes any version schema that is not the current suite
// version. This allows testing of CRDs for versions that are not currently the storage version.
func loadVersionedCRD(suiteSpec SuiteSpec, crdFilename string) (*apiextensionsv1.CustomResourceDefinition, error) {
crd, err := loadCRDFromFile(crdFilename)
if err != nil {
return nil, fmt.Errorf("could not load CRD: %w", err)
}
if suiteSpec.Version == "" {
return crd, nil
}
crdVersions := []apiextensionsv1.CustomResourceDefinitionVersion{}
for _, version := range crd.Spec.Versions {
if version.Name != suiteSpec.Version {
continue
}
version.Storage = true
version.Served = true
crdVersions = append(crdVersions, version)
}
if len(crdVersions) == 0 {
return nil, fmt.Errorf("could not find CRD version matching version %s", suiteSpec.Version)
}
crd.Spec.Versions = crdVersions
return crd, nil
}
// generateSuiteName prepends the specified suite name with the GVR string
// for the CRD under test.
func generateSuiteName(suiteSpec SuiteSpec, crdFilename string) (string, error) {
crd, err := loadCRDFromFile(crdFilename)
if err != nil {
return "", fmt.Errorf("could not load CRD: %w", err)
}
featureSet := crd.Annotations["release.openshift.io/feature-set"]
clusterProfiles := clusterProfilesShortNamesFrom(crd.Annotations)
filename := filepath.Base(crdFilename)
gvr := schema.GroupVersionResource{
Group: crd.Spec.Group,
Resource: crd.Spec.Names.Plural,
Version: suiteSpec.Version,
}
return fmt.Sprintf(
"[%s][ClusterProfiles=%v][FeatureSet=%q][FeatureGate=%v][File=%v] %s",
gvr.String(),
strings.Join(clusterProfiles.List(), ","),
featureSet,
suiteSpec.FeatureGate,
filename,
suiteSpec.Name,
), nil
}
// getSuiteSpecTestVersion is used to populate the test suites version
// field when not set.
// This is then used to set storage and served versions as well as
// to generate the test suite name.
func getSuiteSpecTestVersion(suiteSpec SuiteSpec) (string, error) {
version := ""
for _, file := range suiteSpec.PerTestRuntimeInfo.CRDFilenames {
crd, err := loadCRDFromFile(file)
if err != nil {
return "", err
}
if len(crd.Spec.Versions) > 1 {
return "", fmt.Errorf("too many versions, specify one in the suite")
}
if len(version) == 0 {
version = crd.Spec.Versions[0].Name
continue
}
if version != crd.Spec.Versions[0].Name {
return "", fmt.Errorf("too many versions, specify one in the suite. Saw %v and %v", version, crd.Spec.Versions[0].Name)
}
}
return version, nil
}