storj/private/dbutil/pgutil/openunique_test.go
Egon Elbre 85c45cd56f private/dbutil/pgtest: support multiple databases for testing
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
2020-04-28 21:55:49 +03:00

58 lines
1.9 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package pgutil_test
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"storj.io/common/testcontext"
"storj.io/storj/private/dbutil/pgtest"
"storj.io/storj/private/dbutil/tempdb"
"storj.io/storj/private/tagsql"
)
func TestTempPostgresDB(t *testing.T) {
connstr := pgtest.PickPostgres(t)
ctx := testcontext.New(t)
defer ctx.Cleanup()
prefix := "name#spaced/Test/DB"
testDB, err := tempdb.OpenUnique(ctx, connstr, prefix)
require.NoError(t, err)
// assert new test db exists and can be connected to again
otherConn, err := tagsql.Open(testDB.Driver, testDB.ConnStr)
require.NoError(t, err)
defer ctx.Check(otherConn.Close)
// verify the name matches expectation
var name *string
row := otherConn.QueryRowContext(ctx, `SELECT current_schema()`)
err = row.Scan(&name)
require.NoErrorf(t, err, "connStr=%q", testDB.ConnStr)
require.NotNilf(t, name, "PG has no current_schema, which means the one we asked for doesn't exist. connStr=%q", testDB.ConnStr)
require.Truef(t, strings.HasPrefix(*name, prefix), "Expected prefix of %q for current db name, but found %q", prefix, name)
// verify there is an entry in pg_namespace with such a name
var count int
row = otherConn.QueryRowContext(ctx, `SELECT COUNT(*) FROM pg_namespace WHERE nspname = current_schema`)
err = row.Scan(&count)
require.NoError(t, err)
require.Equalf(t, 1, count, "Expected 1 schema with matching name, but counted %d", count)
// close testDB but leave otherConn open
err = testDB.Close()
require.NoError(t, err)
// assert new test schema was deleted
row = otherConn.QueryRowContext(ctx, `SELECT COUNT(*) FROM pg_namespace WHERE nspname = current_schema`)
err = row.Scan(&count)
require.NoError(t, err)
require.Equalf(t, 0, count, "Expected 0 schemas with matching name, but counted %d (deletion failure?)", count)
}