@@ -55,6 +55,19 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
55
55
t .Run ("FailedBuilds_FirstRun_Report_SecondRunTooEarly_NoReport_ThirdRun_Report" , func (t * testing.T ) {
56
56
t .Parallel ()
57
57
58
+ verifyNotification := func (t * testing.T , recipient database.User , notif * testutil.Notification , tmpl database.Template , failedBuilds , totalBuilds int64 , templateVersions []map [string ]interface {}) {
59
+ t .Helper ()
60
+
61
+ require .Equal (t , recipient .ID , notif .UserID )
62
+ require .Equal (t , notifications .TemplateWorkspaceBuildsFailedReport , notif .TemplateID )
63
+ require .Equal (t , tmpl .Name , notif .Labels ["template_name" ])
64
+ require .Equal (t , tmpl .DisplayName , notif .Labels ["template_display_name" ])
65
+ require .Equal (t , failedBuilds , notif .Data ["failed_builds" ])
66
+ require .Equal (t , totalBuilds , notif .Data ["total_builds" ])
67
+ require .Equal (t , "week" , notif .Data ["report_frequency" ])
68
+ require .Equal (t , templateVersions , notif .Data ["template_versions" ])
69
+ }
70
+
58
71
// Setup
59
72
ctx , logger , db , ps , notifEnq , clk := setup (t )
60
73
@@ -128,14 +141,7 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
128
141
129
142
require .Len (t , notifEnq .Sent , 4 ) // 2 templates, 2 template admins
130
143
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 {}{
144
+ verifyNotification (t , templateAdmin , notifEnq .Sent [i ], t1 , 3 , 4 , []map [string ]interface {}{
139
145
{
140
146
"failed_builds" : []map [string ]interface {}{
141
147
{"build_number" : int32 (7 ), "workspace_name" : w3 .Name , "workspace_owner_username" : user1 .Username },
@@ -151,18 +157,11 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
151
157
"failed_count" : 1 ,
152
158
"template_version_name" : t1v2 .Name ,
153
159
},
154
- }, notifEnq . Sent [ i ]. Data [ "template_versions" ] )
160
+ })
155
161
}
156
162
157
163
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 {}{
164
+ verifyNotification (t , templateAdmin , notifEnq .Sent [i + 2 ], t2 , 3 , 5 , []map [string ]interface {}{
166
165
{
167
166
"failed_builds" : []map [string ]interface {}{
168
167
{"build_number" : int32 (8 ), "workspace_name" : w4 .Name , "workspace_owner_username" : user2 .Username },
@@ -178,7 +177,7 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
178
177
"failed_count" : 2 ,
179
178
"template_version_name" : t2v2 .Name ,
180
179
},
181
- }, notifEnq . Sent [ i + 2 ]. Data [ "template_versions" ] )
180
+ })
182
181
}
183
182
184
183
// Given: 6 days later (less than report frequency), and failed build
@@ -209,22 +208,15 @@ func TestReportFailedWorkspaceBuilds(t *testing.T) {
209
208
// Then
210
209
require .Len (t , notifEnq .Sent , 2 ) // a new failed job should be reported
211
210
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 {}{
211
+ verifyNotification (t , templateAdmin , notifEnq .Sent [i ], t1 , 1 , 1 , []map [string ]interface {}{
220
212
{
221
213
"failed_builds" : []map [string ]interface {}{
222
214
{"build_number" : int32 (77 ), "workspace_name" : w1 .Name , "workspace_owner_username" : user1 .Username },
223
215
},
224
216
"failed_count" : 1 ,
225
217
"template_version_name" : t1v2 .Name ,
226
218
},
227
- }, notifEnq . Sent [ i ]. Data [ "template_versions" ] )
219
+ })
228
220
}
229
221
})
230
222
@@ -290,6 +282,5 @@ func setup(t *testing.T) (context.Context, slog.Logger, database.Store, pubsub.P
290
282
291
283
func authedDB (t * testing.T , db database.Store , logger slog.Logger ) database.Store {
292
284
t .Helper ()
293
-
294
285
return dbauthz .New (db , rbac .NewAuthorizer (prometheus .NewRegistry ()), logger , coderdtest .AccessControlStorePointer ())
295
286
}
0 commit comments