2022-05-11 23:03:02 +01:00
package executor_test
import (
"context"
"fmt"
2022-05-13 17:14:24 +01:00
"os"
2022-05-20 00:29:10 +10:00
"strings"
2022-05-11 23:03:02 +01:00
"testing"
"time"
"go.uber.org/goleak"
2022-05-30 20:23:36 +01:00
"github.com/coder/coder/coderd/autobuild/executor"
2022-05-11 23:03:02 +01:00
"github.com/coder/coder/coderd/autobuild/schedule"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/database"
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"
"github.com/google/uuid"
2022-05-30 20:23:36 +01:00
"github.com/stretchr/testify/assert"
2022-05-11 23:03:02 +01:00
"github.com/stretchr/testify/require"
)
func TestExecutorAutostartOK ( t * testing . T ) {
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
ctx = context . Background ()
err error
tickCh = make ( chan time . Time )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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 stopped
workspace = mustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
// When: we enable workspace autostart
sched , err := schedule . Weekly ( "* * * * *" )
require . NoError ( t , err )
require . NoError ( t , client . UpdateWorkspaceAutostart ( ctx , workspace . ID , codersdk . UpdateWorkspaceAutostartRequest {
2022-06-02 11:23:34 +01:00
Schedule : ptr . Ref ( sched . String ()),
2022-05-11 23:03:02 +01:00
}))
// When: the autobuild executor ticks
go func () {
tickCh <- time . Now (). UTC (). Add ( time . Minute )
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-05-11 23:03:02 +01:00
}
func TestExecutorAutostartTemplateUpdated ( t * testing . T ) {
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
ctx = context . Background ()
err error
tickCh = make ( chan time . Time )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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 stopped
workspace = mustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
// 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 ,
}))
// When: we enable workspace autostart
sched , err := schedule . Weekly ( "* * * * *" )
require . NoError ( t , err )
require . NoError ( t , client . UpdateWorkspaceAutostart ( ctx , workspace . ID , codersdk . UpdateWorkspaceAutostartRequest {
2022-06-02 11:23:34 +01:00
Schedule : ptr . Ref ( sched . String ()),
2022-05-11 23:03:02 +01:00
}))
// When: the autobuild executor ticks
go func () {
tickCh <- time . Now (). UTC (). Add ( time . Minute )
close ( tickCh )
}()
// 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-05-11 23:03:02 +01:00
ws := mustWorkspace ( t , client , workspace . ID )
2022-05-30 20:23:36 +01: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-05-30 20:23:36 +01:00
ctx = context . Background ()
err error
tickCh = make ( chan time . Time )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
// Given: we have a user with a workspace
workspace = mustProvisionWorkspace ( t , client )
)
// 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: we enable workspace autostart
sched , err := schedule . Weekly ( "* * * * *" )
require . NoError ( t , err )
require . NoError ( t , client . UpdateWorkspaceAutostart ( ctx , workspace . ID , codersdk . UpdateWorkspaceAutostartRequest {
2022-06-02 11:23:34 +01:00
Schedule : ptr . Ref ( sched . String ()),
2022-05-11 23:03:02 +01:00
}))
// When: the autobuild executor ticks
go func () {
tickCh <- time . Now (). UTC (). Add ( time . Minute )
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 )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
// Given: we have a user with a workspace
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
workspace = mustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
// When: the autobuild executor ticks
go func () {
tickCh <- time . Now (). UTC (). Add ( time . Minute )
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 )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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-05-26 18:08:11 +01:00
tickCh <- workspace . LatestBuild . Deadline . 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-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 )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-26 18:08:11 +01:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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-05-30 20:23:36 +01:00
newDeadline := originalDeadline . 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 () {
tickCh <- originalDeadline . Add ( time . Minute )
}()
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 )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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
workspace = mustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
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-05-26 18:08:11 +01:00
tickCh <- workspace . LatestBuild . Deadline . 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-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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 that has no TTL set
workspace = mustProvisionWorkspace ( t , client , func ( cwr * codersdk . CreateWorkspaceRequest ) {
2022-06-02 11:23:34 +01:00
cwr . TTLMillis = nil
2022-05-23 23:31:41 +01:00
})
2022-05-11 23:03:02 +01:00
)
2022-05-23 23:31:41 +01:00
// Given: workspace has no TTL set
2022-06-02 11:23:34 +01:00
require . Nil ( t , workspace . TTLMillis )
2022-05-23 23:31:41 +01:00
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-05-26 18:08:11 +01:00
tickCh <- workspace . LatestBuild . Deadline . 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-05-30 20:23:36 +01:00
ctx = context . Background ()
err error
tickCh = make ( chan time . Time )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
// Given: we have a user with a workspace
workspace = mustProvisionWorkspace ( t , client )
)
// When: we enable workspace autostart
sched , err := schedule . Weekly ( "* * * * *" )
require . NoError ( t , err )
2022-05-19 20:09:27 +01:00
require . NoError ( t , client . UpdateWorkspaceAutostart ( ctx , workspace . ID , codersdk . UpdateWorkspaceAutostartRequest {
2022-06-02 11:23:34 +01:00
Schedule : ptr . Ref ( sched . String ()),
2022-05-11 23:03:02 +01:00
}))
// Given: workspace is deleted
workspace = mustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionDelete )
// When: the autobuild executor ticks
go func () {
tickCh <- time . Now (). UTC (). Add ( time . Minute )
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-05-30 20:23:36 +01:00
ctx = context . Background ()
err error
tickCh = make ( chan time . Time )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
AutobuildStats : statsCh ,
2022-05-11 23:03:02 +01:00
})
2022-05-23 23:31:41 +01:00
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 = & futureTimeCron
})
2022-05-11 23:03:02 +01:00
)
// When: we enable workspace autostart with some time in the future
sched , err := schedule . Weekly ( futureTimeCron )
require . NoError ( t , err )
2022-05-19 20:09:27 +01:00
require . NoError ( t , client . UpdateWorkspaceAutostart ( ctx , workspace . ID , codersdk . UpdateWorkspaceAutostartRequest {
2022-06-02 11:23:34 +01:00
Schedule : ptr . Ref ( sched . String ()),
2022-05-11 23:03:02 +01:00
}))
// When: the autobuild executor ticks
2022-05-19 20:09:27 +01:00
go func () {
tickCh <- time . Now (). UTC ()
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 )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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-05-26 18:08:11 +01:00
tickCh <- workspace . LatestBuild . Deadline . 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 )
statsCh = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-26 18:08:11 +01:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
AutobuildStats : statsCh ,
2022-05-26 18:08:11 +01:00
})
// 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
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 )
// 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!
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-13 17:14:24 +01:00
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` )
}
t . Parallel ()
var (
2022-05-30 20:23:36 +01:00
tickCh = make ( chan time . Time )
tickCh2 = make ( chan time . Time )
statsCh1 = make ( chan executor . Stats )
statsCh2 = make ( chan executor . Stats )
client = coderdtest . New ( t , & coderdtest . Options {
2022-05-19 15:47:45 -07:00
AutobuildTicker : tickCh ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
AutobuildStats : statsCh1 ,
2022-05-13 17:14:24 +01:00
})
_ = coderdtest . New ( t , & coderdtest . Options {
2022-05-23 23:31:41 +01:00
AutobuildTicker : tickCh2 ,
IncludeProvisionerD : true ,
2022-05-30 20:23:36 +01:00
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-05-13 17:14:24 +01:00
workspace = mustProvisionWorkspace ( t , client )
)
// Given: workspace is stopped
workspace = mustTransitionWorkspace ( t , client , workspace . ID , database . WorkspaceTransitionStart , database . WorkspaceTransitionStop )
// When: the autobuild executor ticks
go func () {
tickCh <- time . Now (). UTC (). Add ( time . Minute )
tickCh2 <- time . Now (). UTC (). Add ( time . Minute )
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
}
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 )
return mustWorkspace ( t , client , ws . ID )
}
func mustTransitionWorkspace ( t * testing . T , client * codersdk . Client , workspaceID uuid . UUID , from , to database . WorkspaceTransition ) codersdk . Workspace {
t . Helper ()
ctx := context . Background ()
workspace , err := client . Workspace ( ctx , workspaceID )
require . NoError ( t , err , "unexpected error fetching workspace" )
2022-05-19 13:04:44 -05:00
require . Equal ( t , workspace . LatestBuild . Transition , codersdk . WorkspaceTransition ( from ), "expected workspace state: %s got: %s" , from , workspace . LatestBuild . Transition )
2022-05-11 23:03:02 +01:00
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 ,
2022-05-19 13:04:44 -05:00
Transition : codersdk . WorkspaceTransition ( to ),
2022-05-11 23:03:02 +01:00
})
require . NoError ( t , err , "unexpected error transitioning workspace to %s" , to )
_ = coderdtest . AwaitWorkspaceBuildJob ( t , client , build . ID )
updated := mustWorkspace ( t , client , workspace . ID )
2022-05-19 13:04:44 -05:00
require . Equal ( t , codersdk . WorkspaceTransition ( to ), updated . LatestBuild . Transition , "expected workspace to be in state %s but got %s" , to , updated . LatestBuild . Transition )
2022-05-11 23:03:02 +01:00
return updated
}
func mustWorkspace ( t * testing . T , client * codersdk . Client , workspaceID uuid . UUID ) codersdk . Workspace {
2022-05-30 20:23:36 +01:00
t . Helper ()
2022-05-11 23:03:02 +01:00
ctx := context . Background ()
ws , err := client . Workspace ( ctx , workspaceID )
2022-05-20 00:29:10 +10:00
if err != nil && strings . Contains ( err . Error (), "status code 410" ) {
ws , err = client . DeletedWorkspace ( ctx , workspaceID )
}
2022-05-11 23:03:02 +01:00
require . NoError ( t , err , "no workspace found with id %s" , workspaceID )
return ws
}
func TestMain ( m * testing . M ) {
goleak . VerifyTestMain ( m )
}