2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-12-10 14:50:12 +00:00
// See LICENSE for copying information.
package satellitedbtest
// This package should be referenced only in test files!
import (
2019-10-18 20:03:10 +01:00
"strconv"
2019-02-04 20:37:46 +00:00
"strings"
2018-12-10 14:50:12 +00:00
"testing"
2019-02-14 21:55:21 +00:00
"go.uber.org/zap/zaptest"
2019-01-31 19:17:12 +00:00
2019-11-14 19:46:15 +00:00
"storj.io/storj/private/dbutil/pgutil"
"storj.io/storj/private/dbutil/pgutil/pgtest"
2018-12-27 09:56:25 +00:00
"storj.io/storj/satellite"
2018-12-10 14:50:12 +00:00
"storj.io/storj/satellite/satellitedb"
)
2019-10-04 15:12:21 +01:00
// SatelliteDatabases maybe name can be better
type SatelliteDatabases struct {
MasterDB Database
PointerDB Database
}
2019-02-04 20:37:46 +00:00
// Database describes a test database
type Database struct {
Name string
URL string
Message string
}
// Databases returns default databases.
2019-10-04 15:12:21 +01:00
func Databases ( ) [ ] SatelliteDatabases {
return [ ] SatelliteDatabases {
{
2019-10-18 20:03:10 +01:00
MasterDB : Database { "Postgres" , * pgtest . ConnStr , "Postgres flag missing, example: -postgres-test-db=" + pgtest . DefaultConnStr + " or use STORJ_POSTGRES_TEST environment variable." } ,
2019-10-04 15:12:21 +01:00
PointerDB : Database { "Postgres" , * pgtest . ConnStr , "" } ,
} ,
2019-11-22 19:59:46 +00:00
{
MasterDB : Database { "Cockroach" , * pgtest . CrdbConnStr , "Cockroach flag missing, example: -cockroach-test-db=" + pgtest . DefaultCrdbConnStr + " or use STORJ_COCKROACH_TEST environment variable." } ,
PointerDB : Database { "Postgres" , * pgtest . ConnStr , "" } ,
} ,
2019-02-04 20:37:46 +00:00
}
}
2019-10-18 20:03:10 +01:00
// SchemaSuffix returns a suffix for schemas.
func SchemaSuffix ( ) string {
return pgutil . CreateRandomTestingSchemaName ( 6 )
}
// SchemaName returns a properly formatted schema string.
func SchemaName ( testname , category string , index int , schemaSuffix string ) string {
// 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/""
indexStr := strconv . Itoa ( index )
var maxTestNameLen = 64 - len ( category ) - len ( indexStr ) - len ( schemaSuffix ) - 2
if len ( testname ) > maxTestNameLen {
testname = testname [ : maxTestNameLen ]
}
if schemaSuffix == "" {
return strings . ToLower ( testname + "/" + category + indexStr )
}
return strings . ToLower ( testname + "/" + schemaSuffix + "/" + category + indexStr )
}
2018-12-10 14:50:12 +00:00
// Run method will iterate over all supported databases. Will establish
// connection and will create tables for each DB.
2018-12-27 09:56:25 +00:00
func Run ( t * testing . T , test func ( t * testing . T , db satellite . DB ) ) {
2019-02-04 20:37:46 +00:00
for _ , dbInfo := range Databases ( ) {
2019-02-06 09:16:05 +00:00
dbInfo := dbInfo
2019-10-04 15:12:21 +01:00
t . Run ( dbInfo . MasterDB . Name + "/" + dbInfo . PointerDB . Name , func ( t * testing . T ) {
2019-02-05 19:44:00 +00:00
t . Parallel ( )
2019-11-22 19:59:46 +00:00
// TODO: remove this skip once all the sql is cockroachdb compatible
if dbInfo . MasterDB . Name == "Cockroach" {
t . Skip ( "CockroachDB not supported yet" )
}
2019-10-04 15:12:21 +01:00
if dbInfo . MasterDB . URL == "" {
2019-10-18 20:03:10 +01:00
t . Fatalf ( "Database %s connection string not provided. %s" , dbInfo . MasterDB . Name , dbInfo . MasterDB . Message )
2018-12-10 14:50:12 +00:00
}
2019-10-18 20:03:10 +01:00
schemaSuffix := SchemaSuffix ( )
t . Log ( "schema-suffix " , schemaSuffix )
2019-02-14 21:55:21 +00:00
log := zaptest . NewLogger ( t )
2019-10-18 20:03:10 +01:00
schema := SchemaName ( t . Name ( ) , "T" , 0 , schemaSuffix )
2019-02-14 21:55:21 +00:00
2019-11-22 19:59:46 +00:00
db , err := satellitedb . New ( log , dbInfo . MasterDB . URL )
2018-12-10 14:50:12 +00:00
if err != nil {
t . Fatal ( err )
}
2019-11-22 19:59:46 +00:00
if dbInfo . MasterDB . Name == "Postgres" {
pgdb , err := satellitedb . New ( log , pgutil . ConnstrWithSchema ( dbInfo . MasterDB . URL , schema ) )
if err != nil {
t . Fatal ( err )
}
db = & SchemaDB {
DB : pgdb ,
Schema : schema ,
AutoDrop : true ,
}
2019-01-31 19:17:12 +00:00
}
2018-12-10 14:50:12 +00:00
defer func ( ) {
2019-10-18 20:03:10 +01:00
err := db . Close ( )
2018-12-10 14:50:12 +00:00
if err != nil {
t . Fatal ( err )
}
} ( )
err = db . CreateTables ( )
if err != nil {
t . Fatal ( err )
}
2018-12-11 09:30:09 +00:00
test ( t , db )
2018-12-10 14:50:12 +00:00
} )
}
}
2019-05-19 16:10:46 +01:00
// Bench method will iterate over all supported databases. Will establish
// connection and will create tables for each DB.
func Bench ( b * testing . B , bench func ( b * testing . B , db satellite . DB ) ) {
for _ , dbInfo := range Databases ( ) {
dbInfo := dbInfo
2019-10-04 15:12:21 +01:00
b . Run ( dbInfo . MasterDB . Name + "/" + dbInfo . PointerDB . Name , func ( b * testing . B ) {
if dbInfo . MasterDB . URL == "" {
b . Skipf ( "Database %s connection string not provided. %s" , dbInfo . MasterDB . Name , dbInfo . MasterDB . Message )
2019-05-19 16:10:46 +01:00
}
2019-10-18 20:03:10 +01:00
schemaSuffix := SchemaSuffix ( )
b . Log ( "schema-suffix " , schemaSuffix )
log := zaptest . NewLogger ( b )
schema := SchemaName ( b . Name ( ) , "X" , 0 , schemaSuffix )
2019-05-19 16:10:46 +01:00
2019-11-22 19:59:46 +00:00
db , err := satellitedb . New ( log , dbInfo . MasterDB . URL )
2019-05-19 16:10:46 +01:00
if err != nil {
b . Fatal ( err )
}
2019-11-22 19:59:46 +00:00
if dbInfo . MasterDB . Name == "Postgres" {
pgdb , err := satellitedb . New ( log , pgutil . ConnstrWithSchema ( dbInfo . MasterDB . URL , schema ) )
if err != nil {
b . Fatal ( err )
}
db = & SchemaDB {
DB : pgdb ,
Schema : schema ,
AutoDrop : true ,
}
2019-05-19 16:10:46 +01:00
}
defer func ( ) {
2019-10-18 20:03:10 +01:00
err := db . Close ( )
2019-05-19 16:10:46 +01:00
if err != nil {
b . Fatal ( err )
}
} ( )
err = db . CreateTables ( )
if err != nil {
b . Fatal ( err )
}
bench ( b , db )
} )
}
}