Files
threshold/internal/oplog/testing.go
T
Jim 591ec476e7 refactor: Update internal/db and oplog to use go-dbw package for database operations. (#1785)
* chore: Add new dbw dependency

* refactor: Convert to use dbw.DB

* refactor (db): Change GetTicket(...) signature

* feature (db tests): Added TestDeleteWhere(...)
Adding this allows us to eliminate a leak of the gorm library

* refactor: Add ctx to ScanRows(...) signature
This is required to refactor all the deprecated errors pkg calls.
This refactor required adding a context to quite a few call chains
and in particular the job scheduler bits

* feature (db/tests): Add support for test opt: WithTestLogLevel(...)
This test only option allows callers to specify a log level for the
underlying db package (where supported/applicable) when calling
TestSetup(...)

* refactor (db): Simplify Db type
Only store the underlying *DB and create the *RW on demand when
needed.

* refactor (oplog): Port to using dbw pkg for db operations

* refactor (db): Remove gormDB(...) func

* tests (db): Add unit tests for TestDeleteWhere(...)

* chore: make fmt changes to formatting
2022-01-05 14:38:38 -05:00

153 lines
4.0 KiB
Go

package oplog
import (
"context"
"crypto/rand"
"testing"
"github.com/hashicorp/boundary/internal/db/common"
"github.com/hashicorp/boundary/internal/db/schema"
"github.com/hashicorp/boundary/internal/oplog/oplog_test"
"github.com/hashicorp/boundary/testing/dbtest"
"github.com/hashicorp/go-dbw"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-kms-wrapping/wrappers/aead"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/driver/postgres"
)
// setup the tests (initialize the database one-time and intialized testDatabaseURL)
func setup(t *testing.T) *dbw.DB {
t.Helper()
require := require.New(t)
cleanup, url, err := testInitDbInDocker(t)
require.NoError(err)
db := testOpen(t, "postgres", url)
require.NoError(err)
oplog_test.Init(t, db)
t.Cleanup(func() {
assert.NoError(t, db.Close(context.Background()))
assert.NoError(t, cleanup())
})
return db
}
func testOpen(t *testing.T, dbType string, connectionUrl string) *dbw.DB {
t.Helper()
require := require.New(t)
var dialect dbw.Dialector
switch dbType {
case "postgres":
dialect = postgres.New(postgres.Config{
DSN: connectionUrl,
},
)
default:
t.Errorf("unable to open %s database type", dbType)
t.FailNow()
}
db, err := dbw.OpenWith(dialect)
require.NoError(err)
return db
}
func testUser(t *testing.T, db *dbw.DB, name, phoneNumber, email string) *oplog_test.TestUser {
t.Helper()
u := &oplog_test.TestUser{
Name: name,
PhoneNumber: phoneNumber,
Email: email,
}
require.NoError(t, dbw.New(db).Create(context.Background(), u))
return u
}
func testFindUser(t *testing.T, db *dbw.DB, userId uint32) *oplog_test.TestUser {
t.Helper()
var foundUser oplog_test.TestUser
require.NoError(t, dbw.New(db).LookupWhere(context.Background(), &foundUser, "id = ?", []interface{}{userId}))
return &foundUser
}
func testId(t *testing.T) string {
t.Helper()
id, err := dbw.NewId("i")
require.NoError(t, err)
return id
}
func testInitDbInDocker(t *testing.T) (cleanup func() error, retURL string, err error) {
t.Helper()
require := require.New(t)
cleanup, retURL, _, err = dbtest.StartUsingTemplate(dbtest.Postgres)
require.NoError(err)
testInitStore(t, cleanup, retURL)
return
}
// testWrapper initializes an AEAD wrapping.Wrapper for testing the oplog
func testWrapper(t *testing.T) wrapping.Wrapper {
t.Helper()
rootKey := make([]byte, 32)
n, err := rand.Read(rootKey)
require.NoError(t, err)
require.Equal(t, n, 32)
root := aead.NewWrapper(nil)
err = root.SetAESGCMKeyBytes(rootKey)
require.NoError(t, err)
return root
}
// testInitStore will execute the migrations needed to initialize the store for tests
func testInitStore(t *testing.T, cleanup func() error, url string) {
t.Helper()
ctx := context.Background()
dialect := "postgres"
d, err := common.SqlOpen(dialect, url)
require.NoError(t, err)
sm, err := schema.NewManager(ctx, schema.Dialect(dialect), d)
require.NoError(t, err)
require.NoError(t, sm.ApplyMigrations(ctx))
}
type constraintResults struct {
Name string
TableName string
}
func testListConstraints(t *testing.T, db *dbw.DB, tableName string) []constraintResults {
t.Helper()
require := require.New(t)
testCtx := context.Background()
const constraintSql = `select pgc.conname as name,
ccu.table_schema as table_schema,
ccu.table_name,
ccu.column_name,
pgc.consrc as definition
from pg_constraint pgc
join pg_namespace nsp on nsp.oid = pgc.connamespace
join pg_class cls on pgc.conrelid = cls.oid
left join information_schema.constraint_column_usage ccu
on pgc.conname = ccu.constraint_name
and nsp.nspname = ccu.constraint_schema
-- where contype ='c'
order by ccu.table_name,pgc.conname `
rw := dbw.New(db)
rows, err := rw.Query(testCtx, constraintSql, []interface{}{tableName})
require.NoError(err)
type result struct {
Name string
TableName string
}
results := []constraintResults{}
for rows.Next() {
var r constraintResults
rw.ScanRows(rows, &r)
results = append(results, r)
}
return results
}