github.com/polarismesh/polaris@v1.17.8/common/model/routing_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package model 19 20 import ( 21 "strings" 22 "testing" 23 24 "github.com/golang/protobuf/proto" 25 apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" 26 "github.com/stretchr/testify/assert" 27 "google.golang.org/protobuf/types/known/anypb" 28 29 "github.com/polarismesh/polaris/common/utils" 30 ) 31 32 func TestParseRouteRuleFromAPI(t *testing.T) { 33 ruleRouting := &apitraffic.RuleRoutingConfig{ 34 Sources: []*apitraffic.SourceService{ 35 { 36 Service: "testSvc", 37 Namespace: "test", 38 }, 39 }, 40 } 41 anyValue, err := anypb.New(proto.MessageV2(ruleRouting)) 42 assert.Nil(t, err) 43 routing := &apitraffic.RouteRule{ 44 Id: "ssss", RoutingPolicy: apitraffic.RoutingPolicy_RulePolicy, RoutingConfig: anyValue} 45 rConfig := &RouterConfig{} 46 err = rConfig.ParseRouteRuleFromAPI(routing) 47 assert.Nil(t, err) 48 assert.True(t, true, strings.HasPrefix(rConfig.Config, "{")) 49 } 50 51 func TestToExpendRoutingConfig(t *testing.T) { 52 ruleRouting := &apitraffic.RuleRoutingConfig{ 53 Sources: []*apitraffic.SourceService{ 54 { 55 Service: "testSvc", 56 Namespace: "test", 57 }, 58 }, 59 } 60 // 1. check json text 61 text, err := utils.MarshalToJsonString(ruleRouting) 62 assert.Nil(t, err) 63 rConfig := &RouterConfig{} 64 rConfig.Config = text 65 rConfig.Policy = "RulePolicy" 66 erConfig, err := rConfig.ToExpendRoutingConfig() 67 assert.Nil(t, err) 68 assert.Equal(t, ruleRouting.Sources[0].Service, erConfig.RuleRouting.Sources[0].Service) 69 70 // 2. check v1 binary 71 anyValue, err := anypb.New(proto.MessageV2(ruleRouting)) 72 assert.Nil(t, err) 73 v1AnyStr := string(anyValue.GetValue()) 74 rConfig.Config = v1AnyStr 75 erConfig, err = rConfig.ToExpendRoutingConfig() 76 assert.Nil(t, err) 77 assert.Equal(t, ruleRouting.Sources[0].Service, erConfig.RuleRouting.Sources[0].Service) 78 79 // 3. check v2 binary 80 //ruleRoutingV2 := &v2.RuleRoutingConfig{ 81 // Sources: []*v2.Source{ 82 // { 83 // Service: "testSvc", 84 // Namespace: "test", 85 // Arguments: []*v2.SourceMatch{}, 86 // }, 87 // }, 88 //} 89 //anyValue, err = anypb.New(proto.MessageV2(ruleRoutingV2)) 90 //assert.Nil(t, err) 91 //v2AnyStr := string(anyValue.GetValue()) 92 //rConfig.Config = v2AnyStr 93 //erConfig, err = rConfig.ToExpendRoutingConfig() 94 //assert.Nil(t, err) 95 //assert.Equal(t, ruleRoutingV2.Sources[0].Service, erConfig.RuleRouting.Sources[0].Service) 96 //assert.Equal(t, v1AnyStr, v2AnyStr) 97 }