* refact: Changes required for gorm v2 * refact: Add db.DB wrapper and refact all test fixtures to use it Stop leaking the gorm abstraction within our unit tests. * refact: Migrate direct calls to pq to pgx driver The refactor will remove the remaining direct dependencies on the pq driver which has been deprecated in favor of the pgx driver. It includes the addition of common.SqlOpen which is a wrapper around sql.Open, which is needed to deal with the driver name changing from "postgres" to "pgx" but in our code base the dialect and driver are synonyms.
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package target
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/go-uuid"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTcpTarget(t *testing.T, conn *db.DB, scopeId, name string, opt ...Option) *TcpTarget {
|
|
t.Helper()
|
|
opt = append(opt, WithName(name))
|
|
opts := getOpts(opt...)
|
|
require := require.New(t)
|
|
rw := db.New(conn)
|
|
target, err := NewTcpTarget(scopeId, opt...)
|
|
require.NoError(err)
|
|
id, err := newTcpTargetId()
|
|
require.NoError(err)
|
|
target.PublicId = id
|
|
err = rw.Create(context.Background(), target)
|
|
require.NoError(err)
|
|
|
|
if len(opts.withHostSources) > 0 {
|
|
newHostSets := make([]interface{}, 0, len(opts.withHostSources))
|
|
for _, s := range opts.withHostSources {
|
|
hostSet, err := NewTargetHostSet(target.PublicId, s)
|
|
require.NoError(err)
|
|
newHostSets = append(newHostSets, hostSet)
|
|
}
|
|
err := rw.CreateItems(context.Background(), newHostSets)
|
|
require.NoError(err)
|
|
}
|
|
if len(opts.withCredentialSources) > 0 {
|
|
newCredLibs := make([]interface{}, 0, len(opts.withCredentialSources))
|
|
for _, cl := range opts.withCredentialSources {
|
|
newCl, err := NewCredentialLibrary(target.PublicId, cl)
|
|
require.NoError(err)
|
|
newCredLibs = append(newCredLibs, newCl)
|
|
}
|
|
err := rw.CreateItems(context.Background(), newCredLibs)
|
|
require.NoError(err)
|
|
}
|
|
return target
|
|
}
|
|
|
|
func testTargetName(t *testing.T, scopeId string) string {
|
|
t.Helper()
|
|
return fmt.Sprintf("%s-%s", scopeId, testId(t))
|
|
}
|
|
|
|
func testId(t *testing.T) string {
|
|
t.Helper()
|
|
id, err := uuid.GenerateUUID()
|
|
require.NoError(t, err)
|
|
return id
|
|
}
|
|
|
|
// TestCredentialLibrary creates a CredentialLibrary for targetId and
|
|
// libraryId.
|
|
func TestCredentialLibrary(t *testing.T, conn *db.DB, targetId, libraryId string) *CredentialLibrary {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
rw := db.New(conn)
|
|
lib, err := NewCredentialLibrary(targetId, libraryId)
|
|
require.NoError(err)
|
|
err = rw.Create(context.Background(), lib)
|
|
require.NoError(err)
|
|
return lib
|
|
}
|