istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/xds/rds_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  package xds_test
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
    21  
    22  	v3 "istio.io/istio/pilot/pkg/xds/v3"
    23  	"istio.io/istio/pilot/test/xds"
    24  )
    25  
    26  func TestRDS(t *testing.T) {
    27  	tests := []struct {
    28  		name   string
    29  		node   string
    30  		routes []string
    31  	}{
    32  		{
    33  			"sidecar_new",
    34  			sidecarID(app3Ip, "app3"),
    35  			[]string{"80", "8080"},
    36  		},
    37  		{
    38  			"gateway_new",
    39  			gatewayID(gatewayIP),
    40  			[]string{"http.80", "https.443.https.my-gateway.testns"},
    41  		},
    42  		{
    43  			// Even if we get a bad route, we should still send Envoy an empty response, rather than
    44  			// ignore it. If we ignore the route, the listeners can get stuck waiting forever.
    45  			"sidecar_badroute",
    46  			sidecarID(app3Ip, "app3"),
    47  			[]string{"ht&p"},
    48  		},
    49  	}
    50  
    51  	s := xds.NewFakeDiscoveryServer(t, xds.FakeOptions{})
    52  	for _, tt := range tests {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			ads := s.ConnectADS().WithType(v3.RouteType).WithID(tt.node)
    55  			ads.RequestResponseAck(t, &discovery.DiscoveryRequest{ResourceNames: tt.routes})
    56  		})
    57  	}
    58  }
    59  
    60  const (
    61  	app3Ip    = "10.2.0.1"
    62  	gatewayIP = "10.3.0.1"
    63  )
    64  
    65  // Common code for the xds testing.
    66  // The tests in this package use an in-process pilot using mock service registry and
    67  // envoy.
    68  
    69  func sidecarID(ip, deployment string) string { // nolint: unparam
    70  	return fmt.Sprintf("sidecar~%s~%s-644fc65469-96dza.testns~testns.svc.cluster.local", ip, deployment)
    71  }
    72  
    73  func gatewayID(ip string) string { //nolint: unparam
    74  	return fmt.Sprintf("router~%s~istio-gateway-644fc65469-96dzt.istio-system~istio-system.svc.cluster.local", ip)
    75  }