Skip to content

Commit

Permalink
spanner: update integration tests to run for the emulator.
Browse files Browse the repository at this point in the history
Change-Id: I23f422c761e629e145190aa2b6db0a57f1294384
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/50091
Reviewed-by: Knut Olav Løite <koloite@gmail.com>
  • Loading branch information
hengfengli committed Jan 9, 2020
1 parent cca6882 commit cec21c7
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions spanner/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"google.golang.org/api/option"
adminpb "google.golang.org/genproto/googleapis/spanner/admin/database/v1"
instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -154,6 +155,20 @@ func initIntegrationTests() (cleanup func()) {
var err error

opts := append(grpcHeaderChecker.CallOptions(), option.WithTokenSource(ts), option.WithEndpoint(endpoint))

// Run integration tests against the given emulator. Currently, the database and
// instance admin clients are auto-generated, which do not support to configure
// SPANNER_EMULATOR_HOST.
emulatorAddr := os.Getenv("SPANNER_EMULATOR_HOST")
if emulatorAddr != "" {
opts = append(
grpcHeaderChecker.CallOptions(),
option.WithEndpoint(emulatorAddr),
option.WithGRPCDialOption(grpc.WithInsecure()),
option.WithoutAuthentication(),
)
}

// Create InstanceAdmin and DatabaseAdmin clients.
instanceAdmin, err = instance.NewInstanceAdminClient(ctx, opts...)
if err != nil {
Expand Down Expand Up @@ -1374,6 +1389,7 @@ func TestIntegration_QueryExpressions(t *testing.T) {
}

func TestIntegration_QueryStats(t *testing.T) {
skipEmulatorTest(t)
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
Expand Down Expand Up @@ -2233,6 +2249,7 @@ func TestIntegration_StructParametersBind(t *testing.T) {
}

func TestIntegration_PDML(t *testing.T) {
skipEmulatorTest(t)
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
Expand Down Expand Up @@ -2636,9 +2653,17 @@ func isNaN(x interface{}) bool {

// createClient creates Cloud Spanner data client.
func createClient(ctx context.Context, dbPath string, spc SessionPoolConfig) (client *Client, err error) {
client, err = NewClientWithConfig(ctx, dbPath, ClientConfig{
SessionPoolConfig: spc,
}, option.WithTokenSource(testutil.TokenSource(ctx, Scope, AdminScope)), option.WithEndpoint(endpoint))
if os.Getenv("SPANNER_EMULATOR_HOST") == "" {
client, err = NewClientWithConfig(ctx, dbPath, ClientConfig{
SessionPoolConfig: spc,
}, option.WithTokenSource(testutil.TokenSource(ctx, Scope, AdminScope)), option.WithEndpoint(endpoint))
} else {
// When the emulator is enabled, option.WithoutAuthentication()
// will be added automatically which is incompatible with
// option.WithTokenSource(testutil.TokenSource(ctx, Scope)).
client, err = NewClientWithConfig(ctx, dbPath, ClientConfig{SessionPoolConfig: spc})
}

if err != nil {
return nil, fmt.Errorf("cannot create data client on DB %v: %v", dbPath, err)
}
Expand Down Expand Up @@ -2723,3 +2748,9 @@ func maxDuration(a, b time.Duration) time.Duration {
}
return b
}

func skipEmulatorTest(t *testing.T) {
if os.Getenv("SPANNER_EMULATOR_HOST") != "" {
t.Skip("Skipping testing against the emulator.")
}
}

0 comments on commit cec21c7

Please sign in to comment.