@@ -2,7 +2,6 @@ package coderd_test
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"net/http"
7
6
"sync/atomic"
8
7
"testing"
@@ -250,70 +249,57 @@ func TestWorkspaceAutobuild(t *testing.T) {
250
249
auditRecorder = audit .NewMock ()
251
250
)
252
251
253
- client , user := coderdenttest .New (t , & coderdenttest.Options {
252
+ client , db , user := coderdenttest .NewWithDatabase (t , & coderdenttest.Options {
254
253
Options : & coderdtest.Options {
255
- AutobuildTicker : ticker ,
256
- IncludeProvisionerDaemon : true ,
257
- AutobuildStats : statCh ,
258
- TemplateScheduleStore : schedule .NewEnterpriseTemplateScheduleStore (agplUserQuietHoursScheduleStore ()),
259
- Auditor : auditRecorder ,
254
+ AutobuildTicker : ticker ,
255
+ AutobuildStats : statCh ,
256
+ TemplateScheduleStore : schedule .NewEnterpriseTemplateScheduleStore (agplUserQuietHoursScheduleStore ()),
257
+ Auditor : auditRecorder ,
260
258
},
261
259
LicenseOptions : & coderdenttest.LicenseOptions {
262
260
Features : license.Features {codersdk .FeatureAdvancedTemplateScheduling : 1 },
263
261
},
264
262
})
265
263
266
- version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
267
- Parse : echo .ParseComplete ,
268
- ProvisionPlan : echo .PlanComplete ,
269
- ProvisionApply : echo .ApplyComplete ,
270
- })
271
- template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID , func (ctr * codersdk.CreateTemplateRequest ) {
272
- ctr .TimeTilDormantMillis = ptr.Ref [int64 ](inactiveTTL .Milliseconds ())
273
- })
274
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
264
+ tpl := dbfake .TemplateVersion (t , db ).Seed (database.TemplateVersion {
265
+ OrganizationID : user .OrganizationID ,
266
+ CreatedBy : user .UserID ,
267
+ }).Do ().Template
275
268
276
- ws := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
269
+ template := coderdtest .UpdateTemplateMeta (t , client , tpl .ID , codersdk.UpdateTemplateMeta {
270
+ TimeTilDormantMillis : inactiveTTL .Milliseconds (),
271
+ })
277
272
278
- coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , ws .LatestBuild .ID )
273
+ resp := dbfake .WorkspaceBuild (t , db , database.Workspace {
274
+ OrganizationID : user .OrganizationID ,
275
+ OwnerID : user .UserID ,
276
+ TemplateID : template .ID ,
277
+ }).Seed (database.WorkspaceBuild {
278
+ Transition : database .WorkspaceTransitionStart ,
279
+ }).Do ()
280
+ require .Equal (t , database .WorkspaceTransitionStart , resp .Build .Transition )
281
+ workspace := resp .Workspace
279
282
280
- // Reset the audit log so we can verify a log is generated.
281
283
auditRecorder .ResetLogs ()
282
284
// Simulate being inactive.
283
- ticker <- ws .LastUsedAt .Add (inactiveTTL * 2 )
285
+ ticker <- workspace .LastUsedAt .Add (inactiveTTL * 2 )
284
286
stats := <- statCh
285
287
286
288
// Expect workspace to transition to stopped state for breaching
287
289
// failure TTL.
288
290
require .Len (t , stats .Transitions , 1 )
289
- require .Equal (t , stats .Transitions [ws .ID ], database .WorkspaceTransitionStop )
291
+ require .Equal (t , stats .Transitions [workspace .ID ], database .WorkspaceTransitionStop )
290
292
291
- ws = coderdtest .MustWorkspace (t , client , ws .ID )
293
+ ws := coderdtest .MustWorkspace (t , client , workspace .ID )
294
+ // Should be dormant now.
292
295
require .NotNil (t , ws .DormantAt )
293
-
294
- coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , ws .LatestBuild .ID )
295
- // We should get 2 audit logs, one for stopping the workspace, and one for
296
- // making it dormant.
297
- alogs := auditRecorder .AuditLogs ()
298
- require .Len (t , alogs , 2 )
299
-
300
- for _ , alog := range alogs {
301
- require .Equal (t , int32 (http .StatusOK ), alog .StatusCode )
302
-
303
- switch alog .Action {
304
- case database .AuditActionWrite :
305
- require .Equal (t , database .ResourceTypeWorkspace , alog .ResourceType )
306
- case database .AuditActionStop :
307
- var fields audit.AdditionalFields
308
- err := json .Unmarshal (alog .AdditionalFields , & fields )
309
- require .NoError (t , err )
310
- require .Equal (t , ws .Name , fields .WorkspaceName )
311
- require .Equal (t , database .BuildReasonDormancy , fields .BuildReason )
312
-
313
- default :
314
- t .Fatalf ("unexpected audit log (%+v)" , alog )
315
- }
316
- }
296
+ // Should be transitioned to stop.
297
+ require .Equal (t , codersdk .WorkspaceTransitionStop , ws .LatestBuild .Transition )
298
+ require .Len (t , auditRecorder .AuditLogs (), 1 )
299
+ alog := auditRecorder .AuditLogs ()[0 ]
300
+ require .Equal (t , int32 (http .StatusOK ), alog .StatusCode )
301
+ require .Equal (t , database .AuditActionWrite , alog .Action )
302
+ require .Equal (t , workspace .Name , alog .ResourceTarget )
317
303
318
304
dormantLastUsedAt := ws .LastUsedAt
319
305
// nolint:gocritic // this test is not testing RBAC.
0 commit comments