Skip to content

Commit

Permalink
[admin_api] t refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Mar 18, 2021
1 parent 294d95b commit d41a582
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 49 deletions.
20 changes: 5 additions & 15 deletions admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@ import (
)

func TestRegisterNoCSDS(t *testing.T) {
if err := test.RunRegisterTests([]test.RegisterTestCase{
{
Name: "channelz",
Run: test.RunChannelz,
Code: codes.OK,
},
{
Name: "CSDS",
Run: test.RunCSDS,
// CSDS is not registered because xDS isn't imported.
Code: codes.Unimplemented,
},
}); err != nil {
t.Fatal(err)
}
test.RunRegisterTests(t, test.ExpectedStatusCodes{
ChannelzCode: codes.OK,
// CSDS is not registered because xDS isn't imported.
CSDSCode: codes.Unimplemented,
})
}
18 changes: 4 additions & 14 deletions admin/test/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,8 @@ import (
)

func TestRegisterWithCSDS(t *testing.T) {
if err := test.RunRegisterTests([]test.RegisterTestCase{
{
Name: "channelz",
Run: test.RunChannelz,
Code: codes.OK,
},
{
Name: "CSDS",
Run: test.RunCSDS,
Code: codes.OK,
},
}); err != nil {
t.Fatal(err)
}
test.RunRegisterTests(t, test.ExpectedStatusCodes{
ChannelzCode: codes.OK,
CSDSCode: codes.OK,
})
}
37 changes: 17 additions & 20 deletions admin/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package test

import (
"context"
"fmt"
"net"
"testing"
"time"

v3statuspb "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
Expand All @@ -40,41 +40,37 @@ const (
defaultTestTimeout = 10 * time.Second
)

// RegisterTestCase contains the function to make an RPC and the expected status code
// (can be OK).
type RegisterTestCase struct {
// Name of this RPC.
Name string
// The function to make the RPC on the ClientConn.
Run func(*grpc.ClientConn) error
// The expected code returned from the RPC.
Code codes.Code
// ExpectedStatusCodes contains the expected status code for each RPC (can be
// OK).
type ExpectedStatusCodes struct {
ChannelzCode codes.Code
CSDSCode codes.Code
}

// RunRegisterTests makes a client, runs the RPCs, and compares the status
// codes.
func RunRegisterTests(rcs []RegisterTestCase) error {
func RunRegisterTests(t *testing.T, ec ExpectedStatusCodes) {
nodeID := uuid.New().String()
bootstrapCleanup, err := xds.SetupBootstrapFile(xds.BootstrapOptions{
Version: xds.TransportV3,
NodeID: nodeID,
ServerURI: "no.need.for.a.server",
})
if err != nil {
return err
t.Fatal(err)
}
defer bootstrapCleanup()

lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
return fmt.Errorf("cannot create listener: %v", err)
t.Fatalf("cannot create listener: %v", err)
}

server := grpc.NewServer()
defer server.Stop()
cleanup, err := admin.Register(server)
if err != nil {
return fmt.Errorf("failed to register admin: %v", err)
t.Fatalf("failed to register admin: %v", err)
}
defer cleanup()
go func() {
Expand All @@ -83,15 +79,16 @@ func RunRegisterTests(rcs []RegisterTestCase) error {

conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
if err != nil {
return fmt.Errorf("cannot connect to server: %v", err)
t.Fatalf("cannot connect to server: %v", err)
}

for _, rc := range rcs {
if err := rc.Run(conn); status.Code(err) != rc.Code {
return fmt.Errorf("%s test failed with error %v, want code %v", rc.Name, err, rc.Code)
}
if err := RunChannelz(conn); status.Code(err) != ec.ChannelzCode {
t.Fatalf("%s test failed with error %v, want code %v", "channelz", err, ec.ChannelzCode)
}

if err := RunCSDS(conn); status.Code(err) != ec.CSDSCode {
t.Fatalf("%s test failed with error %v, want code %v", "CSDS", err, ec.CSDSCode)
}
return nil
}

// RunChannelz makes a channelz RPC.
Expand Down

0 comments on commit d41a582

Please sign in to comment.