@@ -649,6 +649,93 @@ func TestWorkspaceAutobuild(t *testing.T) {
649
649
stats = <- statsCh
650
650
require .Len (t , stats .Transitions , 0 )
651
651
})
652
+
653
+ // Test that failing to auto-delete a workspace will only retry
654
+ // once a day.
655
+ t .Run ("FailedDeleteRetryDaily" , func (t * testing.T ) {
656
+ t .Parallel ()
657
+
658
+ var (
659
+ ticker = make (chan time.Time )
660
+ statCh = make (chan autobuild.Stats )
661
+ transitionTTL = time .Minute
662
+ ctx = testutil .Context (t , testutil .WaitMedium )
663
+ )
664
+
665
+ client , user := coderdenttest .New (t , & coderdenttest.Options {
666
+ Options : & coderdtest.Options {
667
+ AutobuildTicker : ticker ,
668
+ IncludeProvisionerDaemon : true ,
669
+ AutobuildStats : statCh ,
670
+ TemplateScheduleStore : schedule .NewEnterpriseTemplateScheduleStore (agplUserQuietHoursScheduleStore ()),
671
+ },
672
+ LicenseOptions : & coderdenttest.LicenseOptions {
673
+ Features : license.Features {codersdk .FeatureAdvancedTemplateScheduling : 1 },
674
+ },
675
+ })
676
+
677
+ // Create a template version that passes to get a functioning workspace.
678
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
679
+ Parse : echo .ParseComplete ,
680
+ ProvisionPlan : echo .PlanComplete ,
681
+ ProvisionApply : echo .ApplyComplete ,
682
+ })
683
+ coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
684
+
685
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
686
+
687
+ ws := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
688
+ coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , ws .LatestBuild .ID )
689
+
690
+ // Create a new version that will fail when we try to delete a workspace.
691
+ version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
692
+ Parse : echo .ParseComplete ,
693
+ ProvisionPlan : echo .PlanComplete ,
694
+ ProvisionApply : echo .ApplyFailed ,
695
+ }, func (ctvr * codersdk.CreateTemplateVersionRequest ) {
696
+ ctvr .TemplateID = template .ID
697
+ })
698
+ coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
699
+
700
+ // Try to delete the workspace. This simulates a "failed" autodelete.
701
+ build , err := client .CreateWorkspaceBuild (ctx , ws .ID , codersdk.CreateWorkspaceBuildRequest {
702
+ Transition : codersdk .WorkspaceTransitionDelete ,
703
+ TemplateVersionID : version .ID ,
704
+ })
705
+ require .NoError (t , err )
706
+
707
+ build = coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , build .ID )
708
+ require .NotEmpty (t , build .Job .Error )
709
+
710
+ // Update our workspace to be dormant so that it qualifies for auto-deletion.
711
+ err = client .UpdateWorkspaceDormancy (ctx , ws .ID , codersdk.UpdateWorkspaceDormancy {
712
+ Dormant : true ,
713
+ })
714
+ require .NoError (t , err )
715
+
716
+ // Enable auto-deletion for the template.
717
+ _ , err = client .UpdateTemplateMeta (ctx , template .ID , codersdk.UpdateTemplateMeta {
718
+ TimeTilDormantAutoDeleteMillis : transitionTTL .Milliseconds (),
719
+ })
720
+ require .NoError (t , err )
721
+
722
+ ws = coderdtest .MustWorkspace (t , client , ws .ID )
723
+ require .NotNil (t , ws .DeletingAt )
724
+
725
+ // Simulate ticking an hour after the workspace is expected to be deleted.
726
+ // Under normal circumstances this should result in a transition but
727
+ // since our last build resulted in failure it should be skipped.
728
+ ticker <- build .Job .CompletedAt .Add (time .Hour )
729
+ stats := <- statCh
730
+ require .Len (t , stats .Transitions , 0 )
731
+
732
+ // Simulate ticking a day after the workspace was last attempted to
733
+ // be deleted. This should result in an attempt.
734
+ ticker <- build .Job .CompletedAt .Add (time .Hour * 25 )
735
+ stats = <- statCh
736
+ require .Len (t , stats .Transitions , 1 )
737
+ require .Equal (t , database .WorkspaceTransitionDelete , stats .Transitions [ws .ID ])
738
+ })
652
739
}
653
740
654
741
func TestWorkspacesFiltering (t * testing.T ) {
0 commit comments