@@ -1039,223 +1039,6 @@ func TestCompleteJob(t *testing.T) {
1039
1039
require .False (t ,job .Error .Valid )
1040
1040
})
1041
1041
1042
- // TODO(@dean): remove this legacy test for MaxTTL
1043
- t .Run ("WorkspaceBuildLegacy" ,func (t * testing.T ) {
1044
- t .Parallel ()
1045
-
1046
- cases := []struct {
1047
- name string
1048
- templateAllowAutostop bool
1049
- templateDefaultTTL time.Duration
1050
- workspaceTTL time.Duration
1051
- transition database.WorkspaceTransition
1052
- // The TTL is actually a deadline time on the workspace_build row,
1053
- // so during the test this will be compared to be within 15 seconds
1054
- // of the expected value.
1055
- expectedTTL time.Duration
1056
- }{
1057
- {
1058
- name :"OK" ,
1059
- templateAllowAutostop :true ,
1060
- templateDefaultTTL :0 ,
1061
- workspaceTTL :0 ,
1062
- transition :database .WorkspaceTransitionStart ,
1063
- expectedTTL :0 ,
1064
- },
1065
- {
1066
- name :"Delete" ,
1067
- templateAllowAutostop :true ,
1068
- templateDefaultTTL :0 ,
1069
- workspaceTTL :0 ,
1070
- transition :database .WorkspaceTransitionDelete ,
1071
- expectedTTL :0 ,
1072
- },
1073
- {
1074
- name :"WorkspaceTTL" ,
1075
- templateAllowAutostop :true ,
1076
- templateDefaultTTL :0 ,
1077
- workspaceTTL :time .Hour ,
1078
- transition :database .WorkspaceTransitionStart ,
1079
- expectedTTL :time .Hour ,
1080
- },
1081
- {
1082
- name :"TemplateDefaultTTLIgnored" ,
1083
- templateAllowAutostop :true ,
1084
- templateDefaultTTL :time .Hour ,
1085
- workspaceTTL :0 ,
1086
- transition :database .WorkspaceTransitionStart ,
1087
- expectedTTL :0 ,
1088
- },
1089
- {
1090
- name :"WorkspaceTTLOverridesTemplateDefaultTTL" ,
1091
- templateAllowAutostop :true ,
1092
- templateDefaultTTL :2 * time .Hour ,
1093
- workspaceTTL :time .Hour ,
1094
- transition :database .WorkspaceTransitionStart ,
1095
- expectedTTL :time .Hour ,
1096
- },
1097
- {
1098
- name :"TemplateMaxTTL" ,
1099
- templateAllowAutostop :true ,
1100
- templateDefaultTTL :0 ,
1101
- workspaceTTL :0 ,
1102
- transition :database .WorkspaceTransitionStart ,
1103
- expectedTTL :0 ,
1104
- },
1105
- {
1106
- name :"TemplateMaxTTLOverridesWorkspaceTTL" ,
1107
- templateAllowAutostop :true ,
1108
- templateDefaultTTL :0 ,
1109
- workspaceTTL :3 * time .Hour ,
1110
- transition :database .WorkspaceTransitionStart ,
1111
- expectedTTL :3 * time .Hour ,
1112
- },
1113
- {
1114
- name :"TemplateMaxTTLOverridesTemplateDefaultTTL" ,
1115
- templateAllowAutostop :true ,
1116
- templateDefaultTTL :3 * time .Hour ,
1117
- workspaceTTL :0 ,
1118
- transition :database .WorkspaceTransitionStart ,
1119
- expectedTTL :0 ,
1120
- },
1121
- {
1122
- name :"TemplateBlockWorkspaceTTL" ,
1123
- templateAllowAutostop :false ,
1124
- templateDefaultTTL :3 * time .Hour ,
1125
- workspaceTTL :4 * time .Hour ,
1126
- transition :database .WorkspaceTransitionStart ,
1127
- expectedTTL :3 * time .Hour ,
1128
- },
1129
- }
1130
-
1131
- for _ ,c := range cases {
1132
- c := c
1133
-
1134
- t .Run (c .name ,func (t * testing.T ) {
1135
- t .Parallel ()
1136
-
1137
- tss := & atomic.Pointer [schedule.TemplateScheduleStore ]{}
1138
- srv ,db ,ps ,pd := setup (t ,false ,& overrides {templateScheduleStore :tss })
1139
-
1140
- var store schedule.TemplateScheduleStore = schedule.MockTemplateScheduleStore {
1141
- GetFn :func (_ context.Context ,_ database.Store ,_ uuid.UUID ) (schedule.TemplateScheduleOptions ,error ) {
1142
- return schedule.TemplateScheduleOptions {
1143
- UserAutostartEnabled :false ,
1144
- UserAutostopEnabled :c .templateAllowAutostop ,
1145
- DefaultTTL :c .templateDefaultTTL ,
1146
- },nil
1147
- },
1148
- }
1149
- tss .Store (& store )
1150
-
1151
- user := dbgen .User (t ,db , database.User {})
1152
- template := dbgen .Template (t ,db , database.Template {
1153
- Name :"template" ,
1154
- Provisioner :database .ProvisionerTypeEcho ,
1155
- OrganizationID :pd .OrganizationID ,
1156
- })
1157
- version := dbgen .TemplateVersion (t ,db , database.TemplateVersion {
1158
- OrganizationID :pd .OrganizationID ,
1159
- TemplateID : uuid.NullUUID {
1160
- UUID :template .ID ,
1161
- Valid :true ,
1162
- },
1163
- JobID :uuid .New (),
1164
- })
1165
- err := db .UpdateTemplateScheduleByID (ctx , database.UpdateTemplateScheduleByIDParams {
1166
- ID :template .ID ,
1167
- UpdatedAt :dbtime .Now (),
1168
- AllowUserAutostart :c .templateAllowAutostop ,
1169
- DefaultTTL :int64 (c .templateDefaultTTL ),
1170
- })
1171
- require .NoError (t ,err )
1172
- file := dbgen .File (t ,db , database.File {CreatedBy :user .ID })
1173
- workspaceTTL := sql.NullInt64 {}
1174
- if c .workspaceTTL != 0 {
1175
- workspaceTTL = sql.NullInt64 {
1176
- Int64 :int64 (c .workspaceTTL ),
1177
- Valid :true ,
1178
- }
1179
- }
1180
- workspace := dbgen .Workspace (t ,db , database.Workspace {
1181
- TemplateID :template .ID ,
1182
- Ttl :workspaceTTL ,
1183
- OrganizationID :pd .OrganizationID ,
1184
- })
1185
- build := dbgen .WorkspaceBuild (t ,db , database.WorkspaceBuild {
1186
- WorkspaceID :workspace .ID ,
1187
- TemplateVersionID :version .ID ,
1188
- Transition :c .transition ,
1189
- Reason :database .BuildReasonInitiator ,
1190
- })
1191
- job := dbgen .ProvisionerJob (t ,db ,ps , database.ProvisionerJob {
1192
- OrganizationID :pd .OrganizationID ,
1193
- FileID :file .ID ,
1194
- Type :database .ProvisionerJobTypeWorkspaceBuild ,
1195
- Input :must (json .Marshal (provisionerdserver.WorkspaceProvisionJob {
1196
- WorkspaceBuildID :build .ID ,
1197
- })),
1198
- })
1199
- _ ,err = db .AcquireProvisionerJob (ctx , database.AcquireProvisionerJobParams {
1200
- OrganizationID :pd .OrganizationID ,
1201
- WorkerID : uuid.NullUUID {
1202
- UUID :pd .ID ,
1203
- Valid :true ,
1204
- },
1205
- Types : []database.ProvisionerType {database .ProvisionerTypeEcho },
1206
- })
1207
- require .NoError (t ,err )
1208
-
1209
- publishedWorkspace := make (chan struct {})
1210
- closeWorkspaceSubscribe ,err := ps .Subscribe (codersdk .WorkspaceNotifyChannel (build .WorkspaceID ),func (_ context.Context ,_ []byte ) {
1211
- close (publishedWorkspace )
1212
- })
1213
- require .NoError (t ,err )
1214
- defer closeWorkspaceSubscribe ()
1215
- publishedLogs := make (chan struct {})
1216
- closeLogsSubscribe ,err := ps .Subscribe (provisionersdk .ProvisionerJobLogsNotifyChannel (job .ID ),func (_ context.Context ,_ []byte ) {
1217
- close (publishedLogs )
1218
- })
1219
- require .NoError (t ,err )
1220
- defer closeLogsSubscribe ()
1221
-
1222
- _ ,err = srv .CompleteJob (ctx ,& proto.CompletedJob {
1223
- JobId :job .ID .String (),
1224
- Type :& proto.CompletedJob_WorkspaceBuild_ {
1225
- WorkspaceBuild :& proto.CompletedJob_WorkspaceBuild {
1226
- State : []byte {},
1227
- Resources : []* sdkproto.Resource {{
1228
- Name :"example" ,
1229
- Type :"aws_instance" ,
1230
- }},
1231
- },
1232
- },
1233
- })
1234
- require .NoError (t ,err )
1235
-
1236
- <- publishedWorkspace
1237
- <- publishedLogs
1238
-
1239
- workspace ,err = db .GetWorkspaceByID (ctx ,workspace .ID )
1240
- require .NoError (t ,err )
1241
- require .Equal (t ,c .transition == database .WorkspaceTransitionDelete ,workspace .Deleted )
1242
-
1243
- workspaceBuild ,err := db .GetWorkspaceBuildByID (ctx ,build .ID )
1244
- require .NoError (t ,err )
1245
-
1246
- if c .expectedTTL == 0 {
1247
- require .True (t ,workspaceBuild .Deadline .IsZero ())
1248
- }else {
1249
- require .WithinDuration (t ,time .Now ().Add (c .expectedTTL ),workspaceBuild .Deadline ,15 * time .Second ,"deadline does not match expected" )
1250
- }
1251
-
1252
- // Legacy TTL does not use scheduling requirements that will set
1253
- // a max deadline.
1254
- require .True (t ,workspaceBuild .MaxDeadline .IsZero ())
1255
- })
1256
- }
1257
- })
1258
-
1259
1042
t .Run ("WorkspaceBuild" ,func (t * testing.T ) {
1260
1043
t .Parallel ()
1261
1044