101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information
|
|
|
|
package testplanet
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/zeebo/errs"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
"storj.io/storj/internal/dbutil/pgutil"
|
|
"storj.io/storj/internal/testcontext"
|
|
"storj.io/storj/satellite"
|
|
"storj.io/storj/satellite/satellitedb"
|
|
"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)) {
|
|
schemaSuffix := pgutil.CreateRandomTestingSchemaName(8)
|
|
t.Log("schema-suffix ", schemaSuffix)
|
|
|
|
for _, satelliteDB := range satellitedbtest.Databases() {
|
|
satelliteDB := satelliteDB
|
|
t.Run(satelliteDB.MasterDB.Name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testcontext.New(t)
|
|
defer ctx.Cleanup()
|
|
|
|
if satelliteDB.MasterDB.URL == "" {
|
|
t.Skipf("Database %s connection string not provided. %s", satelliteDB.MasterDB.Name, satelliteDB.MasterDB.Message)
|
|
}
|
|
|
|
planetConfig := config
|
|
planetConfig.Reconfigure.NewSatelliteDB = func(log *zap.Logger, index int) (satellite.DB, error) {
|
|
schema := strings.ToLower(t.Name() + "-satellite/" + strconv.Itoa(index) + "-" + schemaSuffix)
|
|
db, err := satellitedb.New(log, pgutil.ConnstrWithSchema(satelliteDB.MasterDB.URL, schema))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = db.CreateSchema(schema)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return &satelliteSchema{
|
|
DB: db,
|
|
schema: schema,
|
|
}, nil
|
|
}
|
|
|
|
if satelliteDB.PointerDB.URL != "" {
|
|
satReconfigure := planetConfig.Reconfigure.Satellite
|
|
planetConfig.Reconfigure.Satellite = func(log *zap.Logger, index int, config *satellite.Config) {
|
|
// TODO: use a different unique schema name to ensure we can drop it separately instead of relying
|
|
// master database to drop it
|
|
// Something like:
|
|
// schema := strings.ToLower(t.Name() + "-satellite/" + strconv.Itoa(index) + "-metainfo" + "-" + schemaSuffix)
|
|
schema := strings.ToLower(t.Name() + "-satellite/" + strconv.Itoa(index) + "-" + schemaSuffix)
|
|
config.Metainfo.DatabaseURL = pgutil.ConnstrWithSchema(satelliteDB.PointerDB.URL, schema)
|
|
if satReconfigure != nil {
|
|
satReconfigure(log, index, config)
|
|
}
|
|
}
|
|
}
|
|
|
|
planet, err := NewCustom(zaptest.NewLogger(t), planetConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
planet.Start(ctx)
|
|
|
|
// make sure nodes are refreshed in db
|
|
planet.Satellites[0].Discovery.Service.Refresh.TriggerWait()
|
|
|
|
test(t, ctx, planet)
|
|
})
|
|
}
|
|
}
|
|
|
|
// satelliteSchema closes database and drops the associated schema
|
|
type satelliteSchema struct {
|
|
satellite.DB
|
|
schema string
|
|
}
|
|
|
|
func (db *satelliteSchema) Close() error {
|
|
return errs.Combine(
|
|
db.DB.DropSchema(db.schema),
|
|
db.DB.Close(),
|
|
)
|
|
}
|