Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the package v3router/judger test coverage rate reach 80% #1260

Merged
merged 4 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions cluster/router/v3router/judger/attachment_match_judger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,34 @@ type AttachmentMatchJudger struct {
// nolint
func (amj *AttachmentMatchJudger) Judge(invocation protocol.Invocation) bool {
invAttaMap := invocation.Attachments()
if amj.EagleeyeContext != nil {
for k, v := range amj.EagleeyeContext {
invAttaValue, ok := invAttaMap[k]
if !ok {
if v.Empty == "" {
return false
}
continue
}
// exist this key
str, ok := invAttaValue.(string)
if !ok {
return false
}
strJudger := NewStringMatchJudger(v)
if !strJudger.Judge(str) {
return false
}
}
if amj.EagleeyeContext != nil && !judge(amj.EagleeyeContext, invAttaMap) {
return false
}

if amj.DubboContext != nil {
for k, v := range amj.DubboContext {
invAttaValue, ok := invAttaMap[k]
if !ok {
if v.Empty == "" {
return false
}
continue
}
// exist this key
str, ok := invAttaValue.(string)
if !ok {
return false
}
strJudger := NewStringMatchJudger(v)
if !strJudger.Judge(str) {
if amj.DubboContext != nil && !judge(amj.DubboContext, invAttaMap) {
Mulavar marked this conversation as resolved.
Show resolved Hide resolved
return false
}

return true
}

func judge(condition map[string]*config.StringMatch, invAttaMap map[string]interface{}) bool {
for k, v := range condition {
invAttaValue, ok := invAttaMap[k]
if !ok {
if v.Empty == "" {
return false
}
continue
}
// exist this key
str, ok := invAttaValue.(string)
if !ok {
return false
}
strJudger := NewStringMatchJudger(v)
if !strJudger.Judge(str) {
gaoxinge marked this conversation as resolved.
Show resolved Hide resolved
return false
}
}

Expand Down
15 changes: 10 additions & 5 deletions cluster/router/v3router/judger/attachment_match_judger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ import (
)

func TestAttachmentMatchJudger(t *testing.T) {
dubboCtxMap := make(map[string]*config.StringMatch)
conditionMap := make(map[string]*config.StringMatch)
dubboIvkMap := make(map[string]interface{})
dubboCtxMap["test-key"] = &config.StringMatch{
conditionMap["test-key"] = &config.StringMatch{
Exact: "abc",
}
dubboIvkMap["test-key"] = "abc"
assert.True(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
DubboContext: dubboCtxMap,
DubboContext: conditionMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))
assert.True(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
EagleeyeContext: conditionMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))

dubboIvkMap["test-key"] = "abd"
assert.False(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
DubboContext: dubboCtxMap,
DubboContext: conditionMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))
assert.False(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
EagleeyeContext: conditionMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))

}
34 changes: 34 additions & 0 deletions cluster/router/v3router/judger/list_string_match_judger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 judger

import (
"dubbo.apache.org/dubbo-go/v3/config"
Mulavar marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/assert"
"testing"
)

func TestListStringMatchJudger(t *testing.T) {
assert.True(t, newListStringMatchJudger(&config.ListStringMatch{
Oneof: []*config.StringMatch{{Exact: "abd"}},
}).Judge("abd"))

assert.False(t, newListStringMatchJudger(&config.ListStringMatch{
Oneof: []*config.StringMatch{{Exact: "abc"}},
}).Judge("abd"))
}
1 change: 1 addition & 0 deletions cluster/router/v3router/judger/method_match_judger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (mmj *MethodMatchJudger) Judge(invocation protocol.Invocation) bool {
if !newListStringMatchJudger(v.StrValue).Judge(value.String()) {
return false
}
// FIXME int invoke Float may cause panic
case "float", "int":
// todo now numbers Must not be zero, else it will ignore this match
if !newListDoubleMatchJudger(v.NumValue).Judge(value.Float()) {
Expand Down
72 changes: 72 additions & 0 deletions cluster/router/v3router/judger/method_match_judger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 judger

import (
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
Mulavar marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

func TestMethodMatchJudger(t *testing.T) {

methodArgs := make([]*config.DubboMethodArg, 0)
Mulavar marked this conversation as resolved.
Show resolved Hide resolved
methodArgs = append(methodArgs, &config.DubboMethodArg{
Index: 1,
Type: "string",
StrValue: &config.ListStringMatch{Oneof: []*config.StringMatch{{Exact: "hello world"}}},
NumValue: nil,
BoolValue: nil,
})
methodArgs = append(methodArgs, &config.DubboMethodArg{
Index: 2,
Type: "bool",
StrValue: nil,
NumValue: nil,
BoolValue: &config.BoolMatch{Exact: true},
})
methodArgs = append(methodArgs, &config.DubboMethodArg{
Index: 3,
Type: "float64",
StrValue: nil,
NumValue: &config.ListDoubleMatch{Oneof: []*config.DoubleMatch{{Exact: 10}}},
BoolValue: nil,
})

methodMatch := &config.DubboMethodMatch{
NameMatch: &config.StringMatch{Exact: "Greet"},
Argc: 3,
Args: methodArgs,
Argp: nil,
Headers: nil,
}

stringValue := reflect.ValueOf("hello world")
boolValue := reflect.ValueOf(true)
numValue := reflect.ValueOf(10.0)
ivc := invocation.NewRPCInvocationWithOptions(
invocation.WithMethodName("Greet"),
invocation.WithParameterValues([]reflect.Value{stringValue, boolValue, numValue}),
)

assert.False(t, NewMethodMatchJudger(&config.DubboMethodMatch{NameMatch: &config.StringMatch{Exact: "Great"}}).Judge(ivc))
assert.False(t, NewMethodMatchJudger(&config.DubboMethodMatch{NameMatch: &config.StringMatch{Exact: "Greet"}, Argc: 1}).Judge(ivc))
assert.True(t, NewMethodMatchJudger(methodMatch).Judge(ivc))
}
35 changes: 35 additions & 0 deletions cluster/router/v3router/judger/url_label_match_judge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 judger

import (
"dubbo.apache.org/dubbo-go/v3/common"
Mulavar marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/assert"
"testing"
)

func TestJudgeUrlLabel(t *testing.T) {
url := common.NewURLWithOptions(common.WithParamsValue("a", "A"))

labels := make(map[string]string)
labels["a"] = "A"
assert.True(t, JudgeUrlLabel(url, labels))

labels["a"] = "B"
assert.False(t, JudgeUrlLabel(url, labels))
}
6 changes: 6 additions & 0 deletions cluster/router/v3router/router_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func parseFromConfigToRouters(virtualServiceConfig, destinationRuleConfig []byte

vsDecoder := yaml.NewDecoder(strings.NewReader(string(virtualServiceConfig)))
drDecoder := yaml.NewDecoder(strings.NewReader(string(destinationRuleConfig)))
// parse virtual service
for {
virtualServiceCfg := &config.VirtualServiceConfig{}

Expand All @@ -195,6 +196,7 @@ func parseFromConfigToRouters(virtualServiceConfig, destinationRuleConfig []byte
virtualServiceConfigList = append(virtualServiceConfigList, virtualServiceCfg)
}

// parse destination rule
for {
destRuleCfg := &config.DestinationRuleConfig{}
err := drDecoder.Decode(destRuleCfg)
Expand All @@ -205,10 +207,14 @@ func parseFromConfigToRouters(virtualServiceConfig, destinationRuleConfig []byte
logger.Error("parseFromConfigTo destination rule err = ", err)
return nil, err
}

// name -> labels
destRuleCfgMap := make(map[string]map[string]string)
for _, v := range destRuleCfg.Spec.SubSets {
destRuleCfgMap[v.Name] = v.Labels
}

// host -> name -> labels
destRuleConfigsMap[destRuleCfg.Spec.Host] = destRuleCfgMap
}

Expand Down
4 changes: 2 additions & 2 deletions metadata/report/etcd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (e *etcdMetadataReport) GetAppMetadata(metadataIdentifier *identifier.Subsc
if err != nil {
return nil, err
}

info := &common.MetadataInfo{}
return info, json.Unmarshal([]byte(data), info)
}
Expand All @@ -70,7 +70,7 @@ func (e *etcdMetadataReport) PublishAppMetadata(metadataIdentifier *identifier.S
if err == nil {
err = e.client.Put(key, string(value))
}

return err
}

Expand Down