sigs.k8s.io/cluster-api@v1.6.3/bootstrap/kubeadm/api/v1alpha4/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 v1alpha4
    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  const (
    31  	fakeID     = "abcdef"
    32  	fakeSecret = "abcdef0123456789"
    33  )
    34  
    35  func TestFuzzyConversion(t *testing.T) {
    36  	t.Run("for KubeadmConfig", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    37  		Hub:         &bootstrapv1.KubeadmConfig{},
    38  		Spoke:       &KubeadmConfig{},
    39  		FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
    40  	}))
    41  	t.Run("for KubeadmConfigTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    42  		Hub:         &bootstrapv1.KubeadmConfigTemplate{},
    43  		Spoke:       &KubeadmConfigTemplate{},
    44  		FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
    45  	}))
    46  }
    47  
    48  func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
    49  	return []interface{}{
    50  		// This custom functions are needed when ConvertTo/ConvertFrom functions
    51  		// uses the json package to unmarshal the bootstrap token string.
    52  		//
    53  		// The Kubeadm BootstrapTokenString type ships with a custom
    54  		// json string representation, in particular it supplies a customized
    55  		// UnmarshalJSON function that can return an error if the string
    56  		// isn't in the correct form.
    57  		//
    58  		// This function effectively disables any fuzzing for the token by setting
    59  		// the values for ID and Secret to working alphanumeric values.
    60  		kubeadmBootstrapTokenStringFuzzerV1Beta1,
    61  		kubeadmBootstrapTokenStringFuzzerV1Alpha4,
    62  	}
    63  }
    64  
    65  func kubeadmBootstrapTokenStringFuzzerV1Beta1(in *bootstrapv1.BootstrapTokenString, _ fuzz.Continue) {
    66  	in.ID = fakeID
    67  	in.Secret = fakeSecret
    68  }
    69  
    70  func kubeadmBootstrapTokenStringFuzzerV1Alpha4(in *BootstrapTokenString, _ fuzz.Continue) {
    71  	in.ID = fakeID
    72  	in.Secret = fakeSecret
    73  }