-
Notifications
You must be signed in to change notification settings - Fork 342
/
db.go
245 lines (215 loc) · 6.01 KB
/
db.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package testutil
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"testing"
"cloud.google.com/go/storage"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/google/uuid"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/ory/dockertest/v3"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/block/gs"
"github.com/treeverse/lakefs/pkg/block/mem"
lakefsS3 "github.com/treeverse/lakefs/pkg/block/s3"
"github.com/treeverse/lakefs/pkg/db"
"github.com/treeverse/lakefs/pkg/db/params"
)
const (
DBContainerTimeoutSeconds = 60 * 30 // 30 minutes
EnvKeyUseBlockAdapter = "USE_BLOCK_ADAPTER"
envKeyAwsKeyID = "AWS_ACCESS_KEY_ID"
envKeyAwsSecretKey = "AWS_SECRET_ACCESS_KEY" //nolint:gosec
envKeyAwsRegion = "AWS_DEFAULT_REGION"
)
var keepDB = flag.Bool("keep-db", false, "keep test DB instance running")
func GetDBInstance(pool *dockertest.Pool) (string, func()) {
// connect using docker container name
containerName := os.Getenv("PG_DB_CONTAINER")
if containerName != "" {
resource, ok := pool.ContainerByName(containerName)
if !ok {
log.Fatalf("Cloud not find DB container (%s)", containerName)
}
uri := formatPostgresResourceURI(resource)
return uri, func() {}
}
// connect using supply address
dbURI := os.Getenv("PG_TEST_URI")
if len(dbURI) > 0 {
// use supplied DB connection for testing
if err := verifyDBConnectionString(dbURI); err != nil {
log.Fatalf("could not connect to postgres: %s", err)
}
return dbURI, func() {}
}
// run new instance and connect
resource, err := pool.Run("postgres", "11", []string{
"POSTGRES_USER=lakefs",
"POSTGRES_PASSWORD=lakefs",
"POSTGRES_DB=lakefs_db",
})
if err != nil {
log.Fatalf("Could not start postgresql: %s", err)
}
// expire the container, just to be on the safe side
if !*keepDB {
err = resource.Expire(DBContainerTimeoutSeconds)
if err != nil {
log.Fatalf("could not expire postgres container")
}
}
// format db uri
uri := formatPostgresResourceURI(resource)
// wait for container to start and connect to db
if err = pool.Retry(func() error {
return verifyDBConnectionString(uri)
}); err != nil {
log.Fatalf("could not connect to postgres: %s", err)
}
// set cleanup
closer := func() {
if *keepDB {
return
}
err := pool.Purge(resource)
if err != nil {
log.Fatalf("could not kill postgres container")
}
}
// return DB address and closer func
return uri, closer
}
func formatPostgresResourceURI(resource *dockertest.Resource) string {
dbParams := map[string]string{
"POSTGRES_DB": "lakefs_db",
"POSTGRES_USER": "lakefs",
"POSTGRES_PASSWORD": "lakefs",
"POSTGRES_PORT": resource.GetPort("5432/tcp"),
}
env := resource.Container.Config.Env
for _, entry := range env {
for key := range dbParams {
if strings.HasPrefix(entry, key+"=") {
dbParams[key] = entry[len(key)+1:]
break
}
}
}
uri := fmt.Sprintf("postgres://%s:%s@localhost:%s/%s?sslmode=disable",
dbParams["POSTGRES_USER"],
dbParams["POSTGRES_PASSWORD"],
dbParams["POSTGRES_PORT"],
dbParams["POSTGRES_DB"],
)
return uri
}
func verifyDBConnectionString(uri string) error {
ctx := context.Background()
pool, err := pgxpool.Connect(ctx, uri)
if err != nil {
return err
}
defer pool.Close()
return db.Ping(ctx, pool)
}
type GetDBOptions struct {
ApplyDDL bool
}
type GetDBOption func(options *GetDBOptions)
func WithGetDBApplyDDL(apply bool) GetDBOption {
return func(options *GetDBOptions) {
options.ApplyDDL = apply
}
}
func GetDB(t testing.TB, uri string, opts ...GetDBOption) (db.Database, string) {
ctx := context.Background()
options := &GetDBOptions{
ApplyDDL: true,
}
for _, opt := range opts {
opt(options)
}
// generate uuid as schema name
generatedSchema := fmt.Sprintf("schema_%s",
strings.ReplaceAll(uuid.New().String(), "-", ""))
// create connection
connURI := fmt.Sprintf("%s&search_path=%s,public", uri, generatedSchema)
pool, err := pgxpool.Connect(ctx, connURI)
if err != nil {
t.Fatalf("could not connect to PostgreSQL: %s", err)
}
err = db.Ping(ctx, pool)
if err != nil {
pool.Close()
t.Fatalf("could not ping PostgreSQL: %s", err)
}
t.Cleanup(func() {
pool.Close()
})
database := db.NewPgxDatabase(pool)
_, err = database.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return tx.Exec("CREATE SCHEMA IF NOT EXISTS " + generatedSchema)
})
if err != nil {
t.Fatalf("could not create schema: %v", err)
}
if options.ApplyDDL {
// do the actual migration
err := db.MigrateUp(params.Database{ConnectionString: connURI})
if err != nil {
t.Fatal("could not create schema:", err)
}
}
// return DB
return database, connURI
}
func Must(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatalf("error returned for operation: %v", err)
}
}
func MustDo(t testing.TB, what string, err error) {
t.Helper()
if err != nil {
t.Fatalf("%s, expected no error, got err=%s", what, err)
}
}
func NewBlockAdapterByType(t testing.TB, translator block.UploadIDTranslator, blockstoreType string) block.Adapter {
switch blockstoreType {
case block.BlockstoreTypeGS:
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
t.Fatal("Google Storage new client", err)
}
return gs.NewAdapter(client, gs.WithTranslator(translator))
case block.BlockstoreTypeS3:
awsRegion, regionOk := os.LookupEnv(envKeyAwsRegion)
if !regionOk {
awsRegion = "us-east-1"
}
cfg := &aws.Config{
Region: aws.String(awsRegion),
}
awsSecret, secretOk := os.LookupEnv(envKeyAwsSecretKey)
awsKey, keyOk := os.LookupEnv(envKeyAwsKeyID)
if keyOk && secretOk {
cfg.Credentials = credentials.NewStaticCredentials(awsKey, awsSecret, "")
} else {
cfg.Credentials = credentials.NewSharedCredentials("", "default")
}
sess := session.Must(session.NewSession(cfg))
return lakefsS3.NewAdapter(sess, lakefsS3.WithTranslator(translator))
default:
return mem.New(mem.WithTranslator(translator))
}
}