refact(e2e): Update RunCommand to have context argument

This commit is contained in:
Michael Li committed 2022-10-19 10:46:57 -04:00
1 parent 84aa65ccc5
commit a4e27d877d
12 files changed
+64 -76

No files matched your search

+1 -1
View File
@@ -64,7 +64,7 @@ func AuthenticateCli(t testing.TB) {
c, err := loadConfig()
require.NoError(t, err)
output := e2e.RunCommand("boundary", "authenticate", "password",
output := e2e.RunCommand(context.Background(), "boundary", "authenticate", "password",
"-addr", c.Address,
"-auth-method-id", c.AuthMethodId,
"-login-name", c.AdminLoginName,
+4 -4
View File
@@ -64,7 +64,7 @@ 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("boundary", "host-catalogs", "create", "static",
output := e2e.RunCommand(context.Background(), "boundary", "host-catalogs", "create", "static",
"-scope-id", projectId,
"-name", "e2e Automated Test Host Catalog",
"-format", "json",
@@ -82,7 +82,7 @@ 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("boundary", "host-sets", "create", "static",
output := e2e.RunCommand(context.Background(), "boundary", "host-sets", "create", "static",
"-host-catalog-id", hostCatalogId,
"-name", "e2e Automated Test Host Set",
"-format", "json",
@@ -100,7 +100,7 @@ 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("boundary", "hosts", "create", "static",
output := e2e.RunCommand(context.Background(), "boundary", "hosts", "create", "static",
"-host-catalog-id", hostCatalogId,
"-name", address,
"-address", address,
@@ -118,6 +118,6 @@ 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("boundary", "host-sets", "add-hosts", "-id", hostSetId, "-host", hostId)
output := e2e.RunCommand(context.Background(), "boundary", "host-sets", "add-hosts", "-id", hostSetId, "-host", hostId)
require.NoError(t, output.Err, string(output.Stderr))
}
+5 -3
View File
@@ -44,7 +44,8 @@ func CreateNewProjectApi(t testing.TB, ctx context.Context, client *api.Client,
// CreateNewOrgCli creates a new organization in boundary using the cli.
// Returns the id of the new org.
func CreateNewOrgCli(t testing.TB) string {
output := e2e.RunCommand("boundary", "scopes", "create",
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "scopes", "create",
"-name", "e2e Automated Test Org",
"-scope-id", "global",
"-format", "json",
@@ -57,7 +58,7 @@ func CreateNewOrgCli(t testing.TB) string {
newOrgId := newOrgResult.Item.Id
t.Cleanup(func() {
output := e2e.RunCommand("boundary", "scopes", "delete", "-id", newOrgId)
output := e2e.RunCommand(ctx, "boundary", "scopes", "delete", "-id", newOrgId)
require.NoError(t, output.Err, string(output.Stderr))
})
@@ -69,7 +70,8 @@ func CreateNewOrgCli(t testing.TB) string {
// under the provided org id.
// Returns the id of the new project.
func CreateNewProjectCli(t testing.TB, orgId string) string {
output := e2e.RunCommand("boundary", "scopes", "create",
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "scopes", "create",
"-name", "e2e Automated Test Project",
"-scope-id", orgId,
"-format", "json",
+2 -2
View File
@@ -42,7 +42,7 @@ 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("boundary", "targets", "create", "tcp",
output := e2e.RunCommand(context.Background(), "boundary", "targets", "create", "tcp",
"-scope-id", projectId,
"-default-port", defaultPort,
"-name", "e2e Automated Test Target",
@@ -60,7 +60,7 @@ 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("boundary", "targets", "add-host-sources",
output := e2e.RunCommand(context.Background(), "boundary", "targets", "add-host-sources",
"-id", targetId,
"-host-source", hostSourceId,
)
@@ -63,12 +63,13 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) {
boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId)
// Configure vault
ctx := context.Background()
vaultAddr, boundaryPolicyName := vault.Setup(t)
output := e2e.RunCommand("vault", "secrets", "enable", "-path="+c.VaultSecretPath, "kv-v2")
output := e2e.RunCommand(ctx, "vault", "secrets", "enable", "-path="+c.VaultSecretPath, "kv-v2")
require.NoError(t, output.Err, string(output.Stderr))
t.Cleanup(func() {
output := e2e.RunCommand("vault", "secrets", "disable", c.VaultSecretPath)
output := e2e.RunCommand(ctx, "vault", "secrets", "disable", c.VaultSecretPath)
require.NoError(t, output.Err, string(output.Stderr))
})
@@ -78,7 +79,7 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) {
t.Log("Created Vault Credential")
// Create vault token for boundary
output = e2e.RunCommand("vault", "token", "create",
output = e2e.RunCommand(ctx, "vault", "token", "create",
"-no-default-policy=true",
"-policy="+boundaryPolicyName,
"-policy="+credentialPolicyName,
@@ -95,7 +96,7 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) {
t.Log("Created Vault Cred Store Token")
// Create a credential store
output = e2e.RunCommand("boundary", "credential-stores", "create", "vault",
output = e2e.RunCommand(ctx, "boundary", "credential-stores", "create", "vault",
"-scope-id", newProjectId,
"-vault-address", vaultAddr,
"-vault-token", credStoreToken,
@@ -109,7 +110,7 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) {
t.Logf("Created Credential Store: %s", newCredentialStoreId)
// Create a credential library
output = e2e.RunCommand("boundary", "credential-libraries", "create", "vault",
output = e2e.RunCommand(ctx, "boundary", "credential-libraries", "create", "vault",
"-credential-store-id", newCredentialStoreId,
"-vault-path", c.VaultSecretPath+"/data/"+secretName,
"-name", "e2e Automated Test Vault Credential Library",
@@ -124,14 +125,14 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) {
t.Logf("Created Credential Library: %s", newCredentialLibraryId)
// Add brokered credentials to target
output = e2e.RunCommand("boundary", "targets", "add-credential-sources",
output = e2e.RunCommand(ctx, "boundary", "targets", "add-credential-sources",
"-id", newTargetId,
"-brokered-credential-source", newCredentialLibraryId,
)
require.NoError(t, output.Err, string(output.Stderr))
// Get credentials for target
output = e2e.RunCommand("boundary", "targets", "authorize-session", "-id", newTargetId, "-format", "json")
output = e2e.RunCommand(ctx, "boundary", "targets", "authorize-session", "-id", newTargetId, "-format", "json")
require.NoError(t, output.Err, string(output.Stderr))
var newSessionAuthorizationResult targets.SessionAuthorizationResult
err = json.Unmarshal(output.Stdout, &newSessionAuthorizationResult)
@@ -157,7 +158,7 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) {
require.NoError(t, err)
// Connect to target and print host's IP address using retrieved credentials
output = e2e.RunCommand("boundary", "connect",
output = e2e.RunCommand(ctx, "boundary", "connect",
"-target-id", newTargetId,
"-exec", "/usr/bin/ssh", "--",
"-l", retrievedUser,
@@ -201,10 +202,10 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) {
// Configure vault
vaultAddr, boundaryPolicyName := vault.Setup(t)
output := e2e.RunCommand("vault", "secrets", "enable", "-path="+c.VaultSecretPath, "kv-v2")
output := e2e.RunCommand(ctx, "vault", "secrets", "enable", "-path="+c.VaultSecretPath, "kv-v2")
require.NoError(t, output.Err, string(output.Stderr))
t.Cleanup(func() {
output := e2e.RunCommand("vault", "secrets", "disable", c.VaultSecretPath)
output := e2e.RunCommand(ctx, "vault", "secrets", "disable", c.VaultSecretPath)
require.NoError(t, output.Err, string(output.Stderr))
})
@@ -214,7 +215,7 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) {
t.Log("Created Vault Credential")
// Create vault token for boundary
output = e2e.RunCommand("vault", "token", "create",
output = e2e.RunCommand(ctx, "vault", "token", "create",
"-no-default-policy=true",
"-policy="+boundaryPolicyName,
"-policy="+credentialPolicyName,
+3 -27
View File
@@ -48,35 +48,11 @@ const EnvToCheckSkip = "E2E_PASSWORD_AUTH_METHOD_ID"
// RunCommand executes external commands on the system. Returns the results
// of running the provided command.
//
// RunCommand("ls")
// RunCommand("ls", "-al", "/path")
// RunCommand(context.Background(), "ls")
// RunCommand(context.Background(), "ls", "-al", "/path")
//
// CommandResult is always valid even if there is an error.
func RunCommand(name string, args ...string) *CommandResult {
var outbuf, errbuf bytes.Buffer
cmd := exec.Command(name, args...)
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err := cmd.Run()
var ee *exec.ExitError
var exitCode int
if errors.As(err, &ee) {
exitCode = ee.ExitCode()
}
return &CommandResult{
Stdout: outbuf.Bytes(),
Stderr: errbuf.Bytes(),
ExitCode: exitCode,
Err: err,
}
}
// RunCommandContext is similar to RunCommand but allows passing in a context
func RunCommandContext(ctx context.Context, name string, args ...string) *CommandResult {
func RunCommand(ctx context.Context, name string, args ...string) *CommandResult {
var outbuf, errbuf bytes.Buffer
cmd := exec.CommandContext(ctx, name, args...)
@@ -55,7 +55,8 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) {
newProjectId := boundary.CreateNewProjectCli(t, newOrgId)
// Create a dynamic host catalog
output := e2e.RunCommand("boundary", "host-catalogs", "create", "plugin",
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "host-catalogs", "create", "plugin",
"-scope-id", newProjectId,
"-plugin-name", "aws",
"-attr", "disable_credential_rotation=true",
@@ -72,7 +73,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) {
t.Logf("Created Host Catalog: %s", newHostCatalogId)
// Create a host set
output = e2e.RunCommand("boundary", "host-sets", "create", "plugin",
output = e2e.RunCommand(ctx, "boundary", "host-sets", "create", "plugin",
"-host-catalog-id", newHostCatalogId,
"-attr", "filters="+c.AwsHostSetFilter1,
"-name", "e2e Automated Test Host Set",
@@ -91,7 +92,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) {
var actualHostSetCount1 int
err = backoff.RetryNotify(
func() error {
output = e2e.RunCommand("boundary", "host-sets", "read",
output = e2e.RunCommand(ctx, "boundary", "host-sets", "read",
"-id", newHostSetId1,
"-format", "json",
)
@@ -127,7 +128,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) {
assert.Equal(t, expectedHostSetCount1, actualHostSetCount1, "Numbers of hosts in host set did not match expected amount")
// Create another host set
output = e2e.RunCommand("boundary", "host-sets", "create", "plugin",
output = e2e.RunCommand(ctx, "boundary", "host-sets", "create", "plugin",
"-host-catalog-id", newHostCatalogId,
"-attr", "filters="+c.AwsHostSetFilter2,
"-name", "e2e Automated Test Host Set2",
@@ -145,7 +146,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) {
var actualHostSetCount2 int
err = backoff.RetryNotify(
func() error {
output = e2e.RunCommand("boundary", "host-sets", "read",
output = e2e.RunCommand(ctx, "boundary", "host-sets", "read",
"-id", newHostSetId2,
"-format", "json",
)
@@ -184,7 +185,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) {
var actualHostCatalogCount int
err = backoff.RetryNotify(
func() error {
output = e2e.RunCommand("boundary", "hosts", "list",
output = e2e.RunCommand(ctx, "boundary", "hosts", "list",
"-host-catalog-id", newHostCatalogId,
"-format", "json",
)
@@ -221,7 +222,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) {
boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId1)
// Connect to target
output = e2e.RunCommand("boundary", "connect",
output = e2e.RunCommand(ctx, "boundary", "connect",
"-target-id", newTargetId,
"-exec", "/usr/bin/ssh", "--",
"-l", c.TargetSshUser,
@@ -1,6 +1,7 @@
package static_test
import (
"context"
"encoding/json"
"fmt"
"os"
@@ -34,7 +35,8 @@ func TestConnectTargetWithAuthzTokenCli(t *testing.T) {
boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId)
// Create a credential store
output := e2e.RunCommand("boundary", "credential-stores", "create", "static",
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "credential-stores", "create", "static",
"-scope-id", newProjectId,
"-format", "json",
)
@@ -46,7 +48,7 @@ func TestConnectTargetWithAuthzTokenCli(t *testing.T) {
t.Logf("Created Credential Store: %s", newCredentialStoreId)
// Create credentials
output = e2e.RunCommand("boundary", "credentials", "create", "ssh-private-key",
output = e2e.RunCommand(ctx, "boundary", "credentials", "create", "ssh-private-key",
"-credential-store-id", newCredentialStoreId,
"-username", c.TargetSshUser,
"-private-key", "file://"+c.TargetSshKeyPath,
@@ -60,14 +62,14 @@ func TestConnectTargetWithAuthzTokenCli(t *testing.T) {
t.Logf("Created Credentials: %s", newCredentialsId)
// Add credentials to target
output = e2e.RunCommand("boundary", "targets", "add-credential-sources",
output = e2e.RunCommand(ctx, "boundary", "targets", "add-credential-sources",
"-id", newTargetId,
"-brokered-credential-source", newCredentialsId,
)
require.NoError(t, output.Err, string(output.Stderr))
// Get credentials for target
output = e2e.RunCommand("boundary", "targets", "authorize-session", "-id", newTargetId, "-format", "json")
output = e2e.RunCommand(ctx, "boundary", "targets", "authorize-session", "-id", newTargetId, "-format", "json")
require.NoError(t, output.Err, string(output.Stderr))
var newSessionAuthorizationResult targets.SessionAuthorizationResult
err = json.Unmarshal(output.Stdout, &newSessionAuthorizationResult)
@@ -96,7 +98,7 @@ func TestConnectTargetWithAuthzTokenCli(t *testing.T) {
require.NoError(t, err)
// Connect to target and print host's IP address using retrieved credentials
output = e2e.RunCommand("boundary", "connect",
output = e2e.RunCommand(ctx, "boundary", "connect",
"-authz-token", newAuthToken,
"-exec", "/usr/bin/ssh", "--",
"-l", retrievedUser,
@@ -35,7 +35,8 @@ func TestConnectTargetWithSshCli(t *testing.T) {
boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId)
// Create a credential store
output := e2e.RunCommand("boundary", "credential-stores", "create", "static",
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "credential-stores", "create", "static",
"-scope-id", newProjectId,
"-format", "json",
)
@@ -47,7 +48,7 @@ func TestConnectTargetWithSshCli(t *testing.T) {
t.Logf("Created Credential Store: %s", newCredentialStoreId)
// Create credentials
output = e2e.RunCommand("boundary", "credentials", "create", "ssh-private-key",
output = e2e.RunCommand(ctx, "boundary", "credentials", "create", "ssh-private-key",
"-credential-store-id", newCredentialStoreId,
"-username", c.TargetSshUser,
"-private-key", "file://"+c.TargetSshKeyPath,
@@ -61,14 +62,14 @@ func TestConnectTargetWithSshCli(t *testing.T) {
t.Logf("Created Credentials: %s", newCredentialsId)
// Add credentials to target
output = e2e.RunCommand("boundary", "targets", "add-credential-sources",
output = e2e.RunCommand(ctx, "boundary", "targets", "add-credential-sources",
"-id", newTargetId,
"-brokered-credential-source", newCredentialsId,
)
require.NoError(t, output.Err, string(output.Stderr))
// Get credentials for target
output = e2e.RunCommand("boundary", "targets", "authorize-session", "-id", newTargetId, "-format", "json")
output = e2e.RunCommand(ctx, "boundary", "targets", "authorize-session", "-id", newTargetId, "-format", "json")
require.NoError(t, output.Err, string(output.Stderr))
var newSessionAuthorizationResult targets.SessionAuthorizationResult
err = json.Unmarshal(output.Stdout, &newSessionAuthorizationResult)
@@ -85,7 +86,7 @@ func TestConnectTargetWithSshCli(t *testing.T) {
t.Log("Successfully retrieved credentials for target")
// Connect to target and print host's IP address using retrieved credentials
output = e2e.RunCommand("boundary", "connect", "ssh",
output = e2e.RunCommand(ctx, "boundary", "connect", "ssh",
"-target-id", newTargetId, "--",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
@@ -29,7 +29,8 @@ func TestConnectTargetCli(t *testing.T) {
boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId)
// Connect to target and print host's IP address
output := e2e.RunCommand("boundary", "connect",
ctx := context.Background()
output := e2e.RunCommand(ctx, "boundary", "connect",
"-target-id", newTargetId,
"-exec", "/usr/bin/ssh", "--",
"-l", c.TargetSshUser,
@@ -31,10 +31,10 @@ func TestSessionCancelingCli(t *testing.T) {
boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId)
// Connect to target to create a session
ctx, cancel := context.WithCancel(context.Background())
ctxCancel, cancel := context.WithCancel(context.Background())
errChan := make(chan *e2e.CommandResult)
go func() {
errChan <- e2e.RunCommandContext(ctx, "boundary", "connect",
errChan <- e2e.RunCommand(ctxCancel, "boundary", "connect",
"-target-id", newTargetId,
"-exec", "/usr/bin/ssh", "--",
"-l", c.TargetSshUser,
@@ -50,10 +50,11 @@ func TestSessionCancelingCli(t *testing.T) {
t.Cleanup(cancel)
// Get list of sessions
ctx := context.Background()
var session *sessions.Session
err = backoff.RetryNotify(
func() error {
output := e2e.RunCommand("boundary", "sessions", "list", "-scope-id", newProjectId, "-format", "json")
output := e2e.RunCommand(ctx, "boundary", "sessions", "list", "-scope-id", newProjectId, "-format", "json")
if output.Err != nil {
return backoff.Permanent(errors.New(string(output.Stderr)))
}
@@ -88,10 +89,10 @@ func TestSessionCancelingCli(t *testing.T) {
require.Equal(t, "active", session.Status)
// Cancel session
output := e2e.RunCommand("boundary", "sessions", "cancel", "-id", session.Id)
output := e2e.RunCommand(ctx, "boundary", "sessions", "cancel", "-id", session.Id)
require.NoError(t, output.Err, string(output.Stderr))
output = e2e.RunCommand("boundary", "sessions", "read", "-id", session.Id, "-format", "json")
output = e2e.RunCommand(ctx, "boundary", "sessions", "read", "-id", session.Id, "-format", "json")
require.NoError(t, output.Err, string(output.Stderr))
var newSessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
+8 -5
View File
@@ -2,6 +2,7 @@
package vault
import (
"context"
"fmt"
"os"
"path"
@@ -36,13 +37,14 @@ func Setup(t testing.TB) (string, string) {
_, filename, _, ok := runtime.Caller(0)
require.True(t, ok)
ctx := context.Background()
policyName := "boundary-controller"
output := e2e.RunCommand("vault", "policy", "write", policyName,
output := e2e.RunCommand(ctx, "vault", "policy", "write", policyName,
path.Join(path.Dir(filename), "boundary-controller-policy.hcl"),
)
require.NoError(t, output.Err, string(output.Stderr))
t.Cleanup(func() {
output := e2e.RunCommand("vault", "policy", "delete", policyName)
output := e2e.RunCommand(ctx, "vault", "policy", "delete", policyName)
require.NoError(t, output.Err, string(output.Stderr))
})
@@ -65,16 +67,17 @@ func CreateKvPrivateKeyCredential(t testing.TB, secretName string, secretPath st
require.NoError(t, err)
// Add policy to vault
ctx := context.Background()
policyName := "kv-read"
output := e2e.RunCommand("vault", "policy", "write", policyName, kvPolicyFilePath)
output := e2e.RunCommand(ctx, "vault", "policy", "write", policyName, kvPolicyFilePath)
require.NoError(t, output.Err, string(output.Stderr))
t.Cleanup(func() {
output := e2e.RunCommand("vault", "policy", "delete", policyName)
output := e2e.RunCommand(ctx, "vault", "policy", "delete", policyName)
require.NoError(t, output.Err, string(output.Stderr))
})
// Create secret
output = e2e.RunCommand("vault", "kv", "put",
output = e2e.RunCommand(ctx, "vault", "kv", "put",
"-mount", secretPath,
secretName,
"username="+user,