github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cluster/register_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 cluster 21 22 import ( 23 "io" 24 "os" 25 "path/filepath" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 ) 30 31 var _ = Describe("cluster register", func() { 32 It("test builtin chart", func() { 33 mysql := &embedConfig{ 34 chartFS: mysqlChart, 35 name: "apecloud-mysql-cluster.tgz", 36 alias: "", 37 } 38 Expect(mysql.register("mysql")).Should(HaveOccurred()) 39 Expect(mysql.register("mysql-other")).Should(Succeed()) 40 Expect(mysql.getChartFileName()).Should(Equal("apecloud-mysql-cluster.tgz")) 41 Expect(mysql.getAlias()).Should(Equal("")) 42 chart, err := mysql.loadChart() 43 Expect(err).Should(Succeed()) 44 bytes, err := io.ReadAll(chart) 45 Expect(bytes).ShouldNot(BeEmpty()) 46 Expect(err).Should(Succeed()) 47 }) 48 49 It("test external chart", func() { 50 fakeChart := &TypeInstance{ 51 Name: "fake", 52 URL: "www.fake-chart-hub/fake.tgz", 53 Alias: "", 54 ChartName: "fake.tgz", 55 } 56 Expect(fakeChart.getAlias()).Should(Equal("")) 57 Expect(fakeChart.getChartFileName()).Should(Equal("fake.tgz")) 58 _, err := fakeChart.loadChart() 59 Expect(err).Should(HaveOccurred()) 60 Expect(fakeChart.register("fake")).Should(HaveOccurred()) 61 }) 62 63 Context("test Config reader", func() { 64 var tempConfigPath string 65 66 var tempCLusterConfig clusterConfig 67 var configContent = `- name: orioledb 68 helmChartUrl: https://github.com/apecloud/helm-charts/releases/download/orioledb-cluster-0.7.0-alpha.7/orioledb-cluster-0.7.0-alpha.7.tgz 69 alias: "" 70 ` 71 BeforeEach(func() { 72 tempConfigPath = filepath.Join(os.TempDir(), "kbcli_test") 73 Expect(os.WriteFile(tempConfigPath, []byte(configContent), 0666)).Should(Succeed()) 74 }) 75 76 AfterEach(func() { 77 os.Remove(tempConfigPath) 78 }) 79 80 It("test read configs and remove", func() { 81 Expect(tempCLusterConfig.ReadConfigs(tempConfigPath)).Should(Succeed()) 82 Expect(tempCLusterConfig.Len()).Should(Equal(1)) 83 Expect(tempCLusterConfig.RemoveConfig("orioledb")).Should(BeTrue()) 84 Expect(tempCLusterConfig.Len()).Should(Equal(0)) 85 }) 86 87 It("test add config and write", func() { 88 tempCLusterConfig.AddConfig(&TypeInstance{ 89 Name: "orioledb", 90 URL: "https://fakeurl.com", 91 Alias: "", 92 }) 93 Expect(tempCLusterConfig.Len()).Should(Equal(1)) 94 Expect(tempCLusterConfig.WriteConfigs(tempConfigPath)).Should(Succeed()) 95 96 file, _ := os.ReadFile(tempConfigPath) 97 Expect(string(file)).Should(Equal("- name: orioledb\n helmChartUrl: https://fakeurl.com\n alias: \"\"\n chartName: \"\"\n")) 98 }) 99 100 }) 101 })