85c45cd56f
Currently Cockroach isn't performant for concurrent database setup and tear-down. Instead of a single instance allow setting multiple potential connection strings and let the tests pick one connection string randomly. This improves test duration by ~10 minutes. While we are at significantly changing how pgtest works, introduce helper PickPostgres and PickCockroach for selecting the database to reduce code duplications in multiple places. Change-Id: I8ad171d5c4c8a4fc081ec2ae9bdd0cc948a80619
75 lines
2.5 KiB
Go
75 lines
2.5 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package cockroachutil_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"storj.io/common/testcontext"
|
|
"storj.io/storj/private/dbutil"
|
|
"storj.io/storj/private/dbutil/cockroachutil"
|
|
"storj.io/storj/private/dbutil/pgtest"
|
|
"storj.io/storj/private/dbutil/tempdb"
|
|
"storj.io/storj/private/tagsql"
|
|
)
|
|
|
|
func TestTempCockroachDB(t *testing.T) {
|
|
connstr := pgtest.PickCockroach(t)
|
|
|
|
ctx := testcontext.New(t)
|
|
defer ctx.Cleanup()
|
|
|
|
prefix := "name#spaced/Test/DB"
|
|
testDB, err := tempdb.OpenUnique(ctx, connstr, prefix)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "cockroach", testDB.Driver)
|
|
require.Equal(t, dbutil.Cockroach, testDB.Implementation)
|
|
require.IsType(t, &cockroachutil.Driver{}, testDB.DB.Driver())
|
|
|
|
// save these so we can close testDB down below and then still try connecting to the same place
|
|
// (without requiring that the values stay intact in the testDB struct when we close it)
|
|
driverCopy := testDB.Driver
|
|
connStrCopy := testDB.ConnStr
|
|
|
|
// assert new test db exists and can be connected to again
|
|
otherConn, err := tagsql.Open(driverCopy, connStrCopy)
|
|
require.NoError(t, err)
|
|
defer ctx.Check(otherConn.Close)
|
|
|
|
// verify the name matches expectation
|
|
var dbName string
|
|
row := otherConn.QueryRowContext(ctx, `SELECT current_database()`)
|
|
err = row.Scan(&dbName)
|
|
require.NoError(t, err)
|
|
require.Truef(t, strings.HasPrefix(dbName, prefix), "Expected prefix of %q for current db name, but found %q", prefix, dbName)
|
|
|
|
// verify there is a db with such a name
|
|
var count int
|
|
row = otherConn.QueryRowContext(ctx, `SELECT COUNT(*) FROM pg_database WHERE datname = current_database()`)
|
|
err = row.Scan(&count)
|
|
require.NoError(t, err)
|
|
require.Equalf(t, 1, count, "Expected 1 DB with matching name, but counted %d", count)
|
|
|
|
// close testDB
|
|
err = testDB.Close()
|
|
require.NoError(t, err)
|
|
|
|
// make a new connection back to the master connstr just to check that the our temp db
|
|
// really was dropped
|
|
plainDBConn, err := tagsql.Open("cockroach", connstr)
|
|
require.NoError(t, err)
|
|
defer ctx.Check(plainDBConn.Close)
|
|
|
|
// assert new test db was deleted (we expect this connection to keep working, even though its
|
|
// database was deleted out from under it!)
|
|
row = plainDBConn.QueryRowContext(ctx, `SELECT COUNT(*) FROM pg_database WHERE datname = $1`, dbName)
|
|
err = row.Scan(&count)
|
|
require.NoError(t, err)
|
|
require.Equalf(t, 0, count, "Expected 0 DB with matching name, but counted %d (deletion failure?)", count)
|
|
}
|