forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rls_test.go
168 lines (157 loc) · 5.07 KB
/
rls_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
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
/*
*
* Copyright 2021 gRPC authors.
*
* Licensed 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 rls
import (
"encoding/json"
"testing"
"github.com/golang/protobuf/proto"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
_ "google.golang.org/grpc/balancer/rls"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/proto/grpc_lookup_v1"
"google.golang.org/grpc/internal/testutils"
_ "google.golang.org/grpc/xds/internal/balancer/cdsbalancer"
"google.golang.org/grpc/xds/internal/clusterspecifier"
"google.golang.org/protobuf/types/known/durationpb"
)
func init() {
clusterspecifier.Register(rls{})
}
type s struct {
grpctest.Tester
}
func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}
// TestParseClusterSpecifierConfig tests the parsing functionality of the RLS
// Cluster Specifier Plugin.
func (s) TestParseClusterSpecifierConfig(t *testing.T) {
tests := []struct {
name string
rlcs proto.Message
wantConfig clusterspecifier.BalancerConfig
wantErr bool
}{
{
name: "invalid-rls-cluster-specifier",
rlcs: rlsClusterSpecifierConfigError,
wantErr: true,
},
{
name: "valid-rls-cluster-specifier",
rlcs: rlsClusterSpecifierConfigWithoutTransformations,
wantConfig: configWithoutTransformationsWant,
},
}
for _, test := range tests {
cs := clusterspecifier.Get("type.googleapis.com/grpc.lookup.v1.RouteLookupClusterSpecifier")
if cs == nil {
t.Fatal("Error getting cluster specifier")
}
lbCfg, err := cs.ParseClusterSpecifierConfig(test.rlcs)
if (err != nil) != test.wantErr {
t.Fatalf("ParseClusterSpecifierConfig(%+v) returned err: %v, wantErr: %v", test.rlcs, err, test.wantErr)
}
if test.wantErr { // Successfully received an error.
return
}
// Marshal and then unmarshal into interface{} to get rid of
// nondeterministic protojson Marshaling.
lbCfgJSON, err := json.Marshal(lbCfg)
if err != nil {
t.Fatalf("json.Marshal(%+v) returned err %v", lbCfg, err)
}
var got interface{}
err = json.Unmarshal(lbCfgJSON, got)
if err != nil {
t.Fatalf("json.Unmarshal(%+v) returned err %v", lbCfgJSON, err)
}
wantCfgJSON, err := json.Marshal(test.wantConfig)
if err != nil {
t.Fatalf("json.Marshal(%+v) returned err %v", test.wantConfig, err)
}
var want interface{}
err = json.Unmarshal(wantCfgJSON, want)
if err != nil {
t.Fatalf("json.Unmarshal(%+v) returned err %v", lbCfgJSON, err)
}
if diff := cmp.Diff(want, got, cmpopts.EquateEmpty()); diff != "" {
t.Fatalf("ParseClusterSpecifierConfig(%+v) returned expected, diff (-want +got) %v", test.rlcs, diff)
}
}
}
// This will error because the required match field is set in grpc key builder.
var rlsClusterSpecifierConfigError = testutils.MarshalAny(&grpc_lookup_v1.RouteLookupClusterSpecifier{
RouteLookupConfig: &grpc_lookup_v1.RouteLookupConfig{
GrpcKeybuilders: []*grpc_lookup_v1.GrpcKeyBuilder{
{
Names: []*grpc_lookup_v1.GrpcKeyBuilder_Name{
{
Service: "service",
Method: "method",
},
},
Headers: []*grpc_lookup_v1.NameMatcher{
{
Key: "k1",
RequiredMatch: true,
Names: []string{"v1"},
},
},
},
},
},
})
// Corresponds to the rls unit test case in
// balancer/rls/internal/config_test.go.
var rlsClusterSpecifierConfigWithoutTransformations = testutils.MarshalAny(&grpc_lookup_v1.RouteLookupClusterSpecifier{
RouteLookupConfig: &grpc_lookup_v1.RouteLookupConfig{
GrpcKeybuilders: []*grpc_lookup_v1.GrpcKeyBuilder{
{
Names: []*grpc_lookup_v1.GrpcKeyBuilder_Name{
{
Service: "service",
Method: "method",
},
},
Headers: []*grpc_lookup_v1.NameMatcher{
{
Key: "k1",
Names: []string{"v1"},
},
},
},
},
LookupService: "target",
LookupServiceTimeout: &durationpb.Duration{Seconds: 100},
MaxAge: &durationpb.Duration{Seconds: 60},
StaleAge: &durationpb.Duration{Seconds: 50},
CacheSizeBytes: 1000,
DefaultTarget: "passthrough:///default",
},
})
var configWithoutTransformationsWant = clusterspecifier.BalancerConfig{{"rls_experimental": &lbConfigJSON{
RouteLookupConfig: []byte(`{"grpcKeybuilders":[{"names":[{"service":"service","method":"method"}],"headers":[{"key":"k1","names":["v1"]}]}],"lookupService":"target","lookupServiceTimeout":"100s","maxAge":"60s","staleAge":"50s","cacheSizeBytes":"1000","defaultTarget":"passthrough:///default"}`),
ChildPolicy: []map[string]json.RawMessage{
{
"cds_experimental": []byte(`{}`),
},
},
ChildPolicyConfigTargetFieldName: "cluster",
}}}