k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/test/util.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 test 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 24 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" 25 kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" 26 certtestutil "k8s.io/kubernetes/cmd/kubeadm/app/util/certs" 27 configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config" 28 "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil" 29 ) 30 31 // SetupTempDir is a utility function for kubeadm testing, that creates a temporary directory 32 // NB. it is up to the caller to cleanup the folder at the end of the test with defer os.RemoveAll(tmpdir) 33 func SetupTempDir(t *testing.T) string { 34 tmpdir, err := os.MkdirTemp("", "") 35 if err != nil { 36 t.Fatalf("Couldn't create tmpdir") 37 } 38 39 return tmpdir 40 } 41 42 // SetupEmptyFiles is a utility function for kubeadm testing that creates one or more empty files (touch) 43 func SetupEmptyFiles(t *testing.T, tmpdir string, fileNames ...string) { 44 for _, fileName := range fileNames { 45 newFile, err := os.Create(filepath.Join(tmpdir, fileName)) 46 if err != nil { 47 t.Fatalf("Error creating file %s in %s: %v", fileName, tmpdir, err) 48 } 49 newFile.Close() 50 } 51 } 52 53 // SetupPkiDirWithCertificateAuthority is a utility function for kubeadm testing that creates a 54 // CertificateAuthority cert/key pair into /pki subfolder of a given temporary directory. 55 // The function returns the path of the created pki. 56 func SetupPkiDirWithCertificateAuthority(t *testing.T, tmpdir string) string { 57 caCert, caKey := certtestutil.SetupCertificateAuthority(t) 58 59 certDir := filepath.Join(tmpdir, "pki") 60 if err := pkiutil.WriteCertAndKey(certDir, kubeadmconstants.CACertAndKeyBaseName, caCert, caKey); err != nil { 61 t.Fatalf("failure while saving CA certificate and key: %v", err) 62 } 63 64 return certDir 65 } 66 67 // AssertFilesCount is a utility function for kubeadm testing that asserts if the given folder contains 68 // count files. 69 func AssertFilesCount(t *testing.T, dirName string, count int) { 70 files, err := os.ReadDir(dirName) 71 if err != nil { 72 t.Fatalf("Couldn't read files from tmpdir: %s", err) 73 } 74 75 countFiles := 0 76 for _, f := range files { 77 if !f.IsDir() { 78 countFiles++ 79 } 80 } 81 82 if countFiles != count { 83 t.Errorf("dir does contains %d, %d expected", len(files), count) 84 for _, f := range files { 85 t.Error(f.Name()) 86 } 87 } 88 } 89 90 // AssertFileExists is a utility function for kubeadm testing that asserts if the given folder contains 91 // the given files. 92 func AssertFileExists(t *testing.T, dirName string, fileNames ...string) { 93 for _, fileName := range fileNames { 94 path := filepath.Join(dirName, fileName) 95 96 if _, err := os.Stat(path); os.IsNotExist(err) { 97 t.Errorf("file %s does not exist", fileName) 98 } 99 } 100 } 101 102 // AssertError checks that the provided error matches the expected output 103 func AssertError(t *testing.T, err error, expected string) { 104 if err == nil { 105 t.Errorf("no error was found, but '%s' was expected", expected) 106 return 107 } 108 if err.Error() != expected { 109 t.Errorf("error '%s' does not match expected error: '%s'", err.Error(), expected) 110 } 111 } 112 113 // GetDefaultInternalConfig returns a defaulted kubeadmapi.InitConfiguration 114 func GetDefaultInternalConfig(t *testing.T) *kubeadmapi.InitConfiguration { 115 internalcfg, err := configutil.DefaultedStaticInitConfiguration() 116 if err != nil { 117 t.Fatalf("unexpected error getting default config: %v", err) 118 } 119 return internalcfg 120 }