@@ -563,6 +563,52 @@ func TestExecutorWorkspaceAutostopBeforeDeadline(t *testing.T) {
563
563
assert .Len (t ,stats .Transitions ,0 )
564
564
}
565
565
566
+ func TestExecuteAutostopSuspendedUser (t * testing.T ) {
567
+ t .Parallel ()
568
+
569
+ var (
570
+ ctx = testutil .Context (t ,testutil .WaitShort )
571
+ tickCh = make (chan time.Time )
572
+ statsCh = make (chan autobuild.Stats )
573
+ client = coderdtest .New (t ,& coderdtest.Options {
574
+ AutobuildTicker :tickCh ,
575
+ IncludeProvisionerDaemon :true ,
576
+ AutobuildStats :statsCh ,
577
+ })
578
+ )
579
+
580
+ admin := coderdtest .CreateFirstUser (t ,client )
581
+ version := coderdtest .CreateTemplateVersion (t ,client ,admin .OrganizationID ,nil )
582
+ coderdtest .AwaitTemplateVersionJobCompleted (t ,client ,version .ID )
583
+ template := coderdtest .CreateTemplate (t ,client ,admin .OrganizationID ,version .ID )
584
+ userClient ,user := coderdtest .CreateAnotherUser (t ,client ,admin .OrganizationID )
585
+ workspace := coderdtest .CreateWorkspace (t ,userClient ,admin .OrganizationID ,template .ID )
586
+ coderdtest .AwaitWorkspaceBuildJobCompleted (t ,userClient ,workspace .LatestBuild .ID )
587
+
588
+ // Given: workspace is running, and the user is suspended.
589
+ workspace = coderdtest .MustWorkspace (t ,userClient ,workspace .ID )
590
+ require .Equal (t ,codersdk .WorkspaceStatusRunning ,workspace .LatestBuild .Status )
591
+ _ ,err := client .UpdateUserStatus (ctx ,user .ID .String (),codersdk .UserStatusSuspended )
592
+ require .NoError (t ,err ,"update user status" )
593
+
594
+ // When: the autobuild executor ticks after the scheduled time
595
+ go func () {
596
+ tickCh <- time .Unix (0 ,0 )// the exact time is not important
597
+ close (tickCh )
598
+ }()
599
+
600
+ // Then: the workspace should be stopped
601
+ stats := <- statsCh
602
+ assert .Len (t ,stats .Errors ,0 )
603
+ assert .Len (t ,stats .Transitions ,1 )
604
+ assert .Equal (t ,stats .Transitions [workspace .ID ],database .WorkspaceTransitionStop )
605
+
606
+ // Wait for stop to complete
607
+ workspace = coderdtest .MustWorkspace (t ,client ,workspace .ID )
608
+ workspaceBuild := coderdtest .AwaitWorkspaceBuildJobCompleted (t ,client ,workspace .LatestBuild .ID )
609
+ assert .Equal (t ,codersdk .WorkspaceStatusStopped ,workspaceBuild .Status )
610
+ }
611
+
566
612
func TestExecutorWorkspaceAutostopNoWaitChangedMyMind (t * testing.T ) {
567
613
t .Parallel ()
568
614