@@ -871,6 +871,74 @@ func TestTemplateTTL(t *testing.T) {
871
871
require .Equal (t , sql.NullInt64 {Valid : true , Int64 : int64 (otherTTL )}, ws .Ttl )
872
872
})
873
873
}
874
+
875
+ t .Run ("WorkspaceTTLUpdatedWhenAllowUserAutostopGetsDisabled" , func (t * testing.T ) {
876
+ t .Parallel ()
877
+
878
+ var (
879
+ logger = slogtest .Make (t , & slogtest.Options {IgnoreErrors : true }).Leveled (slog .LevelDebug )
880
+ db , _ = dbtestutil .NewDB (t )
881
+ ctx = testutil .Context (t , testutil .WaitLong )
882
+ user = dbgen .User (t , db , database.User {})
883
+ file = dbgen .File (t , db , database.File {CreatedBy : user .ID })
884
+ // Create first template
885
+ templateJob = dbgen .ProvisionerJob (t , db , nil , database.ProvisionerJob {
886
+ FileID : file .ID ,
887
+ InitiatorID : user .ID ,
888
+ Tags : database.StringMap {"foo" : "bar" },
889
+ })
890
+ templateVersion = dbgen .TemplateVersion (t , db , database.TemplateVersion {
891
+ CreatedBy : user .ID ,
892
+ JobID : templateJob .ID ,
893
+ OrganizationID : templateJob .OrganizationID ,
894
+ })
895
+ template = dbgen .Template (t , db , database.Template {
896
+ ActiveVersionID : templateVersion .ID ,
897
+ CreatedBy : user .ID ,
898
+ OrganizationID : templateJob .OrganizationID ,
899
+ })
900
+ )
901
+
902
+ // Setup the template schedule store
903
+ notifyEnq := notifications .NewNoopEnqueuer ()
904
+ const userQuietHoursSchedule = "CRON_TZ=UTC 0 0 * * *" // midnight UTC
905
+ userQuietHoursStore , err := schedule .NewEnterpriseUserQuietHoursScheduleStore (userQuietHoursSchedule , true )
906
+ require .NoError (t , err )
907
+ userQuietHoursStorePtr := & atomic.Pointer [agplschedule.UserQuietHoursScheduleStore ]{}
908
+ userQuietHoursStorePtr .Store (& userQuietHoursStore )
909
+ templateScheduleStore := schedule .NewEnterpriseTemplateScheduleStore (userQuietHoursStorePtr , notifyEnq , logger , nil )
910
+
911
+ // Enable AllowUserAutostop
912
+ template , err = templateScheduleStore .Set (ctx , db , template , agplschedule.TemplateScheduleOptions {
913
+ DefaultTTL : 24 * time .Hour ,
914
+ UserAutostopEnabled : true ,
915
+ })
916
+ require .NoError (t , err )
917
+
918
+ // Create a workspace with a TTL different to the template schedule
919
+ workspace := dbgen .Workspace (t , db , database.WorkspaceTable {
920
+ OwnerID : user .ID ,
921
+ TemplateID : template .ID ,
922
+ OrganizationID : templateJob .OrganizationID ,
923
+ LastUsedAt : dbtime .Now (),
924
+ Ttl : sql.NullInt64 {Valid : true , Int64 : int64 (48 * time .Hour )},
925
+ })
926
+
927
+ // Ensure the workspace's start with the correct TTLs
928
+ require .Equal (t , sql.NullInt64 {Valid : true , Int64 : int64 (48 * time .Hour )}, workspace .Ttl )
929
+
930
+ // Disable AllowUserAutostop
931
+ template , err = templateScheduleStore .Set (ctx , db , template , agplschedule.TemplateScheduleOptions {
932
+ DefaultTTL : 24 * time .Hour ,
933
+ UserAutostopEnabled : false ,
934
+ })
935
+ require .NoError (t , err )
936
+
937
+ // Ensure the workspace's ends with the correct TTLs
938
+ ws , err := db .GetWorkspaceByID (ctx , workspace .ID )
939
+ require .NoError (t , err )
940
+ require .Equal (t , sql.NullInt64 {Valid : true , Int64 : int64 (24 * time .Hour )}, ws .Ttl )
941
+ })
874
942
}
875
943
876
944
func must [V any ](v V , err error ) V {
0 commit comments