utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 test contains test only functions for package admin. It's used by
  19. // admin/admin_test.go and admin/test/admin_test.go.
  20. package test
  21. import (
  22. "context"
  23. "net"
  24. "testing"
  25. "time"
  26. "github.com/google/uuid"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/admin"
  29. "google.golang.org/grpc/codes"
  30. "google.golang.org/grpc/credentials/insecure"
  31. "google.golang.org/grpc/internal/testutils/xds/bootstrap"
  32. "google.golang.org/grpc/status"
  33. v3statusgrpc "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
  34. v3statuspb "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
  35. channelzgrpc "google.golang.org/grpc/channelz/grpc_channelz_v1"
  36. channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
  37. )
  38. const (
  39. defaultTestTimeout = 10 * time.Second
  40. )
  41. // ExpectedStatusCodes contains the expected status code for each RPC (can be
  42. // OK).
  43. type ExpectedStatusCodes struct {
  44. ChannelzCode codes.Code
  45. CSDSCode codes.Code
  46. }
  47. // RunRegisterTests makes a client, runs the RPCs, and compares the status
  48. // codes.
  49. func RunRegisterTests(t *testing.T, ec ExpectedStatusCodes) {
  50. nodeID := uuid.New().String()
  51. bootstrapCleanup, err := bootstrap.CreateFile(bootstrap.Options{
  52. NodeID: nodeID,
  53. ServerURI: "no.need.for.a.server",
  54. })
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer bootstrapCleanup()
  59. lis, err := net.Listen("tcp", "localhost:0")
  60. if err != nil {
  61. t.Fatalf("cannot create listener: %v", err)
  62. }
  63. server := grpc.NewServer()
  64. defer server.Stop()
  65. cleanup, err := admin.Register(server)
  66. if err != nil {
  67. t.Fatalf("failed to register admin: %v", err)
  68. }
  69. defer cleanup()
  70. go func() {
  71. server.Serve(lis)
  72. }()
  73. conn, err := grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
  74. if err != nil {
  75. t.Fatalf("cannot connect to server: %v", err)
  76. }
  77. t.Run("channelz", func(t *testing.T) {
  78. if err := RunChannelz(conn); status.Code(err) != ec.ChannelzCode {
  79. t.Fatalf("%s RPC failed with error %v, want code %v", "channelz", err, ec.ChannelzCode)
  80. }
  81. })
  82. t.Run("csds", func(t *testing.T) {
  83. if err := RunCSDS(conn); status.Code(err) != ec.CSDSCode {
  84. t.Fatalf("%s RPC failed with error %v, want code %v", "CSDS", err, ec.CSDSCode)
  85. }
  86. })
  87. }
  88. // RunChannelz makes a channelz RPC.
  89. func RunChannelz(conn *grpc.ClientConn) error {
  90. c := channelzgrpc.NewChannelzClient(conn)
  91. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  92. defer cancel()
  93. _, err := c.GetTopChannels(ctx, &channelzpb.GetTopChannelsRequest{}, grpc.WaitForReady(true))
  94. return err
  95. }
  96. // RunCSDS makes a CSDS RPC.
  97. func RunCSDS(conn *grpc.ClientConn) error {
  98. c := v3statusgrpc.NewClientStatusDiscoveryServiceClient(conn)
  99. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  100. defer cancel()
  101. _, err := c.FetchClientStatus(ctx, &v3statuspb.ClientStatusRequest{}, grpc.WaitForReady(true))
  102. return err
  103. }