Skip to content

Commit

Permalink
Add failure tests for the resolver transformer
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
  • Loading branch information
ulucinar committed Jan 25, 2024
1 parent 1493017 commit 994c07a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
40 changes: 32 additions & 8 deletions pkg/transformers/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/spf13/afero"
"golang.org/x/tools/go/packages/packagestest"
)
Expand All @@ -26,7 +27,8 @@ func TestTransformPackages(t *testing.T) {

// want struct to define the expected outcome for each test case
type want struct {
err error
// errFunc receives the transformed resolver file's path
errFunc func(string) error
transformedPath string
}

Expand All @@ -43,28 +45,46 @@ func TestTransformPackages(t *testing.T) {
args: args{
apiGroupSuffix: "aws.upbound.io",
resolverFilePattern: "zz_generated.resolvers.go",
inputFilePath: "testdata/SuccessfulTransformation.go.txt",
inputFilePath: "testdata/apigatewayv2.resolvers.go.txt",
ignorePackageLoadErrors: true,
patterns: []string{"./testdata"},
},
want: want{
transformedPath: "testdata/SuccessfulTransformation.transformed.go.txt",
transformedPath: "testdata/apigatewayv2.resolvers.transformed.go.txt",
},
},
"TransformationIdempotency": {
reason: "The applied transformation is idempotent, i.e., applying the transformer on an already transformed file does not change the transformed file.",
args: args{
apiGroupSuffix: "aws.upbound.io",
resolverFilePattern: "zz_generated.resolvers.go",
inputFilePath: "testdata/SuccessfulTransformation.transformed.go.txt",
inputFilePath: "testdata/apigatewayv2.resolvers.transformed.go.txt",
ignorePackageLoadErrors: true,
patterns: []string{"./testdata"},
},
want: want{
transformedPath: "testdata/SuccessfulTransformation.transformed.go.txt",
transformedPath: "testdata/apigatewayv2.resolvers.transformed.go.txt",
},
},
"TransformationFailure": {
reason: "The transformation source is not a valid angryjet-generated resolver file: List type is missing for a resolution source.",
args: args{
apiGroupSuffix: "aws.upbound.io",
resolverFilePattern: "zz_generated.resolvers.go",
inputFilePath: "testdata/missing_list_type.resolvers.go.txt",
ignorePackageLoadErrors: true,
patterns: []string{"./testdata"},
},
want: want{
errFunc: func(transformedPath string) error {
return errors.Wrapf(
errors.Wrap(
errors.New(`failed to extract the GVKs for the reference targets. Group: "fake.aws.upbound.io", Version: "testdata", Kind: "Project", List Kind: ""`),
"failed to inspect the resolver file for transformation"),
"failed to transform the resolver file %s", transformedPath)
},
},
},
// Other test cases
}

for name, tc := range cases {
Expand All @@ -83,10 +103,14 @@ func TestTransformPackages(t *testing.T) {

r := NewResolver(memFS, tc.args.apiGroupSuffix, tc.args.ignorePackageLoadErrors, nil, WithLoaderConfig(exported.Config))
err := r.TransformPackages("zz_generated.resolvers.go", tc.args.patterns...)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
var wantErr error
if tc.want.errFunc != nil {
wantErr = tc.want.errFunc(transformedFilePath)
}
if diff := cmp.Diff(wantErr, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nResolver.TransformPackages(...): -wantErr, +gotErr:\n%s", tc.reason, diff)
}
if tc.want.err != nil {
if wantErr != nil {
return
}
if diff := cmp.Diff(readFile(t, afero.NewOsFs(), tc.want.transformedPath, tc.reason),
Expand Down
42 changes: 42 additions & 0 deletions pkg/transformers/testdata/missing_list_type.resolvers.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0
// Code generated by angryjet. DO NOT EDIT.

package v1beta1

import (
"context"
reference "github.com/crossplane/crossplane-runtime/pkg/reference"
resource "github.com/crossplane/upjet/pkg/resource"
errors "github.com/pkg/errors"
client "sigs.k8s.io/controller-runtime/pkg/client"
)

// ResolveReferences of this Feature.
func (mg *Feature) ResolveReferences(ctx context.Context, c client.Reader) error {
r := reference.NewAPIResolver(c, mg)

var rsp reference.ResolutionResponse
var err error

rsp, err = r.Resolve(ctx, reference.ResolutionRequest{
CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Project),
Extract: resource.ExtractParamPath("name", false),
Reference: mg.Spec.ForProvider.ProjectRef,
Selector: mg.Spec.ForProvider.ProjectSelector,
// Please note the missing List type in the below expression
// This is not expected for the generated resolver
// implementations by angryjet.
To: reference.To{
Managed: &Project{},
},
})
if err != nil {
return errors.Wrap(err, "mg.Spec.ForProvider.Project")
}
mg.Spec.ForProvider.Project = reference.ToPtrValue(rsp.ResolvedValue)
mg.Spec.ForProvider.ProjectRef = rsp.ResolvedReference

return nil
}

0 comments on commit 994c07a

Please sign in to comment.