Skip to content

Commit

Permalink
Improve our Pipeline tests (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
theunrepentantgeek authored Jul 22, 2021
1 parent 1e5b3d2 commit ad2459e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 81 deletions.
42 changes: 36 additions & 6 deletions hack/generator/pkg/codegen/pipeline_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/sebdah/goldie/v2"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
"github.com/Azure/azure-service-operator/hack/generator/pkg/codegen/pipeline"
"github.com/Azure/azure-service-operator/hack/generator/pkg/config"

. "github.com/onsi/gomega"
Expand All @@ -25,14 +26,32 @@ func TestNewARMCodeGeneratorFromConfigCreatesRightPipeline(t *testing.T) {

idFactory := astmodel.NewIdentifierFactory()
configuration := config.NewConfiguration()
configuration.Pipeline = config.GenerationPipelineAzure

codegen, err := NewCodeGeneratorFromConfig(configuration, idFactory)
g.Expect(err).To(BeNil())
codegen, err := NewTargetedCodeGeneratorFromConfig(configuration, idFactory, pipeline.ARMTarget)
g.Expect(err).To(Succeed())

result := writePipeline("Expected Pipeline Stages for ARM Code Generation", codegen)

gold.Assert(t, "ARMCodeGeneratorPipeline", result)
// When reviewing changes to the golden file, ensure they make sense in the context of an operator built to work
// against Azure ARM - we don't want to see any Crossplane specific stages showing up there.
gold.Assert(t, t.Name(), result)
}

func TestNewCrossplaneCodeGeneratorFromConfigCreatesRightPipeline(t *testing.T) {
gold := goldie.New(t)
g := NewGomegaWithT(t)

idFactory := astmodel.NewIdentifierFactory()
configuration := config.NewConfiguration()

codegen, err := NewTargetedCodeGeneratorFromConfig(configuration, idFactory, pipeline.CrossplaneTarget)
g.Expect(err).To(Succeed())

result := writePipeline("Expected Pipeline Stages for ARM Code Generation", codegen)

// When reviewing changes to the golden file, ensure they make sense in the context of an operator built to work
// with Crossplane - we don't want to see any Azure ARM specific stages showing up there.
gold.Assert(t, t.Name(), result)
}

func TestNewTestCodeGeneratorCreatesRightPipeline(t *testing.T) {
Expand All @@ -45,7 +64,9 @@ func TestNewTestCodeGeneratorCreatesRightPipeline(t *testing.T) {

result := writePipeline("Expected Pipeline Stages for Test Code Generation", codegen)

gold.Assert(t, "TestCodeGeneratorPipeline", result)
// When reviewing changes to the golden file, ensure they make sense in the context of the tests we are running
// of the entire pipeline; you may need to explicity exclude some stages.
gold.Assert(t, t.Name(), result)
}

func writePipeline(title string, codegen *CodeGenerator) []byte {
Expand All @@ -54,6 +75,15 @@ func writePipeline(title string, codegen *CodeGenerator) []byte {
fmt.Fprintln(&b, title)
fmt.Fprintln(&b, strings.Repeat("-", len(title)))

idWidth := 0
for _, s := range codegen.pipeline {
if len(s.Id()) > idWidth {
idWidth = len(s.Id())
}
}

format := fmt.Sprintf("%%-%ds %%-10s %%s\n", idWidth+4)

for _, s := range codegen.pipeline {
targets := ""
for _, t := range s.Targets() {
Expand All @@ -64,7 +94,7 @@ func writePipeline(title string, codegen *CodeGenerator) []byte {
targets = targets + t.String()
}

fmt.Fprintf(&b, "%-35s %-10s %s\n", s.Id(), targets, s.Description())
fmt.Fprintf(&b, format, s.Id(), targets, s.Description())
}

return b.Bytes()
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Expected Pipeline Stages for ARM Code Generation
------------------------------------------------
loadSchema Load and walk schema
addStatusFromSwagger Add information from Swagger specs for 'status' fields
allof-anyof-objects Convert allOf and oneOf to object types
flattenResources Flatten nested resource types
augmentSpecWithStatus Merges information from Status into Spec
stripUnreferenced Strip unreferenced types
nameTypes Name inner types for CRD
propertyRewrites Modify property types using configured transforms
determineResourceOwnership Determine ARM resource relationships
removeAliases Remove type aliases
collapseCrossGroupReferences Finds and removes cross group references
pluralizeNames Improve resource pluralization
stripUnreferenced Strip unreferenced types
assertTypesStructureValid Verify that all local TypeNames refer to a type
removeEmbeddedResources azure Remove properties that point to embedded resources. Only removes structural aspects of embedded resources, Id/ARMId references are retained.
filterTypes Apply export filters to reduce the number of generated types
stripUnreferenced Strip unreferenced types
replaceAnyTypeWithJSON Replace properties using interface{} with arbitrary JSON
addCrossResourceReferences azure Replace cross-resource references with genruntime.ResourceReference
reportTypesAndVersions azure Generate reports on types and versions in each package
createArmTypes azure Create types for interaction with ARM
applyArmConversionInterface azure Add ARM conversion interfaces to Kubernetes types
applyKubernetesResourceInterface azure Add the KubernetesResource interface to every resource
flattenProperties Apply flattening to properties marked for flattening
stripUnreferenced Strip unreferenced types
createConversionGraph azure Create the graph of conversions between versions of each resource group
injectOriginalVersionFunction azure Inject the function OriginalVersion() into each Spec type
createStorageTypes azure Create storage versions of CRD types
injectOriginalVersionProperty azure Inject the property OriginalVersion into each Storage Spec type
injectPropertyAssignmentFunctions azure Inject property assignment functions AssignFrom() and AssignTo() into resources and objects
injectOriginalGVKFunction azure Inject the function OriginalGVK() into each Resource type
simplifyDefinitions Flatten definitions by removing wrapper types
jsonTestCases azure Add test cases to verify JSON serialization
markStorageVersion Mark the latest version of each resource as the storage version
rogueCheck Check for rogue definitions using AnyTypes
ensureArmTypeExistsForEveryType azure Check that an ARM type exists for both Spec and Status of each resource
deleteGenerated Delete generated code from .
exportPackages Export packages to "."
exportControllerResourceRegistrations azure Export resource registrations to ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Expected Pipeline Stages for ARM Code Generation
------------------------------------------------
loadSchema Load and walk schema
addStatusFromSwagger Add information from Swagger specs for 'status' fields
allof-anyof-objects Convert allOf and oneOf to object types
flattenResources Flatten nested resource types
augmentSpecWithStatus Merges information from Status into Spec
stripUnreferenced Strip unreferenced types
nameTypes Name inner types for CRD
propertyRewrites Modify property types using configured transforms
determineResourceOwnership Determine ARM resource relationships
removeAliases Remove type aliases
collapseCrossGroupReferences Finds and removes cross group references
pluralizeNames Improve resource pluralization
stripUnreferenced Strip unreferenced types
assertTypesStructureValid Verify that all local TypeNames refer to a type
filterTypes Apply export filters to reduce the number of generated types
stripUnreferenced Strip unreferenced types
replaceAnyTypeWithJSON Replace properties using interface{} with arbitrary JSON
flattenProperties Apply flattening to properties marked for flattening
stripUnreferenced Strip unreferenced types
addCrossplaneOwnerProperties crossplane Add the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource
addCrossplaneForProviderProperty crossplane Add a 'ForProvider' property on every spec
addCrossplaneAtProviderProperty crossplane Add an 'AtProvider' property on every status
addCrossplaneEmbeddedResourceSpec crossplane Add an embedded runtimev1alpha1.ResourceSpec to every spec type
addCrossplaneEmbeddedResourceStatus crossplane Add an embedded runtimev1alpha1.ResourceStatus to every status type
simplifyDefinitions Flatten definitions by removing wrapper types
markStorageVersion Mark the latest version of each resource as the storage version
rogueCheck Check for rogue definitions using AnyTypes
deleteGenerated Delete generated code from .
exportPackages Export packages to "."
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Expected Pipeline Stages for Test Code Generation
-------------------------------------------------
loadTestSchema Load and walk schema (test)
addStatusFromSwagger Add information from Swagger specs for 'status' fields
allof-anyof-objects Convert allOf and oneOf to object types
flattenResources Flatten nested resource types
augmentSpecWithStatus Merges information from Status into Spec
stripUnused Strip unused types for test
nameTypes Name inner types for CRD
propertyRewrites Modify property types using configured transforms
determineResourceOwnership Determine ARM resource relationships
removeAliases Remove type aliases
pluralizeNames Improve resource pluralization
stripUnused Strip unused types for test
assertTypesStructureValid Verify that all local TypeNames refer to a type
filterTypes Apply export filters to reduce the number of generated types
stripUnused Strip unused types for test
replaceAnyTypeWithJSON Replace properties using interface{} with arbitrary JSON
addCrossResourceReferences azure Replace cross-resource references with genruntime.ResourceReference
applyKubernetesResourceInterface azure Add the KubernetesResource interface to every resource
flattenProperties Apply flattening to properties marked for flattening
stripUnused Strip unused types for test
createConversionGraph azure Create the graph of conversions between versions of each resource group
simplifyDefinitions Flatten definitions by removing wrapper types
jsonTestCases azure Add test cases to verify JSON serialization
markStorageVersion Mark the latest version of each resource as the storage version
ensureArmTypeExistsForEveryType azure Check that an ARM type exists for both Spec and Status of each resource
exportTestPackages Export packages for test
exportControllerResourceRegistrations azure Export resource registrations to ""

0 comments on commit ad2459e

Please sign in to comment.