-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
spanner_test.go
88 lines (78 loc) · 2.73 KB
/
spanner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//go:build spanner
// +build spanner
package testfixtures
import (
"context"
"fmt"
"os"
"testing"
database "cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
instance "cloud.google.com/go/spanner/admin/instance/apiv1"
"cloud.google.com/go/spanner/admin/instance/apiv1/instancepb"
)
func TestSpanner(t *testing.T) {
prepareSpannerDB(t)
db := openDB(t, "spanner", os.Getenv("SPANNER_CONN_STRING"))
loadSchemaInBatchesBySplitter(t, db, "testdata/schema/spanner.sql", []byte(";\n"))
testLoader(t, db, "spanner", DangerousSkipTestDatabaseCheck())
}
func prepareSpannerDB(t *testing.T) {
t.Helper()
if err := os.Setenv("SPANNER_EMULATOR_HOST", "spanner:9010"); err != nil {
t.Fatalf("failed to set SPANNER_EMULATOR_HOST: %v", err)
}
projectId, instanceId, databaseId := "test-project", "test-instance", "testdb"
if err := createInstance(projectId, instanceId); err != nil {
t.Fatalf("failed to create instance on emulator: %v", err)
}
if err := createSampleDB(projectId, instanceId, databaseId); err != nil {
t.Fatalf("failed to create database on emulator: %v", err)
}
}
func createInstance(projectId, instanceId string) error {
ctx := context.Background()
instanceAdmin, err := instance.NewInstanceAdminClient(ctx)
if err != nil {
return err
}
defer instanceAdmin.Close()
op, err := instanceAdmin.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
Parent: fmt.Sprintf("projects/%s", projectId),
InstanceId: instanceId,
Instance: &instancepb.Instance{
Config: fmt.Sprintf("projects/%s/instanceConfigs/%s", projectId, "emulator-config"),
DisplayName: instanceId,
NodeCount: 1,
},
})
if err != nil {
return fmt.Errorf("could not create instance %s: %v", fmt.Sprintf("projects/%s/instances/%s", projectId, instanceId), err)
}
// Wait for the instance creation to finish.
if _, err := op.Wait(ctx); err != nil {
return fmt.Errorf("waiting for instance creation to finish failed: %v", err)
}
return nil
}
func createSampleDB(projectId, instanceId, databaseId string, statements ...string) error {
ctx := context.Background()
databaseAdminClient, err := database.NewDatabaseAdminClient(ctx)
if err != nil {
return err
}
defer databaseAdminClient.Close()
opDB, err := databaseAdminClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
Parent: fmt.Sprintf("projects/%s/instances/%s", projectId, instanceId),
CreateStatement: fmt.Sprintf("CREATE DATABASE `%s`", databaseId),
ExtraStatements: statements,
})
if err != nil {
return err
}
// Wait for the database creation to finish.
if _, err := opDB.Wait(ctx); err != nil {
return fmt.Errorf("waiting for database creation to finish failed: %v", err)
}
return nil
}