- Notifications
You must be signed in to change notification settings - Fork928
feat: add API/SDK support for autostop extension#1778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
c7dff0e
8987e9e
f6cb705
759fb8a
b92c86e
597fbd3
b6fcb20
e3df43a
b12e19e
03ec987
6e63fc3
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -190,14 +190,14 @@ func TestExecutorAutostopOK(t *testing.T) { | ||
}) | ||
// Given: we have a user with a workspace | ||
workspace = mustProvisionWorkspace(t, client) | ||
) | ||
// Given: workspace is running | ||
require.Equal(t, codersdk.WorkspaceTransitionStart, workspace.LatestBuild.Transition) | ||
require.NotZero(t, workspace.LatestBuild.Deadline) | ||
// When: the autobuild executor ticks *after* thedeadline: | ||
go func() { | ||
tickCh <-workspace.LatestBuild.Deadline.Add(time.Minute) | ||
close(tickCh) | ||
}() | ||
@@ -209,6 +209,55 @@ func TestExecutorAutostopOK(t *testing.T) { | ||
require.Equal(t, codersdk.WorkspaceTransitionStop, ws.LatestBuild.Transition, "expected workspace not to be running") | ||
} | ||
func TestExecutorAutostopExtend(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
ctx = context.Background() | ||
tickCh = make(chan time.Time) | ||
client = coderdtest.New(t, &coderdtest.Options{ | ||
AutobuildTicker: tickCh, | ||
IncludeProvisionerD: true, | ||
}) | ||
// Given: we have a user with a workspace | ||
workspace = mustProvisionWorkspace(t, client) | ||
originalDeadline = workspace.LatestBuild.Deadline | ||
) | ||
// Given: workspace is running | ||
require.Equal(t, codersdk.WorkspaceTransitionStart, workspace.LatestBuild.Transition) | ||
require.NotZero(t, originalDeadline) | ||
// Given: we extend the workspace deadline | ||
err := client.PutExtendWorkspace(ctx, workspace.ID, codersdk.PutExtendWorkspaceRequest{ | ||
Deadline: originalDeadline.Add(30 * time.Minute), | ||
}) | ||
require.NoError(t, err, "extend workspace deadline") | ||
// When: the autobuild executor ticks *after* the original deadline: | ||
go func() { | ||
tickCh <- originalDeadline.Add(time.Minute) | ||
}() | ||
// Then: nothing should happen | ||
<-time.After(5 * time.Second) | ||
ws := mustWorkspace(t, client, workspace.ID) | ||
require.Equal(t, workspace.LatestBuild.ID, ws.LatestBuild.ID, "expected no further workspace builds to occur") | ||
require.Equal(t, codersdk.WorkspaceTransitionStart, ws.LatestBuild.Transition, "expected workspace to be running") | ||
// When: the autobuild executor ticks after the *new* deadline: | ||
go func() { | ||
tickCh <- ws.LatestBuild.Deadline.Add(time.Minute) | ||
close(tickCh) | ||
}() | ||
// Then: the workspace should be stopped | ||
<-time.After(5 * time.Second) | ||
ws = mustWorkspace(t, client, workspace.ID) | ||
require.NotEqual(t, workspace.LatestBuild.ID, ws.LatestBuild.ID, "expected a workspace build to occur") | ||
require.Equal(t, codersdk.ProvisionerJobSucceeded, ws.LatestBuild.Job.Status, "expected provisioner job to have succeeded") | ||
require.Equal(t, codersdk.WorkspaceTransitionStop, ws.LatestBuild.Transition, "expected workspace not to be running") | ||
} | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
func TestExecutorAutostopAlreadyStopped(t *testing.T) { | ||
t.Parallel() | ||
@@ -222,15 +271,14 @@ func TestExecutorAutostopAlreadyStopped(t *testing.T) { | ||
workspace = mustProvisionWorkspace(t, client, func(cwr *codersdk.CreateWorkspaceRequest) { | ||
cwr.AutostartSchedule = nil | ||
}) | ||
) | ||
// Given: workspace is stopped | ||
workspace = mustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop) | ||
// When: the autobuild executor ticks past the TTL | ||
go func() { | ||
tickCh <-workspace.LatestBuild.Deadline.Add(time.Minute) | ||
close(tickCh) | ||
}() | ||
@@ -264,7 +312,7 @@ func TestExecutorAutostopNotEnabled(t *testing.T) { | ||
// When: the autobuild executor ticks past the TTL | ||
go func() { | ||
tickCh <-workspace.LatestBuild.Deadline.Add(time.Minute) | ||
close(tickCh) | ||
}() | ||
@@ -352,7 +400,7 @@ func TestExecutorWorkspaceAutostartTooEarly(t *testing.T) { | ||
require.Equal(t, codersdk.WorkspaceTransitionStart, ws.LatestBuild.Transition, "expected workspace to be running") | ||
} | ||
funcTestExecutorWorkspaceAutostopBeforeDeadline(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
@@ -367,7 +415,7 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) { | ||
// When: the autobuild executor ticks before the TTL | ||
go func() { | ||
tickCh <-workspace.LatestBuild.Deadline.Add(-1 * time.Minute) | ||
close(tickCh) | ||
}() | ||
@@ -378,6 +426,38 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) { | ||
require.Equal(t, codersdk.WorkspaceTransitionStart, ws.LatestBuild.Transition, "expected workspace to be running") | ||
} | ||
func TestExecutorWorkspaceAutostopNoWaitChangedMyMind(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
ctx = context.Background() | ||
tickCh = make(chan time.Time) | ||
client = coderdtest.New(t, &coderdtest.Options{ | ||
AutobuildTicker: tickCh, | ||
IncludeProvisionerD: true, | ||
}) | ||
// Given: we have a user with a workspace | ||
workspace = mustProvisionWorkspace(t, client) | ||
) | ||
// Given: the user changes their mind and decides their workspace should not auto-stop | ||
err := client.UpdateWorkspaceTTL(ctx, workspace.ID, codersdk.UpdateWorkspaceTTLRequest{TTL: nil}) | ||
require.NoError(t, err) | ||
// When: the autobuild executor ticks after the deadline | ||
go func() { | ||
tickCh <- workspace.LatestBuild.Deadline.Add(time.Minute) | ||
close(tickCh) | ||
}() | ||
// Then: the workspace should still stop - sorry! | ||
<-time.After(5 * time.Second) | ||
ws := mustWorkspace(t, client, workspace.ID) | ||
require.NotEqual(t, workspace.LatestBuild.ID, ws.LatestBuild.ID, "expected a workspace build to occur") | ||
require.Equal(t, codersdk.ProvisionerJobSucceeded, ws.LatestBuild.Job.Status, "expected provisioner job to have succeeded") | ||
require.Equal(t, codersdk.WorkspaceTransitionStop, ws.LatestBuild.Transition, "expected workspace not to be running") | ||
} | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
func TestExecutorAutostartMultipleOK(t *testing.T) { | ||
if os.Getenv("DB") == "" { | ||
t.Skip(`This test only really works when using a "real" database, similar to a HA setup`) | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE ONLY workspace_builds DROP COLUMN deadline; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE ONLY workspace_builds ADD COLUMN deadline TIMESTAMPTZ NOT NULL DEFAULT TIMESTAMPTZ '0001-01-01 00:00:00+00:00'; |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.