...

Source file src/google.golang.org/grpc/admin/test/utils.go

Documentation: google.golang.org/grpc/admin/test

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

View as plain text