From dcba136b362e8a8096b3986e700047f8ca6302ac Mon Sep 17 00:00:00 2001 From: janardhanvissa <47281167+janardhanvissa@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:57:01 +0530 Subject: [PATCH] test/xds: remove redundant server when using stubserver in tests (#7846) --- ...ds_client_ignore_resource_deletion_test.go | 12 +--- .../xds_server_certificate_providers_test.go | 52 ++++----------- test/xds/xds_server_integration_test.go | 8 +-- test/xds/xds_server_serving_mode_test.go | 66 +++++++------------ test/xds/xds_server_test.go | 60 +++++------------ 5 files changed, 58 insertions(+), 140 deletions(-) diff --git a/test/xds/xds_client_ignore_resource_deletion_test.go b/test/xds/xds_client_ignore_resource_deletion_test.go index a8078cd206fb..b85ec16aef44 100644 --- a/test/xds/xds_client_ignore_resource_deletion_test.go +++ b/test/xds/xds_client_ignore_resource_deletion_test.go @@ -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 }, @@ -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 } diff --git a/test/xds/xds_server_certificate_providers_test.go b/test/xds/xds_server_certificate_providers_test.go index f277db1376ed..9fcf6f49cf99 100644 --- a/test/xds/xds_server_certificate_providers_test.go +++ b/test/xds/xds_server_certificate_providers_test.go @@ -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 @@ -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. diff --git a/test/xds/xds_server_integration_test.go b/test/xds/xds_server_integration_test.go index eacc6463c395..054e1fc7b0f0 100644 --- a/test/xds/xds_server_integration_test.go +++ b/test/xds/xds_server_integration_test.go @@ -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(). @@ -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) } }() @@ -144,7 +142,7 @@ func setupGRPCServer(t *testing.T, bootstrapContents []byte) (net.Listener, func } return lis, func() { - server.Stop() + stub.S.Stop() } } diff --git a/test/xds/xds_server_serving_mode_test.go b/test/xds/xds_server_serving_mode_test.go index 3ed6750a6353..0299d6954ddb 100644 --- a/test/xds/xds_server_serving_mode_test.go +++ b/test/xds/xds_server_serving_mode_test.go @@ -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" @@ -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) @@ -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(): @@ -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. @@ -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(): @@ -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() diff --git a/test/xds/xds_server_test.go b/test/xds/xds_server_test.go index 6912757e5e13..bee9d401423b 100644 --- a/test/xds/xds_server_test.go +++ b/test/xds/xds_server_test.go @@ -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" @@ -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") @@ -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 { @@ -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 }, @@ -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)