35
35
func TestReportFailedWorkspaceBuilds (t * testing.T ) {
36
36
t .Parallel ()
37
37
38
- t .Run ("InitialState_NoBuilds_NoReport " , func (t * testing.T ) {
38
+ t .Run ("EmptyState_NoBuilds_NoReport " , func (t * testing.T ) {
39
39
t .Parallel ()
40
40
41
41
// Setup
@@ -44,10 +44,73 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
44
44
// Database is ready, so we can clear notifications queue
45
45
notifEnq .Clear ()
46
46
47
+ // When: first run
48
+ err := reportFailedWorkspaceBuilds (ctx , logger , db , notifEnq , clk )
49
+
50
+ // Then: no report should be generated
51
+ require .NoError (t , err )
52
+ require .Empty (t , notifEnq .Sent )
53
+
54
+ // Given: one week later and no jobs were executed
55
+ clk .Advance (failedWorkspaceBuildsReportFrequency + time .Minute )
56
+
47
57
// When
58
+ notifEnq .Clear ()
59
+ err = reportFailedWorkspaceBuilds (ctx , logger , db , notifEnq , clk )
60
+
61
+ // Then: report is still empty
62
+ require .NoError (t , err )
63
+ require .Empty (t , notifEnq .Sent )
64
+ })
65
+
66
+ t .Run ("InitialState_NoBuilds_NoReport" , func (t * testing.T ) {
67
+ t .Parallel ()
68
+
69
+ // Setup
70
+ ctx , logger , db , ps , notifEnq , clk := setup (t )
71
+ now := clk .Now ()
72
+
73
+ // Organization
74
+ org := dbgen .Organization (t , db , database.Organization {})
75
+
76
+ // Template admins
77
+ templateAdmin1 := dbgen .User (t , db , database.User {Username : "template-admin-1" , RBACRoles : []string {rbac .RoleTemplateAdmin ().Name }})
78
+ _ = dbgen .OrganizationMember (t , db , database.OrganizationMember {UserID : templateAdmin1 .ID , OrganizationID : org .ID })
79
+
80
+ // Regular users
81
+ user1 := dbgen .User (t , db , database.User {})
82
+ _ = dbgen .OrganizationMember (t , db , database.OrganizationMember {UserID : user1 .ID , OrganizationID : org .ID })
83
+ user2 := dbgen .User (t , db , database.User {})
84
+ _ = dbgen .OrganizationMember (t , db , database.OrganizationMember {UserID : user2 .ID , OrganizationID : org .ID })
85
+
86
+ // Templates
87
+ t1 := dbgen .Template (t , db , database.Template {Name : "template-1" , DisplayName : "First Template" , CreatedBy : templateAdmin1 .ID , OrganizationID : org .ID })
88
+
89
+ // Template versions
90
+ t1v1 := dbgen .TemplateVersion (t , db , database.TemplateVersion {Name : "template-1-version-1" , CreatedBy : templateAdmin1 .ID , OrganizationID : org .ID , TemplateID : uuid.NullUUID {UUID : t1 .ID , Valid : true }, JobID : uuid .New ()})
91
+
92
+ // Workspaces
93
+ w1 := dbgen .Workspace (t , db , database.Workspace {TemplateID : t1 .ID , OwnerID : user1 .ID , OrganizationID : org .ID })
94
+
95
+ w1wb1pj := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {OrganizationID : org .ID , Error : jobError , ErrorCode : jobErrorCode , CompletedAt : sql.NullTime {Time : now .Add (- 6 * dayDuration ), Valid : true }})
96
+ _ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w1 .ID , BuildNumber : 1 , TemplateVersionID : t1v1 .ID , JobID : w1wb1pj .ID , CreatedAt : now .Add (- 2 * dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
97
+
98
+ // When: first run
99
+ notifEnq .Clear ()
48
100
err := reportFailedWorkspaceBuilds (ctx , logger , db , notifEnq , clk )
49
101
50
- // Then
102
+ // Then: failed builds should not be reported
103
+ require .NoError (t , err )
104
+ require .Empty (t , notifEnq .Sent )
105
+
106
+ // Given: one week later, but still no jobs
107
+ clk .Advance (failedWorkspaceBuildsReportFrequency + time .Minute )
108
+
109
+ // When
110
+ notifEnq .Clear ()
111
+ err = reportFailedWorkspaceBuilds (ctx , logger , db , notifEnq , clk )
112
+
113
+ // Then: report is still empty
51
114
require .NoError (t , err )
52
115
require .Empty (t , notifEnq .Sent )
53
116
})
@@ -72,6 +135,7 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
72
135
ctx , logger , db , ps , notifEnq , clk := setup (t )
73
136
74
137
// Given
138
+
75
139
// Organization
76
140
org := dbgen .Organization (t , db , database.Organization {})
77
141
@@ -105,6 +169,16 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
105
169
w3 := dbgen .Workspace (t , db , database.Workspace {TemplateID : t1 .ID , OwnerID : user1 .ID , OrganizationID : org .ID })
106
170
w4 := dbgen .Workspace (t , db , database.Workspace {TemplateID : t2 .ID , OwnerID : user2 .ID , OrganizationID : org .ID })
107
171
172
+ // When: first run
173
+ notifEnq .Clear ()
174
+ err := reportFailedWorkspaceBuilds (ctx , logger , db , notifEnq , clk )
175
+
176
+ // Then
177
+ require .NoError (t , err )
178
+ require .Empty (t , notifEnq .Sent ) // no notifications
179
+
180
+ // One week later...
181
+ clk .Advance (failedWorkspaceBuildsReportFrequency + time .Minute )
108
182
now := clk .Now ()
109
183
110
184
// Workspace builds
@@ -130,11 +204,9 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
130
204
w4wb2pj := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {OrganizationID : org .ID , CompletedAt : sql.NullTime {Time : now .Add (- dayDuration ), Valid : true }})
131
205
_ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w4 .ID , BuildNumber : 9 , TemplateVersionID : t2v2 .ID , JobID : w4wb2pj .ID , CreatedAt : now .Add (- dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
132
206
133
- // Database is ready, so we can clear notifications queue
134
- notifEnq .Clear ()
135
-
136
207
// When
137
- err := reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
208
+ notifEnq .Clear ()
209
+ err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
138
210
139
211
// Then
140
212
require .NoError (t , err )
@@ -182,30 +254,28 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
182
254
183
255
// Given: 6 days later (less than report frequency), and failed build
184
256
clk .Advance (6 * dayDuration ).MustWait (context .Background ())
185
-
186
257
now = clk .Now ()
187
258
188
259
w1wb4pj := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {OrganizationID : org .ID , Error : jobError , ErrorCode : jobErrorCode , CompletedAt : sql.NullTime {Time : now .Add (- dayDuration ), Valid : true }})
189
260
_ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w1 .ID , BuildNumber : 77 , TemplateVersionID : t1v2 .ID , JobID : w1wb4pj .ID , CreatedAt : now .Add (- dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
190
261
191
- notifEnq .Clear ()
192
-
193
262
// When
263
+ notifEnq .Clear ()
194
264
err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
195
265
require .NoError (t , err )
196
266
197
- // Then
198
- require .Empty (t , notifEnq .Sent ) // no notifications as it is too early.
267
+ // Then: no notifications as it is too early
268
+ require .Empty (t , notifEnq .Sent )
199
269
200
270
// Given: 1 day 1 hour later
201
271
clk .Advance (dayDuration + time .Hour ).MustWait (context .Background ())
202
- notifEnq .Clear ()
203
272
204
273
// When
274
+ notifEnq .Clear ()
205
275
err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
206
276
require .NoError (t , err )
207
277
208
- // Then
278
+ // Then: we should see the failed job in the report
209
279
require .Len (t , notifEnq .Sent , 2 ) // a new failed job should be reported
210
280
for i , templateAdmin := range []database.User {templateAdmin1 , templateAdmin2 } {
211
281
verifyNotification (t , templateAdmin , notifEnq .Sent [i ], t1 , 1 , 1 , []map [string ]interface {}{
@@ -247,23 +317,30 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
247
317
// Workspaces
248
318
w1 := dbgen .Workspace (t , db , database.Workspace {TemplateID : t1 .ID , OwnerID : user1 .ID , OrganizationID : org .ID })
249
319
320
+ // When: first run
321
+ notifEnq .Clear ()
322
+ err := reportFailedWorkspaceBuilds (ctx , logger , db , notifEnq , clk )
323
+
324
+ // Then: no notifications
325
+ require .NoError (t , err )
326
+ require .Empty (t , notifEnq .Sent )
327
+
328
+ // Given: one week later, and a successful few jobs being executed
329
+ clk .Advance (failedWorkspaceBuildsReportFrequency + time .Minute )
250
330
now := clk .Now ()
251
331
252
332
// Workspace builds
253
333
w1wb1pj := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {OrganizationID : org .ID , CompletedAt : sql.NullTime {Time : now .Add (- 6 * dayDuration ), Valid : true }})
254
- _ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w1 .ID , BuildNumber : 1 , TemplateVersionID : t1v1 .ID , JobID : w1wb1pj .ID , CreatedAt : now .Add (- 6 * dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
334
+ _ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w1 .ID , BuildNumber : 1 , TemplateVersionID : t1v1 .ID , JobID : w1wb1pj .ID , CreatedAt : now .Add (- 2 * dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
255
335
w1wb2pj := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {OrganizationID : org .ID , CompletedAt : sql.NullTime {Time : now .Add (- 5 * dayDuration ), Valid : true }})
256
- _ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w1 .ID , BuildNumber : 2 , TemplateVersionID : t1v1 .ID , JobID : w1wb2pj .ID , CreatedAt : now .Add (- 5 * dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
257
-
258
- // Database is ready, so we can clear notifications queue
259
- notifEnq .Clear ()
336
+ _ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w1 .ID , BuildNumber : 2 , TemplateVersionID : t1v1 .ID , JobID : w1wb2pj .ID , CreatedAt : now .Add (- 1 * dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
260
337
261
338
// When
262
- err := reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
339
+ notifEnq .Clear ()
340
+ err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
263
341
264
- // Then
342
+ // Then: no failures? nothing to report
265
343
require .NoError (t , err )
266
-
267
344
require .Len (t , notifEnq .Sent , 0 ) // all jobs succeeded so nothing to report
268
345
})
269
346
}
0 commit comments