2020-10-28 15:28:06 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-05 13:30:39 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
2020-11-05 13:30:39 +00:00
|
|
|
_ "storj.io/storj/private/dbutil/cockroachutil" // register cockroach driver
|
2020-10-28 15:28:06 +00:00
|
|
|
"storj.io/storj/satellite/metainfo/metabase"
|
2020-11-05 13:30:39 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2020-10-28 15:28:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var databases = flag.String("databases", os.Getenv("STORJ_TEST_DATABASES"), "databases to use for testing")
|
|
|
|
|
2021-01-22 17:34:08 +00:00
|
|
|
type dbinfo struct {
|
|
|
|
name string
|
|
|
|
driver string
|
|
|
|
connstr string
|
|
|
|
}
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2021-01-22 17:34:08 +00:00
|
|
|
func databaseInfos() []dbinfo {
|
2020-10-28 15:28:06 +00:00
|
|
|
infos := []dbinfo{
|
|
|
|
{"pg", "pgx", "postgres://storj:storj-pass@localhost/metabase?sslmode=disable"},
|
2020-11-05 13:30:39 +00:00
|
|
|
{"crdb", "pgx", "cockroach://root@localhost:26257/metabase?sslmode=disable"},
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
if *databases != "" {
|
|
|
|
infos = nil
|
|
|
|
for _, db := range strings.Split(*databases, ";") {
|
|
|
|
toks := strings.Split(strings.TrimSpace(db), "|")
|
|
|
|
infos = append(infos, dbinfo{toks[0], toks[1], toks[2]})
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 17:34:08 +00:00
|
|
|
return infos
|
|
|
|
}
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2021-01-22 17:34:08 +00:00
|
|
|
func All(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) {
|
|
|
|
for _, info := range databaseInfos() {
|
2020-10-28 15:28:06 +00:00
|
|
|
info := info
|
|
|
|
t.Run(info.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-11-05 13:30:39 +00:00
|
|
|
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, satellitedbtest.Database{
|
|
|
|
Name: info.name,
|
|
|
|
URL: info.connstr,
|
|
|
|
Message: "",
|
|
|
|
})
|
2020-10-28 15:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := db.MigrateToLatest(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:30:39 +00:00
|
|
|
fn(ctx, t, db.InternalImplementation().(*metabase.DB))
|
2020-10-28 15:28:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 17:34:08 +00:00
|
|
|
func Bench(b *testing.B, fn func(ctx *testcontext.Context, b *testing.B, db *metabase.DB)) {
|
|
|
|
for _, info := range databaseInfos() {
|
|
|
|
info := info
|
|
|
|
b.Run(info.name, func(b *testing.B) {
|
|
|
|
ctx := testcontext.New(b)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(b), b.Name(), "M", 0, satellitedbtest.Database{
|
|
|
|
Name: info.name,
|
|
|
|
URL: info.connstr,
|
|
|
|
Message: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := db.MigrateToLatest(ctx); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
fn(ctx, b, db.InternalImplementation().(*metabase.DB))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:28:06 +00:00
|
|
|
func TestSetup(t *testing.T) {
|
|
|
|
All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) {
|
|
|
|
err := db.Ping(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = db.TestingGetState(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|