Skip to content

Commit

Permalink
Add round trip tests of Property Assignment methods (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
theunrepentantgeek authored Aug 25, 2021
1 parent 9d1f49f commit 452439c
Show file tree
Hide file tree
Showing 22 changed files with 2,686 additions and 33 deletions.
4 changes: 4 additions & 0 deletions hack/generator/pkg/astmodel/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ func (objectType *ObjectType) TestCases() []TestCase {
result = append(result, tc)
}

sort.Slice(result, func(i int, j int) bool {
return result[i].Name() < result[j].Name()
})

return result
}

Expand Down
7 changes: 5 additions & 2 deletions hack/generator/pkg/astmodel/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ func (resource *ResourceType) WithStatus(statusType Type) *ResourceType {
}

if specResource, ok := statusType.(*ResourceType); ok {
// type is a resource, take its StatusType instead
// so we don't nest resources
// type is a resource, take its StatusType instead, so we don't nest resources
return resource.WithStatus(specResource.StatusType())
}

Expand Down Expand Up @@ -226,6 +225,10 @@ func (resource *ResourceType) TestCases() []TestCase {
result = append(result, tc)
}

sort.Slice(result, func(i int, j int) bool {
return result[i].Name() < result[j].Name()
})

return result
}

Expand Down
1 change: 1 addition & 0 deletions hack/generator/pkg/codegen/code_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func createAllPipelineStages(idFactory astmodel.IdentifierFactory, configuration

pipeline.SimplifyDefinitions(),
pipeline.InjectJsonSerializationTests(idFactory).UsedFor(pipeline.ARMTarget),
pipeline.InjectPropertyAssignmentTests(idFactory).UsedFor(pipeline.ARMTarget),

pipeline.MarkStorageVersion(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package pipeline

import (
"context"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -48,19 +47,11 @@ func TestInjectPropertyAssignmentFunctions(t *testing.T) {

state := NewState().WithTypes(types)

// Use CreateConversionGraph to create the conversion graph needed prior to injecting property assignment funcs
createConversionGraphStage := CreateConversionGraph()
state, err := createConversionGraphStage.Run(context.TODO(), state)
g.Expect(err).To(Succeed())

// Next, create storage types so we have targets for the assignment functions
createStorageTypesStage := CreateStorageTypes()
state, err = createStorageTypesStage.Run(context.TODO(), state)
g.Expect(err).To(Succeed())

// Now run our stage and inject property assignment functions
injectFunctions := InjectPropertyAssignmentFunctions(idFactory)
finalState, err := injectFunctions.Run(context.TODO(), state)
finalState, err := RunTestPipeline(
state,
CreateConversionGraph(),
CreateStorageTypes(),
InjectPropertyAssignmentFunctions(idFactory))
g.Expect(err).To(Succeed())

test.AssertPackagesGenerateExpectedCode(t, finalState.Types())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package pipeline

import (
"github.com/pkg/errors"
"golang.org/x/net/context"
kerrors "k8s.io/apimachinery/pkg/util/errors"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
"github.com/Azure/azure-service-operator/hack/generator/pkg/functions"
"github.com/Azure/azure-service-operator/hack/generator/pkg/testcases"
)

// InjectPropertyAssignmentTestsID is the unique identifier for this stage
const InjectPropertyAssignmentTestsID = "injectPropertyAssignmentTestCases"

func InjectPropertyAssignmentTests(idFactory astmodel.IdentifierFactory) Stage {

stage := MakeStage(
InjectPropertyAssignmentTestsID,
"Add test cases to verify PropertyAssignment functions",
func(ctx context.Context, state *State) (*State, error) {
factory := makePropertyAssignmentTestCaseFactory(idFactory)
modifiedTypes := make(astmodel.Types)
var errs []error
for _, d := range state.Types() {
if factory.NeedsTest(d) {
updated, err := factory.AddTestTo(d)
if err != nil {
errs = append(errs, err)
} else {
modifiedTypes[updated.Name()] = updated
}
}
}

if len(errs) > 0 {
return nil, kerrors.NewAggregate(errs)
}

return state.WithTypes(state.Types().OverlayWith(modifiedTypes)), nil
})

stage.RequiresPrerequisiteStages(
InjectPropertyAssignmentFunctionsStageID, // Need PropertyAssignmentFunctions to test
InjectJsonSerializationTestsID, // We reuse the generators from the JSON tests
)

return stage
}

type propertyAssignmentTestCaseFactory struct {
injector *astmodel.TestCaseInjector
idFactory astmodel.IdentifierFactory
}

func makePropertyAssignmentTestCaseFactory(idFactory astmodel.IdentifierFactory) propertyAssignmentTestCaseFactory {
return propertyAssignmentTestCaseFactory{
injector: astmodel.NewTestCaseInjector(),
idFactory: idFactory,
}
}

func (s *propertyAssignmentTestCaseFactory) NeedsTest(def astmodel.TypeDefinition) bool {
container, ok := astmodel.AsFunctionContainer(def.Type())
if !ok {
return false
}

for _, fn := range container.Functions() {
if _, ok := fn.(*functions.PropertyAssignmentFunction); ok {
return true
}
}

return false
}

func (s *propertyAssignmentTestCaseFactory) AddTestTo(def astmodel.TypeDefinition) (astmodel.TypeDefinition, error) {
container, ok := astmodel.AsFunctionContainer(def.Type())
if !ok {
return astmodel.TypeDefinition{}, errors.Errorf("expected %s to be a function container", def.Name())
}

testCase := testcases.NewPropertyAssignmentTestCase(def.Name(), container, s.idFactory)
return s.injector.Inject(def, testCase)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package pipeline

import (
"testing"

. "github.com/onsi/gomega"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
"github.com/Azure/azure-service-operator/hack/generator/pkg/test"
)

func TestInjectPropertyAssignmentTests(t *testing.T) {
g := NewGomegaWithT(t)

idFactory := astmodel.NewIdentifierFactory()
// Test Resource V1

specV1 := test.CreateSpec(
test.Pkg2020,
"Person",
test.FullNameProperty,
test.FamilyNameProperty,
test.KnownAsProperty)
statusV1 := test.CreateStatus(test.Pkg2020, "Person")
resourceV1 := test.CreateResource(test.Pkg2020, "Person", specV1, statusV1)

// Test Resource V2

specV2 := test.CreateSpec(
test.Pkg2021,
"Person",
test.FullNameProperty,
test.FamilyNameProperty,
test.KnownAsProperty,
test.ResidentialAddress2021,
test.PostalAddress2021)
statusV2 := test.CreateStatus(test.Pkg2021, "Person")
resourceV2 := test.CreateResource(test.Pkg2021, "Person", specV2, statusV2)

types := make(astmodel.Types)
types.AddAll(resourceV1, specV1, statusV1, resourceV2, specV2, statusV2, test.Address2021)

state := NewState().WithTypes(types)

finalState, err := RunTestPipeline(
state,
CreateConversionGraph(),
CreateStorageTypes(),
InjectPropertyAssignmentFunctions(idFactory),
InjectJsonSerializationTests(idFactory),
InjectPropertyAssignmentTests(idFactory))
g.Expect(err).To(Succeed())

test.AssertPackagesGenerateExpectedCode(t, finalState.Types())
}
Loading

0 comments on commit 452439c

Please sign in to comment.