Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: modify tests to use stubserver #7951

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions test/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/resolver"
Expand Down Expand Up @@ -431,10 +432,15 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) {
t.Fatal(err)
}
cred := &authorityCheckCreds{}
s := grpc.NewServer()
go s.Serve(lis)
defer s.Stop()

stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(),
}
stubserver.StartTestService(t, stub)
defer stub.S.Stop()
r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
Expand Down Expand Up @@ -463,10 +469,16 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) {
t.Fatal(err)
}
cred := &authorityCheckCreds{}
s := grpc.NewServer()
go s.Serve(lis)
defer s.Stop()

stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(),
}
stubserver.StartTestService(t, stub)
defer stub.S.Stop()
r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
Expand Down
69 changes: 46 additions & 23 deletions test/goaway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,20 @@ func (s) TestGracefulClientOnGoAway(t *testing.T) {
const maxConnAge = 100 * time.Millisecond
const testTime = maxConnAge * 10

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

ss := &stubserver.StubServer{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this can be ss1 and then others can ss2, ss3 and so on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Listener: lis,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do the same with listeners to be consistent. lis1, lis2, lis3.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{MaxConnectionAge: maxConnAge})),
}

s := grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{MaxConnectionAge: maxConnAge}))
defer s.Stop()
testgrpc.RegisterTestServiceServer(s, ss)

lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Failed to create listener: %v", err)
}
go s.Serve(lis)
defer ss.S.Stop()
stubserver.StartTestService(t, ss)

cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
Expand Down Expand Up @@ -551,13 +550,15 @@ func (s) TestGoAwayThenClose(t *testing.T) {
if err != nil {
t.Fatalf("Error while listening. Err: %v", err)
}
s1 := grpc.NewServer()
defer s1.Stop()
ts := &funcServer{
unaryCall: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
//s1 := grpc.NewServer()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are these commented out? They should be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//defer s1.Stop()
//ts := &funcServer{
ss1 := &stubserver.StubServer{
Listener: lis1,
UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{}, nil
},
fullDuplexCall: func(stream testgrpc.TestService_FullDuplexCallServer) error {
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil {
t.Errorf("unexpected error from send: %v", err)
return err
Expand All @@ -569,18 +570,42 @@ func (s) TestGoAwayThenClose(t *testing.T) {
}
return err
},
S: grpc.NewServer(),
}
testgrpc.RegisterTestServiceServer(s1, ts)
go s1.Serve(lis1)
stubserver.StartTestService(t, ss1)
defer ss1.S.Stop()
//testgrpc.RegisterTestServiceServer(s1, ts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same. Please remove instead of commenting out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//go s1.Serve(lis1)

conn2Established := grpcsync.NewEvent()
lis2, err := listenWithNotifyingListener("tcp", "localhost:0", conn2Established)
if err != nil {
t.Fatalf("Error while listening. Err: %v", err)
}
s2 := grpc.NewServer()
/*s2 := grpc.NewServer()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

defer s2.Stop()
testgrpc.RegisterTestServiceServer(s2, ts)
testgrpc.RegisterTestServiceServer(s2, ts)*/
ss2 := &stubserver.StubServer{
Listener: lis2,
UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{}, nil
},
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil {
t.Errorf("unexpected error from send: %v", err)
return err
}
// Wait forever.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not wait forever. Its wait until a message is received from client

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

_, err := stream.Recv()
if err == nil {
t.Error("expected to never receive any message")
}
return err
},
S: grpc.NewServer(),
}
stubserver.StartTestService(t, ss2)
defer ss2.S.Stop()

r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{
Expand Down Expand Up @@ -613,10 +638,8 @@ func (s) TestGoAwayThenClose(t *testing.T) {
t.Fatalf("unexpected error from first recv: %v", err)
}

go s2.Serve(lis2)

t.Log("Gracefully stopping server 1.")
go s1.GracefulStop()
go ss1.S.GracefulStop()

t.Log("Waiting for the ClientConn to enter IDLE state.")
testutils.AwaitState(ctx, t, cc, connectivity.Idle)
Expand All @@ -637,7 +660,7 @@ func (s) TestGoAwayThenClose(t *testing.T) {
lis2.Close()

t.Log("Hard closing connection 1.")
s1.Stop()
ss1.S.Stop()

t.Log("Waiting for the first stream to error.")
if _, err = stream.Recv(); err == nil {
Expand Down
58 changes: 27 additions & 31 deletions test/insecure_creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ func (s) TestInsecureCreds(t *testing.T) {

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err)
}

ss := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
if !test.serverInsecureCreds {
return &testpb.Empty{}, nil
Expand All @@ -104,22 +110,14 @@ func (s) TestInsecureCreds(t *testing.T) {
return &testpb.Empty{}, nil
},
}

sOpts := []grpc.ServerOption{}
if test.serverInsecureCreds {
sOpts = append(sOpts, grpc.Creds(insecure.NewCredentials()))
}
s := grpc.NewServer(sOpts...)
defer s.Stop()

testgrpc.RegisterTestServiceServer(s, ss)

lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err)
ss.S = grpc.NewServer(grpc.Creds(insecure.NewCredentials()))
} else {
ss.S = grpc.NewServer()
}
defer ss.S.Stop()

go s.Serve(lis)
stubserver.StartTestService(t, ss)

addr := lis.Addr().String()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
Expand All @@ -143,21 +141,21 @@ func (s) TestInsecureCreds(t *testing.T) {
}

func (s) TestInsecureCreds_WithPerRPCCredentials_AsCallOption(t *testing.T) {
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err)
}

ss := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(grpc.Creds(insecure.NewCredentials())),
}

s := grpc.NewServer(grpc.Creds(insecure.NewCredentials()))
defer s.Stop()
testgrpc.RegisterTestServiceServer(s, ss)

lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err)
}
go s.Serve(lis)
defer ss.S.Stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer ss.S.Stop() should after stubserver.StartTestService()

stubserver.StartTestService(t, ss)

addr := lis.Addr().String()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand All @@ -179,21 +177,19 @@ func (s) TestInsecureCreds_WithPerRPCCredentials_AsCallOption(t *testing.T) {
}

func (s) TestInsecureCreds_WithPerRPCCredentials_AsDialOption(t *testing.T) {
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err)
}
ss := &stubserver.StubServer{
EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listener also needs to be assigned here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return &testpb.Empty{}, nil
},
S: grpc.NewServer(grpc.Creds(insecure.NewCredentials())),
}

s := grpc.NewServer(grpc.Creds(insecure.NewCredentials()))
defer s.Stop()
testgrpc.RegisterTestServiceServer(s, ss)

lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err)
}
go s.Serve(lis)
defer ss.S.Stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same defer ss.S.Stop() should be after stubserver.StartTestService(

stubserver.StartTestService(t, ss)

addr := lis.Addr().String()
dopts := []grpc.DialOption{
Expand Down
43 changes: 19 additions & 24 deletions test/local_creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ import (
testpb "google.golang.org/grpc/interop/grpc_testing"
)

func testLocalCredsE2ESucceed(network, address string) error {
func testLocalCredsE2ESucceed(t *testing.T, network, address string) error {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nix new line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

lis, err := net.Listen(network, address)
if err != nil {
return fmt.Errorf("Failed to create listener: %v", err)
}
ss := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
pr, ok := peer.FromContext(ctx)
if !ok {
Expand Down Expand Up @@ -69,20 +75,12 @@ func testLocalCredsE2ESucceed(network, address string) error {
}
return &testpb.Empty{}, nil
},
S: grpc.NewServer(grpc.Creds(local.NewCredentials())),
}

sopts := []grpc.ServerOption{grpc.Creds(local.NewCredentials())}
s := grpc.NewServer(sopts...)
defer s.Stop()
defer ss.S.Stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same defer ss.S.Stop() should be after stubserver.StartTestService()


testgrpc.RegisterTestServiceServer(s, ss)

lis, err := net.Listen(network, address)
if err != nil {
return fmt.Errorf("Failed to create listener: %v", err)
}

go s.Serve(lis)
stubserver.StartTestService(t, ss)

var cc *grpc.ClientConn
lisAddr := lis.Addr().String()
Expand Down Expand Up @@ -114,14 +112,14 @@ func testLocalCredsE2ESucceed(network, address string) error {
}

func (s) TestLocalCredsLocalhost(t *testing.T) {
if err := testLocalCredsE2ESucceed("tcp", "localhost:0"); err != nil {
if err := testLocalCredsE2ESucceed(t, "tcp", "localhost:0"); err != nil {
t.Fatalf("Failed e2e test for localhost: %v", err)
}
}

func (s) TestLocalCredsUDS(t *testing.T) {
addr := fmt.Sprintf("/tmp/grpc_fullstck_test%d", time.Now().UnixNano())
if err := testLocalCredsE2ESucceed("unix", addr); err != nil {
if err := testLocalCredsE2ESucceed(t, "unix", addr); err != nil {
t.Fatalf("Failed e2e test for UDS: %v", err)
}
}
Expand Down Expand Up @@ -162,18 +160,15 @@ func spoofDialer(addr net.Addr) func(target string, t time.Duration) (net.Conn,
}
}

func testLocalCredsE2EFail(dopts []grpc.DialOption) error {
func testLocalCredsE2EFail(t *testing.T, dopts []grpc.DialOption) error {
ss := &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same. Listener needs to be assigned here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return &testpb.Empty{}, nil
},
S: grpc.NewServer(grpc.Creds(local.NewCredentials())),
}

sopts := []grpc.ServerOption{grpc.Creds(local.NewCredentials())}
s := grpc.NewServer(sopts...)
defer s.Stop()

testgrpc.RegisterTestServiceServer(s, ss)
defer ss.S.Stop()
stubserver.StartTestService(t, ss)

lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
Expand All @@ -190,7 +185,7 @@ func testLocalCredsE2EFail(dopts []grpc.DialOption) error {
Zone: "",
}

go s.Serve(spoofListener(lis, fakeClientAddr))
go ss.S.Serve(spoofListener(lis, fakeClientAddr))

cc, err := grpc.NewClient(lis.Addr().String(), append(dopts, grpc.WithDialer(spoofDialer(fakeServerAddr)))...)
if err != nil {
Expand All @@ -214,15 +209,15 @@ func (s) TestLocalCredsClientFail(t *testing.T) {
// Use local creds at client-side which should lead to client-side failure.
opts := []grpc.DialOption{grpc.WithTransportCredentials(local.NewCredentials())}
want := status.Error(codes.Unavailable, "transport: authentication handshake failed: local credentials rejected connection to non-local address")
if err := testLocalCredsE2EFail(opts); !isExpected(err, want) {
if err := testLocalCredsE2EFail(t, opts); !isExpected(err, want) {
t.Fatalf("testLocalCredsE2EFail() = %v; want %v", err, want)
}
}

func (s) TestLocalCredsServerFail(t *testing.T) {
// Use insecure at client-side which should lead to server-side failure.
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
if err := testLocalCredsE2EFail(opts); status.Code(err) != codes.Unavailable {
if err := testLocalCredsE2EFail(t, opts); status.Code(err) != codes.Unavailable {
t.Fatalf("testLocalCredsE2EFail() = %v; want %v", err, codes.Unavailable)
}
}
Loading