2019-11-26 16:39:57 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedbtest_test
|
|
|
|
|
|
|
|
import (
|
2019-12-04 13:55:10 +00:00
|
|
|
"regexp"
|
2019-11-26 16:39:57 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/private/dbutil"
|
|
|
|
"storj.io/storj/private/dbutil/pgutil/pgtest"
|
|
|
|
"storj.io/storj/private/testcontext"
|
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewCockroach(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
if *pgtest.CrdbConnStr == "" {
|
|
|
|
t.Skip("Cockroachdb flag missing")
|
|
|
|
}
|
2019-12-03 16:24:45 +00:00
|
|
|
namespacedDBName := "name#spaced/Test/DB"
|
2019-11-26 16:39:57 +00:00
|
|
|
testdb, err := satellitedbtest.NewCockroach(zap.L(), namespacedDBName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert new test db exists
|
2019-12-02 21:05:21 +00:00
|
|
|
driver, source, _, err := dbutil.SplitConnStr(*pgtest.CrdbConnStr)
|
2019-12-03 16:24:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-11-26 16:39:57 +00:00
|
|
|
db, err := dbx.Open(driver, source)
|
2019-12-03 16:24:45 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-26 16:39:57 +00:00
|
|
|
defer ctx.Check(db.Close)
|
|
|
|
|
2019-12-04 13:55:10 +00:00
|
|
|
// NewCockroach removes all non-alphanumeric characters from the name. We need it to match.
|
|
|
|
r := regexp.MustCompile(`\W`)
|
|
|
|
formattedName := r.ReplaceAllString(namespacedDBName, "")
|
|
|
|
|
2019-11-26 16:39:57 +00:00
|
|
|
var exists *bool
|
|
|
|
row := db.QueryRow(`SELECT EXISTS (
|
|
|
|
SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower($1)
|
2019-12-04 13:55:10 +00:00
|
|
|
);`, formattedName,
|
2019-11-26 16:39:57 +00:00
|
|
|
)
|
|
|
|
err = row.Scan(&exists)
|
2019-12-03 16:24:45 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-26 16:39:57 +00:00
|
|
|
assert.True(t, *exists)
|
|
|
|
|
|
|
|
err = testdb.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert new test db was deleted
|
|
|
|
row = db.QueryRow(`SELECT EXISTS (
|
|
|
|
SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower($1)
|
2019-12-04 13:55:10 +00:00
|
|
|
);`, formattedName,
|
2019-11-26 16:39:57 +00:00
|
|
|
)
|
|
|
|
err = row.Scan(&exists)
|
2019-12-03 16:24:45 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-26 16:39:57 +00:00
|
|
|
assert.False(t, *exists)
|
|
|
|
}
|