33
33
// setup sets up the environment for the tests.
34
34
func setup (t * testing.T ) {
35
35
database = dbtest .Setup (t )
36
- app = NewAPI (database )
36
+ app = NewAPI (database , configs . RuleFormatV2 )
37
37
counter = 0
38
38
privateAPI = dbStore {db : database }
39
39
}
@@ -75,22 +75,46 @@ func makeUserID() string {
75
75
}
76
76
77
77
// makeRulerConfig makes an arbitrary ruler config
78
- func makeRulerConfig () configs.RulesConfig {
79
- return configs .RulesConfig (map [string ]string {
80
- "filename.rules" : makeString (`
78
+ func makeRulerConfig (rfv configs.RuleFormatVersion ) configs.RulesConfig {
79
+ switch rfv {
80
+ case configs .RuleFormatV1 :
81
+ return configs .RulesConfig (map [string ]string {
82
+ "filename.rules" : makeString (`
83
+ # Config no. %d.
84
+ ALERT ScrapeFailed
85
+ IF up != 1
86
+ FOR 10m
87
+ LABELS { severity="warning" }
88
+ ANNOTATIONS {
89
+ summary = "Scrape of {{$labels.job}} (pod: {{$labels.instance}}) failed.",
90
+ description = "Prometheus cannot reach the /metrics page on the {{$labels.instance}} pod.",
91
+ impact = "We have no monitoring data for {{$labels.job}} - {{$labels.instance}}. At worst, it's completely down. At best, we cannot reliably respond to operational issues.",
92
+ dashboardURL = "$${base_url}/admin/prometheus/targets",
93
+ }
94
+ ` ),
95
+ })
96
+ case configs .RuleFormatV2 :
97
+ return configs .RulesConfig (map [string ]string {
98
+ "filename.rules" : makeString (`
81
99
# Config no. %d.
82
- ALERT ScrapeFailed
83
- IF up != 1
84
- FOR 10m
85
- LABELS { severity="warning" }
86
- ANNOTATIONS {
87
- summary = "Scrape of {{$labels.job}} (pod: {{$labels.instance}}) failed.",
88
- description = "Prometheus cannot reach the /metrics page on the {{$labels.instance}} pod.",
89
- impact = "We have no monitoring data for {{$labels.job}} - {{$labels.instance}}. At worst, it's completely down. At best, we cannot reliably respond to operational issues.",
90
- dashboardURL = "$${base_url}/admin/prometheus/targets",
91
- }
92
- ` ),
93
- })
100
+ groups:
101
+ - name: example
102
+ rules:
103
+ - alert: ScrapeFailed
104
+ expr: 'up != 1'
105
+ for: 10m
106
+ labels:
107
+ severity: warning
108
+ annotations:
109
+ summary: "Scrape of {{$labels.job}} (pod: {{$labels.instance}}) failed."
110
+ description: "Prometheus cannot reach the /metrics page on the {{$labels.instance}} pod."
111
+ impact: "We have no monitoring data for {{$labels.job}} - {{$labels.instance}}. At worst, it's completely down. At best, we cannot reliably respond to operational issues."
112
+ dashboardURL: "$${base_url}/admin/prometheus/targets"
113
+ ` ),
114
+ })
115
+ default :
116
+ panic ("unknown rule format" )
117
+ }
94
118
}
95
119
96
120
// parseVersionedRulesConfig parses a configs.VersionedRulesConfig from JSON.
@@ -146,7 +170,7 @@ func Test_PostConfig_CreatesConfig(t *testing.T) {
146
170
defer cleanup (t )
147
171
148
172
userID := makeUserID ()
149
- config := makeRulerConfig ()
173
+ config := makeRulerConfig (configs . RuleFormatV2 )
150
174
result := post (t , userID , nil , config )
151
175
assert .Equal (t , config , result .Config )
152
176
}
@@ -177,27 +201,69 @@ func Test_PostConfig_InvalidNewConfig(t *testing.T) {
177
201
}
178
202
}
179
203
180
- // Posting to a configuration sets it so that you can get it again.
181
- func Test_PostConfig_UpdatesConfig (t * testing.T ) {
204
+ // Posting a v1 rule format configuration sets it so that you can get it again.
205
+ func Test_PostConfig_UpdatesConfig_V1RuleFormat (t * testing.T ) {
206
+ setup (t )
207
+ app = NewAPI (database , configs .RuleFormatV1 )
208
+ defer cleanup (t )
209
+
210
+ userID := makeUserID ()
211
+ config1 := makeRulerConfig (configs .RuleFormatV1 )
212
+ view1 := post (t , userID , nil , config1 )
213
+ config2 := makeRulerConfig (configs .RuleFormatV1 )
214
+ view2 := post (t , userID , config1 , config2 )
215
+ assert .True (t , view2 .ID > view1 .ID , "%v > %v" , view2 .ID , view1 .ID )
216
+ assert .Equal (t , config2 , view2 .Config )
217
+ }
218
+
219
+ // Posting an invalid v1 rule format config when there's one already set returns an error and leaves the config as is.
220
+ func Test_PostConfig_InvalidChangedConfig_V1RuleFormat (t * testing.T ) {
221
+ setup (t )
222
+ app = NewAPI (database , configs .RuleFormatV1 )
223
+ defer cleanup (t )
224
+
225
+ userID := makeUserID ()
226
+ config := makeRulerConfig (configs .RuleFormatV1 )
227
+ post (t , userID , nil , config )
228
+ invalidConfig := map [string ]string {
229
+ "some.rules" : "invalid config" ,
230
+ }
231
+ updateRequest := configUpdateRequest {
232
+ OldConfig : nil ,
233
+ NewConfig : invalidConfig ,
234
+ }
235
+ b , err := json .Marshal (updateRequest )
236
+ require .NoError (t , err )
237
+ reader := bytes .NewReader (b )
238
+ {
239
+ w := requestAsUser (t , app , userID , "POST" , endpoint , reader )
240
+ require .Equal (t , http .StatusBadRequest , w .Code )
241
+ }
242
+ result := get (t , userID )
243
+ assert .Equal (t , config , result .Config )
244
+ }
245
+
246
+ // Posting a v2 rule format configuration sets it so that you can get it again.
247
+ func Test_PostConfig_UpdatesConfig_V2RuleFormat (t * testing.T ) {
182
248
setup (t )
183
249
defer cleanup (t )
184
250
185
251
userID := makeUserID ()
186
- config1 := makeRulerConfig ()
252
+ config1 := makeRulerConfig (configs . RuleFormatV2 )
187
253
view1 := post (t , userID , nil , config1 )
188
- config2 := makeRulerConfig ()
254
+ config2 := makeRulerConfig (configs . RuleFormatV2 )
189
255
view2 := post (t , userID , config1 , config2 )
190
256
assert .True (t , view2 .ID > view1 .ID , "%v > %v" , view2 .ID , view1 .ID )
191
257
assert .Equal (t , config2 , view2 .Config )
192
258
}
193
259
194
- // Posting an invalid config when there's one already set returns an error and leaves the config as is.
195
- func Test_PostConfig_InvalidChangedConfig (t * testing.T ) {
260
+ // Posting an invalid v2 rule format config when there's one already set returns an error and leaves the config as is.
261
+ func Test_PostConfig_InvalidChangedConfig_V2RuleFormat (t * testing.T ) {
196
262
setup (t )
197
263
defer cleanup (t )
198
264
199
265
userID := makeUserID ()
200
- config := makeRulerConfig ()
266
+ config := makeRulerConfig (configs . RuleFormatV2 )
201
267
post (t , userID , nil , config )
202
268
invalidConfig := map [string ]string {
203
269
"some.rules" : "invalid config" ,
@@ -224,8 +290,8 @@ func Test_PostConfig_MultipleUsers(t *testing.T) {
224
290
225
291
userID1 := makeUserID ()
226
292
userID2 := makeUserID ()
227
- config1 := post (t , userID1 , nil , makeRulerConfig ())
228
- config2 := post (t , userID2 , nil , makeRulerConfig ())
293
+ config1 := post (t , userID1 , nil , makeRulerConfig (configs . RuleFormatV2 ))
294
+ config2 := post (t , userID2 , nil , makeRulerConfig (configs . RuleFormatV2 ))
229
295
foundConfig1 := get (t , userID1 )
230
296
assert .Equal (t , config1 , foundConfig1 )
231
297
foundConfig2 := get (t , userID2 )
@@ -249,7 +315,7 @@ func Test_GetAllConfigs(t *testing.T) {
249
315
defer cleanup (t )
250
316
251
317
userID := makeUserID ()
252
- config := makeRulerConfig ()
318
+ config := makeRulerConfig (configs . RuleFormatV2 )
253
319
view := post (t , userID , nil , config )
254
320
255
321
found , err := privateAPI .GetConfigs (0 )
@@ -266,9 +332,9 @@ func Test_GetAllConfigs_Newest(t *testing.T) {
266
332
267
333
userID := makeUserID ()
268
334
269
- config1 := post (t , userID , nil , makeRulerConfig ())
270
- config2 := post (t , userID , config1 .Config , makeRulerConfig ())
271
- lastCreated := post (t , userID , config2 .Config , makeRulerConfig ())
335
+ config1 := post (t , userID , nil , makeRulerConfig (configs . RuleFormatV2 ))
336
+ config2 := post (t , userID , config1 .Config , makeRulerConfig (configs . RuleFormatV2 ))
337
+ lastCreated := post (t , userID , config2 .Config , makeRulerConfig (configs . RuleFormatV2 ))
272
338
273
339
found , err := privateAPI .GetConfigs (0 )
274
340
assert .NoError (t , err , "error getting configs" )
@@ -281,10 +347,10 @@ func Test_GetConfigs_IncludesNewerConfigsAndExcludesOlder(t *testing.T) {
281
347
setup (t )
282
348
defer cleanup (t )
283
349
284
- post (t , makeUserID (), nil , makeRulerConfig ())
285
- config2 := post (t , makeUserID (), nil , makeRulerConfig ())
350
+ post (t , makeUserID (), nil , makeRulerConfig (configs . RuleFormatV2 ))
351
+ config2 := post (t , makeUserID (), nil , makeRulerConfig (configs . RuleFormatV2 ))
286
352
userID3 := makeUserID ()
287
- config3 := post (t , userID3 , nil , makeRulerConfig ())
353
+ config3 := post (t , userID3 , nil , makeRulerConfig (configs . RuleFormatV2 ))
288
354
289
355
found , err := privateAPI .GetConfigs (config2 .ID )
290
356
assert .NoError (t , err , "error getting configs" )
@@ -302,14 +368,14 @@ func postAlertmanagerConfig(t *testing.T, userID, configFile string) {
302
368
b , err := json .Marshal (config )
303
369
require .NoError (t , err )
304
370
reader := bytes .NewReader (b )
305
- configsAPI := api .New (database )
371
+ configsAPI := api .New (database , configs . RuleFormatV2 )
306
372
w := requestAsUser (t , configsAPI , userID , "POST" , "/api/prom/configs/alertmanager" , reader )
307
373
require .Equal (t , http .StatusNoContent , w .Code )
308
374
}
309
375
310
376
// getAlertmanagerConfig posts an alertmanager config to the alertmanager configs API.
311
377
func getAlertmanagerConfig (t * testing.T , userID string ) string {
312
- w := requestAsUser (t , api .New (database ), userID , "GET" , "/api/prom/configs/alertmanager" , nil )
378
+ w := requestAsUser (t , api .New (database , configs . RuleFormatV2 ), userID , "GET" , "/api/prom/configs/alertmanager" , nil )
313
379
var x configs.View
314
380
b := w .Body .Bytes ()
315
381
err := json .Unmarshal (b , & x )
@@ -351,7 +417,7 @@ func Test_AlertmanagerConfig_RulerConfigDoesntChangeIt(t *testing.T) {
351
417
- name: noop` )
352
418
postAlertmanagerConfig (t , userID , alertmanagerConfig )
353
419
354
- rulerConfig := makeRulerConfig ()
420
+ rulerConfig := makeRulerConfig (configs . RuleFormatV2 )
355
421
post (t , userID , nil , rulerConfig )
356
422
357
423
newAlertmanagerConfig := getAlertmanagerConfig (t , userID )
0 commit comments