@@ -14,6 +14,7 @@ import (
1414
1515 "github.com/weaveworks/common/user"
1616 "github.com/weaveworks/cortex/pkg/configs"
17+ "github.com/weaveworks/cortex/pkg/configs/api"
1718 "github.com/weaveworks/cortex/pkg/configs/db"
1819 "github.com/weaveworks/cortex/pkg/configs/db/dbtest"
1920)
@@ -43,22 +44,22 @@ func cleanup(t *testing.T) {
4344}
4445
4546// 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 {
4748 w := httptest .NewRecorder ()
4849 r , err := http .NewRequest (method , urlStr , body )
4950 require .NoError (t , err )
50- app .ServeHTTP (w , r )
51+ handler .ServeHTTP (w , r )
5152 return w
5253}
5354
5455// 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 {
5657 w := httptest .NewRecorder ()
5758 r , err := http .NewRequest (method , urlStr , body )
5859 require .NoError (t , err )
5960 r = r .WithContext (user .InjectOrgID (r .Context (), userID ))
6061 user .InjectOrgIDIntoHTTPRequest (r .Context (), r )
61- app .ServeHTTP (w , r )
62+ handler .ServeHTTP (w , r )
6263 return w
6364}
6465
@@ -109,14 +110,14 @@ func post(t *testing.T, userID string, oldConfig configs.RulesConfig, newConfig
109110 b , err := json .Marshal (updateRequest )
110111 require .NoError (t , err )
111112 reader := bytes .NewReader (b )
112- w := requestAsUser (t , userID , "POST" , endpoint , reader )
113+ w := requestAsUser (t , app , userID , "POST" , endpoint , reader )
113114 require .Equal (t , http .StatusNoContent , w .Code )
114115 return get (t , userID )
115116}
116117
117118// get a config
118119func 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 )
120121 return parseVersionedRulesConfig (t , w .Body .Bytes ())
121122}
122123
@@ -126,7 +127,7 @@ func Test_GetConfig_NotFound(t *testing.T) {
126127 defer cleanup (t )
127128
128129 userID := makeUserID ()
129- w := requestAsUser (t , userID , "GET" , endpoint , nil )
130+ w := requestAsUser (t , app , userID , "GET" , endpoint , nil )
130131 assert .Equal (t , http .StatusNotFound , w .Code )
131132}
132133
@@ -135,7 +136,7 @@ func Test_PostConfig_Anonymous(t *testing.T) {
135136 setup (t )
136137 defer cleanup (t )
137138
138- w := request (t , "POST" , endpoint , nil )
139+ w := request (t , app , "POST" , endpoint , nil )
139140 assert .Equal (t , http .StatusUnauthorized , w .Code )
140141}
141142
@@ -167,11 +168,11 @@ func Test_PostConfig_InvalidNewConfig(t *testing.T) {
167168 require .NoError (t , err )
168169 reader := bytes .NewReader (b )
169170 {
170- w := requestAsUser (t , userID , "POST" , endpoint , reader )
171+ w := requestAsUser (t , app , userID , "POST" , endpoint , reader )
171172 require .Equal (t , http .StatusBadRequest , w .Code )
172173 }
173174 {
174- w := requestAsUser (t , userID , "GET" , endpoint , nil )
175+ w := requestAsUser (t , app , userID , "GET" , endpoint , nil )
175176 require .Equal (t , http .StatusNotFound , w .Code )
176177 }
177178}
@@ -209,7 +210,7 @@ func Test_PostConfig_InvalidChangedConfig(t *testing.T) {
209210 require .NoError (t , err )
210211 reader := bytes .NewReader (b )
211212 {
212- w := requestAsUser (t , userID , "POST" , endpoint , reader )
213+ w := requestAsUser (t , app , userID , "POST" , endpoint , reader )
213214 require .Equal (t , http .StatusBadRequest , w .Code )
214215 }
215216 result := get (t , userID )
@@ -292,6 +293,67 @@ func Test_GetConfigs_IncludesNewerConfigsAndExcludesOlder(t *testing.T) {
292293 }, found )
293294}
294295
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