-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathadmin_test.go
More file actions
131 lines (114 loc) · 3.16 KB
/
admin_test.go
File metadata and controls
131 lines (114 loc) · 3.16 KB
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
package spool
import (
"context"
"fmt"
"testing"
"cloud.google.com/go/spanner"
admin "cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"github.com/cloudspannerecosystem/spool/model"
)
func connect(ctx context.Context, t *testing.T, conf *Config) (*spanner.Client, func()) {
t.Helper()
client, err := spanner.NewClient(ctx, conf.Database(), conf.ClientOptions()...)
if err != nil {
t.Fatal(err)
}
t.Cleanup(client.Close)
return client, func() {
if err := clean(ctx, client, conf,
func(ctx context.Context, txn *spanner.ReadWriteTransaction) ([]*model.SpoolDatabase, error) {
return model.FindAllSpoolDatabases(ctx, txn)
},
); err != nil {
t.Fatal(err)
}
}
}
func TestSetup(t *testing.T) {
t.Parallel()
cfg := ConfigTestDatabase(t)
ctx := context.Background()
adminClient, err := admin.NewDatabaseAdminClient(ctx, cfg.ClientOptions()...)
if err != nil {
t.Fatal(err)
}
op, err := adminClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
Parent: cfg.Instance(),
CreateStatement: fmt.Sprintf("CREATE DATABASE `%s`", cfg.DatabaseID()),
})
if err != nil {
t.Fatal(err)
}
if _, err := op.Wait(ctx); err != nil {
t.Fatal(err)
}
if err := Setup(ctx, cfg); err != nil {
t.Fatal(err)
}
}
func TestListAll(t *testing.T) {
t.Parallel()
cfg := SetupTestDatabase(t)
ctx := context.Background()
client, truncate := connect(ctx, t, cfg)
t.Cleanup(truncate)
sdb1 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-1",
Checksum: "checksum-1",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
sdb2 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-2",
Checksum: "checksum-2",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
if _, err := client.Apply(ctx, []*spanner.Mutation{sdb1.Insert(ctx), sdb2.Insert(ctx)}); err != nil {
t.Fatalf("failed to setup fixture: %s", err)
}
sdbs, err := ListAll(ctx, cfg)
if err != nil {
t.Fatal(err)
}
if len(sdbs) != 2 {
t.Errorf("failed to find all: found %d", len(sdbs))
}
}
func TestCleanAll(t *testing.T) {
t.Parallel()
cfg := SetupTestDatabase(t)
ctx := context.Background()
client, truncate := connect(ctx, t, cfg)
t.Cleanup(truncate)
sdb1 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-1",
Checksum: "checksum-1",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
sdb2 := &model.SpoolDatabase{
DatabaseName: "zoncoen-spool-test-2",
Checksum: "checksum-2",
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
if _, err := client.Apply(ctx, []*spanner.Mutation{sdb1.Insert(ctx), sdb2.Insert(ctx)}); err != nil {
t.Fatalf("failed to setup fixture: %s", err)
}
if err := CleanAll(ctx, cfg); err != nil {
t.Fatal(err)
}
sdbs, err := model.FindAllSpoolDatabases(ctx, client.Single())
if err != nil {
t.Error(err)
}
if len(sdbs) != 0 {
t.Error("failed to clean all")
}
}