diff --git a/hack/generator/pkg/codegen/pipeline_factory_test.go b/hack/generator/pkg/codegen/pipeline_factory_test.go index deab9739f70..8d1713d1d02 100644 --- a/hack/generator/pkg/codegen/pipeline_factory_test.go +++ b/hack/generator/pkg/codegen/pipeline_factory_test.go @@ -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" @@ -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) { @@ -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 { @@ -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() { @@ -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() diff --git a/hack/generator/pkg/codegen/testdata/ARMCodeGeneratorPipeline.golden b/hack/generator/pkg/codegen/testdata/ARMCodeGeneratorPipeline.golden deleted file mode 100644 index 23147f1e5fc..00000000000 --- a/hack/generator/pkg/codegen/testdata/ARMCodeGeneratorPipeline.golden +++ /dev/null @@ -1,46 +0,0 @@ -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 -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 -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 "" diff --git a/hack/generator/pkg/codegen/testdata/TestCodeGeneratorPipeline.golden b/hack/generator/pkg/codegen/testdata/TestCodeGeneratorPipeline.golden deleted file mode 100644 index da49712d2bc..00000000000 --- a/hack/generator/pkg/codegen/testdata/TestCodeGeneratorPipeline.golden +++ /dev/null @@ -1,29 +0,0 @@ -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 "" diff --git a/hack/generator/pkg/codegen/testdata/TestNewARMCodeGeneratorFromConfigCreatesRightPipeline.golden b/hack/generator/pkg/codegen/testdata/TestNewARMCodeGeneratorFromConfigCreatesRightPipeline.golden new file mode 100644 index 00000000000..ce9ebe6c642 --- /dev/null +++ b/hack/generator/pkg/codegen/testdata/TestNewARMCodeGeneratorFromConfigCreatesRightPipeline.golden @@ -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 "" diff --git a/hack/generator/pkg/codegen/testdata/TestNewCrossplaneCodeGeneratorFromConfigCreatesRightPipeline.golden b/hack/generator/pkg/codegen/testdata/TestNewCrossplaneCodeGeneratorFromConfigCreatesRightPipeline.golden new file mode 100644 index 00000000000..02b6861daac --- /dev/null +++ b/hack/generator/pkg/codegen/testdata/TestNewCrossplaneCodeGeneratorFromConfigCreatesRightPipeline.golden @@ -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 "." diff --git a/hack/generator/pkg/codegen/testdata/TestNewTestCodeGeneratorCreatesRightPipeline.golden b/hack/generator/pkg/codegen/testdata/TestNewTestCodeGeneratorCreatesRightPipeline.golden new file mode 100644 index 00000000000..9cae5b2011c --- /dev/null +++ b/hack/generator/pkg/codegen/testdata/TestNewTestCodeGeneratorCreatesRightPipeline.golden @@ -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 ""