google.golang.org/grpc@v1.72.2/credentials/google/xds_test.go (about)

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package google
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  
    25  	"google.golang.org/grpc/credentials"
    26  	icredentials "google.golang.org/grpc/internal/credentials"
    27  	"google.golang.org/grpc/internal/xds"
    28  	"google.golang.org/grpc/resolver"
    29  )
    30  
    31  func (s) TestIsDirectPathCluster(t *testing.T) {
    32  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    33  	defer cancel()
    34  	c := func(cluster string) context.Context {
    35  		return icredentials.NewClientHandshakeInfoContext(ctx, credentials.ClientHandshakeInfo{
    36  			Attributes: xds.SetXDSHandshakeClusterName(resolver.Address{}, cluster).Attributes,
    37  		})
    38  	}
    39  
    40  	testCases := []struct {
    41  		name string
    42  		ctx  context.Context
    43  		want bool
    44  	}{
    45  		{"not an xDS cluster", ctx, false},
    46  		{"cfe", c("google_cfe_bigtable.googleapis.com"), false},
    47  		{"non-cfe", c("google_bigtable.googleapis.com"), true},
    48  		{"starts with xdstp but not cfe format", c("xdstp:google_cfe_bigtable.googleapis.com"), true},
    49  		{"no authority", c("xdstp:///envoy.config.cluster.v3.Cluster/google_cfe_"), true},
    50  		{"wrong authority", c("xdstp://foo.bar/envoy.config.cluster.v3.Cluster/google_cfe_"), true},
    51  		{"xdstp CFE", c("xdstp://traffic-director-c2p.xds.googleapis.com/envoy.config.cluster.v3.Cluster/google_cfe_"), false},
    52  	}
    53  	for _, tc := range testCases {
    54  		t.Run(tc.name, func(t *testing.T) {
    55  			if got := isDirectPathCluster(tc.ctx); got != tc.want {
    56  				t.Errorf("isDirectPathCluster(_) = %v; want %v", got, tc.want)
    57  			}
    58  		})
    59  	}
    60  }