Skip to content

Commit

Permalink
testing: add helpers to start test service, and retrieve port (#6187)
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars authored Apr 12, 2023
1 parent 5a50b97 commit fe72db9
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 170 deletions.
19 changes: 19 additions & 0 deletions internal/stubserver/stubserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"fmt"
"net"
"testing"
"time"

"google.golang.org/grpc"
Expand Down Expand Up @@ -179,3 +180,21 @@ func parseCfg(r *manual.Resolver, s string) *serviceconfig.ParseResult {
}
return g
}

// StartTestService spins up a stub server exposing the TestService on a local
// port. If the passed in server is nil, a stub server that implements only the
// EmptyCall and UnaryCall RPCs is started.
func StartTestService(t *testing.T, server *StubServer) *StubServer {
if server == nil {
server = &StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{}, nil
},
}
}
server.StartServer()

t.Logf("Started test service backend at %q", server.Address)
return server
}
39 changes: 39 additions & 0 deletions internal/testutils/parse_port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package testutils

import (
"net"
"strconv"
"testing"
)

// ParsePort returns the port from the given address string, as a unit32.
func ParsePort(t *testing.T, addr string) uint32 {
t.Helper()

_, p, err := net.SplitHostPort(addr)
if err != nil {
t.Fatalf("Invalid serving address: %v", err)
}
port, err := strconv.ParseUint(p, 10, 32)
if err != nil {
t.Fatalf("Invalid serving port: %v", err)
}
return uint32(port)
}
7 changes: 4 additions & 3 deletions test/xds/xds_client_ack_nack_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/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"

Expand Down Expand Up @@ -123,15 +124,15 @@ func (s) TestClientResourceVersionAfterStreamRestart(t *testing.T) {
})
defer cleanup1()

port, cleanup2 := startTestService(t, nil)
defer cleanup2()
server := stubserver.StartTestService(t, nil)
defer server.Stop()

const serviceName = "my-service-client-side-xds"
resources := e2e.DefaultClientResources(e2e.ResourceParams{
DialTarget: serviceName,
NodeID: nodeID,
Host: "localhost",
Port: port,
Port: testutils.ParsePort(t, server.Address),
SecLevel: e2e.SecurityLevelNone,
})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down
8 changes: 5 additions & 3 deletions test/xds/xds_client_affinity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"

v3clusterpb "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
Expand Down Expand Up @@ -91,15 +93,15 @@ func (s) TestClientSideAffinitySanityCheck(t *testing.T) {
managementServer, nodeID, _, resolver, cleanup1 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{})
defer cleanup1()

port, cleanup2 := startTestService(t, nil)
defer cleanup2()
server := stubserver.StartTestService(t, nil)
defer server.Stop()

const serviceName = "my-service-client-side-xds"
resources := e2e.DefaultClientResources(e2e.ResourceParams{
DialTarget: serviceName,
NodeID: nodeID,
Host: "localhost",
Port: port,
Port: testutils.ParsePort(t, server.Address),
SecLevel: e2e.SecurityLevelNone,
})
// Replace RDS and CDS resources with ringhash config, but keep the resource
Expand Down
14 changes: 8 additions & 6 deletions test/xds/xds_client_federation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/bootstrap"
"google.golang.org/grpc/internal/testutils/xds/e2e"
"google.golang.org/grpc/resolver"
Expand Down Expand Up @@ -89,8 +91,8 @@ func (s) TestClientSideFederation(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create xDS resolver for testing: %v", err)
}
port, cleanup := startTestService(t, nil)
defer cleanup()
server := stubserver.StartTestService(t, nil)
defer server.Stop()

const serviceName = "my-service-client-side-xds"
// LDS is old style name.
Expand All @@ -115,7 +117,7 @@ func (s) TestClientSideFederation(t *testing.T) {
NodeID: nodeID,
// This has only RDS and EDS.
Routes: []*v3routepb.RouteConfiguration{e2e.DefaultRouteConfig(rdsName, ldsName, cdsName)},
Endpoints: []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint(edsName, "localhost", []uint32{port})},
Endpoints: []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint(edsName, "localhost", []uint32{testutils.ParsePort(t, server.Address)})},
SkipValidation: true,
}

Expand Down Expand Up @@ -161,15 +163,15 @@ func (s) TestFederation_UnknownAuthorityInDialTarget(t *testing.T) {
managementServer, nodeID, _, resolver, cleanup1 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{})
defer cleanup1()

port, cleanup2 := startTestService(t, nil)
defer cleanup2()
server := stubserver.StartTestService(t, nil)
defer server.Stop()

const serviceName = "my-service-client-side-xds"
resources := e2e.DefaultClientResources(e2e.ResourceParams{
DialTarget: serviceName,
NodeID: nodeID,
Host: "localhost",
Port: port,
Port: testutils.ParsePort(t, server.Address),
SecLevel: e2e.SecurityLevelNone,
})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down
13 changes: 7 additions & 6 deletions test/xds/xds_client_ignore_resource_deletion_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"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/bootstrap"
"google.golang.org/grpc/internal/testutils/xds/e2e"
Expand Down Expand Up @@ -89,11 +90,11 @@ var (
//
// Resource deletion is only applicable to Listener and Cluster resources.
func (s) TestIgnoreResourceDeletionOnClient(t *testing.T) {
port1, cleanup := startTestService(t, nil)
t.Cleanup(cleanup)
server1 := stubserver.StartTestService(t, nil)
t.Cleanup(server1.Stop)

port2, cleanup := startTestService(t, nil)
t.Cleanup(cleanup)
server2 := stubserver.StartTestService(t, nil)
t.Cleanup(server2.Stop)

initialResourceOnServer := func(nodeID string) e2e.UpdateOptions {
return e2e.UpdateOptions{
Expand All @@ -105,8 +106,8 @@ func (s) TestIgnoreResourceDeletionOnClient(t *testing.T) {
e2e.DefaultCluster(cdsName2, edsName2, e2e.SecurityLevelNone),
},
Endpoints: []*endpointpb.ClusterLoadAssignment{
e2e.DefaultEndpoint(edsName1, "localhost", []uint32{port1}),
e2e.DefaultEndpoint(edsName2, "localhost", []uint32{port2}),
e2e.DefaultEndpoint(edsName1, "localhost", []uint32{testutils.ParsePort(t, server1.Address)}),
e2e.DefaultEndpoint(edsName2, "localhost", []uint32{testutils.ParsePort(t, server2.Address)}),
},
SkipValidation: true,
}
Expand Down
37 changes: 4 additions & 33 deletions test/xds/xds_client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ package xds_test
import (
"context"
"fmt"
"net"
"strconv"
"testing"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"

testgrpc "google.golang.org/grpc/interop/grpc_testing"
Expand All @@ -49,47 +48,19 @@ const (
defaultTestShortTimeout = 10 * time.Millisecond // For events expected to *not* happen.
)

// startTestService spins up a server exposing the TestService on a local port.
//
// Returns the following:
// - the port the server is listening on
// - cleanup function to be invoked by the tests when done
func startTestService(t *testing.T, server *stubserver.StubServer) (uint32, func()) {
if server == nil {
server = &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{}, nil
},
}
}
server.StartServer()

_, p, err := net.SplitHostPort(server.Address)
if err != nil {
t.Fatalf("invalid serving address for stub server: %v", err)
}
port, err := strconv.ParseUint(p, 10, 32)
if err != nil {
t.Fatalf("invalid serving port for stub server: %v", err)
}
t.Logf("Started test service backend at %q", server.Address)
return uint32(port), server.Stop
}

func (s) TestClientSideXDS(t *testing.T) {
managementServer, nodeID, _, resolver, cleanup1 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{})
defer cleanup1()

port, cleanup2 := startTestService(t, nil)
defer cleanup2()
server := stubserver.StartTestService(t, nil)
defer server.Stop()

const serviceName = "my-service-client-side-xds"
resources := e2e.DefaultClientResources(e2e.ResourceParams{
DialTarget: serviceName,
NodeID: nodeID,
Host: "localhost",
Port: port,
Port: testutils.ParsePort(t, server.Address),
SecLevel: e2e.SecurityLevelNone,
})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down
43 changes: 19 additions & 24 deletions test/xds/xds_client_outlier_detection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
testpb "google.golang.org/grpc/interop/grpc_testing"
Expand All @@ -52,15 +53,19 @@ func (s) TestOutlierDetection_NoopConfig(t *testing.T) {
managementServer, nodeID, _, resolver, cleanup1 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{})
defer cleanup1()

port, cleanup2 := startTestService(t, nil)
defer cleanup2()
server := &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
}
server.StartServer()
t.Logf("Started test service backend at %q", server.Address)
defer server.Stop()

const serviceName = "my-service-client-side-xds"
resources := e2e.DefaultClientResources(e2e.ResourceParams{
DialTarget: serviceName,
NodeID: nodeID,
Host: "localhost",
Port: port,
Port: testutils.ParsePort(t, server.Address),
SecLevel: e2e.SecurityLevelNone,
})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down Expand Up @@ -170,31 +175,21 @@ func (s) TestOutlierDetectionWithOutlier(t *testing.T) {
defer cleanup()

// Working backend 1.
backend1 := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
port1, cleanup1 := startTestService(t, backend1)
defer cleanup1()
backend1 := stubserver.StartTestService(t, nil)
port1 := testutils.ParsePort(t, backend1.Address)
defer backend1.Stop()

// Working backend 2.
backend2 := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
port2, cleanup2 := startTestService(t, backend2)
defer cleanup2()
backend2 := stubserver.StartTestService(t, nil)
port2 := testutils.ParsePort(t, backend2.Address)
defer backend2.Stop()

// Backend 3 that will always return an error and eventually ejected.
backend3 := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return nil, errors.New("some error")
},
}
port3, cleanup3 := startTestService(t, backend3)
defer cleanup3()
backend3 := stubserver.StartTestService(t, &stubserver.StubServer{
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return nil, errors.New("some error") },
})
port3 := testutils.ParsePort(t, backend3.Address)
defer backend3.Stop()

const serviceName = "my-service-client-side-xds"
resources := clientResourcesMultipleBackendsAndOD(e2e.ResourceParams{
Expand Down
18 changes: 9 additions & 9 deletions test/xds/xds_client_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand All @@ -39,28 +40,27 @@ import (
func (s) TestClientSideRetry(t *testing.T) {
ctr := 0
errs := []codes.Code{codes.ResourceExhausted}
ss := &stubserver.StubServer{

managementServer, nodeID, _, resolver, cleanup1 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{})
defer cleanup1()

server := stubserver.StartTestService(t, &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
defer func() { ctr++ }()
if ctr < len(errs) {
return nil, status.Errorf(errs[ctr], "this should be retried")
}
return &testpb.Empty{}, nil
},
}

managementServer, nodeID, _, resolver, cleanup1 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{})
defer cleanup1()

port, cleanup2 := startTestService(t, ss)
defer cleanup2()
})
defer server.Stop()

const serviceName = "my-service-client-side-xds"
resources := e2e.DefaultClientResources(e2e.ResourceParams{
DialTarget: serviceName,
NodeID: nodeID,
Host: "localhost",
Port: port,
Port: testutils.ParsePort(t, server.Address),
SecLevel: e2e.SecurityLevelNone,
})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down
Loading

0 comments on commit fe72db9

Please sign in to comment.