Skip to content

Commit

Permalink
Add unit tests for runtime API conversions
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed May 7, 2024
1 parent b2b515f commit 50d6703
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/controller/conversion/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/crossplane/upjet/pkg/resource"
)

const (
errFmtPrioritizedManagedConversion = "cannot apply the PrioritizedManagedConversion for the %q object"
errFmtPavedConversion = "cannot apply the PavedConversion for the %q object"
errFmtManagedConversion = "cannot apply the ManagedConversion for the %q object"
)

// RoundTrip round-trips from `src` to `dst` via an unstructured map[string]any
// representation of the `src` object and applies the registered webhook
// conversion functions of this registry.
Expand All @@ -21,7 +27,7 @@ func (r *registry) RoundTrip(dst, src resource.Terraformed) error { //nolint:goc
for _, c := range r.GetConversions(dst) {
if pc, ok := c.(conversion.PrioritizedManagedConversion); ok {
if _, err := pc.ConvertManaged(src, dst); err != nil {
return errors.Wrapf(err, "cannot apply the PrioritizedManagedConversion for the %q object", dst.GetTerraformResourceType())
return errors.Wrapf(err, errFmtPrioritizedManagedConversion, dst.GetTerraformResourceType())
}
}
}
Expand All @@ -41,7 +47,7 @@ func (r *registry) RoundTrip(dst, src resource.Terraformed) error { //nolint:goc
for _, c := range r.GetConversions(dst) {
if pc, ok := c.(conversion.PavedConversion); ok {
if _, err := pc.ConvertPaved(srcPaved, dstPaved); err != nil {
return errors.Wrapf(err, "cannot apply the PavedConversion for the %q object", dst.GetTerraformResourceType())
return errors.Wrapf(err, errFmtPavedConversion, dst.GetTerraformResourceType())
}
}
}
Expand All @@ -58,7 +64,7 @@ func (r *registry) RoundTrip(dst, src resource.Terraformed) error { //nolint:goc
continue // then already run in the first stage
}
if _, err := tc.ConvertManaged(src, dst); err != nil {
return errors.Wrapf(err, "cannot apply the ManagedConversion for the %q object", dst.GetTerraformResourceType())
return errors.Wrapf(err, errFmtManagedConversion, dst.GetTerraformResourceType())
}
}
}
Expand Down
69 changes: 69 additions & 0 deletions pkg/controller/conversion/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"fmt"
"testing"

"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"

"github.com/crossplane/upjet/pkg/config"
"github.com/crossplane/upjet/pkg/config/conversion"
Expand All @@ -25,6 +28,7 @@ const (
val2 = "val2"
commonKey = "commonKey"
commonVal = "commonVal"
errTest = "test error"
)

func TestRoundTrip(t *testing.T) {
Expand Down Expand Up @@ -76,6 +80,39 @@ func TestRoundTrip(t *testing.T) {
dst: fake.NewTerraformed(fake.WithParameters(fake.NewMap(commonKey, commonVal, key2, val1))),
},
},
"RoundTripFailedPrioritizedConversion": {
reason: "Should return an error if a PrioritizedConversion fails.",
args: args{
dst: fake.NewTerraformed(),
src: fake.NewTerraformed(),
conversions: []conversion.Conversion{failedPrioritizedConversion{}},
},
want: want{
err: errors.Wrapf(errors.New(errTest), errFmtPrioritizedManagedConversion, ""),
},
},
"RoundTripFailedPavedConversion": {
reason: "Should return an error if a PavedConversion fails.",
args: args{
dst: fake.NewTerraformed(),
src: fake.NewTerraformed(),
conversions: []conversion.Conversion{failedPavedConversion{}},
},
want: want{
err: errors.Wrapf(errors.New(errTest), errFmtPavedConversion, ""),
},
},
"RoundTripFailedManagedConversion": {
reason: "Should return an error if a ManagedConversion fails.",
args: args{
dst: fake.NewTerraformed(),
src: fake.NewTerraformed(),
conversions: []conversion.Conversion{failedManagedConversion{}},
},
want: want{
err: errors.Wrapf(errors.New(errTest), errFmtManagedConversion, ""),
},
},
"RoundTripWithExcludedFields": {
reason: "Source object is successfully copied into the target object with certain fields excluded.",
args: args{
Expand Down Expand Up @@ -114,3 +151,35 @@ func TestRoundTrip(t *testing.T) {
})
}
}

type failedPrioritizedConversion struct{}

func (failedPrioritizedConversion) Applicable(_, _ runtime.Object) bool {
return true
}

func (failedPrioritizedConversion) ConvertManaged(_, _ xpresource.Managed) (bool, error) {
return false, errors.New(errTest)
}

func (failedPrioritizedConversion) Prioritized() {}

type failedPavedConversion struct{}

func (failedPavedConversion) Applicable(_, _ runtime.Object) bool {
return true
}

func (failedPavedConversion) ConvertPaved(_, _ *fieldpath.Paved) (bool, error) {
return false, errors.New(errTest)
}

type failedManagedConversion struct{}

func (failedManagedConversion) Applicable(_, _ runtime.Object) bool {
return true
}

func (failedManagedConversion) ConvertManaged(_, _ xpresource.Managed) (bool, error) {
return false, errors.New(errTest)
}

0 comments on commit 50d6703

Please sign in to comment.