/
dstmetaRouting_suite.go
269 lines (244 loc) · 9.55 KB
/
dstmetaRouting_suite.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* Tencent is pleased to support the open source community by making polaris-go 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 serviceroute
import (
"fmt"
"log"
"net"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/google/uuid"
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
"github.com/polarismesh/specification/source/go/api/v1/service_manage"
"google.golang.org/grpc"
"gopkg.in/check.v1"
"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/model"
commontest "github.com/polarismesh/polaris-go/test/common"
"github.com/polarismesh/polaris-go/test/mock"
"github.com/polarismesh/polaris-go/test/util"
)
const (
// 测试的默认命名空间
dstMetaNamespace = "dstMetaNs"
// 测试的默认服务名
dstMetaService = "dstMetaSvc"
// 测试服务器的默认地址
dstMetaIPAddress = "127.0.0.1"
// 测试服务器的端口
dstMetaPort = commontest.DestMetadataSuitServerPort
// 测试monitor的默认地址
dstMetaMonitorAddress = "127.0.0.1"
// 测试monitor的端口
dstMetaMonitorPort = 8119
)
const (
// 带元数据的实例数
addMetaCount = 2
// 直接过滤的实例数
normalInstances = 3
)
const (
// 测试直接过滤的KEY
addMetaKey = "env"
// 测试直接过滤的Value
addMetaValue = "test"
// 错误的直接过滤的Value
wrongAddMetaValue = "test1"
)
// 元数据过滤路由插件测试用例
type DstMetaTestingSuite struct {
mockServer mock.NamingServer
grpcServer *grpc.Server
grpcListener net.Listener
serviceToken string
testService *service_manage.Service
}
// 套件名字
func (t *DstMetaTestingSuite) GetName() string {
return "DstMetaTestingSuite"
}
// SetUpSuite 启动测试套程序
func (t *DstMetaTestingSuite) SetUpSuite(c *check.C) {
grpcOptions := make([]grpc.ServerOption, 0)
maxStreams := 100000
grpcOptions = append(grpcOptions, grpc.MaxConcurrentStreams(uint32(maxStreams)))
// get the grpc server wired up
grpc.EnableTracing = true
ipAddr := dstMetaIPAddress
shopPort := dstMetaPort
var err error
t.grpcServer = grpc.NewServer(grpcOptions...)
t.serviceToken = uuid.New().String()
t.mockServer = mock.NewNamingServer()
token := t.mockServer.RegisterServerService(config.ServerDiscoverService)
t.mockServer.RegisterServerInstance(ipAddr, shopPort, config.ServerDiscoverService, token, true)
t.mockServer.RegisterNamespace(&apimodel.Namespace{
Name: &wrappers.StringValue{Value: dstMetaNamespace},
Comment: &wrappers.StringValue{Value: "for consumer api test"},
Owners: &wrappers.StringValue{Value: "ConsumerAPI"},
})
t.mockServer.RegisterServerServices(ipAddr, shopPort)
t.testService = &service_manage.Service{
Name: &wrappers.StringValue{Value: dstMetaService},
Namespace: &wrappers.StringValue{Value: dstMetaNamespace},
Token: &wrappers.StringValue{Value: t.serviceToken},
}
t.mockServer.RegisterService(t.testService)
t.mockServer.GenTestInstances(t.testService, normalInstances)
t.mockServer.GenTestInstancesWithMeta(t.testService, addMetaCount, map[string]string{addMetaKey: addMetaValue})
service_manage.RegisterPolarisGRPCServer(t.grpcServer, t.mockServer)
t.grpcListener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", ipAddr, shopPort))
if err != nil {
log.Fatalf("error listening appserver %v", err)
}
log.Printf("appserver listening on %s:%d\n", ipAddr, shopPort)
go func() {
if err := t.grpcServer.Serve(t.grpcListener); err != nil {
panic(err)
}
}()
}
// SetUpSuite 结束测试套程序
func (t *DstMetaTestingSuite) TearDownSuite(c *check.C) {
t.grpcServer.GracefulStop()
util.InsertLog(t, c.GetTestLog())
}
// 测试正常获取元数据实例
func (t *DstMetaTestingSuite) TestGetMetaNormal(c *check.C) {
cfg := config.NewDefaultConfiguration(
[]string{fmt.Sprintf("%s:%d", dstMetaIPAddress, dstMetaPort)})
cfg.GetConsumer().GetServiceRouter().SetChain([]string{config.DefaultServiceRouterDstMeta})
consumer, err := api.NewConsumerAPIByConfig(cfg)
c.Assert(err, check.IsNil)
defer consumer.Destroy()
request := &api.GetInstancesRequest{}
request.Namespace = dstMetaNamespace
request.Service = dstMetaService
request.Metadata = map[string]string{
addMetaKey: addMetaValue,
}
resp, err := consumer.GetInstances(request)
c.Assert(err, check.IsNil)
c.Assert(len(resp.Instances), check.Equals, addMetaCount)
}
// // 测试正常获取元数据实例
// func (t *DstMetaTestingSuite) TestGetMetaWrong(c *check.C) {
// cfg := config.NewDefaultConfiguration(
// []string{fmt.Sprintf("%s:%d", dstMetaIPAddress, dstMetaPort)})
// cfg.GetConsumer().GetServiceRouter().SetChain([]string{config.DefaultServiceRouterDstMeta})
// consumer, err := api.NewConsumerAPIByConfig(cfg)
// c.Assert(err, check.IsNil)
// c.Assert(consumer, check.NotNil)
// defer consumer.Destroy()
// request := &api.GetInstancesRequest{}
// request.Namespace = dstMetaNamespace
// request.Service = dstMetaService
// request.Metadata = map[string]string{
// addMetaKey: wrongAddMetaValue,
// }
// _, err = consumer.GetInstances(request)
// log.Printf("TestGetMetaWrong.GetInstances : %+v", err.Error())
// c.Assert(err, check.NotNil)
// c.Assert(err.(model.SDKError).ErrorCode(), check.Equals, model.ErrCodeDstMetaMismatch)
// }
// 测试元数据路由兜底策略正确设置类型
func (t *DstMetaTestingSuite) TestFailOverDefaultMetaWrongWithType(c *check.C) {
consumer, err := t.buildFaultOverConsumer()
c.Assert(err, check.IsNil)
// 没有设置type
request := t.buildFaultOverDefaultInstancesRequest()
_, err = consumer.GetOneInstance(request)
c.Assert(err, check.NotNil)
c.Assert(err.(model.SDKError).ErrorCode(), check.Equals, model.ErrCodeAPIInvalidArgument)
}
// 测试元数据路由兜底策略:通配所有可用ip实例
func (t *DstMetaTestingSuite) TestFailOverDefaultMetaNormalWithGetOneHealth(c *check.C) {
consumer, err := t.buildFaultOverConsumer()
c.Assert(err, check.IsNil)
request := t.buildFaultOverDefaultInstancesRequest()
request.FailOverDefaultMeta.Type = model.GetOneHealth
resp, err := consumer.GetOneInstance(request)
c.Assert(err, check.IsNil)
c.Assert(len(resp.Instances), check.Equals, 1)
}
// 测试元数据路由兜底策略:匹配不带 metaData key路由
func (t *DstMetaTestingSuite) TestFailOverDefaultMetaNormalWithNotContainMeta(c *check.C) {
consumer, err := t.buildFaultOverConsumer()
c.Assert(err, check.IsNil)
request := t.buildFaultOverDefaultInstancesRequest()
request.FailOverDefaultMeta.Type = model.NotContainMetaKey
resp, err := consumer.GetOneInstance(request)
c.Assert(err, check.IsNil)
c.Assert(len(resp.Instances), check.Equals, 1)
}
// 测试元数据路由兜底策略:自定义meta
func (t *DstMetaTestingSuite) TestFailOverDefaultMetaNormalWithCustomMeta(c *check.C) {
consumer, err := t.buildFaultOverConsumer()
c.Assert(err, check.IsNil)
request := t.buildFaultOverDefaultInstancesRequest()
request.FailOverDefaultMeta.Type = model.CustomMeta
request.FailOverDefaultMeta.Meta = map[string]string{
addMetaKey: addMetaValue,
}
resp, err := consumer.GetOneInstance(request)
c.Assert(err, check.IsNil)
c.Assert(len(resp.Instances), check.Equals, 1)
c.Assert(resp.Instances[0].GetMetadata()[addMetaKey], check.Equals, addMetaValue)
}
// 测试元数据路由兜底策略:自定义meta
func (t *DstMetaTestingSuite) TestFailOverDefaultMetaWrongWithCustomMeta01(c *check.C) {
consumer, err := t.buildFaultOverConsumer()
c.Assert(err, check.IsNil)
request := t.buildFaultOverDefaultInstancesRequest()
// 没有设置自定义meta
request.FailOverDefaultMeta.Type = model.CustomMeta
_, err = consumer.GetOneInstance(request)
c.Assert(err, check.NotNil)
c.Assert(err.(model.SDKError).ErrorCode(), check.Equals, model.ErrCodeAPIInvalidArgument)
}
// 测试元数据路由兜底策略:自定义meta
func (t *DstMetaTestingSuite) TestFailOverDefaultMetaWrongWithCustomMeta02(c *check.C) {
consumer, err := t.buildFaultOverConsumer()
c.Assert(err, check.IsNil)
request := t.buildFaultOverDefaultInstancesRequest()
// 设置错误的自定义meta依旧找不到
request.FailOverDefaultMeta.Type = model.CustomMeta
request.FailOverDefaultMeta.Meta = map[string]string{
addMetaKey: wrongAddMetaValue,
}
_, err = consumer.GetOneInstance(request)
c.Assert(err, check.NotNil)
c.Assert(err.(model.SDKError).ErrorCode(), check.Equals, model.ErrCodeDstMetaMismatch)
}
func (t *DstMetaTestingSuite) buildFaultOverConsumer() (api.ConsumerAPI, error) {
cfg := config.NewDefaultConfiguration(
[]string{fmt.Sprintf("%s:%d", dstMetaIPAddress, dstMetaPort)})
cfg.GetConsumer().GetServiceRouter().SetChain([]string{config.DefaultServiceRouterDstMeta})
consumer, err := api.NewConsumerAPIByConfig(cfg)
return consumer, err
}
func (t *DstMetaTestingSuite) buildFaultOverDefaultInstancesRequest() *api.GetOneInstanceRequest {
request := &api.GetOneInstanceRequest{}
request.Namespace = dstMetaNamespace
request.Service = dstMetaService
request.Metadata = map[string]string{
addMetaKey: wrongAddMetaValue,
}
request.EnableFailOverDefaultMeta = true
return request
}