Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve datastore, firestore, pubsub and spanner tests #670

Merged
merged 1 commit into from Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/datastore/datastore_test.go
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
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
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
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