sigs.k8s.io/cluster-api@v1.6.3/controlplane/kubeadm/api/v1alpha4/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 v1alpha4 18 19 import ( 20 "testing" 21 22 fuzz "github.com/google/gofuzz" 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" 25 runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" 26 27 clusterv1alpha4 "sigs.k8s.io/cluster-api/api/v1alpha4" 28 bootstrapv1alpha4 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4" 29 bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" 30 controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" 31 utilconversion "sigs.k8s.io/cluster-api/util/conversion" 32 ) 33 34 const ( 35 fakeID = "abcdef" 36 fakeSecret = "abcdef0123456789" 37 ) 38 39 func TestFuzzyConversion(t *testing.T) { 40 t.Run("for KubeadmControlPlane", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{ 41 Hub: &controlplanev1.KubeadmControlPlane{}, 42 Spoke: &KubeadmControlPlane{}, 43 FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs}, 44 })) 45 46 t.Run("for KubeadmControlPlaneTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{ 47 Hub: &controlplanev1.KubeadmControlPlaneTemplate{}, 48 Spoke: &KubeadmControlPlaneTemplate{}, 49 FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs}, 50 })) 51 } 52 53 func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} { 54 // This custom function is needed when ConvertTo/ConvertFrom functions 55 // uses the json package to unmarshal the bootstrap token string. 56 // 57 // The Kubeadm v1beta1.BootstrapTokenString type ships with a custom 58 // json string representation, in particular it supplies a customized 59 // UnmarshalJSON function that can return an error if the string 60 // isn't in the correct form. 61 // 62 // This function effectively disables any fuzzing for the token by setting 63 // the values for ID and Secret to working alphanumeric values. 64 return []interface{}{ 65 cabpkBootstrapTokenStringFuzzer, 66 kubeadmBootstrapTokenStringFuzzerV1Alpha4, 67 kubeadmControlPlaneTemplateResourceSpecFuzzerV1Alpha4, 68 } 69 } 70 71 func cabpkBootstrapTokenStringFuzzer(in *bootstrapv1.BootstrapTokenString, _ fuzz.Continue) { 72 in.ID = fakeID 73 in.Secret = fakeSecret 74 } 75 76 func kubeadmBootstrapTokenStringFuzzerV1Alpha4(in *bootstrapv1alpha4.BootstrapTokenString, _ fuzz.Continue) { 77 in.ID = fakeID 78 in.Secret = fakeSecret 79 } 80 81 func kubeadmControlPlaneTemplateResourceSpecFuzzerV1Alpha4(in *KubeadmControlPlaneTemplateResource, c fuzz.Continue) { 82 c.Fuzz(in) 83 84 // Fields have been dropped in KCPTemplate. 85 in.Spec.Replicas = nil 86 in.Spec.Version = "" 87 in.Spec.MachineTemplate.ObjectMeta = clusterv1alpha4.ObjectMeta{} 88 in.Spec.MachineTemplate.InfrastructureRef = corev1.ObjectReference{} 89 }