forked from polarismesh/polaris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_file_template_test.go
138 lines (120 loc) · 4.94 KB
/
config_file_template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package config_test
import (
"testing"
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/wrapperspb"
api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/utils"
)
const (
templateName1 = "t1"
templateName2 = "t2"
)
// TestConfigFileTemplateCRUD the base test for config file template
func TestConfigFileTemplateCRUD(t *testing.T) {
testSuit := &ConfigCenterTest{}
if err := testSuit.Initialize(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
testSuit.Destroy()
})
defer func() {
if err := testSuit.clearTestData(); err != nil {
t.Fatal(err)
}
}()
t.Run("first-query", func(t *testing.T) {
queryRsp := testSuit.ConfigServer().GetConfigFileTemplate(testSuit.DefaultCtx, templateName1)
assert.Equal(t, api.NotFoundResource, queryRsp.Code.Value)
})
template1 := assembleConfigFileTemplate(templateName1)
t.Run("first-create", func(t *testing.T) {
createRsp := testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, template1)
assert.Equal(t, api.ExecuteSuccess, createRsp.Code.GetValue())
//repeat create
createRsp = testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, template1)
assert.Equal(t, api.ExistedResource, createRsp.Code.GetValue(), createRsp.GetInfo().GetValue())
})
t.Run("second-query", func(t *testing.T) {
queryRsp := testSuit.ConfigServer().GetConfigFileTemplate(testSuit.DefaultCtx, templateName1)
assert.Equal(t, api.ExecuteSuccess, queryRsp.Code.Value)
assert.Equal(t, template1.Name.GetValue(), queryRsp.ConfigFileTemplate.Name.GetValue())
assert.Equal(t, template1.Content.GetValue(), queryRsp.ConfigFileTemplate.Content.GetValue())
assert.Equal(t, template1.Comment.GetValue(), queryRsp.ConfigFileTemplate.Comment.GetValue())
assert.Equal(t, template1.Format.GetValue(), queryRsp.ConfigFileTemplate.Format.GetValue())
})
template2 := assembleConfigFileTemplate(templateName2)
t.Run("second-create", func(t *testing.T) {
createRsp := testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, template2)
assert.Equal(t, api.ExecuteSuccess, createRsp.Code.GetValue())
})
t.Run("query-all", func(t *testing.T) {
rsp := testSuit.ConfigServer().GetAllConfigFileTemplates(testSuit.DefaultCtx)
assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
assert.True(t, 2 <= len(rsp.ConfigFileTemplates))
})
}
func assembleConfigFileTemplate(name string) *apiconfig.ConfigFileTemplate {
return &apiconfig.ConfigFileTemplate{
Name: utils.NewStringValue(name),
Content: utils.NewStringValue("some content"),
Comment: utils.NewStringValue("comment"),
Format: utils.NewStringValue("json"),
CreateBy: utils.NewStringValue("testUser"),
ModifyBy: utils.NewStringValue("testUser"),
}
}
func TestServer_CreateConfigFileTemplateParam(t *testing.T) {
testSuit := newConfigCenterTestSuit(t)
_ = testSuit
var (
mockTemplName = "mock_templname"
mockContent = "mock_content"
)
t.Run("invalid_tpl_name", func(t *testing.T) {
rsp := testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, &apiconfig.ConfigFileTemplate{
Content: wrapperspb.String(mockContent),
})
assert.Equal(t, uint32(apimodel.Code_InvalidConfigFileTemplateName), rsp.Code.GetValue())
})
t.Run("content_too_long", func(t *testing.T) {
mockContentL := "mock_content"
for {
if len(mockContentL) > int(testSuit.GetBootstrapConfig().Config.ContentMaxLength) {
break
}
mockContentL += mockContentL
}
rsp := testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, &apiconfig.ConfigFileTemplate{
Name: wrapperspb.String(mockTemplName),
Content: wrapperspb.String(mockContentL),
})
assert.Equal(t, uint32(apimodel.Code_InvalidConfigFileContentLength), rsp.Code.GetValue(), rsp.GetInfo().GetValue())
})
t.Run("no_content", func(t *testing.T) {
rsp := testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, &apiconfig.ConfigFileTemplate{
Name: wrapperspb.String(mockTemplName),
Content: wrapperspb.String(""),
})
assert.Equal(t, uint32(apimodel.Code_BadRequest), rsp.Code.GetValue())
})
}