@@ -22,24 +22,26 @@ package client
2222
2323import (
2424 "fmt"
25+ "regexp"
2526 "testing"
2627 "time"
2728
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"
3329 "github.com/golang/protobuf/proto"
34- anypb "github.com/golang/protobuf/ptypes/any"
35- wrapperspb "github.com/golang/protobuf/ptypes/wrappers"
3630 "github.com/google/go-cmp/cmp"
3731 "github.com/google/go-cmp/cmp/cmpopts"
38-
3932 "google.golang.org/grpc/internal/xds/env"
4033 "google.golang.org/grpc/xds/internal/httpfilter"
4134 "google.golang.org/grpc/xds/internal/version"
4235 "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"
4345)
4446
4547func (s ) TestRDSGenerateRDSUpdateFromRouteConfiguration (t * testing.T ) {
@@ -915,6 +917,51 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
915917 }},
916918 wantErr : false ,
917919 },
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+ },
918965 {
919966 name : "query is ignored" ,
920967 routes : []* v3routepb.Route {
@@ -960,6 +1007,44 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
9601007 },
9611008 wantErr : true ,
9621009 },
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+ },
9631048 {
9641049 name : "unrecognized header match specifier" ,
9651050 routes : []* v3routepb.Route {
@@ -1063,7 +1148,7 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
10631148 }
10641149
10651150 cmpOpts := []cmp.Option {
1066- cmp .AllowUnexported (Route {}, HeaderMatcher {}, Int64Range {}),
1151+ cmp .AllowUnexported (Route {}, HeaderMatcher {}, Int64Range {}, regexp. Regexp {} ),
10671152 cmpopts .EquateEmpty (),
10681153 cmp .Transformer ("FilterConfig" , func (fc httpfilter.FilterConfig ) string {
10691154 return fmt .Sprint (fc )
@@ -1074,17 +1159,15 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
10741159 t .Run (tt .name , func (t * testing.T ) {
10751160 oldFI := env .FaultInjectionSupport
10761161 env .FaultInjectionSupport = ! tt .disableFI
1162+ defer func () { env .FaultInjectionSupport = oldFI }()
10771163
10781164 got , err := routesProtoToSlice (tt .routes , nil , false )
10791165 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 )
10821167 }
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 )
10851170 }
1086-
1087- env .FaultInjectionSupport = oldFI
10881171 })
10891172 }
10901173}
0 commit comments