Files
threshold/internal/oplog/testing.go
T

92 lines
2.2 KiB
Go
Raw Normal View History

2020-06-10 06:56:46 -04:00
package oplog
import (
"context"
2020-06-10 06:56:46 -04:00
"crypto/rand"
"database/sql"
2020-06-10 06:56:46 -04:00
"testing"
"github.com/hashicorp/boundary/internal/db/schema"
2020-09-28 16:05:15 -04:00
"github.com/hashicorp/boundary/internal/docker"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/oplog/oplog_test"
2020-06-10 06:56:46 -04:00
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-kms-wrapping/wrappers/aead"
"github.com/hashicorp/go-uuid"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2020-09-28 16:05:15 -04:00
func testCleanup(t *testing.T, cleanupFunc func() error, db *gorm.DB) {
2020-06-10 06:56:46 -04:00
t.Helper()
2020-09-28 16:05:15 -04:00
err := cleanupFunc()
assert.NoError(t, err)
err = db.Close()
2020-06-10 06:56:46 -04:00
assert.NoError(t, err)
}
func testUser(t *testing.T, db *gorm.DB, name, phoneNumber, email string) *oplog_test.TestUser {
t.Helper()
u := &oplog_test.TestUser{
Name: name,
PhoneNumber: phoneNumber,
Email: email,
}
w := GormWriter{db}
err := w.Create(&u)
require.NoError(t, err)
return u
}
func testFindUser(t *testing.T, db *gorm.DB, userId uint32) *oplog_test.TestUser {
t.Helper()
var foundUser oplog_test.TestUser
err := db.Where("id = ?", userId).First(&foundUser).Error
require.NoError(t, err)
return &foundUser
}
func testId(t *testing.T) string {
t.Helper()
id, err := uuid.GenerateUUID()
require.NoError(t, err)
return id
}
2020-09-28 16:05:15 -04:00
func testInitDbInDocker(t *testing.T) (cleanup func() error, retURL string, err error) {
2020-06-10 06:56:46 -04:00
t.Helper()
2021-05-03 13:11:28 -05:00
2020-09-28 16:05:15 -04:00
cleanup, retURL, _, err = docker.StartDbInDocker("postgres")
if err != nil {
t.Fatal(err)
2020-06-10 06:56:46 -04:00
}
2020-09-28 16:05:15 -04:00
testInitStore(t, cleanup, retURL)
return
2020-06-10 06:56:46 -04:00
}
// 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
2020-09-28 16:05:15 -04:00
func testInitStore(t *testing.T, cleanup func() error, url string) {
2020-06-10 06:56:46 -04:00
t.Helper()
ctx := context.Background()
dialect := "postgres"
2020-06-10 06:56:46 -04:00
d, err := sql.Open(dialect, url)
require.NoError(t, err)
sm, err := schema.NewManager(ctx, dialect, d)
require.NoError(t, err)
require.NoError(t, sm.RollForward(ctx))
2020-06-10 06:56:46 -04:00
}