sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/types/upstreamv1beta1/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 upstreamv1beta1
    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  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
    27  	utilconversion "sigs.k8s.io/cluster-api/util/conversion"
    28  )
    29  
    30  func TestFuzzyConversion(t *testing.T) {
    31  	t.Run("for ClusterConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    32  		Hub:   &bootstrapv1.ClusterConfiguration{},
    33  		Spoke: &ClusterConfiguration{},
    34  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    35  		SkipSpokeAnnotationCleanup: true,
    36  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    37  	}))
    38  	t.Run("for ClusterStatus", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    39  		Hub:   &bootstrapv1.ClusterStatus{},
    40  		Spoke: &ClusterStatus{},
    41  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    42  		SkipSpokeAnnotationCleanup: true,
    43  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    44  	}))
    45  	t.Run("for InitConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    46  		Hub:   &bootstrapv1.InitConfiguration{},
    47  		Spoke: &InitConfiguration{},
    48  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    49  		SkipSpokeAnnotationCleanup: true,
    50  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    51  	}))
    52  	t.Run("for JoinConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    53  		Hub:   &bootstrapv1.JoinConfiguration{},
    54  		Spoke: &JoinConfiguration{},
    55  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    56  		SkipSpokeAnnotationCleanup: true,
    57  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    58  	}))
    59  }
    60  
    61  func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
    62  	return []interface{}{
    63  		dnsFuzzer,
    64  		clusterConfigurationFuzzer,
    65  		kubeadmNodeRegistrationOptionsFuzzer,
    66  		kubeadmInitConfigurationFuzzer,
    67  		kubeadmJoinConfigurationFuzzer,
    68  	}
    69  }
    70  
    71  func dnsFuzzer(obj *DNS, c fuzz.Continue) {
    72  	c.FuzzNoCustom(obj)
    73  
    74  	// DNS.Type does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta1 --> v1alpha4 --> v1beta1 round trip errors.
    75  	obj.Type = ""
    76  }
    77  
    78  func clusterConfigurationFuzzer(obj *ClusterConfiguration, c fuzz.Continue) {
    79  	c.FuzzNoCustom(obj)
    80  
    81  	// ClusterConfiguration.UseHyperKubeImage has been removed in v1alpha4, so setting it to false in order to avoid v1beta1 --> v1alpha4 --> v1beta1 round trip errors.
    82  	obj.UseHyperKubeImage = false
    83  }
    84  
    85  func kubeadmNodeRegistrationOptionsFuzzer(obj *bootstrapv1.NodeRegistrationOptions, c fuzz.Continue) {
    86  	c.FuzzNoCustom(obj)
    87  
    88  	// NodeRegistrationOptions.IgnorePreflightErrors does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
    89  	// v1alpha4 --> v1beta1 -> v1alpha4 round trip errors.
    90  	obj.IgnorePreflightErrors = nil
    91  
    92  	// NodeRegistrationOptions.ImagePullPolicy does not exist in
    93  	// kubeadm v1beta1 API, so setting it to empty in order to
    94  	// avoid round trip errors.
    95  	obj.ImagePullPolicy = ""
    96  }
    97  
    98  func kubeadmInitConfigurationFuzzer(obj *bootstrapv1.InitConfiguration, c fuzz.Continue) {
    99  	c.FuzzNoCustom(obj)
   100  
   101  	// InitConfiguration.Patches does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   102  	// v1beta1 --> upstream v1beta1 -> v1beta1 round trip errors.
   103  	obj.Patches = nil
   104  
   105  	// InitConfiguration.SkipPhases does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   106  	// v1beta1 --> upstream v1beta1 -> v1beta1 round trip errors.
   107  	obj.SkipPhases = nil
   108  }
   109  
   110  func kubeadmJoinConfigurationFuzzer(obj *bootstrapv1.JoinConfiguration, c fuzz.Continue) {
   111  	c.FuzzNoCustom(obj)
   112  
   113  	// JoinConfiguration.Patches does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   114  	// v1beta1 --> upstream v1beta1 -> v1beta1 round trip errors.
   115  	obj.Patches = nil
   116  
   117  	// JoinConfiguration.SkipPhases does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   118  	// v1beta1 --> upstream v1beta1 -> v1beta1 round trip errors.
   119  	obj.SkipPhases = nil
   120  }