Skip to content

feat: add lifecycle.Executor to manage autostart and autostop #1183

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
May 11, 2022
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a145d6d
feat: add lifecycle.Executor to autostart workspaces.
johnstcn Apr 19, 2022
8f401ca
refactor: do not expose Store in coderdtest.Options
johnstcn Apr 26, 2022
6d8f5fe
fixup! refactor: do not expose Store in coderdtest.Options
johnstcn Apr 26, 2022
cfd0d1e
stop accessing db directly, only query workspaces with autostart enabled
johnstcn Apr 26, 2022
ce63810
refactor unit tests, add tests for autostop
johnstcn Apr 26, 2022
579f362
make the new tests pass with some refactoring
johnstcn Apr 30, 2022
6e88f67
gitignore *.swp
johnstcn Apr 30, 2022
2b1a383
remove unused methods
johnstcn Apr 30, 2022
f31588e
fixup! remove unused methods
johnstcn Apr 30, 2022
80e0581
fix: range over channel, add continue to default switch case
johnstcn May 9, 2022
d176478
add test for deleted workspace
johnstcn May 9, 2022
bd97c1a
workspaces.sql: remove unused methods
johnstcn May 10, 2022
0931d25
unexport test helper methods
johnstcn May 10, 2022
faebe2e
chore: rename package autostart/lifecycle to lifecycle/executor
johnstcn May 10, 2022
abc0854
add test to ensure workspaces are not autostarted before time
johnstcn May 10, 2022
e53946a
wire up executor to coderd
johnstcn May 10, 2022
364a27c
fix: executor: skip workspaces whose last build was not successful
johnstcn May 10, 2022
e96414f
address PR comments
johnstcn May 11, 2022
b5bf50e
add goleak TestMain
johnstcn May 11, 2022
d37cc2b
fmt
johnstcn May 11, 2022
d11f5d7
mustTransitionWorkspace should return the updated workspace
johnstcn May 11, 2022
f6388b4
remove usage of require.Eventually/Never which is flaky on Windows
johnstcn May 11, 2022
fd0f8a3
make lifecycle executor spawn a new goroutine automatically
johnstcn May 11, 2022
7b6f2e1
rename unit tests
johnstcn May 11, 2022
a7143bd
s/doBuild/build
johnstcn May 11, 2022
7d9b696
rename parent package lifecycle to autobuild
johnstcn May 11, 2022
5cba737
add unit test for behaviour with an updated template
johnstcn May 11, 2022
7627372
add ticket to reference TODO
johnstcn May 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor unit tests, add tests for autostop
  • Loading branch information
johnstcn committed May 10, 2022
commit ce638102dd65791e7e14693c7e853b965e8f41ce
374 changes: 241 additions & 133 deletions coderd/autostart/lifecycle/lifecycle_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,146 +10,254 @@ import (
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/codersdk"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

func Test_Executor_Run(t *testing.T) {
func Test_Executor_Autostart_OK(t *testing.T) {
t.Parallel()

t.Run("OK", func(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
_ = coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
)
// Given: workspace is stopped
build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, codersdk.CreateWorkspaceBuildRequest{
TemplateVersionID: template.ActiveVersionID,
Transition: database.WorkspaceTransitionStop,
var (
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
require.NoError(t, err, "stop workspace")
// Given: we wait for the stop to complete
_ = coderdtest.AwaitWorkspaceBuildJob(t, client, build.ID)

// Given: we update the workspace with its new state
workspace = coderdtest.MustWorkspace(t, client, workspace.ID)
// Given: we ensure the workspace is now in a stopped state
require.Equal(t, database.WorkspaceTransitionStop, workspace.LatestBuild.Transition)

// Given: the workspace initially has autostart disabled
require.Empty(t, workspace.AutostartSchedule)

// When: we enable workspace autostart
sched, err := schedule.Weekly("* * * * *")
require.NoError(t, err)
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: sched.String(),
}))

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should be started
require.Eventually(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.Job.Status == codersdk.ProvisionerJobSucceeded &&
ws.LatestBuild.Transition == database.WorkspaceTransitionStart
}, 5*time.Second, 250*time.Millisecond)
})
// Given: we have a user with a workspace
workspace = MustProvisionWorkspace(t, client)
)
// Given: workspace is stopped
MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)

t.Run("AlreadyRunning", func(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
_ = coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
)

// Given: we ensure the workspace is now in a stopped state
require.Equal(t, database.WorkspaceTransitionStart, workspace.LatestBuild.Transition)

// Given: the workspace initially has autostart disabled
require.Empty(t, workspace.AutostartSchedule)

// When: we enable workspace autostart
sched, err := schedule.Weekly("* * * * *")
require.NoError(t, err)
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: sched.String(),
}))

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should not be started.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID != workspace.LatestBuild.ID
}, 5*time.Second, 250*time.Millisecond)
})
// Given: the workspace initially has autostart disabled
require.Empty(t, workspace.AutostartSchedule)

// When: we enable workspace autostart
sched, err := schedule.Weekly("* * * * *")
require.NoError(t, err)
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: sched.String(),
}))

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should be started
require.Eventually(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.Job.Status == codersdk.ProvisionerJobSucceeded &&
ws.LatestBuild.Transition == database.WorkspaceTransitionStart
}, 5*time.Second, 250*time.Millisecond)
}

func Test_Executor_Autostart_AlreadyRunning(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
workspace = MustProvisionWorkspace(t, client)
)

// Given: we ensure the workspace is running
require.Equal(t, database.WorkspaceTransitionStart, workspace.LatestBuild.Transition)

// Given: the workspace initially has autostart disabled
require.Empty(t, workspace.AutostartSchedule)

// When: we enable workspace autostart
sched, err := schedule.Weekly("* * * * *")
require.NoError(t, err)
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: sched.String(),
}))

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should not be started.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID != workspace.LatestBuild.ID && ws.LatestBuild.Transition == database.WorkspaceTransitionStart
}, 5*time.Second, 250*time.Millisecond)
}

func Test_Executor_Autostart_NotEnabled(t *testing.T) {
t.Parallel()

var (
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
workspace = MustProvisionWorkspace(t, client)
)

// Given: workspace is stopped
MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)

// Given: the workspace has autostart disabled
require.Empty(t, workspace.AutostartSchedule)

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should not be started.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID != workspace.LatestBuild.ID && ws.LatestBuild.Transition == database.WorkspaceTransitionStart
}, 5*time.Second, 250*time.Millisecond)
}

func Test_Executor_Autostop_OK(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
workspace = MustProvisionWorkspace(t, client)
)
// Given: workspace is running
require.Equal(t, database.WorkspaceTransitionStart, workspace.LatestBuild.Transition)

t.Run("NotEnabled", func(t *testing.T) {
t.Parallel()

var (
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
_ = coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
)

// Given: we ensure the workspace is now in a stopped state
require.Equal(t, database.WorkspaceTransitionStart, workspace.LatestBuild.Transition)

// Given: the workspace has autostart disabled
require.Empty(t, workspace.AutostartSchedule)

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should not be started.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID != workspace.LatestBuild.ID
}, 5*time.Second, 250*time.Millisecond)
// Given: the workspace initially has autostop disabled
require.Empty(t, workspace.AutostopSchedule)

// When: we enable workspace autostop
sched, err := schedule.Weekly("* * * * *")
require.NoError(t, err)
require.NoError(t, client.UpdateWorkspaceAutostop(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostopRequest{
Schedule: sched.String(),
}))

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should be started
require.Eventually(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID != workspace.LatestBuild.ID && ws.LatestBuild.Transition == database.WorkspaceTransitionStart
}, 5*time.Second, 250*time.Millisecond)
}
func Test_Executor_Autostop_AlreadyStopped(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
workspace = MustProvisionWorkspace(t, client)
)

// Given: workspace is stopped
MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)

// Given: the workspace initially has autostart disabled
require.Empty(t, workspace.AutostopSchedule)

// When: we enable workspace autostart
sched, err := schedule.Weekly("* * * * *")
require.NoError(t, err)
require.NoError(t, client.UpdateWorkspaceAutostop(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostopRequest{
Schedule: sched.String(),
}))

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should not be stopped.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID == workspace.LatestBuild.ID && ws.LatestBuild.Transition == database.WorkspaceTransitionStop
}, 5*time.Second, 250*time.Millisecond)
}

func Test_Executor_Autostop_NotEnabled(t *testing.T) {
t.Parallel()

var (
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
workspace = MustProvisionWorkspace(t, client)
)

// Given: workspace is running
require.Equal(t, database.WorkspaceTransitionStart, workspace.LatestBuild.Transition)

// Given: the workspace has autostop disabled
require.Empty(t, workspace.AutostopSchedule)

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should not be stopped.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID == workspace.LatestBuild.ID && ws.LatestBuild.Transition == database.WorkspaceTransitionStop
}, 5*time.Second, 250*time.Millisecond)
}

func MustProvisionWorkspace(t *testing.T, client *codersdk.Client) codersdk.Workspace {
t.Helper()
coderdtest.NewProvisionerDaemon(t, client)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
ws := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, ws.LatestBuild.ID)
return coderdtest.MustWorkspace(t, client, ws.ID)
}

func MustTransitionWorkspace(t *testing.T, client *codersdk.Client, workspaceID uuid.UUID, from, to database.WorkspaceTransition) {
t.Helper()
ctx := context.Background()
workspace, err := client.Workspace(ctx, workspaceID)
require.NoError(t, err, "unexpected error fetching workspace")
require.Equal(t, workspace.LatestBuild.Transition, from, "expected workspace state: %s got: %s", from, workspace.LatestBuild.Transition)

template, err := client.Template(ctx, workspace.TemplateID)
require.NoError(t, err, "fetch workspace template")

build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, codersdk.CreateWorkspaceBuildRequest{
TemplateVersionID: template.ActiveVersionID,
Transition: to,
})
require.NoError(t, err, "unexpected error transitioning workspace to %s", to)

_ = coderdtest.AwaitWorkspaceBuildJob(t, client, build.ID)

updated := coderdtest.MustWorkspace(t, client, workspace.ID)
require.Equal(t, to, updated.LatestBuild.Transition, "expected workspace to be in state %s but got %s", to, updated.LatestBuild.Transition)
}