mirror of https://github.com/hashicorp/boundary
Many tests had duplicate code to create the base level domain objects in a boundary environment. This commit refactors that code to use functions for these commonly used operations.pull/2541/head
parent
051d337b21
commit
618eca19f5
@ -0,0 +1,123 @@
|
||||
package boundary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/hostcatalogs"
|
||||
"github.com/hashicorp/boundary/api/hosts"
|
||||
"github.com/hashicorp/boundary/api/hostsets"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// CreateNewHostCatalogApi creates a new host catalog in boundary using the go api.
|
||||
// Returns the id of the new host catalog.
|
||||
func CreateNewHostCatalogApi(t testing.TB, ctx context.Context, client *api.Client, projectId string) string {
|
||||
hcClient := hostcatalogs.NewClient(client)
|
||||
newHostCatalogResult, err := hcClient.Create(ctx, "static", projectId,
|
||||
hostcatalogs.WithName("e2e Automated Test Host Catalog"),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
newHostCatalogId := newHostCatalogResult.Item.Id
|
||||
t.Logf("Created Host Catalog: %s", newHostCatalogId)
|
||||
|
||||
return newHostCatalogId
|
||||
}
|
||||
|
||||
// CreateNewHostSetApi creates a new host set in boundary using the go api.
|
||||
// Returns the id of the new host set.
|
||||
func CreateNewHostSetApi(t testing.TB, ctx context.Context, client *api.Client, hostCatalogId string) string {
|
||||
hsClient := hostsets.NewClient(client)
|
||||
newHostSetResult, err := hsClient.Create(ctx, hostCatalogId)
|
||||
require.NoError(t, err)
|
||||
newHostSetId := newHostSetResult.Item.Id
|
||||
t.Logf("Created Host Set: %s", newHostSetId)
|
||||
|
||||
return newHostSetId
|
||||
}
|
||||
|
||||
// CreateNewHostApi creates a new host in boundary using the go api
|
||||
// Returns the id of the new host.
|
||||
func CreateNewHostApi(t testing.TB, ctx context.Context, client *api.Client, hostCatalogId string, address string) string {
|
||||
hClient := hosts.NewClient(client)
|
||||
newHostResult, err := hClient.Create(ctx, hostCatalogId,
|
||||
hosts.WithName(address),
|
||||
hosts.WithStaticHostAddress(address),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
newHostId := newHostResult.Item.Id
|
||||
t.Logf("Created Host: %s", newHostId)
|
||||
|
||||
return newHostId
|
||||
}
|
||||
|
||||
// AddHostToHostSetApi adds a host to a host set using the go api
|
||||
func AddHostToHostSetApi(t testing.TB, ctx context.Context, client *api.Client, hostSetId string, hostId string) {
|
||||
hsClient := hostsets.NewClient(client)
|
||||
_, err := hsClient.AddHosts(ctx, hostSetId, 0, []string{hostId}, hostsets.WithAutomaticVersioning(true))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// CreateNewHostCatalogCli creates a new host catalog in boundary using the cli.
|
||||
// Returns the id of the new host catalog.
|
||||
func CreateNewHostCatalogCli(t testing.TB, projectId string) string {
|
||||
output := e2e.RunCommand("boundary", "host-catalogs", "create", "static",
|
||||
"-scope-id", projectId,
|
||||
"-name", "e2e Automated Test Host Catalog",
|
||||
"-format", "json",
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var newHostCatalogResult hostcatalogs.HostCatalogCreateResult
|
||||
err := json.Unmarshal(output.Stdout, &newHostCatalogResult)
|
||||
require.NoError(t, err)
|
||||
newHostCatalogId := newHostCatalogResult.Item.Id
|
||||
|
||||
t.Logf("Created Host Catalog: %s", newHostCatalogId)
|
||||
return newHostCatalogId
|
||||
}
|
||||
|
||||
// CreateNewHostSetCli creates a new host set in boundary using the cli.
|
||||
// Returns the id of the new host set.
|
||||
func CreateNewHostSetCli(t testing.TB, hostCatalogId string) string {
|
||||
output := e2e.RunCommand("boundary", "host-sets", "create", "static",
|
||||
"-host-catalog-id", hostCatalogId,
|
||||
"-name", "e2e Automated Test Host Set",
|
||||
"-format", "json",
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var newHostSetResult hostsets.HostSetCreateResult
|
||||
err := json.Unmarshal(output.Stdout, &newHostSetResult)
|
||||
require.NoError(t, err)
|
||||
newHostSetId := newHostSetResult.Item.Id
|
||||
t.Logf("Created Host Set: %s", newHostSetId)
|
||||
|
||||
return newHostSetId
|
||||
}
|
||||
|
||||
// CreateNewHostCli creates a new host in boundary using the cli.
|
||||
// Returns the id of the new host.
|
||||
func CreateNewHostCli(t testing.TB, hostCatalogId string, address string) string {
|
||||
output := e2e.RunCommand("boundary", "hosts", "create", "static",
|
||||
"-host-catalog-id", hostCatalogId,
|
||||
"-name", address,
|
||||
"-address", address,
|
||||
"-format", "json",
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var newHostResult hosts.HostCreateResult
|
||||
err := json.Unmarshal(output.Stdout, &newHostResult)
|
||||
require.NoError(t, err)
|
||||
newHostId := newHostResult.Item.Id
|
||||
t.Logf("Created Host: %s", newHostId)
|
||||
|
||||
return newHostId
|
||||
}
|
||||
|
||||
// AddHostToHostSetCli adds a host to a host set using the cli
|
||||
func AddHostToHostSetCli(t testing.TB, hostSetId string, hostId string) {
|
||||
output := e2e.RunCommand("boundary", "host-sets", "add-hosts", "-id", hostSetId, "-host", hostId)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
package boundary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// CreateNewTargetApi creates a new target in boundary using the go api.
|
||||
// Returns the id of the new target.
|
||||
func CreateNewTargetApi(t testing.TB, ctx context.Context, client *api.Client, projectId string, defaultPort string) string {
|
||||
tClient := targets.NewClient(client)
|
||||
targetPort, err := strconv.ParseInt(defaultPort, 10, 32)
|
||||
require.NoError(t, err)
|
||||
newTargetResult, err := tClient.Create(ctx, "tcp", projectId,
|
||||
targets.WithName("e2e Automated Test Target"),
|
||||
targets.WithTcpTargetDefaultPort(uint32(targetPort)),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
newTargetId := newTargetResult.Item.Id
|
||||
t.Logf("Created Target: %s", newTargetId)
|
||||
|
||||
return newTargetId
|
||||
}
|
||||
|
||||
// AddHostSourceToTargetApi adds a host source (host set or host) to a target using the go api
|
||||
func AddHostSourceToTargetApi(t testing.TB, ctx context.Context, client *api.Client, targetId string, hostSourceId string) {
|
||||
tClient := targets.NewClient(client)
|
||||
_, err := tClient.AddHostSources(ctx, targetId, 0,
|
||||
[]string{hostSourceId},
|
||||
targets.WithAutomaticVersioning(true),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// CreateNewTargetCli creates a new target in boundary using the cli
|
||||
// Returns the id of the new target.
|
||||
func CreateNewTargetCli(t testing.TB, projectId string, defaultPort string) string {
|
||||
output := e2e.RunCommand("boundary", "targets", "create", "tcp",
|
||||
"-scope-id", projectId,
|
||||
"-default-port", defaultPort,
|
||||
"-name", "e2e Automated Test Target",
|
||||
"-format", "json",
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var newTargetResult targets.TargetCreateResult
|
||||
err := json.Unmarshal(output.Stdout, &newTargetResult)
|
||||
require.NoError(t, err)
|
||||
newTargetId := newTargetResult.Item.Id
|
||||
t.Logf("Created Target: %s", newTargetId)
|
||||
|
||||
return newTargetId
|
||||
}
|
||||
|
||||
// AddHostSourceToTargetCli adds a host source (host set or host) to a target using the cli
|
||||
func AddHostSourceToTargetCli(t testing.TB, targetId string, hostSourceId string) {
|
||||
output := e2e.RunCommand("boundary", "targets", "add-host-sources",
|
||||
"-id", targetId,
|
||||
"-host-source", hostSourceId,
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
}
|
||||
Loading…
Reference in new issue