-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathgolden_files_test.go
438 lines (366 loc) · 13.4 KB
/
golden_files_test.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package codegen
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/go-logr/logr"
"github.com/rotisserie/eris"
"github.com/sebdah/goldie/v2"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/codegen/pipeline"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/config"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/jsonast"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/test"
)
var goldenTestPackageReference = astmodel.MakeLocalPackageReference(test.GoModulePrefix, "test", astmodel.GeneratorVersion, "2020-01-01")
type GoldenTestConfig struct {
HasARMResources bool `yaml:"hasArmResources"`
InjectEmbeddedStruct bool `yaml:"injectEmbeddedStruct"`
Pipelines []config.GenerationPipeline `yaml:"pipelines"`
}
func makeDefaultTestConfig() GoldenTestConfig {
return GoldenTestConfig{
HasARMResources: false,
InjectEmbeddedStruct: false,
Pipelines: []config.GenerationPipeline{config.GenerationPipelineAzure},
}
}
func loadTestConfig(path string) (GoldenTestConfig, error) {
result := makeDefaultTestConfig()
fileBytes, err := os.ReadFile(path)
if err != nil {
// If the file doesn't exist we just use the default
if os.IsNotExist(err) {
return result, nil
}
return result, err
}
err = yaml.Unmarshal(fileBytes, &result)
if err != nil {
return result, eris.Wrapf(err, "unmarshalling golden config %s", path)
}
return result, nil
}
func makeEmbeddedTestTypeDefinition() astmodel.TypeDefinition {
name := astmodel.MakeInternalTypeName(goldenTestPackageReference, "EmbeddedTestType")
t := astmodel.NewObjectType()
t = t.WithProperty(astmodel.NewPropertyDefinition("FancyProp", "fancyProp", astmodel.IntType))
return astmodel.MakeTypeDefinition(name, t)
}
func injectEmbeddedStructType() *pipeline.Stage {
return pipeline.NewStage(
"injectEmbeddedStructType",
"Injects an embedded struct into each object",
func(ctx context.Context, state *pipeline.State) (*pipeline.State, error) {
defs := make(astmodel.TypeDefinitionSet)
embeddedTypeDef := makeEmbeddedTestTypeDefinition()
for _, def := range state.Definitions() {
if astmodel.IsObjectDefinition(def) {
result, err := def.ApplyObjectTransformation(func(objectType *astmodel.ObjectType) (astmodel.Type, error) {
prop := astmodel.NewPropertyDefinition(
"",
",inline",
embeddedTypeDef.Name())
return objectType.WithEmbeddedProperty(prop)
})
if err != nil {
return nil, err
}
defs.Add(result)
} else {
defs.Add(def)
}
}
defs.Add(embeddedTypeDef)
return state.WithDefinitions(defs), nil
})
}
func runGoldenTest(t *testing.T, path string, testConfig GoldenTestConfig) {
ctx := context.Background()
for _, p := range testConfig.Pipelines {
p := p
testName := strings.TrimPrefix(t.Name(), "TestGolden/")
// Append pipeline name at the end of file name if there is more than one pipeline under test
if len(testConfig.Pipelines) > 1 {
testName = filepath.Join(filepath.Dir(testName), fmt.Sprintf("%s_%s", filepath.Base(testName), string(p)))
}
t.Run(string(p), func(t *testing.T) {
t.Parallel()
codegen, err := NewTestCodeGenerator(testName, path, t, testConfig, p)
if err != nil {
t.Fatalf("failed to create code generator: %s", err)
}
err = codegen.Generate(ctx, logr.Discard())
if err != nil {
t.Fatalf("codegen failed: %s", err)
}
})
}
}
func NewTestCodeGenerator(
testName string,
path string,
t *testing.T,
testConfig GoldenTestConfig,
genPipeline config.GenerationPipeline,
) (*CodeGenerator, error) {
idFactory := astmodel.NewIdentifierFactory()
cfg := config.NewConfiguration()
cfg.SetGoModulePath(test.GoModulePrefix)
pipelineTarget, err := pipeline.TranslatePipelineToTarget(genPipeline)
if err != nil {
return nil, err
}
codegen, err := NewTargetedCodeGeneratorFromConfig(cfg, idFactory, pipelineTarget, logr.Discard())
if err != nil {
return nil, err
}
// TODO: This isn't as clean as would be liked -- should we remove panic from RemoveStages?
switch genPipeline {
case config.GenerationPipelineAzure:
codegen.RemoveStages(
pipeline.DeleteGeneratedCodeStageID,
pipeline.CheckForAnyTypeStageID,
pipeline.CreateResourceExtensionsStageID,
pipeline.ReportOnTypesAndVersionsStageID,
pipeline.ReportResourceVersionsStageID,
pipeline.ReportResourceStructureStageId)
if !testConfig.HasARMResources {
codegen.RemoveStages(
pipeline.CreateARMTypesStageID,
pipeline.PruneResourcesWithLifecycleOwnedByParentStageID,
pipeline.ApplyARMConversionInterfaceStageID)
// These stages treat the collection of types as a graph of types rooted by a resource type.
// In the degenerate case where there are no resources it behaves the same as stripUnreferenced - removing
// all types. Remove it in phases that have no resources to avoid this.
codegen.RemoveStages(
pipeline.RemoveEmbeddedResourcesStageID,
pipeline.CollapseCrossGroupReferencesStageID,
pipeline.TransformCrossResourceReferencesStageID)
codegen.ReplaceStage(pipeline.StripUnreferencedTypeDefinitionsStageID, stripUnusedTypesPipelineStage())
} else {
codegen.RemoveStages(pipeline.ApplyCrossResourceReferencesFromConfigStageID)
codegen.ReplaceStage(pipeline.TransformCrossResourceReferencesStageID, addCrossResourceReferencesForTest(idFactory))
}
case config.GenerationPipelineCrossplane:
codegen.RemoveStages(
pipeline.DeleteGeneratedCodeStageID,
pipeline.CheckForAnyTypeStageID,
pipeline.ReportResourceVersionsStageID,
pipeline.ReportResourceStructureStageId)
if !testConfig.HasARMResources {
codegen.ReplaceStage(pipeline.StripUnreferencedTypeDefinitionsStageID, stripUnusedTypesPipelineStage())
}
default:
return nil, eris.Errorf("unknown pipeline kind %q", string(genPipeline))
}
codegen.ReplaceStage(pipeline.LoadTypesStageID, loadTestSchemaIntoTypes(idFactory, cfg, path))
codegen.ReplaceStage(pipeline.ExportPackagesStageID, exportPackagesTestPipelineStage(t, testName))
if testConfig.InjectEmbeddedStruct {
codegen.InjectStageAfter(pipeline.DetermineResourceOwnershipStageId, injectEmbeddedStructType())
}
codegen.RemoveStages(
pipeline.ApplyExportFiltersStageID, // Don't want any filtering of resources during tests
)
//TODO: Enable this check once we fix up the test pipeline so that all the dependencies pass
//if err := codegen.verifyPipeline(); err != nil {
// return nil, err
//}
return codegen, nil
}
// TODO: we still need to replace this with openapi instead of jsonschema
func loadTestSchemaIntoTypes(
idFactory astmodel.IdentifierFactory,
configuration *config.Configuration,
path string,
) *pipeline.Stage {
// TODO(matthchr): ???
// source := configuration.SchemaRoot
return pipeline.NewStage(
"loadTestSchema",
"Load and walk schema (test)",
func(ctx context.Context, state *pipeline.State) (*pipeline.State, error) {
inputFile, err := os.ReadFile(path)
if err != nil {
return nil, eris.Wrapf(err, "cannot read golden test input file")
}
loader := gojsonschema.NewSchemaLoader()
schema, err := loader.Compile(gojsonschema.NewBytesLoader(inputFile))
if err != nil {
return nil, eris.Wrapf(err, "could not compile input")
}
scanner := jsonast.NewSchemaScanner(idFactory, configuration, logr.Discard())
schemaAbstraction := jsonast.MakeGoJSONSchema(schema.Root(), configuration.MakeLocalPackageReference, idFactory)
_, err = scanner.GenerateAllDefinitions(ctx, schemaAbstraction)
if err != nil {
return nil, eris.Wrapf(err, "failed to walk JSON schema")
}
return state.WithDefinitions(scanner.Definitions()), nil
})
}
func exportPackagesTestPipelineStage(t *testing.T, testName string) *pipeline.Stage {
g := goldie.New(t)
return pipeline.NewStage(
"exportTestPackages",
"Export packages for test",
func(ctx context.Context, state *pipeline.State) (*pipeline.State, error) {
if len(state.Definitions()) == 0 {
t.Fatalf("defs was empty")
}
// Create package definitions
pkgs := make(map[astmodel.InternalPackageReference]*astmodel.PackageDefinition)
nonStoragePackageCount := 0
for _, def := range state.Definitions() {
ref := def.Name().InternalPackageReference()
pkg, ok := pkgs[ref]
if !ok {
pkg = astmodel.NewPackageDefinition(ref)
pkgs[ref] = pkg
if !astmodel.IsStoragePackageReference(ref) {
nonStoragePackageCount++
}
}
pkg.AddDefinition(def)
}
// Export each package definition
// We don't export storage packages as they're tested elsewhere
// If there's more than one package to export, we suffix with the package name
for ref, pkg := range pkgs {
if astmodel.IsStoragePackageReference(ref) {
continue
}
fileDef := astmodel.NewFileDefinition(ref, pkg.Definitions().AsSlice(), pkgs)
buf := &bytes.Buffer{}
fileWriter := astmodel.NewGoSourceFileWriter(fileDef)
err := fileWriter.SaveToWriter(buf)
if err != nil {
t.Fatalf("could not generate file: %s", err)
}
fileName := testName
if nonStoragePackageCount > 1 {
fileName = fileName + "_" + ref.PackageName()
}
g.Assert(t, fileName, buf.Bytes())
}
return state, nil
})
}
func stripUnusedTypesPipelineStage() *pipeline.Stage {
return pipeline.NewStage(
"stripUnused",
"Strip unused types for test",
func(ctx context.Context, state *pipeline.State) (*pipeline.State, error) {
// The golden files always generate a top-level Test type - mark
// that as the root.
roots := astmodel.NewTypeNameSet(astmodel.MakeInternalTypeName(goldenTestPackageReference, "Test"))
defs, err := pipeline.StripUnusedDefinitions(roots, state.Definitions())
if err != nil {
return nil, eris.Wrapf(err, "could not strip unused types")
}
return state.WithDefinitions(defs), nil
})
}
// TODO: Ideally we wouldn't need a test specific function here, but currently
// TODO: we're hard-coding references, and even if we were sourcing them from Swagger
// TODO: we have no way to give Swagger to the golden files tests currently.
func addCrossResourceReferencesForTest(idFactory astmodel.IdentifierFactory) *pipeline.Stage {
return pipeline.NewStage(
pipeline.TransformCrossResourceReferencesStageID,
"Add cross resource references for test",
func(ctx context.Context, state *pipeline.State) (*pipeline.State, error) {
defs := make(astmodel.TypeDefinitionSet)
isCrossResourceReference := func(
_ astmodel.InternalTypeName,
prop *astmodel.PropertyDefinition,
) pipeline.ARMIDPropertyClassification {
ref := pipeline.DoesPropertyLookLikeARMReference(prop)
if ref {
return pipeline.ARMIDPropertyClassificationSet
}
return pipeline.ARMIDPropertyClassificationUnspecified
}
crossReferenceVisitor := pipeline.MakeARMIDPropertyTypeVisitor(isCrossResourceReference, logr.Discard())
resourceReferenceVisitor := pipeline.MakeARMIDToResourceReferenceTypeVisitor(idFactory)
for _, def := range state.Definitions() {
// Skip Status types
// TODO: we need flags
if def.Name().IsStatus() {
defs.Add(def)
continue
}
updatedDef, err := crossReferenceVisitor.VisitDefinition(def, def.Name())
if err != nil {
return nil, eris.Wrapf(err, "crossReferenceVisitor failed visiting %q", def.Name())
}
updatedDef, err = resourceReferenceVisitor.VisitDefinition(updatedDef, def.Name())
if err != nil {
return nil, eris.Wrapf(err, "resourceReferenceVisitor failed visiting %q", def.Name())
}
defs.Add(updatedDef)
}
return state.WithDefinitions(defs), nil
})
}
func TestGolden(t *testing.T) {
t.Parallel()
type Test struct {
name string
path string
}
testGroups := make(map[string][]Test)
// find all input .json files
testDataRoot := "testdata"
err := filepath.Walk(testDataRoot, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".json" {
groupName := filepath.Base(filepath.Dir(path))
testName := strings.TrimSuffix(filepath.Base(path), ".json")
testGroups[groupName] = append(testGroups[groupName], Test{testName, path})
}
return nil
})
if err != nil {
t.Fatalf("Error enumerating files: %s", err)
}
// run all tests
// safety check that there are at least a few groups
minExpectedTestGroups := 3
if len(testGroups) < minExpectedTestGroups {
t.Fatalf("Expected at least %d test groups, found: %d", minExpectedTestGroups, len(testGroups))
}
// Skip linting next line, see https://github.com/kunwardeep/paralleltest/issues/14. Linter doesn't support maps
// nolint:paralleltest
for groupName, fs := range testGroups {
fs := fs
groupName := groupName
configPath := fmt.Sprintf("%s/%s/config.yaml", testDataRoot, groupName)
testConfig, err := loadTestConfig(configPath)
if err != nil {
t.Fatalf("could not load test config: %s", err)
}
t.Run(groupName, func(t *testing.T) {
t.Parallel()
// safety check that there is at least one test in each group
if len(fs) == 0 {
t.Fatalf("Test group %s was empty", groupName)
}
for _, f := range fs {
f := f
testConfig := testConfig
t.Run(f.name, func(t *testing.T) {
t.Parallel()
runGoldenTest(t, f.path, testConfig)
})
}
})
}
}