From 01301cd32ad334177ba812ae953c76a083daed9b Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 12 Oct 2022 16:42:48 -0400 Subject: [PATCH 1/8] fix(e2e): Add missing requires --- testing/internal/e2e/credential/vault/vault_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/internal/e2e/credential/vault/vault_test.go b/testing/internal/e2e/credential/vault/vault_test.go index a5de0d07d9..bb7c395b1b 100644 --- a/testing/internal/e2e/credential/vault/vault_test.go +++ b/testing/internal/e2e/credential/vault/vault_test.go @@ -284,6 +284,7 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) { require.NoError(t, output.Err, string(output.Stderr)) var tokenCreateResult createTokenResponse err = json.Unmarshal(output.Stdout, &tokenCreateResult) + require.NoError(t, err) credStoreToken := tokenCreateResult.Auth.Client_Token t.Log("Created Vault Cred Store Token") @@ -351,6 +352,7 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) { // Create a target tClient := targets.NewClient(client) targetPort, err := strconv.ParseInt(c.TargetPort, 10, 32) + require.NoError(t, err) newTargetResult, err := tClient.Create(ctx, "tcp", newProjectId, targets.WithName("e2e Automated Test Target"), targets.WithTcpTargetDefaultPort(uint32(targetPort)), From 1f87da9fd476c29ec83d24facd4281af55de2556 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 17 Oct 2022 19:28:42 -0400 Subject: [PATCH 2/8] fix(e2e): Fix comment typo --- testing/internal/e2e/host/aws/dynamichostcatalog_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go index 819574b1a8..f3da7cc939 100644 --- a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go +++ b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go @@ -209,7 +209,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) { return errors.New("No items are appearing in the host catalog") } - t.Logf("Found %d hosts", actualHostCatalogCount) + t.Logf("Found %d host(s)", actualHostCatalogCount) return nil }, backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5), From 051d337b2183d66e004a33945bf0923b519980a3 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 12 Oct 2022 18:40:22 -0400 Subject: [PATCH 3/8] refact(e2e): Move logging to helper function --- testing/internal/e2e/boundary/scopes.go | 13 ++++--------- testing/internal/e2e/credential/vault/vault_test.go | 4 ---- .../e2e/host/aws/dynamichostcatalog_test.go | 4 ---- .../e2e/host/static/staticcredential_test.go | 4 ---- testing/internal/e2e/host/static/statichost_test.go | 4 ---- 5 files changed, 4 insertions(+), 25 deletions(-) diff --git a/testing/internal/e2e/boundary/scopes.go b/testing/internal/e2e/boundary/scopes.go index 81c7507841..710c530af1 100644 --- a/testing/internal/e2e/boundary/scopes.go +++ b/testing/internal/e2e/boundary/scopes.go @@ -22,6 +22,7 @@ func CreateNewOrgApi(t testing.TB, ctx context.Context, client *api.Client) stri require.NoError(t, err) }) + t.Logf("Created Org Id: %s", newOrgResult.Item.Id) return newOrgResult.Item.Id } @@ -32,11 +33,8 @@ func CreateNewProjectApi(t testing.TB, ctx context.Context, client *api.Client, scopeClient := scopes.NewClient(client) newProjResult, err := scopeClient.Create(ctx, orgId, scopes.WithName("e2e Automated Test Project")) require.NoError(t, err) - t.Cleanup(func() { - _, err := scopeClient.Delete(ctx, newProjResult.Item.Id) - require.NoError(t, err) - }) + t.Logf("Created Project Id: %s", newProjResult.Item.Id) return newProjResult.Item.Id } @@ -59,6 +57,7 @@ func CreateNewOrgCli(t testing.TB) string { require.NoError(t, output.Err, string(output.Stderr)) }) + t.Logf("Created Org Id: %s", newOrgResult.Item.Id) return newOrgResult.Item.Id } @@ -77,10 +76,6 @@ func CreateNewProjectCli(t testing.TB, orgId string) string { err := json.Unmarshal(output.Stdout, &newProjResult) require.NoError(t, err) - t.Cleanup(func() { - output := e2e.RunCommand("boundary", "scopes", "delete", "-id", newProjResult.Item.Id) - require.NoError(t, output.Err, string(output.Stderr)) - }) - + t.Logf("Created Project Id: %s", newProjResult.Item.Id) return newProjResult.Item.Id } diff --git a/testing/internal/e2e/credential/vault/vault_test.go b/testing/internal/e2e/credential/vault/vault_test.go index bb7c395b1b..e97e782ca0 100644 --- a/testing/internal/e2e/credential/vault/vault_test.go +++ b/testing/internal/e2e/credential/vault/vault_test.go @@ -93,9 +93,7 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgCli(t) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectCli(t, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a credential store output = e2e.RunCommand("boundary", "credential-stores", "create", "vault", @@ -295,9 +293,7 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgApi(t, ctx, client) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a credential store csClient := credentialstores.NewClient(client) diff --git a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go index f3da7cc939..4b1f6396f2 100644 --- a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go +++ b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go @@ -55,9 +55,7 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgCli(t) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectCli(t, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a dynamic host catalog output := e2e.RunCommand("boundary", "host-catalogs", "create", "plugin", @@ -287,9 +285,7 @@ func TestCreateAwsDynamicHostCatalogApi(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgApi(t, ctx, client) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a dynamic host catalog hcClient := hostcatalogs.NewClient(client) diff --git a/testing/internal/e2e/host/static/staticcredential_test.go b/testing/internal/e2e/host/static/staticcredential_test.go index 278c3a58ba..dd0f235334 100644 --- a/testing/internal/e2e/host/static/staticcredential_test.go +++ b/testing/internal/e2e/host/static/staticcredential_test.go @@ -32,9 +32,7 @@ func TestConnectTargetWithStaticCredentialStoreCli(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgCli(t) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectCli(t, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a credential store output := e2e.RunCommand("boundary", "credential-stores", "create", "static", @@ -176,9 +174,7 @@ func TestCreateTargetWithStaticCredentialStoreApi(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgApi(t, ctx, client) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a credential store csClient := credentialstores.NewClient(client) diff --git a/testing/internal/e2e/host/static/statichost_test.go b/testing/internal/e2e/host/static/statichost_test.go index ad012c692e..5d2bc9780a 100644 --- a/testing/internal/e2e/host/static/statichost_test.go +++ b/testing/internal/e2e/host/static/statichost_test.go @@ -28,9 +28,7 @@ func TestConnectTargetCli(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgCli(t) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectCli(t, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a host catalog output := e2e.RunCommand("boundary", "host-catalogs", "create", "static", @@ -149,9 +147,7 @@ func TestCreateTargetApi(t *testing.T) { // Create an org and project newOrgId := boundary.CreateNewOrgApi(t, ctx, client) - t.Logf("Created Org Id: %s", newOrgId) newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) - t.Logf("Created Project Id: %s", newProjectId) // Create a host catalog hcClient := hostcatalogs.NewClient(client) From 618eca19f53af26be390137eec7fc8e2aa840460 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 17 Oct 2022 14:33:48 -0400 Subject: [PATCH 4/8] refact(e2e): Create functions for commonly used operations 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. --- testing/internal/e2e/boundary/host.go | 123 +++++++++++++ testing/internal/e2e/boundary/scopes.go | 25 +-- testing/internal/e2e/boundary/target.go | 68 +++++++ .../e2e/credential/vault/vault_test.go | 161 +++-------------- .../e2e/host/aws/dynamichostcatalog_test.go | 27 +-- .../e2e/host/static/staticcredential_test.go | 135 ++------------ .../e2e/host/static/statichost_test.go | 170 ++---------------- 7 files changed, 258 insertions(+), 451 deletions(-) create mode 100644 testing/internal/e2e/boundary/host.go create mode 100644 testing/internal/e2e/boundary/target.go diff --git a/testing/internal/e2e/boundary/host.go b/testing/internal/e2e/boundary/host.go new file mode 100644 index 0000000000..bc3b0e302f --- /dev/null +++ b/testing/internal/e2e/boundary/host.go @@ -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)) +} diff --git a/testing/internal/e2e/boundary/scopes.go b/testing/internal/e2e/boundary/scopes.go index 710c530af1..9bc21fd7bc 100644 --- a/testing/internal/e2e/boundary/scopes.go +++ b/testing/internal/e2e/boundary/scopes.go @@ -17,13 +17,15 @@ func CreateNewOrgApi(t testing.TB, ctx context.Context, client *api.Client) stri scopeClient := scopes.NewClient(client) newOrgResult, err := scopeClient.Create(ctx, "global", scopes.WithName("e2e Automated Test Org")) require.NoError(t, err) + + newOrgId := newOrgResult.Item.Id t.Cleanup(func() { - _, err := scopeClient.Delete(ctx, newOrgResult.Item.Id) + _, err := scopeClient.Delete(ctx, newOrgId) require.NoError(t, err) }) - t.Logf("Created Org Id: %s", newOrgResult.Item.Id) - return newOrgResult.Item.Id + t.Logf("Created Org Id: %s", newOrgId) + return newOrgId } // CreateNewProjectApi creates a new project in boundary using the go api. The project will be created @@ -34,8 +36,9 @@ func CreateNewProjectApi(t testing.TB, ctx context.Context, client *api.Client, newProjResult, err := scopeClient.Create(ctx, orgId, scopes.WithName("e2e Automated Test Project")) require.NoError(t, err) - t.Logf("Created Project Id: %s", newProjResult.Item.Id) - return newProjResult.Item.Id + newProjectId := newProjResult.Item.Id + t.Logf("Created Project Id: %s", newProjectId) + return newProjectId } // CreateNewOrgCli creates a new organization in boundary using the cli. @@ -52,13 +55,14 @@ func CreateNewOrgCli(t testing.TB) string { err := json.Unmarshal(output.Stdout, &newOrgResult) require.NoError(t, err) + newOrgId := newOrgResult.Item.Id t.Cleanup(func() { - output := e2e.RunCommand("boundary", "scopes", "delete", "-id", newOrgResult.Item.Id) + output := e2e.RunCommand("boundary", "scopes", "delete", "-id", newOrgId) require.NoError(t, output.Err, string(output.Stderr)) }) - t.Logf("Created Org Id: %s", newOrgResult.Item.Id) - return newOrgResult.Item.Id + t.Logf("Created Org Id: %s", newOrgId) + return newOrgId } // CreateNewProjectCli creates a new project in boundary using the cli. The project will be created @@ -76,6 +80,7 @@ func CreateNewProjectCli(t testing.TB, orgId string) string { err := json.Unmarshal(output.Stdout, &newProjResult) require.NoError(t, err) - t.Logf("Created Project Id: %s", newProjResult.Item.Id) - return newProjResult.Item.Id + newProjectId := newProjResult.Item.Id + t.Logf("Created Project Id: %s", newProjectId) + return newProjectId } diff --git a/testing/internal/e2e/boundary/target.go b/testing/internal/e2e/boundary/target.go new file mode 100644 index 0000000000..0a5e43c5b9 --- /dev/null +++ b/testing/internal/e2e/boundary/target.go @@ -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)) +} diff --git a/testing/internal/e2e/credential/vault/vault_test.go b/testing/internal/e2e/credential/vault/vault_test.go index e97e782ca0..d5fb8bd7cb 100644 --- a/testing/internal/e2e/credential/vault/vault_test.go +++ b/testing/internal/e2e/credential/vault/vault_test.go @@ -5,15 +5,11 @@ import ( "encoding/json" "fmt" "os" - "strconv" "strings" "testing" "github.com/hashicorp/boundary/api/credentiallibraries" "github.com/hashicorp/boundary/api/credentialstores" - "github.com/hashicorp/boundary/api/hostcatalogs" - "github.com/hashicorp/boundary/api/hosts" - "github.com/hashicorp/boundary/api/hostsets" "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" @@ -56,6 +52,16 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) { c, err := loadConfig() require.NoError(t, err) + boundary.AuthenticateCli(t) + newOrgId := boundary.CreateNewOrgCli(t) + newProjectId := boundary.CreateNewProjectCli(t, newOrgId) + newHostCatalogId := boundary.CreateNewHostCatalogCli(t, newProjectId) + newHostSetId := boundary.CreateNewHostSetCli(t, newHostCatalogId) + newHostId := boundary.CreateNewHostCli(t, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetCli(t, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId) + // Configure vault vaultAddr, boundaryPolicyName := vault.Setup(t) @@ -88,13 +94,6 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) { credStoreToken := tokenCreateResult.Auth.Client_Token t.Log("Created Vault Cred Store Token") - // Authenticate boundary cli - boundary.AuthenticateCli(t) - - // Create an org and project - newOrgId := boundary.CreateNewOrgCli(t) - newProjectId := boundary.CreateNewProjectCli(t, newOrgId) - // Create a credential store output = e2e.RunCommand("boundary", "credential-stores", "create", "vault", "-scope-id", newProjectId, @@ -124,74 +123,6 @@ func TestCreateVaultCredentialStoreCli(t *testing.T) { newCredentialLibraryId := newCredentialLibraryResult.Item.Id t.Logf("Created Credential Library: %s", newCredentialLibraryId) - // Create a host catalog - output = e2e.RunCommand("boundary", "host-catalogs", "create", "static", - "-scope-id", newProjectId, - "-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) - - // Create a host set and add to catalog - output = e2e.RunCommand("boundary", "host-sets", "create", "static", - "-host-catalog-id", newHostCatalogId, - "-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) - - // Create a host - output = e2e.RunCommand("boundary", "hosts", "create", "static", - "-host-catalog-id", newHostCatalogId, - "-name", c.TargetIp, - "-address", c.TargetIp, - "-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) - - // Add host to host set - output = e2e.RunCommand("boundary", "host-sets", "add-hosts", - "-id", newHostSetId, - "-host", newHostId, - ) - require.NoError(t, output.Err, string(output.Stderr)) - - // Create Target - output = e2e.RunCommand("boundary", "targets", "create", "tcp", - "-scope-id", newProjectId, - "-default-port", c.TargetPort, - "-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) - - // Add host set to target - output = e2e.RunCommand("boundary", "targets", "add-host-sources", - "-id", newTargetId, - "-host-source", newHostSetId, - ) - require.NoError(t, output.Err, string(output.Stderr)) - // Add brokered credentials to target output = e2e.RunCommand("boundary", "targets", "add-credential-sources", "-id", newTargetId, @@ -254,6 +185,19 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) { c, err := loadConfig() require.NoError(t, err) + client, err := boundary.NewApiClient() + require.NoError(t, err) + ctx := context.Background() + + newOrgId := boundary.CreateNewOrgApi(t, ctx, client) + newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) + newHostCatalogId := boundary.CreateNewHostCatalogApi(t, ctx, client, newProjectId) + newHostSetId := boundary.CreateNewHostSetApi(t, ctx, client, newHostCatalogId) + newHostId := boundary.CreateNewHostApi(t, ctx, client, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetApi(t, ctx, client, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetApi(t, ctx, client, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetApi(t, ctx, client, newTargetId, newHostSetId) + // Configure vault vaultAddr, boundaryPolicyName := vault.Setup(t) @@ -286,15 +230,6 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) { credStoreToken := tokenCreateResult.Auth.Client_Token t.Log("Created Vault Cred Store Token") - // Create boundary api client - client, err := boundary.NewApiClient() - require.NoError(t, err) - ctx := context.Background() - - // Create an org and project - newOrgId := boundary.CreateNewOrgApi(t, ctx, client) - newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) - // Create a credential store csClient := credentialstores.NewClient(client) newCredentialStoreResult, err := csClient.Create(ctx, "vault", newProjectId, @@ -315,56 +250,8 @@ func TestCreateVaultCredentialStoreApi(t *testing.T) { newCredentialLibraryId := newCredentialLibraryResult.Item.Id t.Logf("Created Credential Library: %s", newCredentialLibraryId) - // Create a host catalog - hcClient := hostcatalogs.NewClient(client) - newHostCatalogResult, err := hcClient.Create(ctx, "static", newProjectId, - hostcatalogs.WithName("e2e Automated Test Host Catalog"), - ) - require.NoError(t, err) - newHostCatalogId := newHostCatalogResult.Item.Id - t.Logf("Created Host Catalog: %s", newHostCatalogId) - - // Create a host set and add to catalog - hsClient := hostsets.NewClient(client) - newHostSetResult, err := hsClient.Create(ctx, newHostCatalogId) - require.NoError(t, err) - newHostSetId := newHostSetResult.Item.Id - t.Logf("Created Host Set: %s", newHostSetId) - - // Create a host - hClient := hosts.NewClient(client) - newHostResult, err := hClient.Create(ctx, newHostCatalogId, - hosts.WithName(c.TargetIp), - hosts.WithStaticHostAddress(c.TargetIp), - ) - require.NoError(t, err) - newHostId := newHostResult.Item.Id - t.Logf("Created Host: %s", newHostId) - - // Add host to host set - _, err = hsClient.AddHosts(ctx, newHostSetId, 0, []string{newHostId}, hostsets.WithAutomaticVersioning(true)) - require.NoError(t, err) - - // Create a target - tClient := targets.NewClient(client) - targetPort, err := strconv.ParseInt(c.TargetPort, 10, 32) - require.NoError(t, err) - newTargetResult, err := tClient.Create(ctx, "tcp", newProjectId, - targets.WithName("e2e Automated Test Target"), - targets.WithTcpTargetDefaultPort(uint32(targetPort)), - ) - require.NoError(t, err) - newTargetId := newTargetResult.Item.Id - t.Logf("Created Target: %s", newTargetId) - - // Add host set to target - _, err = tClient.AddHostSources(ctx, newTargetId, 0, - []string{newHostSetId}, - targets.WithAutomaticVersioning(true), - ) - require.NoError(t, err) - // Add brokered credentials to target + tClient := targets.NewClient(client) _, err = tClient.AddCredentialSources(ctx, newTargetId, 0, targets.WithBrokeredCredentialSourceIds([]string{newCredentialLibraryId}), targets.WithAutomaticVersioning(true), diff --git a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go index 4b1f6396f2..8f2eb81127 100644 --- a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go +++ b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/boundary/api/hostcatalogs" "github.com/hashicorp/boundary/api/hosts" "github.com/hashicorp/boundary/api/hostsets" - "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" "github.com/kelseyhightower/envconfig" @@ -52,8 +51,6 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) { require.NoError(t, err) boundary.AuthenticateCli(t) - - // Create an org and project newOrgId := boundary.CreateNewOrgCli(t) newProjectId := boundary.CreateNewProjectCli(t, newOrgId) @@ -183,7 +180,6 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) { assert.Equal(t, expectedHostSetCount2, actualHostSetCount2, "Numbers of hosts in host set did not match expected amount") // Get list of all hosts from host catalog - // Retry is needed here since it can take a few tries before hosts start appearing t.Logf("Looking for items in the host catalog...") var actualHostCatalogCount int err = backoff.RetryNotify( @@ -221,25 +217,8 @@ func TestCreateAwsDynamicHostCatalogCli(t *testing.T) { assert.Equal(t, expectedHostCatalogCount, actualHostCatalogCount, "Numbers of hosts in host catalog did not match expected amount") // Create target - output = e2e.RunCommand("boundary", "targets", "create", "tcp", - "-scope-id", newProjectId, - "-default-port", c.TargetPort, - "-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) - - // Add host set to target - output = e2e.RunCommand("boundary", "targets", "add-host-sources", - "-id", newTargetId, - "-host-source", newHostSetId1, - ) - require.NoError(t, output.Err, string(output.Stderr)) + newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId1) // Connect to target output = e2e.RunCommand("boundary", "connect", @@ -278,12 +257,10 @@ func TestCreateAwsDynamicHostCatalogApi(t *testing.T) { c, err := loadConfig() require.NoError(t, err) - // Create boundary api client client, err := boundary.NewApiClient() require.NoError(t, err) ctx := context.Background() - // Create an org and project newOrgId := boundary.CreateNewOrgApi(t, ctx, client) newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) diff --git a/testing/internal/e2e/host/static/staticcredential_test.go b/testing/internal/e2e/host/static/staticcredential_test.go index dd0f235334..afa143d31e 100644 --- a/testing/internal/e2e/host/static/staticcredential_test.go +++ b/testing/internal/e2e/host/static/staticcredential_test.go @@ -5,14 +5,10 @@ import ( "encoding/json" "fmt" "os" - "strconv" "testing" "github.com/hashicorp/boundary/api/credentials" "github.com/hashicorp/boundary/api/credentialstores" - "github.com/hashicorp/boundary/api/hostcatalogs" - "github.com/hashicorp/boundary/api/hosts" - "github.com/hashicorp/boundary/api/hostsets" "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" @@ -29,10 +25,14 @@ func TestConnectTargetWithStaticCredentialStoreCli(t *testing.T) { require.NoError(t, err) boundary.AuthenticateCli(t) - - // Create an org and project newOrgId := boundary.CreateNewOrgCli(t) newProjectId := boundary.CreateNewProjectCli(t, newOrgId) + newHostCatalogId := boundary.CreateNewHostCatalogCli(t, newProjectId) + newHostSetId := boundary.CreateNewHostSetCli(t, newHostCatalogId) + newHostId := boundary.CreateNewHostCli(t, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetCli(t, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId) // Create a credential store output := e2e.RunCommand("boundary", "credential-stores", "create", "static", @@ -60,71 +60,6 @@ func TestConnectTargetWithStaticCredentialStoreCli(t *testing.T) { newCredentialsId := newCredentialsResult.Item.Id t.Logf("Created Credentials: %s", newCredentialsId) - // Create a host catalog - output = e2e.RunCommand("boundary", "host-catalogs", "create", "static", - "-scope-id", newProjectId, - "-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) - - // Create a host set and add to catalog - output = e2e.RunCommand("boundary", "host-sets", "create", "static", - "-host-catalog-id", newHostCatalogId, - "-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) - - // Create a host - output = e2e.RunCommand("boundary", "hosts", "create", "static", - "-host-catalog-id", newHostCatalogId, - "-name", c.TargetIp, - "-address", c.TargetIp, - "-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) - - // Add host to host set - output = e2e.RunCommand("boundary", "host-sets", "add-hosts", "-id", newHostSetId, "-host", newHostId) - require.NoError(t, output.Err, string(output.Stderr)) - - // Create a target - output = e2e.RunCommand("boundary", "targets", "create", "tcp", - "-scope-id", newProjectId, - "-default-port", c.TargetPort, - "-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) - - // Add host set to target - output = e2e.RunCommand("boundary", "targets", "add-host-sources", - "-id", newTargetId, - "-host-source", newHostSetId, - ) - require.NoError(t, output.Err, string(output.Stderr)) - // Add credentials to target output = e2e.RunCommand("boundary", "targets", "add-credential-sources", "-id", newTargetId, @@ -167,14 +102,18 @@ func TestCreateTargetWithStaticCredentialStoreApi(t *testing.T) { c, err := loadConfig() require.NoError(t, err) - // Create boundary api client client, err := boundary.NewApiClient() require.NoError(t, err) ctx := context.Background() - // Create an org and project newOrgId := boundary.CreateNewOrgApi(t, ctx, client) newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) + newHostCatalogId := boundary.CreateNewHostCatalogApi(t, ctx, client, newProjectId) + newHostSetId := boundary.CreateNewHostSetApi(t, ctx, client, newHostCatalogId) + newHostId := boundary.CreateNewHostApi(t, ctx, client, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetApi(t, ctx, client, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetApi(t, ctx, client, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetApi(t, ctx, client, newTargetId, newHostSetId) // Create a credential store csClient := credentialstores.NewClient(client) @@ -195,56 +134,8 @@ func TestCreateTargetWithStaticCredentialStoreApi(t *testing.T) { newCredentialsId := newCredentialsResult.Item.Id t.Logf("Created Credentials: %s", newCredentialsId) - // Create a host catalog - hcClient := hostcatalogs.NewClient(client) - newHostCatalogResult, err := hcClient.Create(ctx, "static", newProjectId, - hostcatalogs.WithName("e2e Automated Test Host Catalog"), - ) - require.NoError(t, err) - newHostCatalogId := newHostCatalogResult.Item.Id - t.Logf("Created Host Catalog: %s", newHostCatalogId) - - // Create a host set and add to catalog - hsClient := hostsets.NewClient(client) - newHostSetResult, err := hsClient.Create(ctx, newHostCatalogId) - require.NoError(t, err) - newHostSetId := newHostSetResult.Item.Id - t.Logf("Created Host Set: %s", newHostSetId) - - // Create a host - hClient := hosts.NewClient(client) - newHostResult, err := hClient.Create(ctx, newHostCatalogId, - hosts.WithName(c.TargetIp), - hosts.WithStaticHostAddress(c.TargetIp), - ) - require.NoError(t, err) - newHostId := newHostResult.Item.Id - t.Logf("Created Host: %s", newHostId) - - // Add host to host set - _, err = hsClient.AddHosts(ctx, newHostSetId, 0, []string{newHostId}, hostsets.WithAutomaticVersioning(true)) - require.NoError(t, err) - - // Create a target - tClient := targets.NewClient(client) - targetPort, err := strconv.ParseInt(c.TargetPort, 10, 32) - require.NoError(t, err) - newTargetResult, err := tClient.Create(ctx, "tcp", newProjectId, - targets.WithName("e2e Automated Test Target"), - targets.WithTcpTargetDefaultPort(uint32(targetPort)), - ) - require.NoError(t, err) - newTargetId := newTargetResult.Item.Id - t.Logf("Created Target: %s", newTargetId) - - // Add host set to target - _, err = tClient.AddHostSources(ctx, newTargetId, 0, - []string{newHostSetId}, - targets.WithAutomaticVersioning(true), - ) - require.NoError(t, err) - // Add credentials to target + tClient := targets.NewClient(client) _, err = tClient.AddCredentialSources(ctx, newTargetId, 0, targets.WithAutomaticVersioning(true), targets.WithBrokeredCredentialSourceIds([]string{newCredentialsId}), diff --git a/testing/internal/e2e/host/static/statichost_test.go b/testing/internal/e2e/host/static/statichost_test.go index 5d2bc9780a..f4ffcaa094 100644 --- a/testing/internal/e2e/host/static/statichost_test.go +++ b/testing/internal/e2e/host/static/statichost_test.go @@ -2,15 +2,9 @@ package static_test import ( "context" - "encoding/json" - "strconv" "strings" "testing" - "github.com/hashicorp/boundary/api/hostcatalogs" - "github.com/hashicorp/boundary/api/hosts" - "github.com/hashicorp/boundary/api/hostsets" - "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" "github.com/stretchr/testify/require" @@ -25,94 +19,17 @@ func TestConnectTargetCli(t *testing.T) { require.NoError(t, err) boundary.AuthenticateCli(t) - - // Create an org and project newOrgId := boundary.CreateNewOrgCli(t) newProjectId := boundary.CreateNewProjectCli(t, newOrgId) - - // Create a host catalog - output := e2e.RunCommand("boundary", "host-catalogs", "create", "static", - "-scope-id", newProjectId, - "-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.Cleanup(func() { - output := e2e.RunCommand("boundary", "host-catalogs", "delete", "-id", newHostCatalogId) - require.NoError(t, output.Err, string(output.Stderr)) - }) - t.Logf("Created Host Catalog: %s", newHostCatalogId) - - // Create a host set and add to catalog - output = e2e.RunCommand("boundary", "host-sets", "create", "static", - "-host-catalog-id", newHostCatalogId, - "-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.Cleanup(func() { - output := e2e.RunCommand("boundary", "host-sets", "delete", "-id", newHostSetId) - require.NoError(t, output.Err, string(output.Stderr)) - }) - t.Logf("Created Host Set: %s", newHostSetId) - - // Create a host - output = e2e.RunCommand("boundary", "hosts", "create", "static", - "-host-catalog-id", newHostCatalogId, - "-name", c.TargetIp, - "-address", c.TargetIp, - "-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.Cleanup(func() { - output := e2e.RunCommand("boundary", "hosts", "delete", "-id", newHostId) - require.NoError(t, output.Err, string(output.Stderr)) - }) - t.Logf("Created Host: %s", newHostId) - - // Add host to host set - output = e2e.RunCommand("boundary", "host-sets", "add-hosts", "-id", newHostSetId, "-host", newHostId) - require.NoError(t, output.Err, string(output.Stderr)) - - // Create a target - output = e2e.RunCommand("boundary", "targets", "create", "tcp", - "-scope-id", newProjectId, - "-default-port", c.TargetPort, - "-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.Cleanup(func() { - output := e2e.RunCommand("boundary", "targets", "delete", "-id", newTargetId) - require.NoError(t, output.Err, string(output.Stderr)) - }) - t.Logf("Created Target: %s", newTargetId) - - // Add host set to target - output = e2e.RunCommand("boundary", "targets", "add-host-sources", - "-id", newTargetId, - "-host-source", newHostSetId, - ) - require.NoError(t, output.Err, string(output.Stderr)) + newHostCatalogId := boundary.CreateNewHostCatalogCli(t, newProjectId) + newHostSetId := boundary.CreateNewHostSetCli(t, newHostCatalogId) + newHostId := boundary.CreateNewHostCli(t, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetCli(t, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId) // Connect to target and print host's IP address - output = e2e.RunCommand("boundary", "connect", + output := e2e.RunCommand("boundary", "connect", "-target-id", newTargetId, "-exec", "/usr/bin/ssh", "--", "-l", c.TargetSshUser, @@ -140,77 +57,16 @@ func TestCreateTargetApi(t *testing.T) { c, err := loadConfig() require.NoError(t, err) - // Create boundary api client client, err := boundary.NewApiClient() require.NoError(t, err) ctx := context.Background() - // Create an org and project newOrgId := boundary.CreateNewOrgApi(t, ctx, client) newProjectId := boundary.CreateNewProjectApi(t, ctx, client, newOrgId) - - // Create a host catalog - hcClient := hostcatalogs.NewClient(client) - newHostCatalogResult, err := hcClient.Create(ctx, "static", newProjectId, - hostcatalogs.WithName("e2e Automated Test Host Catalog"), - ) - require.NoError(t, err) - newHostCatalogId := newHostCatalogResult.Item.Id - t.Cleanup(func() { - _, err := hcClient.Delete(ctx, newHostCatalogId) - require.NoError(t, err) - }) - t.Logf("Created Host Catalog: %s", newHostCatalogId) - - // Create a host set and add to catalog - hsClient := hostsets.NewClient(client) - newHostSetResult, err := hsClient.Create(ctx, newHostCatalogId) - require.NoError(t, err) - newHostSetId := newHostSetResult.Item.Id - t.Cleanup(func() { - _, err := hsClient.Delete(ctx, newHostSetId) - require.NoError(t, err) - }) - t.Logf("Created Host Set: %s", newHostSetId) - - // Create a host - hClient := hosts.NewClient(client) - newHostResult, err := hClient.Create(ctx, newHostCatalogId, - hosts.WithName(c.TargetIp), - hosts.WithStaticHostAddress(c.TargetIp), - ) - require.NoError(t, err) - newHostId := newHostResult.Item.Id - t.Cleanup(func() { - _, err := hClient.Delete(ctx, newHostId) - require.NoError(t, err) - }) - t.Logf("Created Host: %s", newHostId) - - // Add host to host set - _, err = hsClient.AddHosts(ctx, newHostSetId, 0, []string{newHostId}, hostsets.WithAutomaticVersioning(true)) - require.NoError(t, err) - - // Create a target - tClient := targets.NewClient(client) - targetPort, err := strconv.ParseInt(c.TargetPort, 10, 32) - require.NoError(t, err) - newTargetResult, err := tClient.Create(ctx, "tcp", newProjectId, - targets.WithName("e2e Automated Test Target"), - targets.WithTcpTargetDefaultPort(uint32(targetPort)), - ) - require.NoError(t, err) - newTargetId := newTargetResult.Item.Id - t.Cleanup(func() { - _, err := tClient.Delete(ctx, newTargetId) - require.NoError(t, err) - }) - t.Logf("Created Target: %s", newTargetId) - - // Add host set to target - _, err = tClient.AddHostSources(ctx, newTargetId, 0, - []string{newHostSetId}, - targets.WithAutomaticVersioning(true), - ) - require.NoError(t, err) + newHostCatalogId := boundary.CreateNewHostCatalogApi(t, ctx, client, newProjectId) + newHostSetId := boundary.CreateNewHostSetApi(t, ctx, client, newHostCatalogId) + newHostId := boundary.CreateNewHostApi(t, ctx, client, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetApi(t, ctx, client, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetApi(t, ctx, client, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetApi(t, ctx, client, newTargetId, newHostSetId) } From ff960e1fb139c339570a36caba5a96a50384fcf4 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 17 Oct 2022 15:29:24 -0400 Subject: [PATCH 5/8] refact(e2e): Rename files for consistency --- testing/internal/e2e/boundary/{scopes.go => scope.go} | 0 .../host/static/{staticcredential_test.go => connect_ssh_test.go} | 0 .../e2e/host/static/{statichost_test.go => connect_test.go} | 0 testing/internal/e2e/host/static/{env.go => env_test.go} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename testing/internal/e2e/boundary/{scopes.go => scope.go} (100%) rename testing/internal/e2e/host/static/{staticcredential_test.go => connect_ssh_test.go} (100%) rename testing/internal/e2e/host/static/{statichost_test.go => connect_test.go} (100%) rename testing/internal/e2e/host/static/{env.go => env_test.go} (100%) diff --git a/testing/internal/e2e/boundary/scopes.go b/testing/internal/e2e/boundary/scope.go similarity index 100% rename from testing/internal/e2e/boundary/scopes.go rename to testing/internal/e2e/boundary/scope.go diff --git a/testing/internal/e2e/host/static/staticcredential_test.go b/testing/internal/e2e/host/static/connect_ssh_test.go similarity index 100% rename from testing/internal/e2e/host/static/staticcredential_test.go rename to testing/internal/e2e/host/static/connect_ssh_test.go diff --git a/testing/internal/e2e/host/static/statichost_test.go b/testing/internal/e2e/host/static/connect_test.go similarity index 100% rename from testing/internal/e2e/host/static/statichost_test.go rename to testing/internal/e2e/host/static/connect_test.go diff --git a/testing/internal/e2e/host/static/env.go b/testing/internal/e2e/host/static/env_test.go similarity index 100% rename from testing/internal/e2e/host/static/env.go rename to testing/internal/e2e/host/static/env_test.go From cb3f0578781adaf26b66c826a4124db6a2c978c2 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 17 Oct 2022 15:30:08 -0400 Subject: [PATCH 6/8] test(e2e): Add test that uses connect authz-token option --- .../host/static/connect_authz_token_test.go | 117 ++++++++++++++++++ .../e2e/host/static/connect_ssh_test.go | 18 ++- 2 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 testing/internal/e2e/host/static/connect_authz_token_test.go diff --git a/testing/internal/e2e/host/static/connect_authz_token_test.go b/testing/internal/e2e/host/static/connect_authz_token_test.go new file mode 100644 index 0000000000..b4ac2cadd1 --- /dev/null +++ b/testing/internal/e2e/host/static/connect_authz_token_test.go @@ -0,0 +1,117 @@ +package static_test + +import ( + "encoding/json" + "fmt" + "os" + "strings" + "testing" + + "github.com/hashicorp/boundary/api/credentials" + "github.com/hashicorp/boundary/api/credentialstores" + "github.com/hashicorp/boundary/api/targets" + "github.com/hashicorp/boundary/testing/internal/e2e" + "github.com/hashicorp/boundary/testing/internal/e2e/boundary" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestConnectTargetWithAuthzTokenCli uses the boundary cli to connect to a target using the +// `authz_token` option +func TestConnectTargetWithAuthzTokenCli(t *testing.T) { + e2e.MaybeSkipTest(t) + c, err := loadConfig() + require.NoError(t, err) + + boundary.AuthenticateCli(t) + newOrgId := boundary.CreateNewOrgCli(t) + newProjectId := boundary.CreateNewProjectCli(t, newOrgId) + newHostCatalogId := boundary.CreateNewHostCatalogCli(t, newProjectId) + newHostSetId := boundary.CreateNewHostSetCli(t, newHostCatalogId) + newHostId := boundary.CreateNewHostCli(t, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetCli(t, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId) + + // Create a credential store + output := e2e.RunCommand("boundary", "credential-stores", "create", "static", + "-scope-id", newProjectId, + "-format", "json", + ) + require.NoError(t, output.Err, string(output.Stderr)) + var newCredentialStoreResult credentialstores.CredentialStoreCreateResult + err = json.Unmarshal(output.Stdout, &newCredentialStoreResult) + require.NoError(t, err) + newCredentialStoreId := newCredentialStoreResult.Item.Id + t.Logf("Created Credential Store: %s", newCredentialStoreId) + + // Create credentials + output = e2e.RunCommand("boundary", "credentials", "create", "ssh-private-key", + "-credential-store-id", newCredentialStoreId, + "-username", c.TargetSshUser, + "-private-key", "file://"+c.TargetSshKeyPath, + "-format", "json", + ) + require.NoError(t, output.Err, string(output.Stderr)) + var newCredentialsResult credentials.CredentialCreateResult + err = json.Unmarshal(output.Stdout, &newCredentialsResult) + require.NoError(t, err) + newCredentialsId := newCredentialsResult.Item.Id + t.Logf("Created Credentials: %s", newCredentialsId) + + // Add credentials to target + output = e2e.RunCommand("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") + require.NoError(t, output.Err, string(output.Stderr)) + var newSessionAuthorizationResult targets.SessionAuthorizationResult + err = json.Unmarshal(output.Stdout, &newSessionAuthorizationResult) + require.NoError(t, err) + + newSessionAuthorization := newSessionAuthorizationResult.Item + retrievedUser := fmt.Sprintf("%s", newSessionAuthorization.Credentials[0].Credential["username"]) + retrievedKey := fmt.Sprintf("%s\n", newSessionAuthorization.Credentials[0].Credential["private_key"]) + assert.Equal(t, c.TargetSshUser, retrievedUser) + + k, err := os.ReadFile(c.TargetSshKeyPath) + require.NoError(t, err) + require.Equal(t, string(k), retrievedKey) + t.Log("Successfully retrieved credentials for target") + + // Get auth token for session + newAuthToken := newSessionAuthorizationResult.Item.AuthorizationToken + + // Create key file + retrievedKeyPath := fmt.Sprintf("%s/%s", t.TempDir(), "target_private_key.pem") + f, err := os.Create(retrievedKeyPath) + require.NoError(t, err) + _, err = f.WriteString(retrievedKey) + require.NoError(t, err) + err = os.Chmod(retrievedKeyPath, 0o400) + require.NoError(t, err) + + // Connect to target and print host's IP address using retrieved credentials + output = e2e.RunCommand("boundary", "connect", + "-authz-token", newAuthToken, + "-exec", "/usr/bin/ssh", "--", + "-l", retrievedUser, + "-i", retrievedKeyPath, + "-o", "UserKnownHostsFile=/dev/null", + "-o", "StrictHostKeyChecking=no", + "-o", "IdentitiesOnly=yes", // forces the use of the provided key + "-p", "{{boundary.port}}", // this is provided by boundary + "{{boundary.ip}}", + "hostname", "-i", + ) + require.NoError(t, output.Err, string(output.Stderr)) + + parts := strings.Fields(string(output.Stdout)) + hostIp := parts[len(parts)-1] + require.Equal(t, c.TargetIp, hostIp, "SSH session did not return expected output") + t.Log("Successfully connected to target") +} diff --git a/testing/internal/e2e/host/static/connect_ssh_test.go b/testing/internal/e2e/host/static/connect_ssh_test.go index afa143d31e..58d3b1a291 100644 --- a/testing/internal/e2e/host/static/connect_ssh_test.go +++ b/testing/internal/e2e/host/static/connect_ssh_test.go @@ -16,10 +16,10 @@ import ( "github.com/stretchr/testify/require" ) -// TestConnectTargetWithStaticCredentialStoreCli uses the boundary cli to create a credential using -// boundary's built-in credential store. The test attaches that credential to a target and attempts -// to connect to that target using those credentials. -func TestConnectTargetWithStaticCredentialStoreCli(t *testing.T) { +// TestConnectTargetWithSshCli uses the boundary cli to create a credential using boundary's +// built-in credential store. The test attaches that credential to a target and attempts to connect +// to that target using those credentials. +func TestConnectTargetWithSshCli(t *testing.T) { e2e.MaybeSkipTest(t) c, err := loadConfig() require.NoError(t, err) @@ -141,4 +141,14 @@ func TestCreateTargetWithStaticCredentialStoreApi(t *testing.T) { targets.WithBrokeredCredentialSourceIds([]string{newCredentialsId}), ) require.NoError(t, err) + + // Authorize Session + newSessionAuthorizationResult, err := tClient.AuthorizeSession(ctx, newTargetId) + require.NoError(t, err) + newSessionAuthorization := newSessionAuthorizationResult.Item + retrievedUser := fmt.Sprintf("%s", newSessionAuthorization.Credentials[0].Credential["username"]) + retrievedKey := fmt.Sprintf("%s", newSessionAuthorization.Credentials[0].Credential["private_key"]) + assert.Equal(t, c.TargetSshUser, retrievedUser) + require.Equal(t, string(k), retrievedKey) + t.Log("Successfully retrieved credentials for target") } From 84aa65ccc5e07910c764d63062e62a63a6d2841f Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 17 Oct 2022 19:29:03 -0400 Subject: [PATCH 7/8] test(e2e): Add test that cancels a session --- testing/internal/e2e/helpers.go | 25 ++++ .../internal/e2e/host/static/session_test.go | 113 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 testing/internal/e2e/host/static/session_test.go diff --git a/testing/internal/e2e/helpers.go b/testing/internal/e2e/helpers.go index c6ea753acb..64a4c5db2a 100644 --- a/testing/internal/e2e/helpers.go +++ b/testing/internal/e2e/helpers.go @@ -2,6 +2,7 @@ package e2e import ( "bytes" + "context" "errors" "fmt" "os" @@ -74,6 +75,30 @@ func RunCommand(name string, args ...string) *CommandResult { } } +// RunCommandContext is similar to RunCommand but allows passing in a context +func RunCommandContext(ctx context.Context, name string, args ...string) *CommandResult { + var outbuf, errbuf bytes.Buffer + + cmd := exec.CommandContext(ctx, 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, + } +} + // WithArgs is an option to RunCommand that allows the user to specify arguments // for the provided command func WithArgs(args ...string) Option { diff --git a/testing/internal/e2e/host/static/session_test.go b/testing/internal/e2e/host/static/session_test.go new file mode 100644 index 0000000000..ca775a8368 --- /dev/null +++ b/testing/internal/e2e/host/static/session_test.go @@ -0,0 +1,113 @@ +package static_test + +import ( + "context" + "encoding/json" + "errors" + "testing" + "time" + + "github.com/cenkalti/backoff/v4" + "github.com/hashicorp/boundary/api/sessions" + "github.com/hashicorp/boundary/testing/internal/e2e" + "github.com/hashicorp/boundary/testing/internal/e2e/boundary" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSessionCancelingCli(t *testing.T) { + e2e.MaybeSkipTest(t) + c, err := loadConfig() + require.NoError(t, err) + + boundary.AuthenticateCli(t) + newOrgId := boundary.CreateNewOrgCli(t) + newProjectId := boundary.CreateNewProjectCli(t, newOrgId) + newHostCatalogId := boundary.CreateNewHostCatalogCli(t, newProjectId) + newHostSetId := boundary.CreateNewHostSetCli(t, newHostCatalogId) + newHostId := boundary.CreateNewHostCli(t, newHostCatalogId, c.TargetIp) + boundary.AddHostToHostSetCli(t, newHostSetId, newHostId) + newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) + boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId) + + // Connect to target to create a session + ctx, cancel := context.WithCancel(context.Background()) + errChan := make(chan *e2e.CommandResult) + go func() { + errChan <- e2e.RunCommandContext(ctx, "boundary", "connect", + "-target-id", newTargetId, + "-exec", "/usr/bin/ssh", "--", + "-l", c.TargetSshUser, + "-i", c.TargetSshKeyPath, + "-o", "UserKnownHostsFile=/dev/null", + "-o", "StrictHostKeyChecking=no", + "-o", "IdentitiesOnly=yes", // forces the use of the provided key + "-p", "{{boundary.port}}", // this is provided by boundary + "{{boundary.ip}}", + "hostname -i; sleep 60", + ) + }() + t.Cleanup(cancel) + + // Get list of sessions + var session *sessions.Session + err = backoff.RetryNotify( + func() error { + output := e2e.RunCommand("boundary", "sessions", "list", "-scope-id", newProjectId, "-format", "json") + if output.Err != nil { + return backoff.Permanent(errors.New(string(output.Stderr))) + } + + var sessionListResult sessions.SessionListResult + err = json.Unmarshal(output.Stdout, &sessionListResult) + if err != nil { + return backoff.Permanent(err) + } + + sessionCount := len(sessionListResult.Items) + if sessionCount == 0 { + return errors.New("No items are appearing in the session list") + } + + t.Logf("Found %d session(s)", sessionCount) + if sessionCount != 1 { + return backoff.Permanent(errors.New("Only one session was expected to be found")) + } + + session = sessionListResult.Items[0] + return nil + }, + backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5), + func(err error, td time.Duration) { + t.Logf("%s. Retrying...", err.Error()) + }, + ) + require.NoError(t, err) + assert.Equal(t, newTargetId, session.TargetId) + assert.Equal(t, newHostId, session.HostId) + require.Equal(t, "active", session.Status) + + // Cancel session + output := e2e.RunCommand("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") + require.NoError(t, output.Err, string(output.Stderr)) + var newSessionReadResult sessions.SessionReadResult + err = json.Unmarshal(output.Stdout, &newSessionReadResult) + require.NoError(t, err) + require.Condition(t, func() bool { + return newSessionReadResult.Item.Status == "canceling" || newSessionReadResult.Item.Status == "terminated" + }) + + // Check output from session + select { + case output := <-errChan: + // `boundary connect` returns a 255 when cancelled + require.Equal(t, output.ExitCode, 255, string(output.Stdout), string(output.Stderr)) + case <-time.After(time.Second * 5): + t.Fatal("Timed out waiting for session command to exit") + } + + t.Log("Successfully cancelled session") +} From a4e27d877d3763610c67b30e7bff56fc79c10c20 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Tue, 18 Oct 2022 16:24:28 -0400 Subject: [PATCH 8/8] refact(e2e): Update RunCommand to have context argument --- testing/internal/e2e/boundary/boundary.go | 2 +- testing/internal/e2e/boundary/host.go | 8 ++--- testing/internal/e2e/boundary/scope.go | 8 +++-- testing/internal/e2e/boundary/target.go | 4 +-- .../e2e/credential/vault/vault_test.go | 23 +++++++------- testing/internal/e2e/helpers.go | 30 ++----------------- .../e2e/host/aws/dynamichostcatalog_test.go | 15 +++++----- .../host/static/connect_authz_token_test.go | 12 ++++---- .../e2e/host/static/connect_ssh_test.go | 11 +++---- .../internal/e2e/host/static/connect_test.go | 3 +- .../internal/e2e/host/static/session_test.go | 11 +++---- testing/internal/e2e/vault/vault.go | 13 ++++---- 12 files changed, 64 insertions(+), 76 deletions(-) diff --git a/testing/internal/e2e/boundary/boundary.go b/testing/internal/e2e/boundary/boundary.go index f48b2d1d52..313e4a57d8 100644 --- a/testing/internal/e2e/boundary/boundary.go +++ b/testing/internal/e2e/boundary/boundary.go @@ -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, diff --git a/testing/internal/e2e/boundary/host.go b/testing/internal/e2e/boundary/host.go index bc3b0e302f..5788d2a128 100644 --- a/testing/internal/e2e/boundary/host.go +++ b/testing/internal/e2e/boundary/host.go @@ -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)) } diff --git a/testing/internal/e2e/boundary/scope.go b/testing/internal/e2e/boundary/scope.go index 9bc21fd7bc..6d39f246f0 100644 --- a/testing/internal/e2e/boundary/scope.go +++ b/testing/internal/e2e/boundary/scope.go @@ -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", diff --git a/testing/internal/e2e/boundary/target.go b/testing/internal/e2e/boundary/target.go index 0a5e43c5b9..54a3deecbc 100644 --- a/testing/internal/e2e/boundary/target.go +++ b/testing/internal/e2e/boundary/target.go @@ -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, ) diff --git a/testing/internal/e2e/credential/vault/vault_test.go b/testing/internal/e2e/credential/vault/vault_test.go index d5fb8bd7cb..34774581ee 100644 --- a/testing/internal/e2e/credential/vault/vault_test.go +++ b/testing/internal/e2e/credential/vault/vault_test.go @@ -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, diff --git a/testing/internal/e2e/helpers.go b/testing/internal/e2e/helpers.go index 64a4c5db2a..f713a29dc5 100644 --- a/testing/internal/e2e/helpers.go +++ b/testing/internal/e2e/helpers.go @@ -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...) diff --git a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go index 8f2eb81127..9cf4ea694e 100644 --- a/testing/internal/e2e/host/aws/dynamichostcatalog_test.go +++ b/testing/internal/e2e/host/aws/dynamichostcatalog_test.go @@ -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, diff --git a/testing/internal/e2e/host/static/connect_authz_token_test.go b/testing/internal/e2e/host/static/connect_authz_token_test.go index b4ac2cadd1..01798aba0e 100644 --- a/testing/internal/e2e/host/static/connect_authz_token_test.go +++ b/testing/internal/e2e/host/static/connect_authz_token_test.go @@ -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, diff --git a/testing/internal/e2e/host/static/connect_ssh_test.go b/testing/internal/e2e/host/static/connect_ssh_test.go index 58d3b1a291..df0d3e9a0e 100644 --- a/testing/internal/e2e/host/static/connect_ssh_test.go +++ b/testing/internal/e2e/host/static/connect_ssh_test.go @@ -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", diff --git a/testing/internal/e2e/host/static/connect_test.go b/testing/internal/e2e/host/static/connect_test.go index f4ffcaa094..62c7c1e420 100644 --- a/testing/internal/e2e/host/static/connect_test.go +++ b/testing/internal/e2e/host/static/connect_test.go @@ -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, diff --git a/testing/internal/e2e/host/static/session_test.go b/testing/internal/e2e/host/static/session_test.go index ca775a8368..51a13ae91e 100644 --- a/testing/internal/e2e/host/static/session_test.go +++ b/testing/internal/e2e/host/static/session_test.go @@ -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) diff --git a/testing/internal/e2e/vault/vault.go b/testing/internal/e2e/vault/vault.go index 42f148534f..8e02473393 100644 --- a/testing/internal/e2e/vault/vault.go +++ b/testing/internal/e2e/vault/vault.go @@ -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,