Skip to content

Commit

Permalink
Improve datastore, firestore, pubsub and spanner tests (#670)
Browse files Browse the repository at this point in the history
Avoid use of env vars due to will not allow running parallel tests.
Use `options` instead to set the container address.
  • Loading branch information
eddumelendez authored Dec 7, 2022
1 parent be28db2 commit 63eb588
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
11 changes: 9 additions & 2 deletions examples/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package datastore
import (
"cloud.google.com/go/datastore"
"context"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"testing"
)

Expand All @@ -25,8 +28,12 @@ func TestDatastore(t *testing.T) {
}
})

t.Setenv("DATASTORE_EMULATOR_HOST", container.URI)
dsClient, err := datastore.NewClient(ctx, "test-project")
options := []option.ClientOption{
option.WithEndpoint(container.URI),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
}
dsClient, err := datastore.NewClient(ctx, "test-project", options...)
if err != nil {
t.Fatal(err)
}
Expand Down
21 changes: 19 additions & 2 deletions examples/firestore/firestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package firestore
import (
"cloud.google.com/go/firestore"
"context"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"testing"
)

Expand All @@ -11,6 +14,16 @@ type Person struct {
Lastname string `json:"lastname"`
}

type emulatorCreds struct {
}

func (ec emulatorCreds) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{"authorization": "Bearer owner"}, nil
}
func (ec emulatorCreds) RequireTransportSecurity() bool {
return false
}

func TestFirestore(t *testing.T) {
ctx := context.Background()

Expand All @@ -26,8 +39,12 @@ func TestFirestore(t *testing.T) {
}
})

t.Setenv("FIRESTORE_EMULATOR_HOST", container.URI)
client, err := firestore.NewClient(ctx, "test-project")
conn, err := grpc.Dial(container.URI, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithPerRPCCredentials(emulatorCreds{}))
if err != nil {
t.Fatal(err)
}
options := []option.ClientOption{option.WithGRPCConn(conn)}
client, err := firestore.NewClient(ctx, "test-project", options...)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 9 additions & 2 deletions examples/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package pubsub
import (
"cloud.google.com/go/pubsub"
"context"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"testing"
)

Expand All @@ -21,8 +24,12 @@ func TestPubsub(t *testing.T) {
}
})

t.Setenv("PUBSUB_EMULATOR_HOST", container.URI)
client, err := pubsub.NewClient(ctx, "my-project-id")
conn, err := grpc.Dial(container.URI, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
options := []option.ClientOption{option.WithGRPCConn(conn)}
client, err := pubsub.NewClient(ctx, "my-project-id", options...)
if err != nil {
t.Fatal(err)
}
Expand Down
18 changes: 14 additions & 4 deletions examples/spanner/spanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
instance "cloud.google.com/go/spanner/admin/instance/apiv1"
"context"
"fmt"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
databasepb "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/credentials/insecure"
"testing"
)

Expand All @@ -32,9 +36,14 @@ func TestSpanner(t *testing.T) {
}
})

t.Setenv("SPANNER_EMULATOR_HOST", container.GRPCEndpoint)
options := []option.ClientOption{
option.WithEndpoint(container.GRPCEndpoint),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
option.WithoutAuthentication(),
internaloption.SkipDialSettingsValidation(),
}

instanceAdmin, err := instance.NewInstanceAdminClient(ctx)
instanceAdmin, err := instance.NewInstanceAdminClient(ctx, options...)
if err != nil {
t.Fatal(err)
}
Expand All @@ -55,7 +64,7 @@ func TestSpanner(t *testing.T) {
t.Fatal(err)
}

c, err := database.NewDatabaseAdminClient(ctx)
c, err := database.NewDatabaseAdminClient(ctx, options...)
if err != nil {
t.Fatal(err)
}
Expand All @@ -76,7 +85,8 @@ func TestSpanner(t *testing.T) {
t.Fatal(err)
}

client, err := spanner.NewClient(ctx, fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseName))
db := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseName)
client, err := spanner.NewClient(ctx, db, options...)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 63eb588

Please sign in to comment.