@@ -52,7 +52,7 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
52
52
require .Empty (t , notifEnq .Sent )
53
53
})
54
54
55
- t .Run ("FailedBuilds_TemplateAdminOptIn_FirstRun_Report_SecondRunTooEarly_NoReport_ThirdRun_Report " , func (t * testing.T ) {
55
+ t .Run ("FailedBuilds_FirstRun_Report_SecondRunTooEarly_NoReport_ThirdRun_Report " , func (t * testing.T ) {
56
56
t .Parallel ()
57
57
58
58
// Setup
@@ -68,7 +68,7 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
68
68
templateAdmin2 := dbgen .User (t , db , database.User {Username : "template-admin-2" , RBACRoles : []string {rbac .RoleTemplateAdmin ().Name }})
69
69
_ = dbgen .OrganizationMember (t , db , database.OrganizationMember {UserID : templateAdmin2 .ID , OrganizationID : org .ID })
70
70
_ = dbgen .User (t , db , database.User {Name : "template-admin-3" , RBACRoles : []string {rbac .RoleTemplateAdmin ().Name }})
71
- // template admin in some other org
71
+ // template admin in some other org, they should not receive any notification
72
72
73
73
// Regular users
74
74
user1 := dbgen .User (t , db , database.User {})
@@ -84,7 +84,7 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
84
84
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 ()})
85
85
t1v2 := dbgen .TemplateVersion (t , db , database.TemplateVersion {Name : "template-1-version-2" , CreatedBy : templateAdmin1 .ID , OrganizationID : org .ID , TemplateID : uuid.NullUUID {UUID : t1 .ID , Valid : true }, JobID : uuid .New ()})
86
86
t2v1 := dbgen .TemplateVersion (t , db , database.TemplateVersion {Name : "template-2-version-1" , CreatedBy : templateAdmin1 .ID , OrganizationID : org .ID , TemplateID : uuid.NullUUID {UUID : t2 .ID , Valid : true }, JobID : uuid .New ()})
87
- t2v2 := dbgen .TemplateVersion (t , db , database.TemplateVersion {Name : "template-1 -version-1 " , CreatedBy : templateAdmin1 .ID , OrganizationID : org .ID , TemplateID : uuid.NullUUID {UUID : t2 .ID , Valid : true }, JobID : uuid .New ()})
87
+ t2v2 := dbgen .TemplateVersion (t , db , database.TemplateVersion {Name : "template-2 -version-2 " , CreatedBy : templateAdmin1 .ID , OrganizationID : org .ID , TemplateID : uuid.NullUUID {UUID : t2 .ID , Valid : true }, JobID : uuid .New ()})
88
88
89
89
// Workspaces
90
90
w1 := dbgen .Workspace (t , db , database.Workspace {TemplateID : t1 .ID , OwnerID : user1 .ID , OrganizationID : org .ID })
@@ -121,76 +121,78 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
121
121
notifEnq .Clear ()
122
122
123
123
// When
124
- err := reportFailedWorkspaceBuilds (ctx , logger , authedDB (db , logger ), notifEnq , clk )
124
+ err := reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
125
125
126
126
// Then
127
127
require .NoError (t , err )
128
128
129
129
require .Len (t , notifEnq .Sent , 4 ) // 2 templates, 2 template admins
130
- require .Equal (t , notifEnq .Sent [0 ].UserID , templateAdmin1 .ID )
131
- require .Equal (t , notifEnq .Sent [0 ].TemplateID , notifications .TemplateWorkspaceBuildsFailedReport )
132
- require .Equal (t , notifEnq .Sent [0 ].Labels ["template_name" ], t1 .Name )
133
- require .Equal (t , notifEnq .Sent [0 ].Labels ["template_display_name" ], t1 .DisplayName )
134
- require .Equal (t , notifEnq .Sent [0 ].Data ["failed_builds" ], int64 (3 ))
135
- require .Equal (t , notifEnq .Sent [0 ].Data ["total_builds" ], int64 (4 ))
136
- require .Equal (t , notifEnq .Sent [0 ].Data ["report_frequency" ], "week" )
137
- require .Equal (t , notifEnq .Sent [0 ].Data ["template_versions" ], []map [string ]interface {}{
138
- {
139
- "failed_builds" : []map [string ]interface {}{
140
- {"build_number" : int32 (7 ), "workspace_name" : w3 .Name , "workspace_owner_username" : user1 .Username },
141
- {"build_number" : int32 (1 ), "workspace_name" : w1 .Name , "workspace_owner_username" : user1 .Username },
130
+ for i , templateAdmin := range []database.User {templateAdmin1 , templateAdmin2 } {
131
+ require .Equal (t , templateAdmin .ID , notifEnq .Sent [i ].UserID )
132
+ require .Equal (t , notifications .TemplateWorkspaceBuildsFailedReport , notifEnq .Sent [i ].TemplateID )
133
+ require .Equal (t , t1 .Name , notifEnq .Sent [i ].Labels ["template_name" ])
134
+ require .Equal (t , t1 .DisplayName , notifEnq .Sent [i ].Labels ["template_display_name" ])
135
+ require .Equal (t , int64 (3 ), notifEnq .Sent [i ].Data ["failed_builds" ])
136
+ require .Equal (t , int64 (4 ), notifEnq .Sent [i ].Data ["total_builds" ])
137
+ require .Equal (t , "week" , notifEnq .Sent [i ].Data ["report_frequency" ])
138
+ require .Equal (t , []map [string ]interface {}{
139
+ {
140
+ "failed_builds" : []map [string ]interface {}{
141
+ {"build_number" : int32 (7 ), "workspace_name" : w3 .Name , "workspace_owner_username" : user1 .Username },
142
+ {"build_number" : int32 (1 ), "workspace_name" : w1 .Name , "workspace_owner_username" : user1 .Username },
143
+ },
144
+ "failed_count" : 2 ,
145
+ "template_version_name" : t1v1 .Name ,
142
146
},
143
- "failed_count" : 2 ,
144
- "template_version_name " : t1v1 . Name ,
145
- },
146
- {
147
- "failed_builds " : [] map [ string ] interface {}{
148
- { "build_number " : int32 ( 3 ), "workspace_name" : w1 .Name , "workspace_owner_username" : user1 . Username } ,
147
+ {
148
+ "failed_builds " : [] map [ string ] interface {}{
149
+ { "build_number" : int32 ( 3 ), "workspace_name" : w1 . Name , "workspace_owner_username" : user1 . Username },
150
+ },
151
+ "failed_count " : 1 ,
152
+ "template_version_name " : t1v2 .Name ,
149
153
},
150
- "failed_count" : 1 ,
151
- "template_version_name" : t1v2 .Name ,
152
- },
153
- })
154
-
155
- require .Equal (t , notifEnq .Sent [1 ].UserID , templateAdmin2 .ID )
156
- require .Equal (t , notifEnq .Sent [1 ].TemplateID , notifications .TemplateWorkspaceBuildsFailedReport )
157
- require .Equal (t , notifEnq .Sent [1 ].Labels ["template_name" ], t1 .Name )
158
- require .Equal (t , notifEnq .Sent [1 ].Labels ["template_display_name" ], t1 .DisplayName )
159
- require .Equal (t , notifEnq .Sent [1 ].Data ["failed_builds" ], int64 (3 ))
160
- require .Equal (t , notifEnq .Sent [1 ].Data ["total_builds" ], int64 (4 ))
161
- require .Equal (t , notifEnq .Sent [1 ].Data ["report_frequency" ], "week" )
162
- // require.Contains(t, notifEnq.Sent[1].Data["template_versions"], "?")
163
-
164
- require .Equal (t , notifEnq .Sent [2 ].UserID , templateAdmin1 .ID )
165
- require .Equal (t , notifEnq .Sent [2 ].TemplateID , notifications .TemplateWorkspaceBuildsFailedReport )
166
- require .Equal (t , notifEnq .Sent [2 ].Labels ["template_name" ], t2 .Name )
167
- require .Equal (t , notifEnq .Sent [2 ].Labels ["template_display_name" ], t2 .DisplayName )
168
- require .Equal (t , notifEnq .Sent [2 ].Data ["failed_builds" ], int64 (3 ))
169
- require .Equal (t , notifEnq .Sent [2 ].Data ["total_builds" ], int64 (5 ))
170
- require .Equal (t , notifEnq .Sent [2 ].Data ["report_frequency" ], "week" )
171
- // require.Contains(t, notifEnq.Sent[0].Data["template_versions"], "?")
172
-
173
- require .Equal (t , notifEnq .Sent [3 ].UserID , templateAdmin2 .ID )
174
- require .Equal (t , notifEnq .Sent [3 ].TemplateID , notifications .TemplateWorkspaceBuildsFailedReport )
175
- require .Equal (t , notifEnq .Sent [3 ].Labels ["template_name" ], t2 .Name )
176
- require .Equal (t , notifEnq .Sent [3 ].Labels ["template_display_name" ], t2 .DisplayName )
177
- require .Equal (t , notifEnq .Sent [3 ].Data ["failed_builds" ], int64 (3 ))
178
- require .Equal (t , notifEnq .Sent [3 ].Data ["total_builds" ], int64 (5 ))
179
- require .Equal (t , notifEnq .Sent [3 ].Data ["report_frequency" ], "week" )
180
- // require.Contains(t, notifEnq.Sent[0].Data["template_versions"], "?")
154
+ }, notifEnq .Sent [i ].Data ["template_versions" ])
155
+ }
156
+
157
+ for i , templateAdmin := range []database.User {templateAdmin1 , templateAdmin2 } {
158
+ require .Equal (t , templateAdmin .ID , notifEnq .Sent [i + 2 ].UserID )
159
+ require .Equal (t , notifications .TemplateWorkspaceBuildsFailedReport , notifEnq .Sent [i + 2 ].TemplateID )
160
+ require .Equal (t , t2 .Name , notifEnq .Sent [i + 2 ].Labels ["template_name" ])
161
+ require .Equal (t , t2 .DisplayName , notifEnq .Sent [i + 2 ].Labels ["template_display_name" ])
162
+ require .Equal (t , int64 (3 ), notifEnq .Sent [i + 2 ].Data ["failed_builds" ])
163
+ require .Equal (t , int64 (5 ), notifEnq .Sent [i + 2 ].Data ["total_builds" ])
164
+ require .Equal (t , "week" , notifEnq .Sent [i + 2 ].Data ["report_frequency" ])
165
+ require .Equal (t , []map [string ]interface {}{
166
+ {
167
+ "failed_builds" : []map [string ]interface {}{
168
+ {"build_number" : int32 (8 ), "workspace_name" : w4 .Name , "workspace_owner_username" : user2 .Username },
169
+ },
170
+ "failed_count" : 1 ,
171
+ "template_version_name" : t2v1 .Name ,
172
+ },
173
+ {
174
+ "failed_builds" : []map [string ]interface {}{
175
+ {"build_number" : int32 (6 ), "workspace_name" : w2 .Name , "workspace_owner_username" : user2 .Username },
176
+ {"build_number" : int32 (5 ), "workspace_name" : w2 .Name , "workspace_owner_username" : user2 .Username },
177
+ },
178
+ "failed_count" : 2 ,
179
+ "template_version_name" : t2v2 .Name ,
180
+ },
181
+ }, notifEnq .Sent [i + 2 ].Data ["template_versions" ])
182
+ }
181
183
182
184
// Given: 6 days later (less than report frequency), and failed build
183
185
clk .Advance (6 * dayDuration ).MustWait (context .Background ())
184
186
185
187
now = clk .Now ()
186
188
187
189
w1wb4pj := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {OrganizationID : org .ID , Error : jobError , ErrorCode : jobErrorCode , CompletedAt : sql.NullTime {Time : now .Add (- dayDuration ), Valid : true }})
188
- _ = dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {WorkspaceID : w1 .ID , BuildNumber : 4 , TemplateVersionID : t1v2 .ID , JobID : w1wb4pj .ID , CreatedAt : now .Add (- dayDuration ), Transition : database .WorkspaceTransitionStart , Reason : database .BuildReasonInitiator })
190
+ _ = 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 })
189
191
190
192
notifEnq .Clear ()
191
193
192
194
// When
193
- err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (db , logger ), notifEnq , clk )
195
+ err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
194
196
require .NoError (t , err )
195
197
196
198
// Then
@@ -201,31 +203,32 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
201
203
notifEnq .Clear ()
202
204
203
205
// When
204
- err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (db , logger ), notifEnq , clk )
206
+ err = reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
205
207
require .NoError (t , err )
206
208
207
209
// Then
208
- require .Len (t , notifEnq .Sent , 2 ) // this time a failed job should be reported
209
- require .Equal (t , notifEnq .Sent [0 ].UserID , templateAdmin1 .ID )
210
- require .Equal (t , notifEnq .Sent [0 ].TemplateID , notifications .TemplateWorkspaceBuildsFailedReport )
211
- require .Equal (t , notifEnq .Sent [0 ].Labels ["template_name" ], t1 .Name )
212
- require .Equal (t , notifEnq .Sent [0 ].Labels ["template_display_name" ], t1 .DisplayName )
213
- require .Equal (t , notifEnq .Sent [0 ].Data ["failed_builds" ], int64 (1 ))
214
- require .Equal (t , notifEnq .Sent [0 ].Data ["total_builds" ], int64 (1 ))
215
- require .Equal (t , notifEnq .Sent [0 ].Data ["report_frequency" ], "week" )
216
- // require.Contains(t, notifEnq.Sent[0].Data["template_versions"], "?")
217
-
218
- require .Equal (t , notifEnq .Sent [1 ].UserID , templateAdmin2 .ID )
219
- require .Equal (t , notifEnq .Sent [1 ].TemplateID , notifications .TemplateWorkspaceBuildsFailedReport )
220
- require .Equal (t , notifEnq .Sent [1 ].Labels ["template_name" ], t1 .Name )
221
- require .Equal (t , notifEnq .Sent [1 ].Labels ["template_display_name" ], t1 .DisplayName )
222
- require .Equal (t , notifEnq .Sent [1 ].Data ["failed_builds" ], int64 (1 ))
223
- require .Equal (t , notifEnq .Sent [1 ].Data ["total_builds" ], int64 (1 ))
224
- require .Equal (t , notifEnq .Sent [1 ].Data ["report_frequency" ], "week" )
225
- // require.Contains(t, notifEnq.Sent[1].Data["template_versions"], "?")
210
+ require .Len (t , notifEnq .Sent , 2 ) // a new failed job should be reported
211
+ for i , templateAdmin := range []database.User {templateAdmin1 , templateAdmin2 } {
212
+ require .Equal (t , templateAdmin .ID , notifEnq .Sent [i ].UserID )
213
+ require .Equal (t , notifications .TemplateWorkspaceBuildsFailedReport , notifEnq .Sent [i ].TemplateID )
214
+ require .Equal (t , t1 .Name , notifEnq .Sent [i ].Labels ["template_name" ])
215
+ require .Equal (t , t1 .DisplayName , notifEnq .Sent [i ].Labels ["template_display_name" ])
216
+ require .Equal (t , int64 (1 ), notifEnq .Sent [i ].Data ["failed_builds" ])
217
+ require .Equal (t , int64 (1 ), notifEnq .Sent [i ].Data ["total_builds" ])
218
+ require .Equal (t , "week" , notifEnq .Sent [i ].Data ["report_frequency" ])
219
+ require .Equal (t , []map [string ]interface {}{
220
+ {
221
+ "failed_builds" : []map [string ]interface {}{
222
+ {"build_number" : int32 (77 ), "workspace_name" : w1 .Name , "workspace_owner_username" : user1 .Username },
223
+ },
224
+ "failed_count" : 1 ,
225
+ "template_version_name" : t1v2 .Name ,
226
+ },
227
+ }, notifEnq .Sent [i ].Data ["template_versions" ])
228
+ }
226
229
})
227
230
228
- t .Run ("NoFailedBuilds_TemplateAdminIn_NoReport " , func (t * testing.T ) {
231
+ t .Run ("NoFailedBuilds_NoReport " , func (t * testing.T ) {
229
232
t .Parallel ()
230
233
231
234
// Setup
@@ -264,12 +267,12 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
264
267
notifEnq .Clear ()
265
268
266
269
// When
267
- err := reportFailedWorkspaceBuilds (ctx , logger , authedDB (db , logger ), notifEnq , clk )
270
+ err := reportFailedWorkspaceBuilds (ctx , logger , authedDB (t , db , logger ), notifEnq , clk )
268
271
269
272
// Then
270
273
require .NoError (t , err )
271
274
272
- require .Len (t , notifEnq .Sent , 0 ) // all jobs succeeded so no report
275
+ require .Len (t , notifEnq .Sent , 0 ) // all jobs succeeded so nothing to report
273
276
})
274
277
}
275
278
@@ -285,6 +288,8 @@ func setup(t *testing.T) (context.Context, slog.Logger, database.Store, pubsub.P
285
288
return ctx , logger , db , ps , notifyEnq , clk
286
289
}
287
290
288
- func authedDB (db database.Store , logger slog.Logger ) database.Store {
291
+ func authedDB (t * testing.T , db database.Store , logger slog.Logger ) database.Store {
292
+ t .Helper ()
293
+
289
294
return dbauthz .New (db , rbac .NewAuthorizer (prometheus .NewRegistry ()), logger , coderdtest .AccessControlStorePointer ())
290
295
}
0 commit comments