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.

79 lines
2.3 KiB
Go
Raw Normal View History

2022-07-20 16:24:46 -07:00
// Copyright (c) HashiCorp, Inc.
2023-02-02 17:15:17 -08:00
// SPDX-License-Identifier: BUSL-1.1
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.
2022-07-20 16:24:46 -07:00
func TestTarget(ctx context.Context, t testing.TB, conn *db.DB, projectId, 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)
2023-11-02 16:03:47 -04:00
tar, err := target.New(ctx, Subtype, projectId, opt...)
2021-09-29 13:13:49 -04:00
require.NoError(err)
id, err := db.NewPublicId(ctx, TargetPrefix)
2021-09-29 13:13:49 -04:00
require.NoError(err)
2024-02-16 08:30:16 -05:00
require.NoError(tar.SetPublicId(ctx, id))
require.NoError(rw.Create(ctx, tar))
2021-09-29 13:13:49 -04:00
if opts.WithAddress != "" {
address, err := target.NewAddress(ctx, tar.GetPublicId(), opts.WithAddress)
require.NoError(err)
require.NotNil(address)
err = rw.Create(context.Background(), address)
require.NoError(err)
}
2021-09-29 13:13:49 -04:00
if len(opts.WithHostSources) > 0 {
newHostSets := make([]*target.TargetHostSet, 0, len(opts.WithHostSources))
2021-09-29 13:13:49 -04:00
for _, s := range opts.WithHostSources {
hostSet, err := target.NewTargetHostSet(ctx, tar.GetPublicId(), s)
2021-09-29 13:13:49 -04:00
require.NoError(err)
newHostSets = append(newHostSets, hostSet)
}
err := rw.CreateItems(ctx, newHostSets)
2021-09-29 13:13:49 -04:00
require.NoError(err)
}
if len(opts.WithCredentialLibraries) > 0 {
newCredLibs := make([]*target.CredentialLibrary, 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(ctx, newCredLibs)
2021-09-29 13:13:49 -04:00
require.NoError(err)
}
if len(opts.WithStaticCredentials) > 0 {
newCreds := make([]*target.StaticCredential, 0, len(opts.WithStaticCredentials))
for _, c := range opts.WithStaticCredentials {
c.TargetId = tar.GetPublicId()
newCreds = append(newCreds, c)
}
err := rw.CreateItems(ctx, newCreds)
require.NoError(err)
}
2021-09-29 13:13:49 -04:00
return tar
}
2022-07-20 16:24:46 -07:00
func testTargetName(t testing.TB, projectId string) string {
2021-09-29 13:13:49 -04:00
t.Helper()
2022-07-20 16:24:46 -07:00
return fmt.Sprintf("%s-%s", projectId, testId(t))
2021-09-29 13:13:49 -04:00
}
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
}