github.com/oam-dev/kubevela@v1.9.11/pkg/config/writer/nacos_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 writer 18 19 import ( 20 "context" 21 "errors" 22 "testing" 23 24 "github.com/golang/mock/gomock" 25 "github.com/nacos-group/nacos-sdk-go/v2/vo" 26 "github.com/stretchr/testify/require" 27 28 "github.com/kubevela/workflow/pkg/cue/model/value" 29 30 configcontext "github.com/oam-dev/kubevela/pkg/config/context" 31 "github.com/oam-dev/kubevela/pkg/cue/script" 32 nacosmock "github.com/oam-dev/kubevela/test/mock/nacos" 33 ) 34 35 func TestNacosWriter(t *testing.T) { 36 r := require.New(t) 37 v, err := value.NewValue(` 38 nacos: { 39 endpoint: { 40 name: "test-nacos-server" 41 } 42 format: "json" 43 } 44 `, nil, "") 45 r.Equal(err, nil) 46 ewc := &ExpandedWriterConfig{} 47 parseNacosConfig(v, ewc) 48 r.Equal(ewc.Nacos.Endpoint.Name, "test-nacos-server") 49 r.Equal(ewc.Nacos.Format, "json") 50 renderContext := configcontext.ConfigRenderContext{Name: "nacos-config", Namespace: "vela"} 51 data, err := renderNacos(ewc.Nacos, script.CUE(` 52 template: { 53 nacos: { 54 // The endpoint can not references the parameter. 55 endpoint: { 56 // Users must create a config base the nacos-server template firstly. 57 name: "test-nacos-server" 58 } 59 format: parameter.contentType 60 61 // could references the parameter 62 metadata: { 63 dataId: parameter.dataId 64 group: parameter.group 65 if parameter.appName != _|_ { 66 appName: parameter.appName 67 } 68 if parameter.namespaceId != _|_ { 69 namespaceId: parameter.namespaceId 70 } 71 if parameter.tenant != _|_ { 72 tenant: parameter.tenant 73 } 74 if parameter.tag != _|_ { 75 tag: parameter.tag 76 } 77 } 78 content: parameter.content 79 } 80 parameter: { 81 // +usage=Configuration ID 82 dataId: string 83 // +usage=Configuration group 84 group: *"DEFAULT_GROUP" | string 85 // +usage=The configuration content. 86 content: { 87 ... 88 } 89 contentType: *"json" | "yaml" | "properties" | "toml" 90 // +usage=The app name of the configuration 91 appName?: string 92 // +usage=The namespaceId of the configuration 93 namespaceId?: string 94 // +usage=The tenant, corresponding to the namespace ID field of Nacos 95 tenant?: string 96 // +usage=The tag of the configuration 97 tag?: string 98 } 99 } 100 `), renderContext, map[string]interface{}{ 101 "dataId": "hello", 102 "content": map[string]interface{}{ 103 "c1": 1, 104 }, 105 "contentType": "properties", 106 "appName": "appName", 107 "namespaceId": "namespaceId", 108 "tenant": "tenant", 109 "tag": "tag", 110 }) 111 r.Equal(err, nil) 112 113 ctl := gomock.NewController(t) 114 nacosClient := nacosmock.NewMockIConfigClient(ctl) 115 data.Client = nacosClient 116 nacosClient.EXPECT().PublishConfig(gomock.Eq(vo.ConfigParam{ 117 DataId: "hello", 118 Group: "DEFAULT_GROUP", 119 Content: "c1 = 1\n", 120 Tag: "tag", 121 AppName: "appName", 122 Type: "properties", 123 })).Return(true, nil) 124 125 err = data.write(context.TODO(), func(ctx context.Context, namespace, name string) (map[string]interface{}, error) { 126 if name == "test-nacos-server" { 127 return map[string]interface{}{ 128 "servers": []interface{}{ 129 map[string]interface{}{ 130 "ipAddr": "127.0.0.1", 131 "port": 8849, 132 }, 133 }, 134 "client": map[string]interface{}{ 135 "endpoint": "", 136 "username": "", 137 "password": "", 138 "accessKey": "accessKey", 139 "secretKey": "secretKey", 140 }, 141 }, nil 142 } 143 return nil, errors.New("config not found") 144 }) 145 r.Equal(err, nil) 146 }