2023-06-22 00:33:22 -04:00
package autobuild_test
2022-05-11 23:03:02 +01:00
import (
"context"
2023-03-10 10:39:02 -06:00
"os"
2022-05-11 23:03:02 +01:00
"testing"
"time"
2023-03-10 10:39:02 -06:00
"github.com/google/uuid"
2023-04-04 22:48:35 +10:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
2023-03-10 10:39:02 -06:00
2023-06-22 00:33:22 -04:00
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/coderd/autobuild"
2022-05-11 23:03:02 +01:00
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/database"
2023-03-08 01:14:58 +11:00
"github.com/coder/coder/coderd/schedule"
2022-06-02 11:23:34 +01:00
"github.com/coder/coder/coderd/util/ptr"
2022-05-11 23:03:02 +01:00
"github.com/coder/coder/codersdk"
2023-01-30 15:34:38 +01:00
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
2022-05-11 23:03:02 +01:00
)
func TestExecutorAutostartOK ( t * testing . T ) {
t . Parallel ()
var (
2022-06-07 13:37:45 +01:00
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-06-07 13:37:45 +01:00
// Given: we have a user with a workspace that has autostart enabled
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
})
2022-05-11 23:03:02 +01:00
)
// Given: workspace is stopped
2022-06-07 13:37:45 +01:00
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
2022-05-11 23:03:02 +01:00
2022-06-07 13:37:45 +01:00
// When: the autobuild executor ticks after the scheduled time
2022-05-11 23:03:02 +01:00
go func () {
2022-06-07 13:37:45 +01:00
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
2022-05-30 20:23:36 +01:00
// Then: the workspace should eventually be started
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 1 )
assert . Contains ( t , stats . Transitions , workspace . ID )
assert . Equal ( t , database . WorkspaceTransitionStart , stats . Transitions [ workspace . ID ])
2022-06-17 13:41:11 -04:00
workspace = coderdtest . MustWorkspace ( t , client , workspace . ID )
assert . Equal ( t , codersdk . BuildReasonAutostart , workspace . LatestBuild . Reason )
2022-05-11 23:03:02 +01:00
}
func TestExecutorAutostartTemplateUpdated ( t * testing . T ) {
t . Parallel ()
var (
2022-06-07 13:37:45 +01:00
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
2022-05-30 20:23:36 +01:00
ctx = context . Background ()
err error
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-06-07 13:37:45 +01:00
// Given: we have a user with a workspace that has autostart enabled
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
})
2022-05-11 23:03:02 +01:00
)
// Given: workspace is stopped
2022-06-07 13:37:45 +01:00
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
2022-05-11 23:03:02 +01:00
// Given: the workspace template has been updated
2022-05-16 15:29:27 -05:00
orgs , err := client . OrganizationsByUser ( ctx , workspace . OwnerID . String ())
2022-05-11 23:03:02 +01:00
require . NoError ( t , err )
require . Len ( t , orgs , 1 )
newVersion := coderdtest . UpdateTemplateVersion ( t , client , orgs [ 0 ]. ID , nil , workspace . TemplateID )
coderdtest . AwaitTemplateVersionJob ( t , client , newVersion . ID )
require . NoError ( t , client . UpdateActiveTemplateVersion ( ctx , workspace . TemplateID , codersdk . UpdateActiveTemplateVersion {
ID : newVersion . ID ,
}))
2022-06-07 13:37:45 +01:00
// When: the autobuild executor ticks after the scheduled time
2022-05-11 23:03:02 +01:00
go func () {
2022-06-07 13:37:45 +01:00
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
2023-04-11 16:14:35 +02:00
// Then: the workspace should be started using the previous template version, and not the updated version.
2022-05-30 20:23:36 +01:00
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 1 )
assert . Contains ( t , stats . Transitions , workspace . ID )
assert . Equal ( t , database . WorkspaceTransitionStart , stats . Transitions [ workspace . ID ])
2022-06-07 13:37:45 +01:00
ws := coderdtest . MustWorkspace ( t , client , workspace . ID )
2023-04-11 16:14:35 +02:00
assert . Equal ( t , workspace . LatestBuild . TemplateVersionID , ws . LatestBuild . TemplateVersionID , "expected workspace build to be using the old template version" )
2022-05-11 23:03:02 +01:00
}
func TestExecutorAutostartAlreadyRunning ( t * testing . T ) {
t . Parallel ()
var (
2022-06-07 13:37:45 +01:00
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-06-07 13:37:45 +01:00
// Given: we have a user with a workspace that has autostart enabled
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
})
2022-05-11 23:03:02 +01:00
)
// Given: we ensure the workspace is running
2022-05-19 13:04:44 -05:00
require . Equal ( t , codersdk . WorkspaceTransitionStart , workspace . LatestBuild . Transition )
2022-05-11 23:03:02 +01:00
// When: the autobuild executor ticks
go func () {
2022-06-07 13:37:45 +01:00
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
// Then: the workspace should not be started.
2022-05-30 20:23:36 +01:00
stats := <- statsCh
require . NoError ( t , stats . Error )
require . Len ( t , stats . Transitions , 0 )
2022-05-11 23:03:02 +01:00
}
func TestExecutorAutostartNotEnabled ( t * testing . T ) {
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-06-07 13:37:45 +01:00
// Given: we have a user with a workspace that does not have autostart enabled
2022-05-23 23:31:41 +01:00
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = nil
})
2022-05-11 23:03:02 +01:00
)
2022-05-23 23:31:41 +01:00
// Given: workspace does not have autostart enabled
require . Empty ( t , workspace . AutostartSchedule )
2022-05-11 23:03:02 +01:00
// Given: workspace is stopped
2022-06-07 13:37:45 +01:00
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
2022-05-11 23:03:02 +01:00
2022-06-07 13:37:45 +01:00
// When: the autobuild executor ticks way into the future
2022-05-11 23:03:02 +01:00
go func () {
2022-06-07 13:37:45 +01:00
tickCh <- workspace . LatestBuild . CreatedAt . Add ( 24 * time . Hour )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
// Then: the workspace should not be started.
2022-05-30 20:23:36 +01:00
stats := <- statsCh
require . NoError ( t , stats . Error )
require . Len ( t , stats . Transitions , 0 )
2022-05-11 23:03:02 +01:00
}
func TestExecutorAutostopOK ( t * testing . T ) {
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
// Given: we have a user with a workspace
workspace = mustProvisionWorkspace ( t , client )
)
// Given: workspace is running
2022-05-19 13:04:44 -05:00
require . Equal ( t , codersdk . WorkspaceTransitionStart , workspace . LatestBuild . Transition )
2022-05-26 18:08:11 +01:00
require . NotZero ( t , workspace . LatestBuild . Deadline )
2022-05-11 23:03:02 +01:00
2022-05-26 18:08:11 +01:00
// When: the autobuild executor ticks *after* the deadline:
2022-05-11 23:03:02 +01:00
go func () {
2022-08-25 19:10:42 +03:00
tickCh <- workspace . LatestBuild . Deadline . Time . Add ( time . Minute )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
2022-05-19 20:09:27 +01:00
// Then: the workspace should be stopped
2022-05-30 20:23:36 +01:00
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 1 )
assert . Contains ( t , stats . Transitions , workspace . ID )
assert . Equal ( t , database . WorkspaceTransitionStop , stats . Transitions [ workspace . ID ])
2022-06-17 13:41:11 -04:00
workspace = coderdtest . MustWorkspace ( t , client , workspace . ID )
assert . Equal ( t , codersdk . BuildReasonAutostop , workspace . LatestBuild . Reason )
2022-05-11 23:03:02 +01:00
}
2022-05-26 18:08:11 +01:00
func TestExecutorAutostopExtend ( t * testing . T ) {
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
ctx = context . Background ()
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-26 18:08:11 +01:00
})
// 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
2022-08-25 19:10:42 +03:00
newDeadline := originalDeadline . Time . Add ( 30 * time . Minute )
2022-05-26 18:08:11 +01:00
err := client . PutExtendWorkspace ( ctx , workspace . ID , codersdk . PutExtendWorkspaceRequest {
2022-05-30 20:23:36 +01:00
Deadline : newDeadline ,
2022-05-26 18:08:11 +01:00
})
require . NoError ( t , err , "extend workspace deadline" )
// When: the autobuild executor ticks *after* the original deadline:
go func () {
2022-08-25 19:10:42 +03:00
tickCh <- originalDeadline . Time . Add ( time . Minute )
2022-05-26 18:08:11 +01:00
}()
2022-05-30 20:23:36 +01:00
// Then: nothing should happen and the workspace should stay running
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 0 )
2022-05-26 18:08:11 +01:00
// When: the autobuild executor ticks after the *new* deadline:
go func () {
2022-05-30 20:23:36 +01:00
tickCh <- newDeadline . Add ( time . Minute )
2022-05-26 18:08:11 +01:00
close ( tickCh )
}()
// Then: the workspace should be stopped
2022-05-30 20:23:36 +01:00
stats = <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 1 )
assert . Contains ( t , stats . Transitions , workspace . ID )
assert . Equal ( t , database . WorkspaceTransitionStop , stats . Transitions [ workspace . ID ])
2022-05-26 18:08:11 +01:00
}
2022-05-11 23:03:02 +01:00
func TestExecutorAutostopAlreadyStopped ( t * testing . T ) {
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-05-23 23:31:41 +01:00
// Given: we have a user with a workspace (disabling autostart)
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = nil
})
2022-05-11 23:03:02 +01:00
)
// Given: workspace is stopped
2022-06-07 13:37:45 +01:00
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
2022-05-11 23:03:02 +01:00
2022-05-19 20:09:27 +01:00
// When: the autobuild executor ticks past the TTL
2022-05-11 23:03:02 +01:00
go func () {
2022-08-25 19:10:42 +03:00
tickCh <- workspace . LatestBuild . Deadline . Time . Add ( time . Minute )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
2022-05-30 20:23:36 +01:00
// Then: the workspace should remain stopped and no build should happen.
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 0 )
2022-05-11 23:03:02 +01:00
}
func TestExecutorAutostopNotEnabled ( t * testing . T ) {
t . Parallel ()
var (
2022-06-07 13:37:45 +01:00
ctx = context . Background ()
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-06-07 13:37:45 +01:00
// Given: we have a user with a workspace
workspace = mustProvisionWorkspace ( t , client )
2022-05-11 23:03:02 +01:00
)
2022-05-23 23:31:41 +01:00
// Given: workspace has no TTL set
2022-06-07 13:37:45 +01:00
err := client . UpdateWorkspaceTTL ( ctx , workspace . ID , codersdk . UpdateWorkspaceTTLRequest { TTLMillis : nil })
require . NoError ( t , err )
workspace , err = client . Workspace ( ctx , workspace . ID )
require . NoError ( t , err )
2022-06-02 11:23:34 +01:00
require . Nil ( t , workspace . TTLMillis )
2022-05-23 23:31:41 +01:00
2022-06-14 17:09:24 +01:00
// TODO(cian): need to stop and start the workspace as we do not update the deadline. See: #2229
2022-06-07 13:37:45 +01:00
coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStop , database . WorkspaceTransitionStart )
2022-05-11 23:03:02 +01:00
// Given: workspace is running
2022-05-19 13:04:44 -05:00
require . Equal ( t , codersdk . WorkspaceTransitionStart , workspace . LatestBuild . Transition )
2022-05-11 23:03:02 +01:00
2022-05-23 23:31:41 +01:00
// When: the autobuild executor ticks past the TTL
2022-05-11 23:03:02 +01:00
go func () {
2022-08-25 19:10:42 +03:00
tickCh <- workspace . LatestBuild . Deadline . Time . Add ( time . Minute )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
// Then: the workspace should not be stopped.
2022-05-30 20:23:36 +01:00
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 0 )
2022-05-11 23:03:02 +01:00
}
func TestExecutorWorkspaceDeleted ( t * testing . T ) {
t . Parallel ()
var (
2022-06-07 13:37:45 +01:00
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-06-07 13:37:45 +01:00
// Given: we have a user with a workspace that has autostart enabled
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
})
2022-05-11 23:03:02 +01:00
)
// Given: workspace is deleted
2022-06-07 13:37:45 +01:00
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionDelete )
2022-05-11 23:03:02 +01:00
// When: the autobuild executor ticks
go func () {
2022-06-07 13:37:45 +01:00
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
// Then: nothing should happen
2022-05-30 20:23:36 +01:00
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 0 )
2022-05-11 23:03:02 +01:00
}
2022-05-19 20:09:27 +01:00
func TestExecutorWorkspaceAutostartTooEarly ( t * testing . T ) {
2022-05-11 23:03:02 +01:00
t . Parallel ()
var (
2022-06-07 13:37:45 +01:00
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-06-07 13:37:45 +01:00
// futureTime = time.Now().Add(time.Hour)
// futureTimeCron = fmt.Sprintf("%d %d * * *", futureTime.Minute(), futureTime.Hour())
2022-05-23 23:31:41 +01:00
// Given: we have a user with a workspace configured to autostart some time in the future
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
2022-06-07 13:37:45 +01:00
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
2022-05-23 23:31:41 +01:00
})
2022-05-11 23:03:02 +01:00
)
2022-06-07 13:37:45 +01:00
// When: the autobuild executor ticks before the next scheduled time
2022-05-19 20:09:27 +01:00
go func () {
2022-06-07 13:37:45 +01:00
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt ). Add ( - time . Minute )
2022-05-19 20:09:27 +01:00
close ( tickCh )
}()
// Then: nothing should happen
2022-05-30 20:23:36 +01:00
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 0 )
2022-05-19 20:09:27 +01:00
}
2022-05-26 18:08:11 +01:00
func TestExecutorWorkspaceAutostopBeforeDeadline ( t * testing . T ) {
2022-05-19 20:09:27 +01:00
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-19 20:09:27 +01:00
})
// Given: we have a user with a workspace
workspace = mustProvisionWorkspace ( t , client )
)
2022-05-23 23:31:41 +01:00
// When: the autobuild executor ticks before the TTL
2022-05-11 23:03:02 +01:00
go func () {
2022-08-25 19:10:42 +03:00
tickCh <- workspace . LatestBuild . Deadline . Time . Add ( - 1 * time . Minute )
2022-05-11 23:03:02 +01:00
close ( tickCh )
}()
// Then: nothing should happen
2022-05-30 20:23:36 +01:00
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 0 )
2022-05-11 23:03:02 +01:00
}
2022-05-26 18:08:11 +01:00
func TestExecutorWorkspaceAutostopNoWaitChangedMyMind ( t * testing . T ) {
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
ctx = context . Background ()
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
2022-05-26 18:08:11 +01:00
})
// Given: we have a user with a workspace
workspace = mustProvisionWorkspace ( t , client )
)
2023-04-04 22:48:35 +10:00
// Given: the user changes their mind and decides their workspace should not autostop
2022-06-02 11:23:34 +01:00
err := client . UpdateWorkspaceTTL ( ctx , workspace . ID , codersdk . UpdateWorkspaceTTLRequest { TTLMillis : nil })
2022-05-26 18:08:11 +01:00
require . NoError ( t , err )
2022-06-14 17:09:24 +01:00
// Then: the deadline should still be the original value
2022-06-09 22:10:24 +01:00
updated := coderdtest . MustWorkspace ( t , client , workspace . ID )
2022-08-25 19:10:42 +03:00
assert . WithinDuration ( t , workspace . LatestBuild . Deadline . Time , updated . LatestBuild . Deadline . Time , time . Minute )
2022-06-09 22:10:24 +01:00
// When: the autobuild executor ticks after the original deadline
2022-05-26 18:08:11 +01:00
go func () {
2022-08-25 19:10:42 +03:00
tickCh <- workspace . LatestBuild . Deadline . Time . Add ( time . Minute )
2022-06-09 22:10:24 +01:00
}()
2022-06-14 17:09:24 +01:00
// Then: the workspace should stop
2022-06-09 22:10:24 +01:00
stats := <- statsCh
assert . NoError ( t , stats . Error )
2022-06-14 17:09:24 +01:00
assert . Len ( t , stats . Transitions , 1 )
assert . Equal ( t , stats . Transitions [ workspace . ID ], database . WorkspaceTransitionStop )
// Wait for stop to complete
updated = coderdtest . MustWorkspace ( t , client , workspace . ID )
_ = coderdtest . AwaitWorkspaceBuildJob ( t , client , updated . LatestBuild . ID )
// Start the workspace again
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStop , database . WorkspaceTransitionStart )
2022-06-09 22:10:24 +01:00
2023-04-04 22:48:35 +10:00
// Given: the user changes their mind again and wants to enable autostop
2022-06-09 22:10:24 +01:00
newTTL := 8 * time . Hour
err = client . UpdateWorkspaceTTL ( ctx , workspace . ID , codersdk . UpdateWorkspaceTTLRequest { TTLMillis : ptr . Ref ( newTTL . Milliseconds ())})
require . NoError ( t , err )
2022-06-14 17:09:24 +01:00
// Then: the deadline should remain at the zero value
2022-06-09 22:10:24 +01:00
updated = coderdtest . MustWorkspace ( t , client , workspace . ID )
2022-06-14 17:09:24 +01:00
assert . Zero ( t , updated . LatestBuild . Deadline )
2022-06-09 22:10:24 +01:00
// When: the relentless onward march of time continues
go func () {
2022-08-25 19:10:42 +03:00
tickCh <- workspace . LatestBuild . Deadline . Time . Add ( newTTL + time . Minute )
2022-05-26 18:08:11 +01:00
close ( tickCh )
}()
2022-06-14 17:09:24 +01:00
// Then: the workspace should not stop
2022-06-09 22:10:24 +01:00
stats = <- statsCh
2022-05-30 20:23:36 +01:00
assert . NoError ( t , stats . Error )
2022-06-14 17:09:24 +01:00
assert . Len ( t , stats . Transitions , 0 )
2022-05-26 18:08:11 +01:00
}
2022-05-13 17:14:24 +01:00
func TestExecutorAutostartMultipleOK ( t * testing . T ) {
2023-03-10 10:39:02 -06:00
if os . Getenv ( "DB" ) == "" {
2022-05-13 17:14:24 +01:00
t . Skip ( `This test only really works when using a "real" database, similar to a HA setup` )
}
t . Parallel ()
var (
2022-06-07 13:37:45 +01:00
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
tickCh2 = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh1 = make ( chan autobuild . Stats )
statsCh2 = make ( chan autobuild . Stats )
2022-05-30 20:23:36 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh1 ,
2022-05-13 17:14:24 +01:00
})
_ = coderdtest . New ( t , & coderdtest . Options {
2022-09-04 11:28:09 -05:00
AutobuildTicker : tickCh2 ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh2 ,
2022-05-13 17:14:24 +01:00
})
2022-05-23 23:31:41 +01:00
// Given: we have a user with a workspace that has autostart enabled (default)
2022-06-07 13:37:45 +01:00
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
})
2022-05-13 17:14:24 +01:00
)
// Given: workspace is stopped
2022-06-07 13:37:45 +01:00
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
2022-05-13 17:14:24 +01:00
2022-06-07 13:37:45 +01:00
// When: the autobuild executor ticks past the scheduled time
2022-05-13 17:14:24 +01:00
go func () {
2022-06-07 13:37:45 +01:00
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
tickCh2 <- sched . Next ( workspace . LatestBuild . CreatedAt )
2022-05-13 17:14:24 +01:00
close ( tickCh )
close ( tickCh2 )
}()
2022-05-30 20:23:36 +01:00
// Then: the workspace should eventually be started
stats1 := <- statsCh1
assert . NoError ( t , stats1 . Error )
assert . Len ( t , stats1 . Transitions , 1 )
assert . Contains ( t , stats1 . Transitions , workspace . ID )
assert . Equal ( t , database . WorkspaceTransitionStart , stats1 . Transitions [ workspace . ID ])
2022-05-18 09:33:33 -07:00
2022-05-30 20:23:36 +01:00
// Then: the other executor should not have done anything
stats2 := <- statsCh2
assert . NoError ( t , stats2 . Error )
assert . Len ( t , stats2 . Transitions , 0 )
2022-05-13 17:14:24 +01:00
}
2023-01-30 15:34:38 +01:00
func TestExecutorAutostartWithParameters ( t * testing . T ) {
t . Parallel ()
const (
stringParameterName = "string_parameter"
stringParameterValue = "abc"
numberParameterName = "number_parameter"
numberParameterValue = "7"
)
var (
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2023-01-30 15:34:38 +01:00
client = coderdtest . New ( t , & coderdtest . Options {
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
})
richParameters = [] * proto . RichParameter {
{ Name : stringParameterName , Type : "string" , Mutable : true },
{ Name : numberParameterName , Type : "number" , Mutable : true },
}
// Given: we have a user with a workspace that has autostart enabled
workspace = mustProvisionWorkspaceWithParameters ( t , client , richParameters , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
cwr . RichParameterValues = [] codersdk . WorkspaceBuildParameter {
{
Name : stringParameterName ,
Value : stringParameterValue ,
},
{
Name : numberParameterName ,
Value : numberParameterValue ,
},
}
})
)
// Given: workspace is stopped
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
// When: the autobuild executor ticks after the scheduled time
go func () {
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
close ( tickCh )
}()
// Then: the workspace with parameters should eventually be started
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 1 )
assert . Contains ( t , stats . Transitions , workspace . ID )
assert . Equal ( t , database . WorkspaceTransitionStart , stats . Transitions [ workspace . ID ])
workspace = coderdtest . MustWorkspace ( t , client , workspace . ID )
mustWorkspaceParameters ( t , client , workspace . LatestBuild . ID )
}
2023-04-04 22:48:35 +10:00
func TestExecutorAutostartTemplateDisabled ( t * testing . T ) {
t . Parallel ()
var (
sched = mustSchedule ( t , "CRON_TZ=UTC 0 * * * *" )
tickCh = make ( chan time . Time )
2023-06-22 00:33:22 -04:00
statsCh = make ( chan autobuild . Stats )
2023-04-04 22:48:35 +10:00
client = coderdtest . New ( t , & coderdtest . Options {
AutobuildTicker : tickCh ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statsCh ,
TemplateScheduleStore : schedule . MockTemplateScheduleStore {
GetFn : func ( _ context . Context , _ database . Store , _ uuid . UUID ) ( schedule . TemplateScheduleOptions , error ) {
return schedule . TemplateScheduleOptions {
UserAutostartEnabled : false ,
UserAutostopEnabled : true ,
DefaultTTL : 0 ,
2023-07-20 06:35:41 -07:00
RestartRequirement : schedule . TemplateRestartRequirement {},
2023-04-04 22:48:35 +10:00
}, nil
},
},
})
// futureTime = time.Now().Add(time.Hour)
// futureTimeCron = fmt.Sprintf("%d %d * * *", futureTime.Minute(), futureTime.Hour())
// Given: we have a user with a workspace configured to autostart some time in the future
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
cwr . AutostartSchedule = ptr . Ref ( sched . String ())
})
)
// Given: workspace is stopped
workspace = coderdtest . MustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
// When: the autobuild executor ticks before the next scheduled time
go func () {
tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt ). Add ( time . Minute )
close ( tickCh )
}()
// Then: nothing should happen
stats := <- statsCh
assert . NoError ( t , stats . Error )
assert . Len ( t , stats . Transitions , 0 )
}
2023-07-02 21:29:52 -05:00
// TestExecutorFailedWorkspace test AGPL functionality which mainly
// ensures that autostop actions as a result of a failed workspace
// build do not trigger.
2023-06-22 00:33:22 -04:00
// For enterprise functionality see enterprise/coderd/workspaces_test.go
func TestExecutorFailedWorkspace ( t * testing . T ) {
t . Parallel ()
// Test that an AGPL TemplateScheduleStore properly disables
// functionality.
t . Run ( "OK" , func ( t * testing . T ) {
t . Parallel ()
var (
ticker = make ( chan time . Time )
statCh = make ( chan autobuild . Stats )
logger = slogtest . Make ( t , & slogtest . Options {
// We ignore errors here since we expect to fail
// builds.
IgnoreErrors : true ,
})
failureTTL = time . Millisecond
client = coderdtest . New ( t , & coderdtest . Options {
Logger : & logger ,
AutobuildTicker : ticker ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statCh ,
TemplateScheduleStore : schedule . NewAGPLTemplateScheduleStore (),
})
)
user := coderdtest . CreateFirstUser ( t , client )
version := coderdtest . CreateTemplateVersion ( t , client , user . OrganizationID , & echo . Responses {
Parse : echo . ParseComplete ,
ProvisionPlan : echo . ProvisionComplete ,
ProvisionApply : echo . ProvisionFailed ,
})
template := coderdtest . CreateTemplate ( t , client , user . OrganizationID , version . ID , func ( ctr * codersdk . CreateTemplateRequest ) {
ctr . FailureTTLMillis = ptr . Ref [ int64 ]( failureTTL . Milliseconds ())
})
coderdtest . AwaitTemplateVersionJob ( t , client , version . ID )
ws := coderdtest . CreateWorkspace ( t , client , user . OrganizationID , template . ID )
build := coderdtest . AwaitWorkspaceBuildJob ( t , client , ws . LatestBuild . ID )
require . Equal ( t , codersdk . WorkspaceStatusFailed , build . Status )
2023-07-02 21:29:52 -05:00
ticker <- build . Job . CompletedAt . Add ( failureTTL * 2 )
stats := <- statCh
// Expect no transitions since we're using AGPL.
require . Len ( t , stats . Transitions , 0 )
})
}
// TestExecutorInactiveWorkspace test AGPL functionality which mainly
// ensures that autostop actions as a result of an inactive workspace
// do not trigger.
// For enterprise functionality see enterprise/coderd/workspaces_test.go
func TestExecutorInactiveWorkspace ( t * testing . T ) {
t . Parallel ()
// Test that an AGPL TemplateScheduleStore properly disables
// functionality.
t . Run ( "OK" , func ( t * testing . T ) {
t . Parallel ()
var (
ticker = make ( chan time . Time )
statCh = make ( chan autobuild . Stats )
logger = slogtest . Make ( t , & slogtest . Options {
// We ignore errors here since we expect to fail
// builds.
IgnoreErrors : true ,
})
inactiveTTL = time . Millisecond
client = coderdtest . New ( t , & coderdtest . Options {
Logger : & logger ,
AutobuildTicker : ticker ,
IncludeProvisionerDaemon : true ,
AutobuildStats : statCh ,
TemplateScheduleStore : schedule . NewAGPLTemplateScheduleStore (),
})
)
user := coderdtest . CreateFirstUser ( t , client )
version := coderdtest . CreateTemplateVersion ( t , client , user . OrganizationID , & echo . Responses {
Parse : echo . ParseComplete ,
ProvisionPlan : echo . ProvisionComplete ,
ProvisionApply : echo . ProvisionComplete ,
})
template := coderdtest . CreateTemplate ( t , client , user . OrganizationID , version . ID , func ( ctr * codersdk . CreateTemplateRequest ) {
ctr . InactivityTTLMillis = ptr . Ref [ int64 ]( inactiveTTL . Milliseconds ())
})
coderdtest . AwaitTemplateVersionJob ( t , client , version . ID )
ws := coderdtest . CreateWorkspace ( t , client , user . OrganizationID , template . ID )
build := coderdtest . AwaitWorkspaceBuildJob ( t , client , ws . LatestBuild . ID )
require . Equal ( t , codersdk . WorkspaceStatusRunning , build . Status )
ticker <- ws . LastUsedAt . Add ( inactiveTTL * 2 )
2023-06-22 00:33:22 -04:00
stats := <- statCh
// Expect no transitions since we're using AGPL.
require . Len ( t , stats . Transitions , 0 )
})
}
2022-05-23 23:31:41 +01:00
func mustProvisionWorkspace ( t * testing . T , client * codersdk . Client , mut ... func ( * codersdk . CreateWorkspaceRequest )) codersdk . Workspace {
2022-05-11 23:03:02 +01:00
t . Helper ()
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 )
2022-05-23 23:31:41 +01:00
ws := coderdtest . CreateWorkspace ( t , client , user . OrganizationID , template . ID , mut ... )
2022-05-11 23:03:02 +01:00
coderdtest . AwaitWorkspaceBuildJob ( t , client , ws . LatestBuild . ID )
2022-06-07 13:37:45 +01:00
return coderdtest . MustWorkspace ( t , client , ws . ID )
2022-05-11 23:03:02 +01:00
}
2023-01-30 15:34:38 +01:00
func mustProvisionWorkspaceWithParameters ( t * testing . T , client * codersdk . Client , richParameters [] * proto . RichParameter , mut ... func ( * codersdk . CreateWorkspaceRequest )) codersdk . Workspace {
t . Helper ()
user := coderdtest . CreateFirstUser ( t , client )
version := coderdtest . CreateTemplateVersion ( t , client , user . OrganizationID , & echo . Responses {
Parse : echo . ParseComplete ,
ProvisionPlan : [] * proto . Provision_Response {
{
Type : & proto . Provision_Response_Complete {
Complete : & proto . Provision_Complete {
Parameters : richParameters ,
},
},
2023-02-18 18:32:09 -06:00
},
},
2023-01-30 15:34:38 +01:00
ProvisionApply : [] * proto . Provision_Response {
{
Type : & proto . Provision_Response_Complete {
Complete : & proto . Provision_Complete {},
},
},
},
})
template := coderdtest . CreateTemplate ( t , client , user . OrganizationID , version . ID )
coderdtest . AwaitTemplateVersionJob ( t , client , version . ID )
ws := coderdtest . CreateWorkspace ( t , client , user . OrganizationID , template . ID , mut ... )
coderdtest . AwaitWorkspaceBuildJob ( t , client , ws . LatestBuild . ID )
return coderdtest . MustWorkspace ( t , client , ws . ID )
}
2022-06-07 13:37:45 +01:00
func mustSchedule ( t * testing . T , s string ) * schedule . Schedule {
2022-05-11 23:03:02 +01:00
t . Helper ()
2022-06-07 13:37:45 +01:00
sched , err := schedule . Weekly ( s )
require . NoError ( t , err )
return sched
2022-05-11 23:03:02 +01:00
}
2023-01-30 15:34:38 +01:00
func mustWorkspaceParameters ( t * testing . T , client * codersdk . Client , workspaceID uuid . UUID ) {
ctx := context . Background ()
buildParameters , err := client . WorkspaceBuildParameters ( ctx , workspaceID )
require . NoError ( t , err )
require . NotEmpty ( t , buildParameters )
}
2022-05-11 23:03:02 +01:00
func TestMain ( m * testing . M ) {
goleak . VerifyTestMain ( m )
}