@@ -22,24 +22,26 @@ package client
22
22
23
23
import (
24
24
"fmt"
25
+ "regexp"
25
26
"testing"
26
27
"time"
27
28
28
- v2xdspb "github.com/envoyproxy/go-control-plane/envoy/api/v2"
29
- v2routepb "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
30
- v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
31
- v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
32
- v3typepb "github.com/envoyproxy/go-control-plane/envoy/type/v3"
33
29
"github.com/golang/protobuf/proto"
34
- anypb "github.com/golang/protobuf/ptypes/any"
35
- wrapperspb "github.com/golang/protobuf/ptypes/wrappers"
36
30
"github.com/google/go-cmp/cmp"
37
31
"github.com/google/go-cmp/cmp/cmpopts"
38
-
39
32
"google.golang.org/grpc/internal/xds/env"
40
33
"google.golang.org/grpc/xds/internal/httpfilter"
41
34
"google.golang.org/grpc/xds/internal/version"
42
35
"google.golang.org/protobuf/types/known/durationpb"
36
+
37
+ v2xdspb "github.com/envoyproxy/go-control-plane/envoy/api/v2"
38
+ v2routepb "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
39
+ v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
40
+ v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
41
+ v3matcherpb "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
42
+ v3typepb "github.com/envoyproxy/go-control-plane/envoy/type/v3"
43
+ anypb "github.com/golang/protobuf/ptypes/any"
44
+ wrapperspb "github.com/golang/protobuf/ptypes/wrappers"
43
45
)
44
46
45
47
func (s ) TestRDSGenerateRDSUpdateFromRouteConfiguration (t * testing.T ) {
@@ -915,6 +917,51 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
915
917
}},
916
918
wantErr : false ,
917
919
},
920
+ {
921
+ name : "good with regex matchers" ,
922
+ routes : []* v3routepb.Route {
923
+ {
924
+ Match : & v3routepb.RouteMatch {
925
+ PathSpecifier : & v3routepb.RouteMatch_SafeRegex {SafeRegex : & v3matcherpb.RegexMatcher {Regex : "/a/" }},
926
+ Headers : []* v3routepb.HeaderMatcher {
927
+ {
928
+ Name : "th" ,
929
+ HeaderMatchSpecifier : & v3routepb.HeaderMatcher_SafeRegexMatch {SafeRegexMatch : & v3matcherpb.RegexMatcher {Regex : "tv" }},
930
+ },
931
+ },
932
+ RuntimeFraction : & v3corepb.RuntimeFractionalPercent {
933
+ DefaultValue : & v3typepb.FractionalPercent {
934
+ Numerator : 1 ,
935
+ Denominator : v3typepb .FractionalPercent_HUNDRED ,
936
+ },
937
+ },
938
+ },
939
+ Action : & v3routepb.Route_Route {
940
+ Route : & v3routepb.RouteAction {
941
+ ClusterSpecifier : & v3routepb.RouteAction_WeightedClusters {
942
+ WeightedClusters : & v3routepb.WeightedCluster {
943
+ Clusters : []* v3routepb.WeightedCluster_ClusterWeight {
944
+ {Name : "B" , Weight : & wrapperspb.UInt32Value {Value : 60 }},
945
+ {Name : "A" , Weight : & wrapperspb.UInt32Value {Value : 40 }},
946
+ },
947
+ TotalWeight : & wrapperspb.UInt32Value {Value : 100 },
948
+ }}}},
949
+ },
950
+ },
951
+ wantRoutes : []* Route {{
952
+ Regex : func () * regexp.Regexp { return regexp .MustCompile ("/a/" ) }(),
953
+ Headers : []* HeaderMatcher {
954
+ {
955
+ Name : "th" ,
956
+ InvertMatch : newBoolP (false ),
957
+ RegexMatch : func () * regexp.Regexp { return regexp .MustCompile ("tv" ) }(),
958
+ },
959
+ },
960
+ Fraction : newUInt32P (10000 ),
961
+ WeightedClusters : map [string ]WeightedCluster {"A" : {Weight : 40 }, "B" : {Weight : 60 }},
962
+ }},
963
+ wantErr : false ,
964
+ },
918
965
{
919
966
name : "query is ignored" ,
920
967
routes : []* v3routepb.Route {
@@ -960,6 +1007,44 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
960
1007
},
961
1008
wantErr : true ,
962
1009
},
1010
+ {
1011
+ name : "bad regex in path specifier" ,
1012
+ routes : []* v3routepb.Route {
1013
+ {
1014
+ Match : & v3routepb.RouteMatch {
1015
+ PathSpecifier : & v3routepb.RouteMatch_SafeRegex {SafeRegex : & v3matcherpb.RegexMatcher {Regex : "??" }},
1016
+ Headers : []* v3routepb.HeaderMatcher {
1017
+ {
1018
+ HeaderMatchSpecifier : & v3routepb.HeaderMatcher_PrefixMatch {PrefixMatch : "tv" },
1019
+ },
1020
+ },
1021
+ },
1022
+ Action : & v3routepb.Route_Route {
1023
+ Route : & v3routepb.RouteAction {ClusterSpecifier : & v3routepb.RouteAction_Cluster {Cluster : clusterName }},
1024
+ },
1025
+ },
1026
+ },
1027
+ wantErr : true ,
1028
+ },
1029
+ {
1030
+ name : "bad regex in header specifier" ,
1031
+ routes : []* v3routepb.Route {
1032
+ {
1033
+ Match : & v3routepb.RouteMatch {
1034
+ PathSpecifier : & v3routepb.RouteMatch_Prefix {Prefix : "/a/" },
1035
+ Headers : []* v3routepb.HeaderMatcher {
1036
+ {
1037
+ HeaderMatchSpecifier : & v3routepb.HeaderMatcher_SafeRegexMatch {SafeRegexMatch : & v3matcherpb.RegexMatcher {Regex : "??" }},
1038
+ },
1039
+ },
1040
+ },
1041
+ Action : & v3routepb.Route_Route {
1042
+ Route : & v3routepb.RouteAction {ClusterSpecifier : & v3routepb.RouteAction_Cluster {Cluster : clusterName }},
1043
+ },
1044
+ },
1045
+ },
1046
+ wantErr : true ,
1047
+ },
963
1048
{
964
1049
name : "unrecognized header match specifier" ,
965
1050
routes : []* v3routepb.Route {
@@ -1063,7 +1148,7 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
1063
1148
}
1064
1149
1065
1150
cmpOpts := []cmp.Option {
1066
- cmp .AllowUnexported (Route {}, HeaderMatcher {}, Int64Range {}),
1151
+ cmp .AllowUnexported (Route {}, HeaderMatcher {}, Int64Range {}, regexp. Regexp {} ),
1067
1152
cmpopts .EquateEmpty (),
1068
1153
cmp .Transformer ("FilterConfig" , func (fc httpfilter.FilterConfig ) string {
1069
1154
return fmt .Sprint (fc )
@@ -1074,17 +1159,15 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
1074
1159
t .Run (tt .name , func (t * testing.T ) {
1075
1160
oldFI := env .FaultInjectionSupport
1076
1161
env .FaultInjectionSupport = ! tt .disableFI
1162
+ defer func () { env .FaultInjectionSupport = oldFI }()
1077
1163
1078
1164
got , err := routesProtoToSlice (tt .routes , nil , false )
1079
1165
if (err != nil ) != tt .wantErr {
1080
- t .Errorf ("routesProtoToSlice() error = %v, wantErr %v" , err , tt .wantErr )
1081
- return
1166
+ t .Fatalf ("routesProtoToSlice() error = %v, wantErr %v" , err , tt .wantErr )
1082
1167
}
1083
- if ! cmp .Equal (got , tt .wantRoutes , cmpOpts ... ) {
1084
- t .Errorf ("routesProtoToSlice() got = %v, want %v, diff: %v " , got , tt . wantRoutes , cmp . Diff ( got , tt . wantRoutes , cmpOpts ... ) )
1168
+ if diff := cmp .Diff (got , tt .wantRoutes , cmpOpts ... ); diff != "" {
1169
+ t .Fatalf ("routesProtoToSlice() returned unexpected diff (-got +want): \n %s " , diff )
1085
1170
}
1086
-
1087
- env .FaultInjectionSupport = oldFI
1088
1171
})
1089
1172
}
1090
1173
}
0 commit comments