2021-04-26 10:35:44 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
2020-10-28 15:28:06 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-04-26 10:35:44 +01:00
|
|
|
package metabasetest
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
"github.com/spf13/pflag"
|
2020-11-05 13:30:39 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
"storj.io/common/memory"
|
2020-10-28 15:28:06 +00:00
|
|
|
"storj.io/common/testcontext"
|
2021-09-24 15:18:21 +01:00
|
|
|
"storj.io/private/cfgstruct"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2021-09-24 15:18:21 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2020-11-05 13:30:39 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2020-10-28 15:28:06 +00:00
|
|
|
)
|
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
// RunWithConfig runs tests with specific metabase configuration.
|
|
|
|
func RunWithConfig(t *testing.T, config metabase.Config, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) {
|
2021-05-11 11:08:06 +01:00
|
|
|
for _, dbinfo := range satellitedbtest.Databases() {
|
|
|
|
dbinfo := dbinfo
|
|
|
|
t.Run(dbinfo.Name, func(t *testing.T) {
|
2020-10-28 15:28:06 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, dbinfo.MetabaseDB, config)
|
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)
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:20:06 +01:00
|
|
|
fn(ctx, t, db)
|
2020-10-28 15:28:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
// Run runs tests against all configured databases.
|
|
|
|
func Run(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) {
|
|
|
|
var config metainfo.Config
|
|
|
|
cfgstruct.Bind(pflag.NewFlagSet("", pflag.PanicOnError), &config,
|
|
|
|
cfgstruct.UseTestDefaults(),
|
|
|
|
)
|
|
|
|
|
|
|
|
RunWithConfig(t, metabase.Config{
|
|
|
|
MinPartSize: config.MinPartSize,
|
|
|
|
MaxNumberOfParts: config.MaxNumberOfParts,
|
|
|
|
}, fn)
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:35:44 +01:00
|
|
|
// Bench runs benchmark for all configured databases.
|
2021-01-22 17:34:08 +00:00
|
|
|
func Bench(b *testing.B, fn func(ctx *testcontext.Context, b *testing.B, db *metabase.DB)) {
|
2021-05-11 11:08:06 +01:00
|
|
|
for _, dbinfo := range satellitedbtest.Databases() {
|
|
|
|
dbinfo := dbinfo
|
|
|
|
b.Run(dbinfo.Name, func(b *testing.B) {
|
2021-01-22 17:34:08 +00:00
|
|
|
ctx := testcontext.New(b)
|
|
|
|
defer ctx.Cleanup()
|
2021-09-24 15:18:21 +01:00
|
|
|
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(b), b.Name(), "M", 0, dbinfo.MetabaseDB, metabase.Config{
|
|
|
|
MinPartSize: 5 * memory.MiB,
|
|
|
|
MaxNumberOfParts: 10000,
|
|
|
|
})
|
2021-01-22 17:34:08 +00:00
|
|
|
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()
|
2021-06-07 10:20:06 +01:00
|
|
|
fn(ctx, b, db)
|
2021-01-22 17:34:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|