sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/types/upstreamv1beta3/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 upstreamv1beta3 18 19 import ( 20 "testing" 21 22 fuzz "github.com/google/gofuzz" 23 . "github.com/onsi/gomega" 24 "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" 25 "k8s.io/apimachinery/pkg/runtime" 26 runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" 27 28 bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" 29 utilconversion "sigs.k8s.io/cluster-api/util/conversion" 30 ) 31 32 func TestFuzzyConversion(t *testing.T) { 33 g := NewWithT(t) 34 scheme := runtime.NewScheme() 35 g.Expect(AddToScheme(scheme)).To(Succeed()) 36 g.Expect(bootstrapv1.AddToScheme(scheme)).To(Succeed()) 37 38 t.Run("for ClusterConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{ 39 Scheme: scheme, 40 Hub: &bootstrapv1.ClusterConfiguration{}, 41 Spoke: &ClusterConfiguration{}, 42 // NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test. 43 SkipSpokeAnnotationCleanup: true, 44 FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs}, 45 })) 46 t.Run("for InitConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{ 47 Scheme: scheme, 48 Hub: &bootstrapv1.InitConfiguration{}, 49 Spoke: &InitConfiguration{}, 50 // NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test. 51 SkipSpokeAnnotationCleanup: true, 52 FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs}, 53 })) 54 t.Run("for JoinConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{ 55 Scheme: scheme, 56 Hub: &bootstrapv1.JoinConfiguration{}, 57 Spoke: &JoinConfiguration{}, 58 // NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test. 59 SkipSpokeAnnotationCleanup: true, 60 FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs}, 61 })) 62 } 63 64 func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} { 65 return []interface{}{ 66 nodeRegistrationOptionsFuzzer, 67 initConfigurationFuzzer, 68 joinConfigurationFuzzer, 69 joinControlPlanesFuzzer, 70 } 71 } 72 73 func nodeRegistrationOptionsFuzzer(obj *NodeRegistrationOptions, c fuzz.Continue) { 74 c.FuzzNoCustom(obj) 75 76 // NodeRegistrationOptions.IgnorePreflightErrors does not exists in v1alpha4, so setting it to nil in order to avoid v1beta3 --> v1alpha4 --> v1beta3 round trip errors. 77 obj.IgnorePreflightErrors = nil 78 } 79 80 func joinControlPlanesFuzzer(obj *JoinControlPlane, c fuzz.Continue) { 81 c.FuzzNoCustom(obj) 82 83 // JoinControlPlane.CertificateKey does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta3 --> v1alpha4 --> v1beta3 round trip errors. 84 obj.CertificateKey = "" 85 } 86 87 func initConfigurationFuzzer(obj *InitConfiguration, c fuzz.Continue) { 88 c.Fuzz(obj) 89 90 // InitConfiguration.CertificateKey does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta3 --> v1alpha4 --> v1beta3 round trip errors. 91 obj.CertificateKey = "" 92 93 // InitConfiguration.SkipPhases does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta3 --> v1alpha4 --> v1beta3 round trip errors. 94 obj.SkipPhases = nil 95 } 96 97 func joinConfigurationFuzzer(obj *JoinConfiguration, c fuzz.Continue) { 98 c.Fuzz(obj) 99 100 // JoinConfiguration.SkipPhases does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta3 --> v1alpha4 --> v1beta3 round trip errors. 101 obj.SkipPhases = nil 102 }