From ada6787961b37252f830fb554022642dbf25933d Mon Sep 17 00:00:00 2001 From: janardhanvissa <47281167+janardhanvissa@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:21:57 +0000 Subject: [PATCH] cleanup: switching to stubserver in tests instead of testservice implementation (#7708) --- ...ds_client_ignore_resource_deletion_test.go | 12 ++++- .../xds_server_certificate_providers_test.go | 22 +++++++-- test/xds/xds_server_integration_test.go | 46 +++++++++---------- test/xds/xds_server_serving_mode_test.go | 25 ++++++++-- test/xds/xds_server_test.go | 41 +++++++++++++++-- 5 files changed, 110 insertions(+), 36 deletions(-) diff --git a/test/xds/xds_client_ignore_resource_deletion_test.go b/test/xds/xds_client_ignore_resource_deletion_test.go index 6466d3311599..a8078cd206fb 100644 --- a/test/xds/xds_client_ignore_resource_deletion_test.go +++ b/test/xds/xds_client_ignore_resource_deletion_test.go @@ -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() { diff --git a/test/xds/xds_server_certificate_providers_test.go b/test/xds/xds_server_certificate_providers_test.go index 4fb06ce454f0..f277db1376ed 100644 --- a/test/xds/xds_server_certificate_providers_test.go +++ b/test/xds/xds_server_certificate_providers_test.go @@ -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" @@ -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 { @@ -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) @@ -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) diff --git a/test/xds/xds_server_integration_test.go b/test/xds/xds_server_integration_test.go index 347f069c53bd..eacc6463c395 100644 --- a/test/xds/xds_server_integration_test.go +++ b/test/xds/xds_server_integration_test.go @@ -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" @@ -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, @@ -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() diff --git a/test/xds/xds_server_serving_mode_test.go b/test/xds/xds_server_serving_mode_test.go index 40bc1f6898c0..3ed6750a6353 100644 --- a/test/xds/xds_server_serving_mode_test.go +++ b/test/xds/xds_server_serving_mode_test.go @@ -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" @@ -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) @@ -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. diff --git a/test/xds/xds_server_test.go b/test/xds/xds_server_test.go index 3ede7af3cb0e..6912757e5e13 100644 --- a/test/xds/xds_server_test.go +++ b/test/xds/xds_server_test.go @@ -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" @@ -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) @@ -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) @@ -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)