2022-09-20 21:17:24 +00:00
|
|
|
package coderd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:14:58 +00:00
|
|
|
"github.com/google/uuid"
|
2022-09-20 21:17:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"cdr.dev/slog/sloggers/slogtest"
|
2023-09-26 11:05:19 +00:00
|
|
|
"github.com/coder/coder/v2/agent/agenttest"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
2023-09-01 16:50:12 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/database/dbtime"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/schedule"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2024-03-26 17:44:31 +00:00
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/provisioner/echo"
|
|
|
|
"github.com/coder/coder/v2/testutil"
|
2022-09-20 21:17:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWorkspaceActivityBump(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-07-20 13:35:41 +00:00
|
|
|
// deadline allows you to forcibly set a max_deadline on the build. This
|
2023-08-29 18:35:05 +00:00
|
|
|
// doesn't use template autostop requirements and instead edits the
|
2023-07-20 13:35:41 +00:00
|
|
|
// max_deadline on the build directly in the database.
|
|
|
|
setupActivityTest := func(t *testing.T, deadline ...time.Duration) (client *codersdk.Client, workspace codersdk.Workspace, assertBumped func(want bool)) {
|
2023-09-15 08:01:32 +00:00
|
|
|
t.Helper()
|
2023-11-15 15:42:27 +00:00
|
|
|
const ttl = time.Hour
|
2022-09-20 21:17:24 +00:00
|
|
|
|
2023-07-20 13:35:41 +00:00
|
|
|
db, pubsub := dbtestutil.NewDB(t)
|
2022-10-14 18:25:11 +00:00
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
2023-07-20 13:35:41 +00:00
|
|
|
Database: db,
|
|
|
|
Pubsub: pubsub,
|
2023-01-16 20:13:34 +00:00
|
|
|
IncludeProvisionerDaemon: true,
|
|
|
|
// Agent stats trigger the activity bump, so we want to report
|
|
|
|
// very frequently in tests.
|
|
|
|
AgentStatsRefreshInterval: time.Millisecond * 100,
|
2023-04-04 12:48:35 +00:00
|
|
|
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
|
|
|
|
GetFn: func(ctx context.Context, db database.Store, templateID uuid.UUID) (schedule.TemplateScheduleOptions, error) {
|
2023-03-07 14:14:58 +00:00
|
|
|
return schedule.TemplateScheduleOptions{
|
2023-04-04 12:48:35 +00:00
|
|
|
UserAutostopEnabled: true,
|
|
|
|
DefaultTTL: ttl,
|
2023-07-20 13:35:41 +00:00
|
|
|
// We set max_deadline manually below.
|
2023-08-29 18:35:05 +00:00
|
|
|
AutostopRequirement: schedule.TemplateAutostopRequirement{},
|
2023-03-07 14:14:58 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2022-10-14 18:25:11 +00:00
|
|
|
})
|
|
|
|
user := coderdtest.CreateFirstUser(t, client)
|
|
|
|
|
2023-03-07 14:14:58 +00:00
|
|
|
ttlMillis := int64(ttl / time.Millisecond)
|
2023-04-06 21:35:27 +00:00
|
|
|
agentToken := uuid.NewString()
|
|
|
|
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
|
2023-08-25 06:10:15 +00:00
|
|
|
Parse: echo.ParseComplete,
|
|
|
|
ProvisionPlan: echo.PlanComplete,
|
|
|
|
ProvisionApply: echo.ProvisionApplyWithAgent(agentToken),
|
2023-04-06 21:35:27 +00:00
|
|
|
})
|
|
|
|
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
|
2023-10-03 17:02:56 +00:00
|
|
|
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
|
2023-04-06 21:35:27 +00:00
|
|
|
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
|
2022-09-20 21:17:24 +00:00
|
|
|
cwr.TTLMillis = &ttlMillis
|
|
|
|
})
|
2023-10-03 17:02:56 +00:00
|
|
|
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
|
2023-04-06 21:35:27 +00:00
|
|
|
|
2023-11-15 15:42:27 +00:00
|
|
|
var maxDeadline time.Time
|
2023-07-20 13:35:41 +00:00
|
|
|
// Update the max deadline.
|
2024-03-20 15:37:57 +00:00
|
|
|
if len(deadline) > 0 {
|
|
|
|
maxDeadline = dbtime.Now().Add(deadline[0])
|
2023-07-20 13:35:41 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 15:42:27 +00:00
|
|
|
err := db.UpdateWorkspaceBuildDeadlineByID(ctx, database.UpdateWorkspaceBuildDeadlineByIDParams{
|
|
|
|
ID: workspace.LatestBuild.ID,
|
|
|
|
UpdatedAt: dbtime.Now(),
|
|
|
|
// Make the deadline really close so it needs to be bumped immediately.
|
|
|
|
Deadline: dbtime.Now().Add(time.Minute),
|
|
|
|
MaxDeadline: maxDeadline,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-26 11:05:19 +00:00
|
|
|
_ = agenttest.New(t, client.URL, agentToken)
|
2023-04-06 21:35:27 +00:00
|
|
|
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
2022-09-20 21:17:24 +00:00
|
|
|
|
2023-11-15 15:42:27 +00:00
|
|
|
// Sanity-check that deadline is nearing requiring a bump.
|
|
|
|
workspace, err = client.Workspace(ctx, workspace.ID)
|
2022-09-20 21:17:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.WithinDuration(t,
|
2023-11-15 15:42:27 +00:00
|
|
|
time.Now().Add(time.Minute),
|
2023-03-07 14:14:58 +00:00
|
|
|
workspace.LatestBuild.Deadline.Time,
|
|
|
|
testutil.WaitMedium,
|
2022-09-20 21:17:24 +00:00
|
|
|
)
|
|
|
|
firstDeadline := workspace.LatestBuild.Deadline.Time
|
|
|
|
|
2024-03-20 15:37:57 +00:00
|
|
|
if !maxDeadline.IsZero() {
|
2023-03-07 14:14:58 +00:00
|
|
|
require.WithinDuration(t,
|
2024-03-20 15:37:57 +00:00
|
|
|
maxDeadline,
|
2023-03-07 14:14:58 +00:00
|
|
|
workspace.LatestBuild.MaxDeadline.Time,
|
|
|
|
testutil.WaitMedium,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
require.True(t, workspace.LatestBuild.MaxDeadline.Time.IsZero())
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-09-15 08:01:32 +00:00
|
|
|
t.Helper()
|
2022-09-20 21:17:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-15 08:01:32 +00:00
|
|
|
var updatedAfter time.Time
|
2022-09-20 21:17:24 +00:00
|
|
|
// The Deadline bump occurs asynchronously.
|
|
|
|
require.Eventuallyf(t,
|
|
|
|
func() bool {
|
|
|
|
workspace, err = client.Workspace(ctx, workspace.ID)
|
|
|
|
require.NoError(t, err)
|
2023-09-15 08:01:32 +00:00
|
|
|
updatedAfter = dbtime.Now()
|
2023-11-15 15:42:27 +00:00
|
|
|
if workspace.LatestBuild.Deadline.Time.Equal(firstDeadline) {
|
2023-09-15 08:01:32 +00:00
|
|
|
updatedAfter = time.Now()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2022-09-20 21:17:24 +00:00
|
|
|
},
|
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-09-15 08:01:32 +00:00
|
|
|
require.Greater(t, workspace.LatestBuild.Deadline.Time, updatedAfter)
|
|
|
|
|
2023-03-07 14:14:58 +00:00
|
|
|
// If the workspace has a max deadline, the deadline must not exceed
|
|
|
|
// it.
|
2023-09-15 08:01:32 +00:00
|
|
|
if workspace.LatestBuild.MaxDeadline.Valid {
|
|
|
|
require.LessOrEqual(t, workspace.LatestBuild.Deadline.Time, workspace.LatestBuild.MaxDeadline.Time)
|
2023-03-07 14:14:58 +00:00
|
|
|
return
|
|
|
|
}
|
2023-11-15 15:42:27 +00:00
|
|
|
now := dbtime.Now()
|
|
|
|
zone, offset := time.Now().Zone()
|
|
|
|
t.Logf("[Zone=%s %d] originDeadline: %s, deadline: %s, now %s, (now-deadline)=%s",
|
|
|
|
zone, offset,
|
|
|
|
firstDeadline, workspace.LatestBuild.Deadline.Time, now,
|
|
|
|
now.Sub(workspace.LatestBuild.Deadline.Time),
|
|
|
|
)
|
2023-09-15 08:01:32 +00:00
|
|
|
require.WithinDuration(t, dbtime.Now().Add(ttl), workspace.LatestBuild.Deadline.Time, testutil.WaitShort)
|
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)
|
2024-03-26 17:44:31 +00:00
|
|
|
conn, err := workspacesdk.New(client).
|
|
|
|
DialAgent(ctx, resources[0].Agents[0].ID, &workspacesdk.DialAgentOptions{
|
|
|
|
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)
|
|
|
|
})
|
2023-03-07 14:14:58 +00:00
|
|
|
|
|
|
|
t.Run("NotExceedMaxDeadline", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-11-15 15:42:27 +00:00
|
|
|
// Set the max deadline to be in 30min. We bump by 1 hour, so we
|
2023-03-07 14:14:58 +00:00
|
|
|
// should expect the deadline to match the max deadline exactly.
|
2023-11-15 15:42:27 +00:00
|
|
|
client, workspace, assertBumped := setupActivityTest(t, time.Minute*30)
|
2023-03-07 14:14:58 +00:00
|
|
|
|
|
|
|
// Bump by dialing the workspace and sending traffic.
|
|
|
|
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
2024-03-26 17:44:31 +00:00
|
|
|
conn, err := workspacesdk.New(client).
|
|
|
|
DialAgent(ctx, resources[0].Agents[0].ID, &workspacesdk.DialAgentOptions{
|
|
|
|
Logger: slogtest.Make(t, nil),
|
|
|
|
})
|
2023-03-07 14:14:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Must send network traffic after a few seconds to surpass bump threshold.
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
sshConn, err := conn.SSHClient(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_ = sshConn.Close()
|
|
|
|
|
2024-03-20 15:37:57 +00:00
|
|
|
assertBumped(true)
|
2023-03-07 14:14:58 +00:00
|
|
|
})
|
2022-09-20 21:17:24 +00:00
|
|
|
}
|