github.com/oam-dev/kubevela@v1.9.11/pkg/config/factory_test.go (about) 1 /* 2 Copyright 2022 The KubeVela 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 config 18 19 import ( 20 "context" 21 "os" 22 "testing" 23 24 "github.com/golang/mock/gomock" 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 "github.com/stretchr/testify/require" 28 29 nacosmock "github.com/oam-dev/kubevela/test/mock/nacos" 30 ) 31 32 func TestParseConfigTemplate(t *testing.T) { 33 r := require.New(t) 34 content, err := os.ReadFile("testdata/helm-repo.cue") 35 r.Equal(err, nil) 36 var inf = &kubeConfigFactory{} 37 template, err := inf.ParseTemplate("default", content) 38 r.Equal(err, nil) 39 r.NotEqual(template, nil) 40 r.Equal(template.Name, "default") 41 r.NotEqual(template.Schema, nil) 42 r.Equal(len(template.Schema.Properties), 4) 43 } 44 45 var _ = Describe("test config factory", func() { 46 47 var fac Factory 48 BeforeEach(func() { 49 fac = NewConfigFactory(k8sClient) 50 }) 51 52 It("apply the nacos server template", func() { 53 data, err := os.ReadFile("./testdata/nacos-server.cue") 54 Expect(err).Should(BeNil()) 55 t, err := fac.ParseTemplate("", data) 56 Expect(err).Should(BeNil()) 57 Expect(fac.CreateOrUpdateConfigTemplate(context.TODO(), "default", t)).Should(BeNil()) 58 }) 59 It("apply a config to the nacos server", func() { 60 61 By("create a nacos server config") 62 nacos, err := fac.ParseConfig(context.TODO(), NamespacedName{Name: "nacos-server", Namespace: "default"}, Metadata{NamespacedName: NamespacedName{Name: "nacos", Namespace: "default"}, Properties: map[string]interface{}{ 63 "servers": []map[string]interface{}{{ 64 "ipAddr": "127.0.0.1", 65 "port": 8849, 66 }}, 67 }}) 68 Expect(err).Should(BeNil()) 69 Expect(len(nacos.Secret.Data[SaveInputPropertiesKey]) > 0).Should(BeTrue()) 70 Expect(fac.CreateOrUpdateConfig(context.Background(), nacos, "default")).Should(BeNil()) 71 72 config, err := fac.ReadConfig(context.TODO(), "default", "nacos") 73 Expect(err).Should(BeNil()) 74 servers, ok := config["servers"].([]interface{}) 75 Expect(ok).Should(BeTrue()) 76 Expect(len(servers)).Should(Equal(1)) 77 78 By("apply a template that with the nacos writer") 79 data, err := os.ReadFile("./testdata/mysql-db-nacos.cue") 80 Expect(err).Should(BeNil()) 81 t, err := fac.ParseTemplate("", data) 82 Expect(err).Should(BeNil()) 83 Expect(t.ExpandedWriter.Nacos).ShouldNot(BeNil()) 84 Expect(t.ExpandedWriter.Nacos.Endpoint.Name).Should(Equal("nacos")) 85 86 Expect(fac.CreateOrUpdateConfigTemplate(context.TODO(), "default", t)).Should(BeNil()) 87 88 db, err := fac.ParseConfig(context.TODO(), NamespacedName{Name: "nacos", Namespace: "default"}, Metadata{NamespacedName: NamespacedName{Name: "db-config", Namespace: "default"}, Properties: map[string]interface{}{ 89 "dataId": "dbconfig", 90 "appName": "db", 91 "content": map[string]interface{}{ 92 "mysqlHost": "127.0.0.1:3306", 93 "mysqlPort": 3306, 94 "username": "test", 95 "password": "string", 96 }, 97 }}) 98 Expect(err).Should(BeNil()) 99 Expect(db.Template.ExpandedWriter).ShouldNot(BeNil()) 100 Expect(db.ExpandedWriterData).ShouldNot(BeNil()) 101 Expect(len(db.ExpandedWriterData.Nacos.Content) > 0).Should(BeTrue()) 102 Expect(db.ExpandedWriterData.Nacos.Metadata.DataID).Should(Equal("dbconfig")) 103 104 Expect(len(db.OutputObjects)).Should(Equal(1)) 105 106 nacosClient := nacosmock.NewMockIConfigClient(ctl) 107 db.ExpandedWriterData.Nacos.Client = nacosClient 108 nacosClient.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) 109 110 Expect(err).Should(BeNil()) 111 Expect(fac.CreateOrUpdateConfig(context.Background(), db, "default")).Should(BeNil()) 112 113 }) 114 115 It("list all templates", func() { 116 templates, err := fac.ListTemplates(context.TODO(), "", "") 117 Expect(err).Should(BeNil()) 118 Expect(len(templates)).Should(Equal(2)) 119 }) 120 121 It("list all configs", func() { 122 configs, err := fac.ListConfigs(context.TODO(), "", "", "", true) 123 Expect(err).Should(BeNil()) 124 Expect(len(configs)).Should(Equal(2)) 125 }) 126 127 It("distribute a config", func() { 128 err := fac.CreateOrUpdateDistribution(context.TODO(), "default", "distribute-db-config", &CreateDistributionSpec{ 129 Configs: []*NamespacedName{ 130 {Name: "db-config", Namespace: "default"}, 131 }, 132 Targets: []*ClusterTarget{ 133 {ClusterName: "local", Namespace: "test"}, 134 }, 135 }) 136 Expect(err).Should(BeNil()) 137 }) 138 139 It("get the config", func() { 140 config, err := fac.GetConfig(context.TODO(), "default", "db-config", true) 141 Expect(err).Should(BeNil()) 142 Expect(len(config.ObjectReferences)).ShouldNot(BeNil()) 143 Expect(config.ObjectReferences[0].Kind).Should(Equal("ConfigMap")) 144 Expect(len(config.Targets)).Should(Equal(1)) 145 }) 146 147 It("check if the config exist", func() { 148 exist, err := fac.IsExist(context.TODO(), "default", "db-config") 149 Expect(err).Should(BeNil()) 150 Expect(exist).Should(BeTrue()) 151 }) 152 153 It("list the distributions", func() { 154 distributions, err := fac.ListDistributions(context.TODO(), "default") 155 Expect(err).Should(BeNil()) 156 Expect(len(distributions)).Should(Equal(1)) 157 }) 158 159 It("delete the distribution", func() { 160 err := fac.DeleteDistribution(context.TODO(), "default", "distribute-db-config") 161 Expect(err).Should(BeNil()) 162 }) 163 164 It("delete the config", func() { 165 err := fac.DeleteConfig(context.TODO(), "default", "db-config") 166 Expect(err).Should(BeNil()) 167 }) 168 169 It("delete the config template", func() { 170 err := fac.DeleteTemplate(context.TODO(), "default", "nacos") 171 Expect(err).Should(BeNil()) 172 }) 173 })