@@ -14,6 +14,7 @@ import (
14
14
15
15
"github.com/weaveworks/common/user"
16
16
"github.com/weaveworks/cortex/pkg/configs"
17
+ "github.com/weaveworks/cortex/pkg/configs/api"
17
18
"github.com/weaveworks/cortex/pkg/configs/db"
18
19
"github.com/weaveworks/cortex/pkg/configs/db/dbtest"
19
20
)
@@ -43,22 +44,22 @@ func cleanup(t *testing.T) {
43
44
}
44
45
45
46
// request makes a request to the configs API.
46
- func request (t * testing.T , method , urlStr string , body io.Reader ) * httptest.ResponseRecorder {
47
+ func request (t * testing.T , handler http. Handler , method , urlStr string , body io.Reader ) * httptest.ResponseRecorder {
47
48
w := httptest .NewRecorder ()
48
49
r , err := http .NewRequest (method , urlStr , body )
49
50
require .NoError (t , err )
50
- app .ServeHTTP (w , r )
51
+ handler .ServeHTTP (w , r )
51
52
return w
52
53
}
53
54
54
55
// requestAsUser makes a request to the configs API as the given user.
55
- func requestAsUser (t * testing.T , userID string , method , urlStr string , body io.Reader ) * httptest.ResponseRecorder {
56
+ func requestAsUser (t * testing.T , handler http. Handler , userID string , method , urlStr string , body io.Reader ) * httptest.ResponseRecorder {
56
57
w := httptest .NewRecorder ()
57
58
r , err := http .NewRequest (method , urlStr , body )
58
59
require .NoError (t , err )
59
60
r = r .WithContext (user .InjectOrgID (r .Context (), userID ))
60
61
user .InjectOrgIDIntoHTTPRequest (r .Context (), r )
61
- app .ServeHTTP (w , r )
62
+ handler .ServeHTTP (w , r )
62
63
return w
63
64
}
64
65
@@ -109,14 +110,14 @@ func post(t *testing.T, userID string, oldConfig configs.RulesConfig, newConfig
109
110
b , err := json .Marshal (updateRequest )
110
111
require .NoError (t , err )
111
112
reader := bytes .NewReader (b )
112
- w := requestAsUser (t , userID , "POST" , endpoint , reader )
113
+ w := requestAsUser (t , app , userID , "POST" , endpoint , reader )
113
114
require .Equal (t , http .StatusNoContent , w .Code )
114
115
return get (t , userID )
115
116
}
116
117
117
118
// get a config
118
119
func get (t * testing.T , userID string ) configs.VersionedRulesConfig {
119
- w := requestAsUser (t , userID , "GET" , endpoint , nil )
120
+ w := requestAsUser (t , app , userID , "GET" , endpoint , nil )
120
121
return parseVersionedRulesConfig (t , w .Body .Bytes ())
121
122
}
122
123
@@ -126,7 +127,7 @@ func Test_GetConfig_NotFound(t *testing.T) {
126
127
defer cleanup (t )
127
128
128
129
userID := makeUserID ()
129
- w := requestAsUser (t , userID , "GET" , endpoint , nil )
130
+ w := requestAsUser (t , app , userID , "GET" , endpoint , nil )
130
131
assert .Equal (t , http .StatusNotFound , w .Code )
131
132
}
132
133
@@ -135,7 +136,7 @@ func Test_PostConfig_Anonymous(t *testing.T) {
135
136
setup (t )
136
137
defer cleanup (t )
137
138
138
- w := request (t , "POST" , endpoint , nil )
139
+ w := request (t , app , "POST" , endpoint , nil )
139
140
assert .Equal (t , http .StatusUnauthorized , w .Code )
140
141
}
141
142
@@ -167,11 +168,11 @@ func Test_PostConfig_InvalidNewConfig(t *testing.T) {
167
168
require .NoError (t , err )
168
169
reader := bytes .NewReader (b )
169
170
{
170
- w := requestAsUser (t , userID , "POST" , endpoint , reader )
171
+ w := requestAsUser (t , app , userID , "POST" , endpoint , reader )
171
172
require .Equal (t , http .StatusBadRequest , w .Code )
172
173
}
173
174
{
174
- w := requestAsUser (t , userID , "GET" , endpoint , nil )
175
+ w := requestAsUser (t , app , userID , "GET" , endpoint , nil )
175
176
require .Equal (t , http .StatusNotFound , w .Code )
176
177
}
177
178
}
@@ -209,7 +210,7 @@ func Test_PostConfig_InvalidChangedConfig(t *testing.T) {
209
210
require .NoError (t , err )
210
211
reader := bytes .NewReader (b )
211
212
{
212
- w := requestAsUser (t , userID , "POST" , endpoint , reader )
213
+ w := requestAsUser (t , app , userID , "POST" , endpoint , reader )
213
214
require .Equal (t , http .StatusBadRequest , w .Code )
214
215
}
215
216
result := get (t , userID )
@@ -292,6 +293,67 @@ func Test_GetConfigs_IncludesNewerConfigsAndExcludesOlder(t *testing.T) {
292
293
}, found )
293
294
}
294
295
295
- // Test user w/ only alertmanager config doesn't show up in getallconfigs
296
- // Test setting ruler config doesn't change alertmanager config
297
- // Test setting alertmanager config doesn't change ruler config
296
+ // postAlertmanagerConfig posts an alertmanager config to the alertmanager configs API.
297
+ func postAlertmanagerConfig (t * testing.T , userID , configFile string ) {
298
+ config := configs.Config {
299
+ AlertmanagerConfig : configFile ,
300
+ RulesFiles : nil ,
301
+ }
302
+ b , err := json .Marshal (config )
303
+ require .NoError (t , err )
304
+ reader := bytes .NewReader (b )
305
+ configsAPI := api .New (database )
306
+ w := requestAsUser (t , configsAPI , userID , "POST" , "/api/prom/configs/alertmanager" , reader )
307
+ require .Equal (t , http .StatusNoContent , w .Code )
308
+ }
309
+
310
+ // getAlertmanagerConfig posts an alertmanager config to the alertmanager configs API.
311
+ func getAlertmanagerConfig (t * testing.T , userID string ) string {
312
+ w := requestAsUser (t , api .New (database ), userID , "GET" , "/api/prom/configs/alertmanager" , nil )
313
+ var x configs.View
314
+ b := w .Body .Bytes ()
315
+ err := json .Unmarshal (b , & x )
316
+ require .NoError (t , err , "Could not unmarshal JSON: %v" , string (b ))
317
+ return x .Config .AlertmanagerConfig
318
+ }
319
+
320
+ // If a user has only got alertmanager config set, then we learn nothing about them via GetConfigs.
321
+ func Test_AlertmanagerConfig_NotInAllConfigs (t * testing.T ) {
322
+ setup (t )
323
+ defer cleanup (t )
324
+
325
+ config := makeString (`
326
+ # Config no. %d.
327
+ route:
328
+ receiver: noop
329
+
330
+ receivers:
331
+ - name: noop` )
332
+ postAlertmanagerConfig (t , makeUserID (), config )
333
+
334
+ found , err := privateAPI .GetConfigs (0 )
335
+ assert .NoError (t , err , "error getting configs" )
336
+ assert .Equal (t , map [string ]configs.VersionedRulesConfig {}, found )
337
+ }
338
+
339
+ // Setting a ruler config doesn't change alertmanager config.
340
+ func Test_AlertmanagerConfig_RulerConfigDoesntChangeIt (t * testing.T ) {
341
+ setup (t )
342
+ defer cleanup (t )
343
+
344
+ userID := makeUserID ()
345
+ alertmanagerConfig := makeString (`
346
+ # Config no. %d.
347
+ route:
348
+ receiver: noop
349
+
350
+ receivers:
351
+ - name: noop` )
352
+ postAlertmanagerConfig (t , userID , alertmanagerConfig )
353
+
354
+ rulerConfig := makeRulerConfig ()
355
+ post (t , userID , nil , rulerConfig )
356
+
357
+ newAlertmanagerConfig := getAlertmanagerConfig (t , userID )
358
+ assert .Equal (t , alertmanagerConfig , newAlertmanagerConfig )
359
+ }
0 commit comments