test(e2e): Add test for boundary connect postgres (#3671)

* refact(e2e): Rename env

* test(e2e): Add test for `connect postgres`

* fixup! test(e2e): Add test for `connect postgres`
This commit is contained in:
Michael Li authored and GitHub committed 2023-09-06 17:29:35 -04:00
1 parent 78161c0187
commit 9a6b0d26cb
43 files changed
+346 -82

No files matched your search

@@ -0,0 +1,24 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package base_with_postgres_test
import "github.com/kelseyhightower/envconfig"
type config struct {
TargetAddress string `envconfig:"E2E_TARGET_ADDRESS" required:"true"` // e.g. 192.168.0.1
TargetPort string `envconfig:"E2E_TARGET_PORT" required:"true"`
PostgresDbName string `envconfig:"E2E_POSTGRES_DB_NAME" required:"true"`
PostgresUser string `envconfig:"E2E_POSTGRES_USER" required:"true"`
PostgresPassword string `envconfig:"E2E_POSTGRES_PASSWORD" required:"true"`
}
func loadTestConfig() (*config, error) {
var c config
err := envconfig.Process("", &c)
if err != nil {
return nil, err
}
return &c, nil
}
@@ -0,0 +1,79 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package base_with_postgres_test
import (
"bytes"
"context"
"io"
"os/exec"
"testing"
"github.com/creack/pty"
"github.com/stretchr/testify/require"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
)
// TestCliTcpTargetConnectPostgres uses the boundary cli to connect to a
// target using `connect postgres`
func TestCliTcpTargetConnectPostgres(t *testing.T) {
e2e.MaybeSkipTest(t)
c, err := loadTestConfig()
require.NoError(t, err)
ctx := context.Background()
boundary.AuthenticateAdminCli(t, ctx)
newOrgId := boundary.CreateNewOrgCli(t, ctx)
t.Cleanup(func() {
ctx := context.Background()
boundary.AuthenticateAdminCli(t, ctx)
output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", newOrgId))
require.NoError(t, output.Err, string(output.Stderr))
})
newProjectId := boundary.CreateNewProjectCli(t, ctx, newOrgId)
newTargetId := boundary.CreateNewTargetCli(
t,
ctx,
newProjectId,
c.TargetPort,
target.WithAddress(c.TargetAddress),
)
newCredentialStoreId := boundary.CreateNewCredentialStoreStaticCli(t, ctx, newProjectId)
newCredentialsId := boundary.CreateNewStaticCredentialPasswordCli(
t,
ctx,
newCredentialStoreId,
c.PostgresUser,
c.PostgresPassword,
)
boundary.AddBrokeredCredentialSourceToTargetCli(t, ctx, newTargetId, newCredentialsId)
var cmd *exec.Cmd
cmd = exec.CommandContext(ctx,
"boundary",
"connect", "postgres",
"-target-id", newTargetId,
"-dbname", c.PostgresDbName,
)
f, err := pty.Start(cmd)
require.NoError(t, err)
t.Cleanup(func() {
err := f.Close()
require.NoError(t, err)
})
f.Write([]byte("\\pset pager off\n")) // disables pagination in output
f.Write([]byte("\\dt\n")) // list all tables
f.Write([]byte("\\q\n")) // exit psql session
f.Write([]byte{4}) // EOT (End of Transmission - marks end of file stream)
var buf bytes.Buffer
io.Copy(&buf, f)
require.Contains(t, buf.String(), "List of relations", "Session did not return expected output")
require.Contains(t, buf.String(), c.PostgresDbName, "Session did not return expected output")
t.Log("Successfully connected to target")
}