...

Source file src/google.golang.org/grpc/xds/internal/testutils/testutils.go

Documentation: google.golang.org/grpc/xds/internal/testutils

     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  // Package testutils provides utility types, for use in xds tests.
    19  package testutils
    20  
    21  import (
    22  	"fmt"
    23  	"testing"
    24  
    25  	"google.golang.org/grpc/internal/xds/bootstrap"
    26  	"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
    27  	"google.golang.org/grpc/xds/internal/xdsclient/xdsresource/version"
    28  )
    29  
    30  // BuildResourceName returns the resource name in the format of an xdstp://
    31  // resource.
    32  func BuildResourceName(typeName, auth, id string, ctxParams map[string]string) string {
    33  	var typS string
    34  	switch typeName {
    35  	case xdsresource.ListenerResourceTypeName:
    36  		typS = version.V3ListenerType
    37  	case xdsresource.RouteConfigTypeName:
    38  		typS = version.V3RouteConfigType
    39  	case xdsresource.ClusterResourceTypeName:
    40  		typS = version.V3ClusterType
    41  	case xdsresource.EndpointsResourceTypeName:
    42  		typS = version.V3EndpointsType
    43  	default:
    44  		// If the name doesn't match any of the standard resources fallback
    45  		// to the type name.
    46  		typS = typeName
    47  	}
    48  	return (&xdsresource.Name{
    49  		Scheme:        "xdstp",
    50  		Authority:     auth,
    51  		Type:          typS,
    52  		ID:            id,
    53  		ContextParams: ctxParams,
    54  	}).String()
    55  }
    56  
    57  // ServerConfigForAddress returns a bootstrap.ServerConfig for the given address
    58  // with default values of insecure channel_creds and v3 server_features.
    59  func ServerConfigForAddress(t *testing.T, addr string) *bootstrap.ServerConfig {
    60  	t.Helper()
    61  
    62  	jsonCfg := fmt.Sprintf(`{
    63  		"server_uri": "%s",
    64  		"channel_creds": [{"type": "insecure"}],
    65  		"server_features": ["xds_v3"]
    66  	}`, addr)
    67  	sc, err := bootstrap.ServerConfigFromJSON([]byte(jsonCfg))
    68  	if err != nil {
    69  		t.Fatalf("Failed to create server config from JSON %s: %v", jsonCfg, err)
    70  	}
    71  	return sc
    72  }
    73  

View as plain text