k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/phases/kubelet/config_test.go (about) 1 /* 2 Copyright 2017 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 kubelet 18 19 import ( 20 "bytes" 21 "io" 22 "os" 23 "path/filepath" 24 "testing" 25 26 v1 "k8s.io/api/core/v1" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/apimachinery/pkg/runtime" 29 "k8s.io/client-go/kubernetes/fake" 30 core "k8s.io/client-go/testing" 31 32 configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config" 33 ) 34 35 func TestCreateConfigMap(t *testing.T) { 36 nodeName := "fake-node" 37 client := fake.NewSimpleClientset() 38 client.PrependReactor("get", "nodes", func(action core.Action) (bool, runtime.Object, error) { 39 return true, &v1.Node{ 40 ObjectMeta: metav1.ObjectMeta{ 41 Name: nodeName, 42 }, 43 Spec: v1.NodeSpec{}, 44 }, nil 45 }) 46 client.PrependReactor("create", "roles", func(action core.Action) (bool, runtime.Object, error) { 47 return true, nil, nil 48 }) 49 client.PrependReactor("create", "rolebindings", func(action core.Action) (bool, runtime.Object, error) { 50 return true, nil, nil 51 }) 52 client.PrependReactor("create", "configmaps", func(action core.Action) (bool, runtime.Object, error) { 53 return true, nil, nil 54 }) 55 56 internalcfg, err := configutil.DefaultedStaticInitConfiguration() 57 if err != nil { 58 t.Fatalf("unexpected failure when defaulting InitConfiguration: %v", err) 59 } 60 61 if err := CreateConfigMap(&internalcfg.ClusterConfiguration, client); err != nil { 62 t.Errorf("CreateConfigMap: unexpected error %v", err) 63 } 64 } 65 66 func TestCreateConfigMapRBACRules(t *testing.T) { 67 client := fake.NewSimpleClientset() 68 client.PrependReactor("create", "roles", func(action core.Action) (bool, runtime.Object, error) { 69 return true, nil, nil 70 }) 71 client.PrependReactor("create", "rolebindings", func(action core.Action) (bool, runtime.Object, error) { 72 return true, nil, nil 73 }) 74 75 if err := createConfigMapRBACRules(client); err != nil { 76 t.Errorf("createConfigMapRBACRules: unexpected error %v", err) 77 } 78 } 79 80 func TestApplyKubeletConfigPatches(t *testing.T) { 81 var ( 82 input = []byte("bar: 0\nfoo: 0\n") 83 patch = []byte("bar: 1\n") 84 expectedOutput = []byte("bar: 1\nfoo: 0\n") 85 ) 86 87 dir, err := os.MkdirTemp("", "patches") 88 if err != nil { 89 t.Fatalf("could not create temp dir: %v", err) 90 } 91 defer os.RemoveAll(dir) 92 93 if err := os.WriteFile(filepath.Join(dir, "kubeletconfiguration.yaml"), patch, 0644); err != nil { 94 t.Fatalf("could not write patch file: %v", err) 95 } 96 97 output, err := applyKubeletConfigPatches(input, dir, io.Discard) 98 if err != nil { 99 t.Fatalf("could not apply patch: %v", err) 100 } 101 102 if !bytes.Equal(output, expectedOutput) { 103 t.Fatalf("expected output:\n%s\ngot\n%s\n", expectedOutput, output) 104 } 105 }