Skip to content

Commit

Permalink
test/xds: remove redundant server when using stubserver in tests (grp…
Browse files Browse the repository at this point in the history
  • Loading branch information
janardhanvissa authored Nov 25, 2024
1 parent 8b70aeb commit dcba136
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 140 deletions.
12 changes: 3 additions & 9 deletions test/xds/xds_client_ignore_resource_deletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func setupGRPCServerWithModeChangeChannelAndServe(t *testing.T, bootstrapContent
updateCh <- args.Mode
})
stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
Expand All @@ -321,17 +322,10 @@ func setupGRPCServerWithModeChangeChannelAndServe(t *testing.T, bootstrapContent
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
t.Cleanup(server.Stop)

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

// Serve.
go func() {
if err := server.Serve(lis); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()
stubserver.StartTestService(t, stub)

return updateCh
}
Expand Down
52 changes: 13 additions & 39 deletions test/xds/xds_server_certificate_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,21 @@ func (s) TestServerSideXDS_WithNoCertificateProvidersInBootstrap_Failure(t *test
close(servingModeCh)
}
})
server, err := xds.NewGRPCServer(grpc.Creds(creds), modeChangeOpt, xds.BootstrapContentsForTesting(bs))
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
defer server.Stop()

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

// Create a local listener and pass it to Serve().
// Create a local listener and assign it to the stub server.
lis, err := testutils.LocalTCPListener()
if err != nil {
t.Fatalf("testutils.LocalTCPListener() failed: %v", err)
}

go func() {
if err := server.Serve(lis); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()
stub := &stubserver.StubServer{
Listener: lis,
}
if stub.S, err = xds.NewGRPCServer(grpc.Creds(creds), modeChangeOpt, xds.BootstrapContentsForTesting(bs)); err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
}
defer stub.S.Stop()
stubserver.StartTestService(t, stub)

// Create an inbound xDS listener resource for the server side that contains
// mTLS security configuration. Since the received certificate provider
Expand Down Expand Up @@ -288,30 +282,10 @@ 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)
}
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)
}
}()
go func() {
if err := server.Serve(lis2); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()
stub1 := createStubServer(t, lis1, creds, modeChangeOpt, bootstrapContents)
defer stub1.S.Stop()
stub2 := createStubServer(t, lis2, creds, modeChangeOpt, bootstrapContents)
defer stub2.S.Stop()

// Create inbound xDS listener resources for the server side that contains
// mTLS security configuration.
Expand Down
8 changes: 3 additions & 5 deletions test/xds/xds_server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ func setupGRPCServer(t *testing.T, bootstrapContents []byte) (net.Listener, func
},
}

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

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

// Create a local listener and pass it to Serve().
Expand All @@ -131,7 +129,7 @@ func setupGRPCServer(t *testing.T, bootstrapContents []byte) (net.Listener, func
}

go func() {
if err := server.Serve(readyLis); err != nil {
if err := stub.S.Serve(readyLis); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()
Expand All @@ -144,7 +142,7 @@ func setupGRPCServer(t *testing.T, bootstrapContents []byte) (net.Listener, func
}

return lis, func() {
server.Stop()
stub.S.Stop()
}
}

Expand Down
66 changes: 23 additions & 43 deletions test/xds/xds_server_serving_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
xdscreds "google.golang.org/grpc/credentials/xds"
"google.golang.org/grpc/internal/stubserver"
Expand Down Expand Up @@ -65,19 +66,8 @@ func (s) TestServerSideXDS_RedundantUpdateSuppression(t *testing.T) {

// 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()

stub.S = server
stubserver.StartTestService(t, stub)
stub := createStubServer(t, lis, creds, modeChangeOpt, bootstrapContents)
defer stub.S.Stop()

// Setup the management server to respond with the listener resources.
host, port, err := hostPortFromListener(lis)
Expand All @@ -95,12 +85,6 @@ func (s) TestServerSideXDS_RedundantUpdateSuppression(t *testing.T) {
t.Fatal(err)
}

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

// Wait for the listener to move to "serving" mode.
select {
case <-ctx.Done():
Expand Down Expand Up @@ -217,19 +201,10 @@ func (s) TestServerSideXDS_ServingModeChanges(t *testing.T) {

// 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()

stub.S = server
stubserver.StartTestService(t, stub)
stub1 := createStubServer(t, lis1, creds, modeChangeOpt, bootstrapContents)
defer stub1.S.Stop()
stub2 := createStubServer(t, lis2, creds, modeChangeOpt, bootstrapContents)
defer stub2.S.Stop()

// Setup the management server to respond with server-side Listener
// resources for both listeners.
Expand All @@ -251,17 +226,6 @@ func (s) TestServerSideXDS_ServingModeChanges(t *testing.T) {
t.Fatal(err)
}

go func() {
if err := server.Serve(lis1); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()
go func() {
if err := server.Serve(lis2); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()

// Wait for both listeners to move to "serving" mode.
select {
case <-ctx.Done():
Expand Down Expand Up @@ -384,6 +348,22 @@ func (s) TestServerSideXDS_ServingModeChanges(t *testing.T) {
waitForSuccessfulRPC(ctx, t, cc2)
}

func createStubServer(t *testing.T, lis net.Listener, creds credentials.TransportCredentials, modeChangeOpt grpc.ServerOption, bootstrapContents []byte) *stubserver.StubServer {
stub := &stubserver.StubServer{
Listener: lis,
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)
}
stub.S = server
stubserver.StartTestService(t, stub)
return stub
}

func waitForSuccessfulRPC(ctx context.Context, t *testing.T, cc *grpc.ClientConn) {
t.Helper()

Expand Down
60 changes: 16 additions & 44 deletions test/xds/xds_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
xdscreds "google.golang.org/grpc/credentials/xds"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
Expand Down Expand Up @@ -93,26 +94,15 @@ func (s) TestServeLDSRDS(t *testing.T) {
serving.Fire()
}
})

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))
// Configure xDS credentials with an insecure fallback to be used on the
// server-side.
creds, err := xdscreds.NewServerCredentials(xdscreds.ServerOptions{FallbackCreds: insecure.NewCredentials()})
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
t.Fatalf("failed to create server credentials: %v", err)
}
defer server.Stop()

stub.S = server
stubserver.StartTestService(t, stub)
stub := createStubServer(t, lis, creds, modeChangeOpt, bootstrapContents)
defer stub.S.Stop()

go func() {
if err := server.Serve(lis); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()
select {
case <-ctx.Done():
t.Fatal("timeout waiting for the xDS Server to go Serving")
Expand Down Expand Up @@ -210,25 +200,15 @@ 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))
// Configure xDS credentials with an insecure fallback to be used on the
// server-side.
creds, err := xdscreds.NewServerCredentials(xdscreds.ServerOptions{FallbackCreds: insecure.NewCredentials()})
if err != nil {
t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
t.Fatalf("failed to create server credentials: %v", err)
}
defer server.Stop()

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

go func() {
if err := server.Serve(lis); err != nil {
t.Errorf("Serve() failed: %v", err)
}
}()
stub := createStubServer(t, lis, creds, modeChangeOpt, bootstrapContents)
defer stub.S.Stop()

cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
Expand Down Expand Up @@ -278,6 +258,7 @@ func (s) TestMultipleUpdatesImmediatelySwitch(t *testing.T) {
}

stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
Expand All @@ -291,21 +272,12 @@ func (s) TestMultipleUpdatesImmediatelySwitch(t *testing.T) {
},
}

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

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

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

cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("failed to dial local test server: %v", err)
Expand Down

0 comments on commit dcba136

Please sign in to comment.