sigs.k8s.io/cluster-api@v1.7.1/internal/apis/controlplane/kubeadm/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  	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  	"sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/upstreamv1beta1"
    28  	controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
    29  	utilconversion "sigs.k8s.io/cluster-api/util/conversion"
    30  )
    31  
    32  func TestFuzzyConversion(t *testing.T) {
    33  	t.Run("for KubeadmControlPlane", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    34  		Hub:         &controlplanev1.KubeadmControlPlane{},
    35  		Spoke:       &KubeadmControlPlane{},
    36  		FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
    37  	}))
    38  }
    39  
    40  func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
    41  	// This custom function is needed when ConvertTo/ConvertFrom functions
    42  	// uses the json package to unmarshal the bootstrap token string.
    43  	//
    44  	// The Kubeadm v1beta1.BootstrapTokenString type ships with a custom
    45  	// json string representation, in particular it supplies a customized
    46  	// UnmarshalJSON function that can return an error if the string
    47  	// isn't in the correct form.
    48  	//
    49  	// This function effectively disables any fuzzing for the token by setting
    50  	// the values for ID and Secret to working alphanumeric values.
    51  	return []interface{}{
    52  		kubeadmBootstrapTokenStringFuzzer,
    53  		cabpkBootstrapTokenStringFuzzer,
    54  		dnsFuzzer,
    55  		kubeadmClusterConfigurationFuzzer,
    56  	}
    57  }
    58  
    59  func kubeadmBootstrapTokenStringFuzzer(in *upstreamv1beta1.BootstrapTokenString, _ fuzz.Continue) {
    60  	in.ID = "abcdef"
    61  	in.Secret = "abcdef0123456789"
    62  }
    63  func cabpkBootstrapTokenStringFuzzer(in *bootstrapv1.BootstrapTokenString, _ fuzz.Continue) {
    64  	in.ID = "abcdef"
    65  	in.Secret = "abcdef0123456789"
    66  }
    67  
    68  func dnsFuzzer(obj *upstreamv1beta1.DNS, c fuzz.Continue) {
    69  	c.FuzzNoCustom(obj)
    70  
    71  	// DNS.Type does not exists in v1alpha4, so setting it to empty string in order to avoid v1alpha3 --> v1alpha4 --> v1alpha3 round trip errors.
    72  	obj.Type = ""
    73  }
    74  
    75  func kubeadmClusterConfigurationFuzzer(obj *upstreamv1beta1.ClusterConfiguration, c fuzz.Continue) {
    76  	c.FuzzNoCustom(obj)
    77  
    78  	// ClusterConfiguration.UseHyperKubeImage has been removed in v1alpha4, so setting it to false in order to avoid v1alpha3 --> v1alpha4 --> v1alpha3 round trip errors.
    79  	obj.UseHyperKubeImage = false
    80  }