k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/componentconfigs/configset_test.go (about) 1 /* 2 Copyright 2019 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 componentconfigs 18 19 import ( 20 "testing" 21 22 "github.com/lithammer/dedent" 23 24 "k8s.io/apimachinery/pkg/runtime" 25 clientsetfake "k8s.io/client-go/kubernetes/fake" 26 27 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" 28 "k8s.io/kubernetes/cmd/kubeadm/app/constants" 29 kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util" 30 ) 31 32 func testClusterCfg() *kubeadmapi.ClusterConfiguration { 33 return &kubeadmapi.ClusterConfiguration{ 34 KubernetesVersion: constants.CurrentKubernetesVersion.String(), 35 } 36 } 37 38 func TestDefault(t *testing.T) { 39 clusterCfg := testClusterCfg() 40 localAPIEndpoint := &kubeadmapi.APIEndpoint{} 41 nodeRegOps := &kubeadmapi.NodeRegistrationOptions{} 42 43 Default(clusterCfg, localAPIEndpoint, nodeRegOps) 44 45 if len(clusterCfg.ComponentConfigs) != len(known) { 46 t.Errorf("mismatch between supported and defaulted type numbers:\n\tgot: %d\n\texpected: %d", len(clusterCfg.ComponentConfigs), len(known)) 47 } 48 } 49 50 func TestFromCluster(t *testing.T) { 51 objects := []runtime.Object{ 52 testKubeProxyConfigMap(` 53 apiVersion: kubeproxy.config.k8s.io/v1alpha1 54 kind: KubeProxyConfiguration 55 `), 56 testKubeletConfigMap(` 57 apiVersion: kubelet.config.k8s.io/v1beta1 58 kind: KubeletConfiguration 59 `), 60 } 61 client := clientsetfake.NewSimpleClientset(objects...) 62 clusterCfg := testClusterCfg() 63 64 if err := FetchFromCluster(clusterCfg, client); err != nil { 65 t.Fatalf("FetchFromCluster failed: %v", err) 66 } 67 68 if len(clusterCfg.ComponentConfigs) != len(objects) { 69 t.Fatalf("mismatch between supplied and loaded type numbers:\n\tgot: %d\n\texpected: %d", len(clusterCfg.ComponentConfigs), len(objects)) 70 } 71 } 72 73 func TestFetchFromDocumentMap(t *testing.T) { 74 test := dedent.Dedent(` 75 apiVersion: kubeproxy.config.k8s.io/v1alpha1 76 kind: KubeProxyConfiguration 77 --- 78 apiVersion: kubelet.config.k8s.io/v1beta1 79 kind: KubeletConfiguration 80 `) 81 gvkmap, err := kubeadmutil.SplitYAMLDocuments([]byte(test)) 82 if err != nil { 83 t.Fatalf("unexpected failure of SplitYAMLDocuments: %v", err) 84 } 85 86 clusterCfg := testClusterCfg() 87 if err = FetchFromDocumentMap(clusterCfg, gvkmap); err != nil { 88 t.Fatalf("FetchFromDocumentMap failed: %v", err) 89 } 90 91 if len(clusterCfg.ComponentConfigs) != len(gvkmap) { 92 t.Fatalf("mismatch between supplied and loaded type numbers:\n\tgot: %d\n\texpected: %d", len(clusterCfg.ComponentConfigs), len(gvkmap)) 93 } 94 }