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