istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/xds/filters/filters_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package filters 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/davecgh/go-spew/spew" 22 router "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/router/v3" 23 hcm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3" 24 25 "istio.io/istio/pilot/pkg/util/protoconv" 26 "istio.io/istio/pkg/wellknown" 27 ) 28 29 func TestBuildRouterFilter(t *testing.T) { 30 tests := []struct { 31 name string 32 ctx RouterFilterContext 33 expected *hcm.HttpFilter 34 }{ 35 { 36 name: "test for build router filter", 37 ctx: RouterFilterContext{StartChildSpan: true}, 38 expected: &hcm.HttpFilter{ 39 Name: wellknown.Router, 40 ConfigType: &hcm.HttpFilter_TypedConfig{ 41 TypedConfig: protoconv.MessageToAny(&router.Router{ 42 StartChildSpan: true, 43 }), 44 }, 45 }, 46 }, 47 { 48 name: "both true", 49 ctx: RouterFilterContext{ 50 StartChildSpan: true, 51 SuppressDebugHeaders: true, 52 }, 53 expected: &hcm.HttpFilter{ 54 Name: wellknown.Router, 55 ConfigType: &hcm.HttpFilter_TypedConfig{ 56 TypedConfig: protoconv.MessageToAny(&router.Router{ 57 StartChildSpan: true, 58 SuppressEnvoyHeaders: true, 59 }), 60 }, 61 }, 62 }, 63 { 64 name: "test for build router filter with start child span false", 65 ctx: RouterFilterContext{StartChildSpan: false}, 66 expected: &hcm.HttpFilter{ 67 Name: wellknown.Router, 68 ConfigType: &hcm.HttpFilter_TypedConfig{ 69 TypedConfig: protoconv.MessageToAny(&router.Router{ 70 StartChildSpan: false, 71 }), 72 }, 73 }, 74 }, 75 } 76 77 for _, tt := range tests { 78 result := BuildRouterFilter(tt.ctx) 79 if !reflect.DeepEqual(result, tt.expected) { 80 t.Errorf("Test %s failed, expected: %v ,got: %v", tt.name, spew.Sdump(result), spew.Sdump(tt.expected)) 81 } 82 } 83 }