2022-09-20 21:17:24 +00:00
|
|
|
package coderd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"cdr.dev/slog/sloggers/slogtest"
|
|
|
|
"github.com/coder/coder/coderd/coderdtest"
|
|
|
|
"github.com/coder/coder/coderd/database"
|
|
|
|
"github.com/coder/coder/codersdk"
|
|
|
|
"github.com/coder/coder/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWorkspaceActivityBump(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-01-16 20:13:34 +00:00
|
|
|
const ttl = time.Minute
|
|
|
|
|
2022-09-20 21:17:24 +00:00
|
|
|
setupActivityTest := func(t *testing.T) (client *codersdk.Client, workspace codersdk.Workspace, assertBumped func(want bool)) {
|
2023-01-16 20:13:34 +00:00
|
|
|
ttlMillis := int64(ttl / time.Millisecond)
|
2022-09-20 21:17:24 +00:00
|
|
|
|
2022-10-14 18:25:11 +00:00
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
2023-01-16 20:13:34 +00:00
|
|
|
AppHostname: proxyTestSubdomainRaw,
|
|
|
|
IncludeProvisionerDaemon: true,
|
|
|
|
// Agent stats trigger the activity bump, so we want to report
|
|
|
|
// very frequently in tests.
|
|
|
|
AgentStatsRefreshInterval: time.Millisecond * 100,
|
2022-10-14 18:25:11 +00:00
|
|
|
})
|
|
|
|
user := coderdtest.CreateFirstUser(t, client)
|
|
|
|
|
2022-11-04 04:45:43 +00:00
|
|
|
workspace = createWorkspaceWithApps(t, client, user.OrganizationID, "", 1234, func(cwr *codersdk.CreateWorkspaceRequest) {
|
2022-09-20 21:17:24 +00:00
|
|
|
cwr.TTLMillis = &ttlMillis
|
|
|
|
})
|
|
|
|
|
|
|
|
// Sanity-check that deadline is near.
|
|
|
|
workspace, err := client.Workspace(ctx, workspace.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.WithinDuration(t,
|
|
|
|
time.Now().Add(time.Duration(ttlMillis)*time.Millisecond),
|
2023-02-26 19:42:01 +00:00
|
|
|
workspace.LatestBuild.Deadline.Time, testutil.WaitMedium,
|
2022-09-20 21:17:24 +00:00
|
|
|
)
|
|
|
|
firstDeadline := workspace.LatestBuild.Deadline.Time
|
|
|
|
|
2022-10-03 21:01:13 +00:00
|
|
|
_ = coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
2022-09-20 21:17:24 +00:00
|
|
|
|
|
|
|
return client, workspace, func(want bool) {
|
|
|
|
if !want {
|
|
|
|
// It is difficult to test the absence of a call in a non-racey
|
|
|
|
// way. In general, it is difficult for the API to generate
|
|
|
|
// false positive activity since Agent networking event
|
|
|
|
// is required. The Activity Bump behavior is also coupled with
|
|
|
|
// Last Used, so it would be obvious to the user if we
|
|
|
|
// are falsely recognizing activity.
|
|
|
|
time.Sleep(testutil.IntervalMedium)
|
|
|
|
workspace, err = client.Workspace(ctx, workspace.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, workspace.LatestBuild.Deadline.Time, firstDeadline)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Deadline bump occurs asynchronously.
|
|
|
|
require.Eventuallyf(t,
|
|
|
|
func() bool {
|
|
|
|
workspace, err = client.Workspace(ctx, workspace.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return workspace.LatestBuild.Deadline.Time != firstDeadline
|
|
|
|
},
|
2023-01-16 20:13:34 +00:00
|
|
|
testutil.WaitLong, testutil.IntervalFast,
|
2022-09-20 21:17:24 +00:00
|
|
|
"deadline %v never updated", firstDeadline,
|
|
|
|
)
|
|
|
|
|
2023-01-16 20:13:34 +00:00
|
|
|
require.WithinDuration(t, database.Now().Add(ttl), workspace.LatestBuild.Deadline.Time, 3*time.Second)
|
2022-09-20 21:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Dial", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, workspace, assertBumped := setupActivityTest(t)
|
|
|
|
|
2022-10-03 21:01:13 +00:00
|
|
|
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
2022-10-17 13:43:30 +00:00
|
|
|
conn, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, &codersdk.DialWorkspaceAgentOptions{
|
|
|
|
Logger: slogtest.Make(t, nil),
|
|
|
|
})
|
2022-09-20 21:17:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-01-16 20:13:34 +00:00
|
|
|
// Must send network traffic after a few seconds to surpass bump threshold.
|
|
|
|
time.Sleep(time.Second * 3)
|
2022-11-08 22:10:48 +00:00
|
|
|
sshConn, err := conn.SSHClient(ctx)
|
2022-09-20 21:17:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
_ = sshConn.Close()
|
|
|
|
|
|
|
|
assertBumped(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoBump", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, workspace, assertBumped := setupActivityTest(t)
|
|
|
|
|
2022-10-03 21:01:13 +00:00
|
|
|
// Benign operations like retrieving workspace must not
|
2022-09-20 21:17:24 +00:00
|
|
|
// bump the deadline.
|
2022-10-03 21:01:13 +00:00
|
|
|
_, err := client.Workspace(ctx, workspace.ID)
|
2022-09-20 21:17:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assertBumped(false)
|
|
|
|
})
|
|
|
|
}
|