sigs.k8s.io/cluster-api@v1.7.1/internal/apis/core/exp/v1alpha3/conversion_test.go (about)

     1  /*
     2  Copyright 2021 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  	fuzz "github.com/google/gofuzz"
    23  	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
    24  	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
    25  
    26  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    27  	clusterv1alpha3 "sigs.k8s.io/cluster-api/internal/apis/core/v1alpha3"
    28  	utilconversion "sigs.k8s.io/cluster-api/util/conversion"
    29  )
    30  
    31  func TestFuzzyConversion(t *testing.T) {
    32  	t.Run("for MachinePool", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    33  		Hub:         &expv1.MachinePool{},
    34  		Spoke:       &MachinePool{},
    35  		FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
    36  	}))
    37  }
    38  
    39  func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
    40  	return []interface{}{
    41  		BootstrapFuzzer,
    42  		MachinePoolSpecFuzzer,
    43  		ObjectMetaFuzzer,
    44  	}
    45  }
    46  
    47  func BootstrapFuzzer(in *clusterv1alpha3.Bootstrap, c fuzz.Continue) {
    48  	c.FuzzNoCustom(in)
    49  
    50  	// Bootstrap.Data has been removed in v1alpha4, so setting it to nil in order to avoid v1alpha3 --> <hub> --> v1alpha3 round trip errors.
    51  	in.Data = nil
    52  }
    53  
    54  func ObjectMetaFuzzer(in *clusterv1alpha3.ObjectMeta, c fuzz.Continue) {
    55  	c.FuzzNoCustom(in)
    56  
    57  	// These fields have been removed in v1beta1
    58  	// data is going to be lost, so we're forcing zero values here.
    59  	in.Name = ""
    60  	in.GenerateName = ""
    61  	in.Namespace = ""
    62  	in.OwnerReferences = nil
    63  }
    64  
    65  func MachinePoolSpecFuzzer(in *MachinePoolSpec, c fuzz.Continue) {
    66  	c.Fuzz(in)
    67  
    68  	// These fields have been removed in v1beta1
    69  	// data is going to be lost, so we're forcing zero values here.
    70  	in.Strategy = nil
    71  }