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