github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/playground/kubeconfig_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package playground 21 22 import ( 23 "encoding/base64" 24 "fmt" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 29 "k8s.io/client-go/tools/clientcmd" 30 ) 31 32 const ( 33 testConfigPath = "./testdata/kubeconfig" 34 testCluster = "test-cluster" 35 testUser = "test-user" 36 testContext = "test-context" 37 ) 38 39 var ( 40 testKubeConfig = fmt.Sprintf(`apiVersion: v1 41 clusters: 42 - cluster: 43 certificate-authority-data: %[4]s 44 server: test-server 45 name: %[1]s 46 contexts: 47 - context: 48 cluster: %[1]s 49 user: %[2]s 50 name: %[3]s 51 current-context: %[3]s 52 kind: Config 53 preferences: {} 54 users: 55 - name: %[2]s 56 user: 57 client-certificate-data: %[4]s 58 client-key-data: %[4]s`, 59 testCluster, testUser, testContext, base64.StdEncoding.EncodeToString([]byte("Hello KubeBlocks!"))) 60 ) 61 62 var _ = Describe("playground kubeconfig", func() { 63 It("get kubeconfig default path", func() { 64 path, err := kubeConfigGetDefaultPath() 65 Expect(err).Should(Succeed()) 66 Expect(path).ShouldNot(BeEmpty()) 67 }) 68 69 It("write invalid config to file", func() { 70 err := kubeConfigWrite("invalid config", testConfigPath, writeKubeConfigOptions{}) 71 Expect(err).Should(HaveOccurred()) 72 }) 73 74 It("write and remove valid config to file", func() { 75 By("write valid config to file") 76 err := kubeConfigWrite(testKubeConfig, testConfigPath, writeKubeConfigOptions{UpdateExisting: true}) 77 Expect(err).Should(Succeed()) 78 config, err := clientcmd.LoadFromFile(testConfigPath) 79 Expect(err).Should(Succeed()) 80 Expect(config).ShouldNot(BeNil()) 81 Expect(config.CurrentContext).Should(Equal(testContext)) 82 83 By("remove config from file") 84 err = kubeConfigRemove(testKubeConfig, testConfigPath) 85 Expect(err).Should(Succeed()) 86 }) 87 })