2019-02-04 20:37:46 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package testplanet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2019-02-14 21:55:21 +00:00
|
|
|
"go.uber.org/zap"
|
2019-02-04 20:37:46 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2019-02-13 16:06:34 +00:00
|
|
|
"storj.io/storj/internal/dbutil/pgutil"
|
2019-02-04 20:37:46 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/satellite"
|
2019-10-10 19:06:26 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2019-02-04 20:37:46 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2019-10-10 19:06:26 +01:00
|
|
|
"storj.io/storj/storage/postgreskv"
|
2019-02-04 20:37:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run runs testplanet in multiple configurations.
|
|
|
|
func Run(t *testing.T, config Config, test func(t *testing.T, ctx *testcontext.Context, planet *Planet)) {
|
2019-10-10 19:06:26 +01:00
|
|
|
schemaSuffix := pgutil.CreateRandomTestingSchemaName(6)
|
2019-02-04 20:37:46 +00:00
|
|
|
t.Log("schema-suffix ", schemaSuffix)
|
|
|
|
|
|
|
|
for _, satelliteDB := range satellitedbtest.Databases() {
|
2019-02-06 09:16:05 +00:00
|
|
|
satelliteDB := satelliteDB
|
2019-10-04 15:12:21 +01:00
|
|
|
t.Run(satelliteDB.MasterDB.Name, func(t *testing.T) {
|
2019-02-05 19:44:00 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-10-10 19:06:26 +01:00
|
|
|
// postgres has a maximum schema length of 64
|
|
|
|
// we need additional 6 bytes for the random suffix
|
|
|
|
// and 4 bytes for the satellite index "/S0/""
|
|
|
|
const MaxTestNameLength = 64 - 6 - 4
|
|
|
|
|
|
|
|
testname := t.Name()
|
|
|
|
if len(testname) > MaxTestNameLength {
|
|
|
|
testname = testname[:MaxTestNameLength]
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
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
|
2019-02-14 21:55:21 +00:00
|
|
|
planetConfig.Reconfigure.NewSatelliteDB = func(log *zap.Logger, index int) (satellite.DB, error) {
|
2019-10-10 19:06:26 +01:00
|
|
|
schema := strings.ToLower(testname + "/S" + strconv.Itoa(index) + "/" + schemaSuffix)
|
2019-10-04 15:12:21 +01:00
|
|
|
db, err := satellitedb.New(log, pgutil.ConnstrWithSchema(satelliteDB.MasterDB.URL, schema))
|
2019-02-04 20:37:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.CreateSchema(schema)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &satelliteSchema{
|
|
|
|
DB: db,
|
|
|
|
schema: schema,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:12:21 +01:00
|
|
|
if satelliteDB.PointerDB.URL != "" {
|
2019-10-10 19:06:26 +01:00
|
|
|
planetConfig.Reconfigure.NewSatellitePointerDB = func(log *zap.Logger, index int) (metainfo.PointerDB, error) {
|
|
|
|
schema := strings.ToLower(testname + "/P" + strconv.Itoa(index) + "/" + schemaSuffix)
|
|
|
|
|
|
|
|
db, err := postgreskv.New(pgutil.ConnstrWithSchema(satelliteDB.PointerDB.URL, schema))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-10-04 15:12:21 +01:00
|
|
|
}
|
2019-10-10 19:06:26 +01:00
|
|
|
|
|
|
|
return &satellitePointerSchema{
|
|
|
|
Client: db,
|
|
|
|
schema: schema,
|
|
|
|
}, nil
|
2019-10-04 15:12:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
planet, err := NewCustom(zaptest.NewLogger(t), planetConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
2019-04-22 10:07:50 +01:00
|
|
|
|
|
|
|
// make sure nodes are refreshed in db
|
|
|
|
planet.Satellites[0].Discovery.Service.Refresh.TriggerWait()
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
test(t, ctx, planet)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// satelliteSchema closes database and drops the associated schema
|
|
|
|
type satelliteSchema struct {
|
|
|
|
satellite.DB
|
|
|
|
schema string
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:06:26 +01:00
|
|
|
// Close closes the database and drops the schema.
|
2019-02-04 20:37:46 +00:00
|
|
|
func (db *satelliteSchema) Close() error {
|
|
|
|
return errs.Combine(
|
|
|
|
db.DB.DropSchema(db.schema),
|
|
|
|
db.DB.Close(),
|
|
|
|
)
|
|
|
|
}
|
2019-10-10 19:06:26 +01:00
|
|
|
|
|
|
|
// satellitePointerSchema closes database and drops the associated schema
|
|
|
|
type satellitePointerSchema struct {
|
|
|
|
*postgreskv.Client
|
|
|
|
schema string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the database and drops the schema.
|
|
|
|
func (db *satellitePointerSchema) Close() error {
|
|
|
|
return errs.Combine(
|
|
|
|
db.Client.DropSchema(db.schema),
|
|
|
|
db.Client.Close(),
|
|
|
|
)
|
|
|
|
}
|