2019-02-04 20:37:46 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package testplanet
|
|
|
|
|
|
|
|
import (
|
2020-05-26 09:05:43 +01:00
|
|
|
"context"
|
2020-10-29 11:58:36 +00:00
|
|
|
"runtime/pprof"
|
2019-02-04 20:37:46 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-02-02 11:34:28 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-09-23 13:25:09 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-02-02 11:34:28 +00:00
|
|
|
"storj.io/common/context2"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2023-02-02 11:34:28 +00:00
|
|
|
"storj.io/private/dbutil"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/pgtest"
|
2023-02-02 11:34:28 +00:00
|
|
|
"storj.io/private/dbutil/pgutil"
|
|
|
|
"storj.io/private/tagsql"
|
2021-10-11 17:29:03 +01:00
|
|
|
"storj.io/storj/private/testmonkit"
|
2019-02-04 20:37:46 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run runs testplanet in multiple configurations.
|
|
|
|
func Run(t *testing.T, config Config, test func(t *testing.T, ctx *testcontext.Context, planet *Planet)) {
|
2020-04-27 20:34:42 +01:00
|
|
|
databases := satellitedbtest.Databases()
|
2021-05-11 11:08:06 +01:00
|
|
|
if len(databases) == 0 {
|
2020-04-27 20:34:42 +01:00
|
|
|
t.Fatal("Databases flag missing, set at least one:\n" +
|
|
|
|
"-postgres-test-db=" + pgtest.DefaultPostgres + "\n" +
|
|
|
|
"-cockroach-test-db=" + pgtest.DefaultCockroach)
|
|
|
|
}
|
|
|
|
|
2021-05-11 11:08:06 +01:00
|
|
|
for _, satelliteDB := range databases {
|
2019-02-06 09:16:05 +00:00
|
|
|
satelliteDB := satelliteDB
|
2020-04-27 17:28:40 +01:00
|
|
|
t.Run(satelliteDB.Name, func(t *testing.T) {
|
2020-02-27 13:31:05 +00:00
|
|
|
parallel := !config.NonParallel
|
|
|
|
if parallel {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2019-02-05 19:44:00 +00:00
|
|
|
|
2019-10-04 15:12:21 +01:00
|
|
|
if satelliteDB.MasterDB.URL == "" {
|
|
|
|
t.Skipf("Database %s connection string not provided. %s", satelliteDB.MasterDB.Name, satelliteDB.MasterDB.Message)
|
2019-02-04 20:37:46 +00:00
|
|
|
}
|
|
|
|
planetConfig := config
|
2020-03-27 16:18:19 +00:00
|
|
|
if planetConfig.Name == "" {
|
|
|
|
planetConfig.Name = t.Name()
|
2019-10-04 15:12:21 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 11:56:27 +00:00
|
|
|
log := NewLogger(t)
|
2021-09-29 08:06:51 +01:00
|
|
|
|
2021-10-11 17:29:03 +01:00
|
|
|
testmonkit.Run(context.Background(), t, func(parent context.Context) {
|
2021-10-20 14:42:41 +01:00
|
|
|
defer pprof.SetGoroutineLabels(parent)
|
|
|
|
parent = pprof.WithLabels(parent, pprof.Labels("test", t.Name()))
|
|
|
|
|
2022-04-11 12:16:47 +01:00
|
|
|
timeout := config.Timeout
|
|
|
|
if timeout == 0 {
|
|
|
|
timeout = testcontext.DefaultTimeout
|
|
|
|
}
|
|
|
|
ctx := testcontext.NewWithContextAndTimeout(parent, t, timeout)
|
2021-09-29 08:06:51 +01:00
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2023-02-02 11:34:28 +00:00
|
|
|
planetConfig.applicationName = "testplanet" + pgutil.CreateRandomTestingSchemaName(6)
|
2021-10-20 14:42:41 +01:00
|
|
|
planet, err := NewCustom(ctx, log, planetConfig, satelliteDB)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
2021-09-29 08:06:51 +01:00
|
|
|
|
2021-10-20 14:42:41 +01:00
|
|
|
planet.Start(ctx)
|
2021-09-29 08:06:51 +01:00
|
|
|
|
2023-02-02 11:34:28 +00:00
|
|
|
var rawDB tagsql.DB
|
|
|
|
var queriesBefore []string
|
|
|
|
if len(planet.Satellites) > 0 && satelliteDB.Name == "Cockroach" {
|
2023-02-06 12:15:36 +00:00
|
|
|
rawDB = planet.Satellites[0].DB.Testing().RawDB()
|
2023-02-02 11:34:28 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
queriesBefore, err = satellitedbtest.FullTableScanQueries(ctx, rawDB, dbutil.Cockroach, planetConfig.applicationName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:42:41 +01:00
|
|
|
test(t, ctx, planet)
|
2023-02-02 11:34:28 +00:00
|
|
|
|
|
|
|
if rawDB != nil {
|
|
|
|
queriesAfter, err := satellitedbtest.FullTableScanQueries(context2.WithoutCancellation(ctx), rawDB, dbutil.Cockroach, planetConfig.applicationName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := cmp.Diff(queriesBefore, queriesAfter)
|
|
|
|
if diff != "" {
|
|
|
|
log.Sugar().Warnf("FULL TABLE SCAN DETECTED\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 17:29:03 +01:00
|
|
|
})
|
2019-02-04 20:37:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 09:05:43 +01:00
|
|
|
|
2022-09-23 13:25:09 +01:00
|
|
|
// Bench makes benchmark with testplanet as easy as running unit tests with Run method.
|
|
|
|
func Bench(b *testing.B, config Config, bench func(b *testing.B, ctx *testcontext.Context, planet *Planet)) {
|
|
|
|
databases := satellitedbtest.Databases()
|
|
|
|
if len(databases) == 0 {
|
|
|
|
b.Fatal("Databases flag missing, set at least one:\n" +
|
|
|
|
"-postgres-test-db=" + pgtest.DefaultPostgres + "\n" +
|
|
|
|
"-cockroach-test-db=" + pgtest.DefaultCockroach)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, satelliteDB := range databases {
|
|
|
|
satelliteDB := satelliteDB
|
|
|
|
b.Run(satelliteDB.Name, func(b *testing.B) {
|
|
|
|
if satelliteDB.MasterDB.URL == "" {
|
|
|
|
b.Skipf("Database %s connection string not provided. %s", satelliteDB.MasterDB.Name, satelliteDB.MasterDB.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
log := zap.NewNop()
|
|
|
|
|
|
|
|
planetConfig := config
|
|
|
|
if planetConfig.Name == "" {
|
|
|
|
planetConfig.Name = b.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
testmonkit.Run(context.Background(), b, func(parent context.Context) {
|
|
|
|
defer pprof.SetGoroutineLabels(parent)
|
|
|
|
parent = pprof.WithLabels(parent, pprof.Labels("test", b.Name()))
|
|
|
|
|
|
|
|
timeout := config.Timeout
|
|
|
|
if timeout == 0 {
|
|
|
|
timeout = testcontext.DefaultTimeout
|
|
|
|
}
|
|
|
|
ctx := testcontext.NewWithContextAndTimeout(parent, b, timeout)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2023-02-02 11:34:28 +00:00
|
|
|
planetConfig.applicationName = "testplanet-bench"
|
2022-09-23 13:25:09 +01:00
|
|
|
planet, err := NewCustom(ctx, log, planetConfig, satelliteDB)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
|
|
|
|
bench(b, ctx, planet)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|