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