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