refact(e2e): Add WithEnv option to RunCommand

This was done to keep the scope of environment variables to just the command execution
This commit is contained in:
Michael Li committed 2022-10-25 16:23:52 -04:00
1 parent 1cc2b0b820
commit a956e8dec9
17 files changed
+487 -266

No files matched your search

+14 -10
View File
@@ -3,7 +3,6 @@ package boundary
import (
"context"
"encoding/json"
"os"
"testing"
"github.com/hashicorp/boundary/api"
@@ -47,14 +46,17 @@ func CreateNewAccountCli(t testing.TB, loginName string) (string, string) {
ctx := context.Background()
password, err := base62.Random(16)
require.NoError(t, err)
os.Setenv("E2E_TEST_ACCOUNT_PASSWORD", password)
output := e2e.RunCommand(ctx, "boundary", "accounts", "create", "password",
"-auth-method-id", c.AuthMethodId,
"-login-name", loginName,
"-password", "env://E2E_TEST_ACCOUNT_PASSWORD",
"-name", "e2e Account "+loginName,
"-description", "e2e Account",
"-format", "json",
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"accounts", "create", "password",
"-auth-method-id", c.AuthMethodId,
"-login-name", loginName,
"-password", "env://E2E_TEST_ACCOUNT_PASSWORD",
"-name", "e2e Account "+loginName,
"-description", "e2e Account",
"-format", "json",
),
e2e.WithEnv("E2E_TEST_ACCOUNT_PASSWORD", password),
)
require.NoError(t, output.Err, string(output.Stderr))
@@ -65,7 +67,9 @@ func CreateNewAccountCli(t testing.TB, loginName string) (string, string) {
newAccountId := newAccountResult.Item.Id
t.Cleanup(func() {
AuthenticateAdminCli(t)
output := e2e.RunCommand(ctx, "boundary", "accounts", "delete", "-id", newAccountId)
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("accounts", "delete", "-id", newAccountId),
)
require.NoError(t, output.Err, string(output.Stderr))
})
+9 -7
View File
@@ -4,7 +4,6 @@ package boundary
import (
"context"
"fmt"
"os"
"testing"
"github.com/hashicorp/boundary/api"
@@ -73,12 +72,15 @@ func AuthenticateCli(t testing.TB, loginName string, password string) {
c, err := loadConfig()
require.NoError(t, err)
os.Setenv("E2E_TEST_BOUNDARY_PASSWORD", password)
output := e2e.RunCommand(context.Background(), "boundary", "authenticate", "password",
"-addr", c.Address,
"-auth-method-id", c.AuthMethodId,
"-login-name", loginName,
"-password", "env://E2E_TEST_BOUNDARY_PASSWORD",
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs(
"authenticate", "password",
"-addr", c.Address,
"-auth-method-id", c.AuthMethodId,
"-login-name", loginName,
"-password", "env://E2E_TEST_BOUNDARY_PASSWORD",
),
e2e.WithEnv("E2E_TEST_BOUNDARY_PASSWORD", password),
)
require.NoError(t, output.Err, string(output.Stderr))
}
+6 -3
View File
@@ -26,9 +26,12 @@ func CreateNewCredentialStoreStaticApi(t testing.TB, ctx context.Context, client
// CreateNewCredentialStoreStaticCli creates a new static credential store using the cli.
// Returns the id of the new credential store
func CreateNewCredentialStoreStaticCli(t testing.TB, projectId string) string {
output := e2e.RunCommand(context.Background(), "boundary", "credential-stores", "create", "static",
"-scope-id", projectId,
"-format", "json",
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs(
"credential-stores", "create", "static",
"-scope-id", projectId,
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newCredentialStoreResult credentialstores.CredentialStoreCreateResult
+25 -14
View File
@@ -62,10 +62,13 @@ func AddHostToHostSetApi(t testing.TB, ctx context.Context, client *api.Client,
// 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(context.Background(), "boundary", "host-catalogs", "create", "static",
"-scope-id", projectId,
"-name", "e2e Host Catalog",
"-format", "json",
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs(
"host-catalogs", "create", "static",
"-scope-id", projectId,
"-name", "e2e Host Catalog",
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newHostCatalogResult hostcatalogs.HostCatalogCreateResult
@@ -80,10 +83,13 @@ func CreateNewHostCatalogCli(t testing.TB, projectId string) string {
// 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(context.Background(), "boundary", "host-sets", "create", "static",
"-host-catalog-id", hostCatalogId,
"-name", "e2e Host Set",
"-format", "json",
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs(
"host-sets", "create", "static",
"-host-catalog-id", hostCatalogId,
"-name", "e2e Host Set",
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newHostSetResult hostsets.HostSetCreateResult
@@ -98,11 +104,14 @@ func CreateNewHostSetCli(t testing.TB, hostCatalogId string) string {
// 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(context.Background(), "boundary", "hosts", "create", "static",
"-host-catalog-id", hostCatalogId,
"-name", address,
"-address", address,
"-format", "json",
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs(
"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
@@ -116,6 +125,8 @@ func CreateNewHostCli(t testing.TB, hostCatalogId string, address string) string
// AddHostToHostSetCli adds a host to a host set using the cli
func AddHostToHostSetCli(t testing.TB, hostSetId string, hostId string) {
output := e2e.RunCommand(context.Background(), "boundary", "host-sets", "add-hosts", "-id", hostSetId, "-host", hostId)
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs("host-sets", "add-hosts", "-id", hostSetId, "-host", hostId),
)
require.NoError(t, output.Err, string(output.Stderr))
}
+17 -9
View File
@@ -45,10 +45,13 @@ func CreateNewProjectApi(t testing.TB, ctx context.Context, client *api.Client,
// Returns the id of the new org.
func CreateNewOrgCli(t testing.TB) string {
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "scopes", "create",
"-name", "e2e Org",
"-scope-id", "global",
"-format", "json",
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"scopes", "create",
"-name", "e2e Org",
"-scope-id", "global",
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
@@ -59,7 +62,9 @@ func CreateNewOrgCli(t testing.TB) string {
newOrgId := newOrgResult.Item.Id
t.Cleanup(func() {
AuthenticateAdminCli(t)
output := e2e.RunCommand(ctx, "boundary", "scopes", "delete", "-id", newOrgId)
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("scopes", "delete", "-id", newOrgId),
)
require.NoError(t, output.Err, string(output.Stderr))
})
@@ -72,10 +77,13 @@ func CreateNewOrgCli(t testing.TB) string {
// Returns the id of the new project.
func CreateNewProjectCli(t testing.TB, orgId string) string {
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "scopes", "create",
"-name", "e2e Project",
"-scope-id", orgId,
"-format", "json",
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"scopes", "create",
"-name", "e2e Project",
"-scope-id", orgId,
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
+10 -8
View File
@@ -42,11 +42,14 @@ func AddHostSourceToTargetApi(t testing.TB, ctx context.Context, client *api.Cli
// 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(context.Background(), "boundary", "targets", "create", "tcp",
"-scope-id", projectId,
"-default-port", defaultPort,
"-name", "e2e Target",
"-format", "json",
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs(
"targets", "create", "tcp",
"-scope-id", projectId,
"-default-port", defaultPort,
"-name", "e2e Target",
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newTargetResult targets.TargetCreateResult
@@ -60,9 +63,8 @@ func CreateNewTargetCli(t testing.TB, projectId string, defaultPort string) stri
// 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(context.Background(), "boundary", "targets", "add-host-sources",
"-id", targetId,
"-host-source", hostSourceId,
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs("targets", "add-host-sources", "-id", targetId, "-host-source", hostSourceId),
)
require.NoError(t, output.Err, string(output.Stderr))
}
+17 -9
View File
@@ -31,11 +31,14 @@ func CreateNewUserApi(t testing.TB, ctx context.Context, client *api.Client, sco
// Returns the id of the new user
func CreateNewUserCli(t testing.TB, scopeId string) string {
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "users", "create",
"-scope-id", scopeId,
"-name", "e2e User",
"-description", "e2e User",
"-format", "json",
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"users", "create",
"-scope-id", scopeId,
"-name", "e2e User",
"-description", "e2e User",
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
@@ -46,7 +49,9 @@ func CreateNewUserCli(t testing.TB, scopeId string) string {
newUserId := newUserResult.Item.Id
t.Cleanup(func() {
AuthenticateAdminCli(t)
output := e2e.RunCommand(ctx, "boundary", "users", "delete", "-id", newUserId)
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("users", "delete", "-id", newUserId),
)
require.NoError(t, output.Err, string(output.Stderr))
})
t.Logf("Created User: %s", newUserId)
@@ -56,9 +61,12 @@ func CreateNewUserCli(t testing.TB, scopeId string) string {
// SetAccountToUserCli sets an account to a the specified user using the cli.
func SetAccountToUserCli(t testing.TB, userId string, accountId string) {
output := e2e.RunCommand(context.Background(), "boundary", "users", "set-accounts",
"-id", userId,
"-account", accountId,
output := e2e.RunCommand(context.Background(), "boundary",
e2e.WithArgs(
"users", "set-accounts",
"-id", userId,
"-account", accountId,
),
)
require.NoError(t, output.Err, string(output.Stderr))
}