sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1alpha3/conversion_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1alpha3
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  
    24  	fuzz "github.com/google/gofuzz"
    25  	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
    26  	runtime "k8s.io/apimachinery/pkg/runtime"
    27  	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
    28  	v1beta1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
    29  	utilconversion "sigs.k8s.io/cluster-api/util/conversion"
    30  )
    31  
    32  func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
    33  	return []interface{}{
    34  		AWSClusterStaticIdentityFuzzer,
    35  		AWSMachineFuzzer,
    36  		AWSMachineTemplateFuzzer,
    37  	}
    38  }
    39  
    40  func AWSClusterStaticIdentityFuzzer(obj *AWSClusterStaticIdentity, c fuzz.Continue) {
    41  	c.FuzzNoCustom(obj)
    42  
    43  	// AWSClusterStaticIdentity.Spec.SecretRef.Namespace has been removed in v1beta1, so setting it to nil in order to avoid v1alpha3 --> <hub> --> v1alpha3 round trip errors.
    44  	obj.Spec.SecretRef.Namespace = ""
    45  }
    46  
    47  func AWSMachineFuzzer(obj *AWSMachine, c fuzz.Continue) {
    48  	c.FuzzNoCustom(obj)
    49  
    50  	// AWSMachine.Spec.AMI.ARN and AWSMachine.Spec.AMI.Filters has been removed in v1beta1, so setting it to nil in order to avoid v1alpha3 --> <hub> --> v1alpha3 round trip errors.
    51  	obj.Spec.AMI.ARN = nil
    52  	obj.Spec.AMI.Filters = nil
    53  }
    54  
    55  func AWSMachineTemplateFuzzer(obj *AWSMachineTemplate, c fuzz.Continue) {
    56  	c.FuzzNoCustom(obj)
    57  
    58  	// AWSMachineTemplate.Spec.Template.Spec.AMI.ARN and AWSMachineTemplate.Spec.Template.Spec.AMI.Filters has been removed in v1beta1, so setting it to nil in order to avoid v1alpha3 --> v1beta1 --> v1alpha3 round trip errors.
    59  	obj.Spec.Template.Spec.AMI.ARN = nil
    60  	obj.Spec.Template.Spec.AMI.Filters = nil
    61  }
    62  
    63  func TestFuzzyConversion(t *testing.T) {
    64  	g := NewWithT(t)
    65  	scheme := runtime.NewScheme()
    66  	g.Expect(AddToScheme(scheme)).To(Succeed())
    67  	g.Expect(v1beta1.AddToScheme(scheme)).To(Succeed())
    68  
    69  	t.Run("for AWSCluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    70  		Scheme: scheme,
    71  		Hub:    &v1beta1.AWSCluster{},
    72  		Spoke:  &AWSCluster{},
    73  	}))
    74  
    75  	t.Run("for AWSMachine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    76  		Scheme:      scheme,
    77  		Hub:         &v1beta1.AWSMachine{},
    78  		Spoke:       &AWSMachine{},
    79  		FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
    80  	}))
    81  
    82  	t.Run("for AWSMachineTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    83  		Scheme:      scheme,
    84  		Hub:         &v1beta1.AWSMachineTemplate{},
    85  		Spoke:       &AWSMachineTemplate{},
    86  		FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
    87  	}))
    88  
    89  	t.Run("for AWSClusterStaticIdentity", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    90  		Scheme:      scheme,
    91  		Hub:         &v1beta1.AWSClusterStaticIdentity{},
    92  		Spoke:       &AWSClusterStaticIdentity{},
    93  		FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
    94  	}))
    95  
    96  	t.Run("for AWSClusterControllerIdentity", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    97  		Scheme: scheme,
    98  		Hub:    &v1beta1.AWSClusterControllerIdentity{},
    99  		Spoke:  &AWSClusterControllerIdentity{},
   100  	}))
   101  
   102  	t.Run("for AWSClusterRoleIdentity", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
   103  		Scheme: scheme,
   104  		Hub:    &v1beta1.AWSClusterRoleIdentity{},
   105  		Spoke:  &AWSClusterRoleIdentity{},
   106  	}))
   107  }