github.com/crossplane/upjet@v1.3.0/pkg/controller/conversion/functions_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package conversion
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
    12  	"github.com/crossplane/crossplane-runtime/pkg/test"
    13  	"github.com/google/go-cmp/cmp"
    14  
    15  	"github.com/crossplane/upjet/pkg/config"
    16  	"github.com/crossplane/upjet/pkg/config/conversion"
    17  	"github.com/crossplane/upjet/pkg/resource"
    18  	"github.com/crossplane/upjet/pkg/resource/fake"
    19  )
    20  
    21  const (
    22  	key1      = "key1"
    23  	val1      = "val1"
    24  	key2      = "key2"
    25  	val2      = "val2"
    26  	commonKey = "commonKey"
    27  	commonVal = "commonVal"
    28  )
    29  
    30  func TestRoundTrip(t *testing.T) {
    31  	type args struct {
    32  		dst         resource.Terraformed
    33  		src         resource.Terraformed
    34  		conversions []conversion.Conversion
    35  	}
    36  	type want struct {
    37  		err error
    38  		dst resource.Terraformed
    39  	}
    40  	tests := map[string]struct {
    41  		reason string
    42  		args   args
    43  		want   want
    44  	}{
    45  		"SuccessfulRoundTrip": {
    46  			reason: "Source object is successfully copied into the target object.",
    47  			args: args{
    48  				dst: fake.NewTerraformed(),
    49  				src: fake.NewTerraformed(fake.WithParameters(fake.NewMap(key1, val1))),
    50  			},
    51  			want: want{
    52  				dst: fake.NewTerraformed(fake.WithParameters(fake.NewMap(key1, val1))),
    53  			},
    54  		},
    55  		"SuccessfulRoundTripWithConversions": {
    56  			reason: "Source object is successfully converted into the target object with a set of conversions.",
    57  			args: args{
    58  				dst: fake.NewTerraformed(),
    59  				src: fake.NewTerraformed(fake.WithParameters(fake.NewMap(commonKey, commonVal, key1, val1))),
    60  				conversions: []conversion.Conversion{
    61  					// Because the parameters of the fake.Terraformed is an unstructured
    62  					// map, all the fields of source (including key1) are successfully
    63  					// copied into dst by registry.RoundTrip.
    64  					// This conversion deletes the copied key "key1".
    65  					conversion.NewCustomConverter(conversion.AllVersions, conversion.AllVersions, func(_, target xpresource.Managed) error {
    66  						tr := target.(*fake.Terraformed)
    67  						delete(tr.Parameters, key1)
    68  						return nil
    69  					}),
    70  					conversion.NewFieldRenameConversion(conversion.AllVersions, fmt.Sprintf("parameterizable.parameters.%s", key1), conversion.AllVersions, fmt.Sprintf("parameterizable.parameters.%s", key2)),
    71  				},
    72  			},
    73  			want: want{
    74  				dst: fake.NewTerraformed(fake.WithParameters(fake.NewMap(commonKey, commonVal, key2, val1))),
    75  			},
    76  		},
    77  	}
    78  	for name, tc := range tests {
    79  		t.Run(name, func(t *testing.T) {
    80  			p := &config.Provider{
    81  				Resources: map[string]*config.Resource{
    82  					tc.args.dst.GetTerraformResourceType(): {
    83  						Conversions: tc.args.conversions,
    84  					},
    85  				},
    86  			}
    87  			r := &registry{}
    88  			if err := r.RegisterConversions(p); err != nil {
    89  				t.Fatalf("\n%s\nRegisterConversions(p): Failed to register the conversions with the registry.\n", tc.reason)
    90  			}
    91  			err := r.RoundTrip(tc.args.dst, tc.args.src)
    92  			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
    93  				t.Errorf("\n%s\nRoundTrip(dst, src): -wantErr, +gotErr:\n%s", tc.reason, diff)
    94  			}
    95  			if tc.want.err != nil {
    96  				return
    97  			}
    98  			if diff := cmp.Diff(tc.want.dst, tc.args.dst); diff != "" {
    99  				t.Errorf("\n%s\nRoundTrip(dst, src): -wantDst, +gotDst:\n%s", tc.reason, diff)
   100  			}
   101  		})
   102  	}
   103  }