-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
165 lines (124 loc) · 4.4 KB
/
client.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mockspanner
import (
"context"
"fmt"
"os"
"testing"
"cloud.google.com/go/spanner"
databaseAdmin "cloud.google.com/go/spanner/admin/database/apiv1"
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
instanceAdmin "cloud.google.com/go/spanner/admin/instance/apiv1"
"cloud.google.com/go/spanner/admin/instance/apiv1/instancepb"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// Client represents the underlying container that is running the mock Spanner instance. It includes all of the
// clients that are needed to interact with the instance, database, and user client.
type Client struct {
t *testing.T
instance *Instance
grpc *grpc.ClientConn
client *spanner.Client
dbAdminClient *databaseAdmin.DatabaseAdminClient
instanceAdminClient *instanceAdmin.InstanceAdminClient
}
// GRPC fetches the connection to the underlying Spanner gRPC server.
func (r *Client) GRPC() *grpc.ClientConn {
r.t.Helper()
return r.grpc
}
// Client returns the underlying Spanner client.
func (r *Client) Client() *spanner.Client {
r.t.Helper()
return r.client
}
// Client returns the underlying Spanner Instance Admin client.
func (r *Client) InstanceAdmin() *instanceAdmin.InstanceAdminClient {
r.t.Helper()
return r.instanceAdminClient
}
// Client returns the underlying Spanner Database Admin client.
func (r *Client) DatabaseAdmin() *databaseAdmin.DatabaseAdminClient {
r.t.Helper()
return r.dbAdminClient
}
// Close closes the underlying Spanner clients and the instance.
func (r *Client) Close(ctx context.Context) {
r.t.Helper()
r.client.Close()
// nolint:errcheck,gosec
r.instanceAdminClient.Close()
// nolint:errcheck,gosec
r.dbAdminClient.Close()
r.instance.Close(ctx)
}
func NewClient(ctx context.Context, t *testing.T) (*Client, error) {
t.Helper()
instance, err := NewInstance(ctx, t)
if err != nil {
return nil, fmt.Errorf("creating the instance: %v", err)
}
spannerPort, err := instance.Port(ctx)
if err != nil {
return nil, fmt.Errorf("getting the mapped port of the instance: %v", err)
}
// nolint:gosec
if err := os.Setenv("GCLOUD_PROJECT", ProjectID); err != nil {
return nil, fmt.Errorf("setting GCLOUD_PROJECT environment variable: %v", err)
}
// nolint:gosec
if err := os.Setenv("SPANNER_EMULATOR_HOST", "localhost:"+spannerPort.Port()); err != nil {
return nil, fmt.Errorf("setting SPANNER_EMULATOR_HOST environment variable: %v", err)
}
fullDatabaseName := fmt.Sprintf("projects/%s/instances/%s/databases/%s", ProjectID, InstanceID, DatabaseID)
conn, err := grpc.Dial("localhost:"+spannerPort.Port(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
// create the spanner mock clients
instanceAdminClient, err := instanceAdmin.NewInstanceAdminClient(ctx, option.WithGRPCConn(conn))
if err != nil {
return nil, fmt.Errorf("creating instance admin client: %v", err)
}
_, err = instanceAdminClient.GetInstance(ctx, &instancepb.GetInstanceRequest{
Name: "projects/" + ProjectID + "/instances/" + InstanceID,
})
if err != nil {
_, err = instanceAdminClient.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
Parent: "projects/" + ProjectID,
InstanceId: InstanceID,
})
if err != nil {
return nil, fmt.Errorf("creating instance: %v", err)
}
}
adminClient, err := databaseAdmin.NewDatabaseAdminClient(ctx, option.WithGRPCConn(conn))
if err != nil {
return nil, fmt.Errorf("creating database admin client: %v", err)
}
op, err := adminClient.CreateDatabase(ctx, &adminpb.CreateDatabaseRequest{
Parent: "projects/" + ProjectID + "/instances/" + InstanceID,
CreateStatement: "CREATE DATABASE `" + DatabaseID + "`",
})
if err != nil {
return nil, fmt.Errorf("creating database: %v", err)
}
_, err = op.Wait(ctx)
if err != nil {
return nil, fmt.Errorf("waiting for database to be created: %v", err)
}
spannerClient, err := spanner.NewClient(ctx, fullDatabaseName, option.WithGRPCConn(conn), option.WithGRPCConnectionPool(10))
if err != nil {
return nil, fmt.Errorf("creating spanner client: %v", err)
}
client := &Client{
t: t,
instance: instance,
grpc: conn,
client: spannerClient,
dbAdminClient: adminClient,
instanceAdminClient: instanceAdminClient,
}
return client, nil
}