2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-10 14:50:12 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedbtest
|
|
|
|
|
|
|
|
// This package should be referenced only in test files!
|
|
|
|
|
|
|
|
import (
|
2019-02-04 20:37:46 +00:00
|
|
|
"strings"
|
2018-12-10 14:50:12 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-01-31 19:17:12 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-05-19 16:10:46 +01:00
|
|
|
"go.uber.org/zap"
|
2019-02-14 21:55:21 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-01-31 19:17:12 +00:00
|
|
|
|
2019-02-13 16:06:34 +00:00
|
|
|
"storj.io/storj/internal/dbutil/pgutil"
|
2019-04-26 14:39:11 +01:00
|
|
|
"storj.io/storj/internal/dbutil/pgutil/pgtest"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-10 14:50:12 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-02-04 20:37:46 +00:00
|
|
|
// DefaultSqliteConn is a connstring that is inmemory
|
|
|
|
DefaultSqliteConn = "sqlite3://file::memory:?mode=memory"
|
2018-12-10 14:50:12 +00:00
|
|
|
)
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
// Database describes a test database
|
|
|
|
type Database struct {
|
|
|
|
Name string
|
|
|
|
URL string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Databases returns default databases.
|
|
|
|
func Databases() []Database {
|
|
|
|
return []Database{
|
|
|
|
{"Sqlite", DefaultSqliteConn, ""},
|
2019-04-26 14:39:11 +01:00
|
|
|
{"Postgres", *pgtest.ConnStr, "Postgres flag missing, example: -postgres-test-db=" + pgtest.DefaultConnStr},
|
2019-02-04 20:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 14:50:12 +00:00
|
|
|
// Run method will iterate over all supported databases. Will establish
|
|
|
|
// connection and will create tables for each DB.
|
2018-12-27 09:56:25 +00:00
|
|
|
func Run(t *testing.T, test func(t *testing.T, db satellite.DB)) {
|
2019-04-02 17:52:25 +01:00
|
|
|
schemaSuffix := pgutil.CreateRandomTestingSchemaName(8)
|
2019-02-04 20:37:46 +00:00
|
|
|
t.Log("schema-suffix ", schemaSuffix)
|
|
|
|
|
|
|
|
for _, dbInfo := range Databases() {
|
2019-02-06 09:16:05 +00:00
|
|
|
dbInfo := dbInfo
|
2019-02-04 20:37:46 +00:00
|
|
|
t.Run(dbInfo.Name, func(t *testing.T) {
|
2019-02-05 19:44:00 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
if dbInfo.URL == "" {
|
|
|
|
t.Skipf("Database %s connection string not provided. %s", dbInfo.Name, dbInfo.Message)
|
2018-12-10 14:50:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 21:55:21 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
schema := strings.ToLower(t.Name() + "-satellite/x-" + schemaSuffix)
|
2019-02-13 16:06:34 +00:00
|
|
|
connstr := pgutil.ConnstrWithSchema(dbInfo.URL, schema)
|
2019-02-14 21:55:21 +00:00
|
|
|
db, err := satellitedb.New(log, connstr)
|
2018-12-10 14:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
err = db.CreateSchema(schema)
|
2019-01-31 19:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-10 14:50:12 +00:00
|
|
|
defer func() {
|
2019-02-04 20:37:46 +00:00
|
|
|
dropErr := db.DropSchema(schema)
|
2019-01-31 19:17:12 +00:00
|
|
|
err := errs.Combine(dropErr, db.Close())
|
2018-12-10 14:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = db.CreateTables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-11 09:30:09 +00:00
|
|
|
test(t, db)
|
2018-12-10 14:50:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 16:10:46 +01:00
|
|
|
|
|
|
|
// Bench method will iterate over all supported databases. Will establish
|
|
|
|
// connection and will create tables for each DB.
|
|
|
|
func Bench(b *testing.B, bench func(b *testing.B, db satellite.DB)) {
|
|
|
|
schemaSuffix := pgutil.CreateRandomTestingSchemaName(8)
|
|
|
|
b.Log("schema-suffix ", schemaSuffix)
|
|
|
|
|
|
|
|
for _, dbInfo := range Databases() {
|
|
|
|
dbInfo := dbInfo
|
|
|
|
b.Run(dbInfo.Name, func(b *testing.B) {
|
|
|
|
if dbInfo.URL == "" {
|
|
|
|
b.Skipf("Database %s connection string not provided. %s", dbInfo.Name, dbInfo.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
log := zap.NewNop()
|
|
|
|
|
|
|
|
schema := strings.ToLower(b.Name() + "-satellite/x-" + schemaSuffix)
|
|
|
|
connstr := pgutil.ConnstrWithSchema(dbInfo.URL, schema)
|
|
|
|
db, err := satellitedb.New(log, connstr)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.CreateSchema(schema)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
dropErr := db.DropSchema(schema)
|
|
|
|
err := errs.Combine(dropErr, db.Close())
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = db.CreateTables()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bench(b, db)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|