Skip to content

Commit

Permalink
cleanup: switching to stubserver in tests instead of testservice impl…
Browse files Browse the repository at this point in the history
…ementation (#7708)
  • Loading branch information
janardhanvissa authored Oct 28, 2024
1 parent cb32937 commit ada6787
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 36 deletions.
12 changes: 11 additions & 1 deletion test/xds/xds_client_ignore_resource_deletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,22 @@ func setupGRPCServerWithModeChangeChannelAndServe(t *testing.T, bootstrapContent
t.Logf("Serving mode for listener %q changed to %q, err: %v", addr.String(), args.Mode, args.Err)
updateCh <- args.Mode
})
stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
UnaryCallF: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{}, nil
},
}
server, err := xds.NewGRPCServer(grpc.Creds(insecure.NewCredentials()), modeChangeOpt, xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
t.Cleanup(server.Stop)
testgrpc.RegisterTestServiceServer(server, &testService{})

stub.S = server
stubserver.StartTestService(t, stub)

// Serve.
go func() {
Expand Down
22 changes: 17 additions & 5 deletions test/xds/xds_server_certificate_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
xdscreds "google.golang.org/grpc/credentials/xds"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
"google.golang.org/grpc/internal/testutils/xds/e2e/setup"
Expand Down Expand Up @@ -161,9 +162,12 @@ func (s) TestServerSideXDS_WithNoCertificateProvidersInBootstrap_Failure(t *test
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
testgrpc.RegisterTestServiceServer(server, &testService{})
defer server.Stop()

stub := &stubserver.StubServer{}
stub.S = server
stubserver.StartTestService(t, stub)

// Create a local listener and pass it to Serve().
lis, err := testutils.LocalTCPListener()
if err != nil {
Expand Down Expand Up @@ -268,9 +272,9 @@ func (s) TestServerSideXDS_WithValidAndInvalidSecurityConfiguration(t *testing.T
t.Fatalf("testutils.LocalTCPListener() failed: %v", err)
}

// Create an xDS-enabled grpc server that is configured to use xDS
// credentials, and register the test service on it. Configure a mode change
// option that closes a channel when listener2 enter serving mode.
// Create an xDS-enabled gRPC server that is configured to use xDS
// credentials and assigned to a stub server, configuring a mode change
// option that closes a channel when listener2 enters serving mode.
creds, err := xdscreds.NewServerCredentials(xdscreds.ServerOptions{FallbackCreds: insecure.NewCredentials()})
if err != nil {
t.Fatal(err)
Expand All @@ -283,13 +287,21 @@ func (s) TestServerSideXDS_WithValidAndInvalidSecurityConfiguration(t *testing.T
}
}
})

stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
server, err := xds.NewGRPCServer(grpc.Creds(creds), modeChangeOpt, xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
testgrpc.RegisterTestServiceServer(server, &testService{})
defer server.Stop()

stub.S = server
stubserver.StartTestService(t, stub)

go func() {
if err := server.Serve(lis1); err != nil {
t.Errorf("Serve() failed: %v", err)
Expand Down
46 changes: 23 additions & 23 deletions test/xds/xds_server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
xdscreds "google.golang.org/grpc/credentials/xds"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
"google.golang.org/grpc/internal/testutils/xds/e2e/setup"
Expand All @@ -45,27 +46,6 @@ import (
testpb "google.golang.org/grpc/interop/grpc_testing"
)

type testService struct {
testgrpc.TestServiceServer
}

func (*testService) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
}

func (*testService) UnaryCall(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{}, nil
}

func (*testService) FullDuplexCall(stream testgrpc.TestService_FullDuplexCallServer) error {
for {
_, err := stream.Recv() // hangs here forever if stream doesn't shut down...doesn't receive EOF without any errors
if err == io.EOF {
return nil
}
}
}

func testModeChangeServerOption(t *testing.T) grpc.ServerOption {
// Create a server option to get notified about serving mode changes. We don't
// do anything other than throwing a log entry here. But this is required,
Expand Down Expand Up @@ -112,12 +92,32 @@ func setupGRPCServer(t *testing.T, bootstrapContents []byte) (net.Listener, func
t.Fatal(err)
}

// Initialize an xDS-enabled gRPC server and register the stubServer on it.
// Initialize a test gRPC server, assign it to the stub server, and start
// the test service.
stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
UnaryCallF: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{}, nil
},
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
for {
_, err := stream.Recv() // hangs here forever if stream doesn't shut down...doesn't receive EOF without any errors
if err == io.EOF {
return nil
}
}
},
}

server, err := xds.NewGRPCServer(grpc.Creds(creds), testModeChangeServerOption(t), xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
testgrpc.RegisterTestServiceServer(server, &testService{})

stub.S = server
stubserver.StartTestService(t, stub)

// Create a local listener and pass it to Serve().
lis, err := testutils.LocalTCPListener()
Expand Down
25 changes: 21 additions & 4 deletions test/xds/xds_server_serving_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
xdscreds "google.golang.org/grpc/credentials/xds"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
"google.golang.org/grpc/internal/testutils/xds/e2e/setup"
Expand Down Expand Up @@ -62,13 +63,21 @@ func (s) TestServerSideXDS_RedundantUpdateSuppression(t *testing.T) {
updateCh <- args.Mode
})

// Initialize an xDS-enabled gRPC server and register the stubServer on it.
// Initialize a test gRPC server, assign it to the stub server, and start
// the test service.
stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
server, err := xds.NewGRPCServer(grpc.Creds(creds), modeChangeOpt, xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
defer server.Stop()
testgrpc.RegisterTestServiceServer(server, &testService{})

stub.S = server
stubserver.StartTestService(t, stub)

// Setup the management server to respond with the listener resources.
host, port, err := hostPortFromListener(lis)
Expand Down Expand Up @@ -206,13 +215,21 @@ func (s) TestServerSideXDS_ServingModeChanges(t *testing.T) {
}
})

// Initialize an xDS-enabled gRPC server and register the stubServer on it.
// Initialize a test gRPC server, assign it to the stub server, and start
// the test service.
stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
server, err := xds.NewGRPCServer(grpc.Creds(creds), modeChangeOpt, xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
defer server.Stop()
testgrpc.RegisterTestServiceServer(server, &testService{})

stub.S = server
stubserver.StartTestService(t, stub)

// Setup the management server to respond with server-side Listener
// resources for both listeners.
Expand Down
41 changes: 38 additions & 3 deletions test/xds/xds_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
"google.golang.org/grpc/internal/testutils/xds/e2e/setup"
Expand Down Expand Up @@ -93,12 +94,20 @@ func (s) TestServeLDSRDS(t *testing.T) {
}
})

stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
server, err := xds.NewGRPCServer(grpc.Creds(insecure.NewCredentials()), modeChangeOpt, xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
defer server.Stop()
testgrpc.RegisterTestServiceServer(server, &testService{})

stub.S = server
stubserver.StartTestService(t, stub)

go func() {
if err := server.Serve(lis); err != nil {
t.Errorf("Serve() failed: %v", err)
Expand Down Expand Up @@ -201,12 +210,20 @@ func (s) TestRDSNack(t *testing.T) {
}
})

stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
server, err := xds.NewGRPCServer(grpc.Creds(insecure.NewCredentials()), modeChangeOpt, xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
defer server.Stop()
testgrpc.RegisterTestServiceServer(server, &testService{})

stub.S = server
stubserver.StartTestService(t, stub)

go func() {
if err := server.Serve(lis); err != nil {
t.Errorf("Serve() failed: %v", err)
Expand Down Expand Up @@ -259,12 +276,30 @@ func (s) TestMultipleUpdatesImmediatelySwitch(t *testing.T) {
if err := managementServer.Update(ctx, resources); err != nil {
t.Fatal(err)
}

stub := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
for {
_, err := stream.Recv() // hangs here forever if stream doesn't shut down...doesn't receive EOF without any errors
if err == io.EOF {
return nil
}
}
},
}

server, err := xds.NewGRPCServer(grpc.Creds(insecure.NewCredentials()), testModeChangeServerOption(t), xds.BootstrapContentsForTesting(bootstrapContents))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
defer server.Stop()
testgrpc.RegisterTestServiceServer(server, &testService{})

stub.S = server
stubserver.StartTestService(t, stub)

go func() {
if err := server.Serve(lis); err != nil {
t.Errorf("Serve() failed: %v", err)
Expand Down

0 comments on commit ada6787

Please sign in to comment.