Files
threshold/internal/target/tcp/testing.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.8 KiB
Go
Raw Normal View History

2021-09-29 13:13:49 -04:00
package tcp
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
)
// TestTarget is used to create a Target that can be used by tests in other packages.
func TestTarget(ctx context.Context, t testing.TB, conn *db.DB, scopeId, name string, opt ...target.Option) target.Target {
2021-09-29 13:13:49 -04:00
t.Helper()
opt = append(opt, target.WithName(name))
opts := target.GetOpts(opt...)
require := require.New(t)
rw := db.New(conn)
2021-11-04 11:37:32 -04:00
tar, err := target.New(ctx, Subtype, scopeId, opt...)
2021-09-29 13:13:49 -04:00
require.NoError(err)
id, err := db.NewPublicId(TargetPrefix)
require.NoError(err)
2021-11-04 11:37:32 -04:00
tar.SetPublicId(ctx, id)
2021-09-29 13:13:49 -04:00
err = rw.Create(context.Background(), tar)
require.NoError(err)
if len(opts.WithHostSources) > 0 {
newHostSets := make([]interface{}, 0, len(opts.WithHostSources))
for _, s := range opts.WithHostSources {
2021-11-04 11:37:32 -04:00
hostSet, err := target.NewTargetHostSet(tar.GetPublicId(), s)
2021-09-29 13:13:49 -04:00
require.NoError(err)
newHostSets = append(newHostSets, hostSet)
}
err := rw.CreateItems(context.Background(), newHostSets)
require.NoError(err)
}
if len(opts.WithCredentialLibraries) > 0 {
newCredLibs := make([]interface{}, 0, len(opts.WithCredentialLibraries))
for _, cl := range opts.WithCredentialLibraries {
2021-11-04 11:37:32 -04:00
cl.TargetId = tar.GetPublicId()
newCredLibs = append(newCredLibs, cl)
2021-09-29 13:13:49 -04:00
}
err := rw.CreateItems(context.Background(), newCredLibs)
require.NoError(err)
}
return tar
}
func testTargetName(t testing.TB, scopeId string) string {
2021-09-29 13:13:49 -04:00
t.Helper()
return fmt.Sprintf("%s-%s", scopeId, testId(t))
}
func testId(t testing.TB) string {
2021-09-29 13:13:49 -04:00
t.Helper()
id, err := uuid.GenerateUUID()
require.NoError(t, err)
2021-11-04 11:37:32 -04:00
return fmt.Sprintf("%s_%s", TargetPrefix, id)
2021-09-29 13:13:49 -04:00
}