2022-12-22 11:11:03 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2023-06-15 11:51:14 -04:00
|
|
|
package base_test
|
2022-12-22 11:11:03 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/api/sessions"
|
2023-03-27 09:50:31 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/session"
|
2022-12-22 11:11:03 -05:00
|
|
|
"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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TestCliBytesUpDownTransferData uses the cli to verify that the bytes_up/bytes_down fields of a
|
|
|
|
|
// session correctly increase when data is transmitting during a session.
|
|
|
|
|
func TestCliBytesUpDownTransferData(t *testing.T) {
|
|
|
|
|
e2e.MaybeSkipTest(t)
|
2023-05-16 13:06:11 -04:00
|
|
|
c, err := loadTestConfig()
|
2022-12-22 11:11:03 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
boundary.AuthenticateAdminCli(t, ctx)
|
2024-04-03 09:44:44 -04:00
|
|
|
orgId, err := boundary.CreateOrgCli(t, ctx)
|
|
|
|
|
require.NoError(t, err)
|
2022-12-22 11:11:03 -05:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
boundary.AuthenticateAdminCli(t, ctx)
|
2024-04-03 09:44:44 -04:00
|
|
|
output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", orgId))
|
2022-12-22 11:11:03 -05:00
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
})
|
2024-04-03 09:44:44 -04:00
|
|
|
projectId, err := boundary.CreateProjectCli(t, ctx, orgId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostCatalogId, err := boundary.CreateHostCatalogCli(t, ctx, projectId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostSetId, err := boundary.CreateHostSetCli(t, ctx, hostCatalogId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
hostId, err := boundary.CreateHostCli(t, ctx, hostCatalogId, c.TargetAddress)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = boundary.AddHostToHostSetCli(t, ctx, hostSetId, hostId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
targetId, err := boundary.CreateTargetCli(t, ctx, projectId, c.TargetPort)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = boundary.AddHostSourceToTargetCli(t, ctx, targetId, hostSetId)
|
|
|
|
|
require.NoError(t, err)
|
2022-12-22 11:11:03 -05:00
|
|
|
|
|
|
|
|
// Create a session where no additional commands are run
|
|
|
|
|
ctxCancel, cancel := context.WithCancel(context.Background())
|
|
|
|
|
errChan := make(chan *e2e.CommandResult)
|
|
|
|
|
|
|
|
|
|
// Run commands on the target to send data through connection
|
|
|
|
|
go func() {
|
|
|
|
|
t.Log("Starting session...")
|
|
|
|
|
errChan <- e2e.RunCommand(ctxCancel, "boundary",
|
|
|
|
|
e2e.WithArgs(
|
|
|
|
|
"connect",
|
2024-04-03 09:44:44 -04:00
|
|
|
"-target-id", targetId,
|
2022-12-22 11:11:03 -05:00
|
|
|
"-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}}",
|
|
|
|
|
"for i in {1..10}; do pwd; sleep 1s; done",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}()
|
|
|
|
|
t.Cleanup(cancel)
|
2024-04-03 09:44:44 -04:00
|
|
|
s := boundary.WaitForSessionCli(t, ctx, projectId)
|
2023-03-27 09:50:31 -04:00
|
|
|
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
|
2024-04-03 09:44:44 -04:00
|
|
|
assert.Equal(t, targetId, s.TargetId)
|
|
|
|
|
assert.Equal(t, hostId, s.HostId)
|
2022-12-22 11:11:03 -05:00
|
|
|
|
|
|
|
|
bytesUp := 0
|
|
|
|
|
bytesDown := 0
|
|
|
|
|
|
2023-01-24 15:38:58 -05:00
|
|
|
// Wait until bytes up and down is greater than 0
|
|
|
|
|
t.Log("Waiting for bytes_up/bytes_down to be greater than 0...")
|
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-12-22 11:11:03 -05:00
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
2023-03-27 09:50:31 -04:00
|
|
|
e2e.WithArgs("sessions", "read", "-id", s.Id, "-format", "json"),
|
2022-12-22 11:11:03 -05:00
|
|
|
)
|
|
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
var newSessionReadResult sessions.SessionReadResult
|
|
|
|
|
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
|
|
|
|
|
require.NoError(t, err)
|
2023-01-24 15:38:58 -05:00
|
|
|
|
2023-03-27 09:50:31 -04:00
|
|
|
if len(newSessionReadResult.Item.Connections) > 0 {
|
|
|
|
|
bytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp)
|
|
|
|
|
bytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown)
|
|
|
|
|
t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown)
|
|
|
|
|
|
|
|
|
|
if bytesUp > 0 && bytesDown > 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
t.Log("No connections found in session. Retrying...")
|
2023-01-24 15:38:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
}
|
|
|
|
|
require.Greater(t, bytesUp, 0)
|
|
|
|
|
require.Greater(t, bytesDown, 0)
|
|
|
|
|
|
|
|
|
|
// Confirm that bytes up and down increases
|
|
|
|
|
t.Log("Waiting for bytes_up/bytes_down to increase...")
|
|
|
|
|
var newBytesUp, newBytesDown int
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
2023-03-27 09:50:31 -04:00
|
|
|
e2e.WithArgs("sessions", "read", "-id", s.Id, "-format", "json"),
|
2023-01-24 15:38:58 -05:00
|
|
|
)
|
|
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
var newSessionReadResult sessions.SessionReadResult
|
|
|
|
|
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
newBytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp)
|
|
|
|
|
newBytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown)
|
|
|
|
|
t.Logf("bytes_up: %d, bytes_down: %d", newBytesUp, newBytesDown)
|
|
|
|
|
|
|
|
|
|
if newBytesDown > bytesDown {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
2022-12-22 11:11:03 -05:00
|
|
|
}
|
2023-01-24 15:38:58 -05:00
|
|
|
require.GreaterOrEqual(t, newBytesUp, bytesUp)
|
|
|
|
|
require.Greater(t, newBytesDown, bytesDown)
|
2022-12-22 11:11:03 -05:00
|
|
|
}
|