github.com/crossplane/upjet@v1.3.0/pkg/config/conversion/conversions_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  	"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
    12  	"github.com/crossplane/crossplane-runtime/pkg/test"
    13  	"github.com/google/go-cmp/cmp"
    14  	"k8s.io/utils/ptr"
    15  )
    16  
    17  const (
    18  	sourceVersion = "v1beta1"
    19  	sourceField   = "testSourceField"
    20  	targetVersion = "v1beta2"
    21  	targetField   = "testTargetField"
    22  )
    23  
    24  func TestConvertPaved(t *testing.T) {
    25  	type args struct {
    26  		sourceVersion string
    27  		sourceField   string
    28  		targetVersion string
    29  		targetField   string
    30  		sourceObj     *fieldpath.Paved
    31  		targetObj     *fieldpath.Paved
    32  	}
    33  	type want struct {
    34  		converted bool
    35  		err       error
    36  		targetObj *fieldpath.Paved
    37  	}
    38  	tests := map[string]struct {
    39  		reason string
    40  		args   args
    41  		want   want
    42  	}{
    43  		"SuccessfulConversion": {
    44  			reason: "Source field in source version is successfully converted to the target field in target version.",
    45  			args: args{
    46  				sourceVersion: sourceVersion,
    47  				sourceField:   sourceField,
    48  				targetVersion: targetVersion,
    49  				targetField:   targetField,
    50  				sourceObj:     getPaved(sourceVersion, sourceField, ptr.To("testValue")),
    51  				targetObj:     getPaved(targetVersion, targetField, nil),
    52  			},
    53  			want: want{
    54  				converted: true,
    55  				targetObj: getPaved(targetVersion, targetField, ptr.To("testValue")),
    56  			},
    57  		},
    58  		"SuccessfulConversionAllVersions": {
    59  			reason: "Source field in source version is successfully converted to the target field in target version when the conversion specifies wildcard version for both of the source and the target.",
    60  			args: args{
    61  				sourceVersion: AllVersions,
    62  				sourceField:   sourceField,
    63  				targetVersion: AllVersions,
    64  				targetField:   targetField,
    65  				sourceObj:     getPaved(sourceVersion, sourceField, ptr.To("testValue")),
    66  				targetObj:     getPaved(targetVersion, targetField, nil),
    67  			},
    68  			want: want{
    69  				converted: true,
    70  				targetObj: getPaved(targetVersion, targetField, ptr.To("testValue")),
    71  			},
    72  		},
    73  		"SourceVersionMismatch": {
    74  			reason: "Conversion is not done if the source version of the object does not match the conversion's source version.",
    75  			args: args{
    76  				sourceVersion: "mismatch",
    77  				sourceField:   sourceField,
    78  				targetVersion: AllVersions,
    79  				targetField:   targetField,
    80  				sourceObj:     getPaved(sourceVersion, sourceField, ptr.To("testValue")),
    81  				targetObj:     getPaved(targetVersion, targetField, nil),
    82  			},
    83  			want: want{
    84  				converted: false,
    85  				targetObj: getPaved(targetVersion, targetField, nil),
    86  			},
    87  		},
    88  		"TargetVersionMismatch": {
    89  			reason: "Conversion is not done if the target version of the object does not match the conversion's target version.",
    90  			args: args{
    91  				sourceVersion: AllVersions,
    92  				sourceField:   sourceField,
    93  				targetVersion: "mismatch",
    94  				targetField:   targetField,
    95  				sourceObj:     getPaved(sourceVersion, sourceField, ptr.To("testValue")),
    96  				targetObj:     getPaved(targetVersion, targetField, nil),
    97  			},
    98  			want: want{
    99  				converted: false,
   100  				targetObj: getPaved(targetVersion, targetField, nil),
   101  			},
   102  		},
   103  		"SourceFieldNotFound": {
   104  			reason: "Conversion is not done if the source field is not found in the source object.",
   105  			args: args{
   106  				sourceVersion: sourceVersion,
   107  				sourceField:   sourceField,
   108  				targetVersion: targetVersion,
   109  				targetField:   targetField,
   110  				sourceObj:     getPaved(sourceVersion, sourceField, nil),
   111  				targetObj:     getPaved(targetVersion, targetField, ptr.To("test")),
   112  			},
   113  			want: want{
   114  				converted: false,
   115  				targetObj: getPaved(targetVersion, targetField, ptr.To("test")),
   116  			},
   117  		},
   118  	}
   119  	for name, tc := range tests {
   120  		t.Run(name, func(t *testing.T) {
   121  			c := NewFieldRenameConversion(tc.args.sourceVersion, tc.args.sourceField, tc.args.targetVersion, tc.args.targetField)
   122  			converted, err := c.(*fieldCopy).ConvertPaved(tc.args.sourceObj, tc.args.targetObj)
   123  			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
   124  				t.Errorf("\n%s\nConvertPaved(sourceObj, targetObj): -wantErr, +gotErr:\n%s", tc.reason, diff)
   125  			}
   126  			if tc.want.err != nil {
   127  				return
   128  			}
   129  			if diff := cmp.Diff(tc.want.converted, converted); diff != "" {
   130  				t.Errorf("\n%s\nConvertPaved(sourceObj, targetObj): -wantConverted, +gotConverted:\n%s", tc.reason, diff)
   131  			}
   132  			if diff := cmp.Diff(tc.want.targetObj.UnstructuredContent(), tc.args.targetObj.UnstructuredContent()); diff != "" {
   133  				t.Errorf("\n%s\nConvertPaved(sourceObj, targetObj): -wantTargetObj, +gotTargetObj:\n%s", tc.reason, diff)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func getPaved(version, field string, value *string) *fieldpath.Paved {
   140  	m := map[string]any{
   141  		"apiVersion": fmt.Sprintf("mockgroup/%s", version),
   142  		"kind":       "mockkind",
   143  	}
   144  	if value != nil {
   145  		m[field] = *value
   146  	}
   147  	return fieldpath.Pave(m)
   148  }