testutils.go 2.2 KB

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