Skip to content

Commit 9c22c68

Browse files
committed
Tests for alertmanager interaction
1 parent 38d81fc commit 9c22c68

File tree

5 files changed

+107
-25
lines changed

5 files changed

+107
-25
lines changed

pkg/configs/configs.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ type View struct {
3131
}
3232

3333
// GetVersionedRulesConfig specializes the view to just the rules config.
34-
func (v View) GetVersionedRulesConfig() VersionedRulesConfig {
35-
return VersionedRulesConfig{
34+
func (v View) GetVersionedRulesConfig() *VersionedRulesConfig {
35+
if v.Config.RulesFiles == nil {
36+
return nil
37+
}
38+
return &VersionedRulesConfig{
3639
ID: v.ID,
3740
Config: v.Config.RulesFiles,
3841
}

pkg/configs/db/memory/memory.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,20 @@ func (d *DB) Close() error {
5858
return nil
5959
}
6060

61-
// GetRulesConfig gets the alertmanager config for a user.
61+
// GetRulesConfig gets the rules config for a user.
6262
func (d *DB) GetRulesConfig(userID string) (configs.VersionedRulesConfig, error) {
6363
c, ok := d.cfgs[userID]
6464
if !ok {
6565
return configs.VersionedRulesConfig{}, sql.ErrNoRows
6666
}
67-
return c.GetVersionedRulesConfig(), nil
67+
cfg := c.GetVersionedRulesConfig()
68+
if cfg == nil {
69+
return configs.VersionedRulesConfig{}, sql.ErrNoRows
70+
}
71+
return *cfg, nil
6872
}
6973

70-
// SetRulesConfig sets the alertmanager config for a user.
74+
// SetRulesConfig sets the rules config for a user.
7175
func (d *DB) SetRulesConfig(userID string, oldConfig, newConfig configs.RulesConfig) (bool, error) {
7276
c, ok := d.cfgs[userID]
7377
if !ok {
@@ -82,24 +86,30 @@ func (d *DB) SetRulesConfig(userID string, oldConfig, newConfig configs.RulesCon
8286
})
8387
}
8488

85-
// GetAllRulesConfigs gets the alertmanager configs for all users that have them.
89+
// GetAllRulesConfigs gets the rules configs for all users that have them.
8690
func (d *DB) GetAllRulesConfigs() (map[string]configs.VersionedRulesConfig, error) {
8791
cfgs := map[string]configs.VersionedRulesConfig{}
8892
for user, c := range d.cfgs {
89-
cfgs[user] = c.GetVersionedRulesConfig()
93+
cfg := c.GetVersionedRulesConfig()
94+
if cfg != nil {
95+
cfgs[user] = *cfg
96+
}
9097
}
9198
return cfgs, nil
9299
}
93100

94-
// GetRulesConfigs gets the alertmanager configs that have changed
101+
// GetRulesConfigs gets the rules configs that have changed
95102
// since the given config version.
96103
func (d *DB) GetRulesConfigs(since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
97104
cfgs := map[string]configs.VersionedRulesConfig{}
98105
for user, c := range d.cfgs {
99106
if c.ID <= since {
100107
continue
101108
}
102-
cfgs[user] = c.GetVersionedRulesConfig()
109+
cfg := c.GetVersionedRulesConfig()
110+
if cfg != nil {
111+
cfgs[user] = *cfg
112+
}
103113
}
104114
return cfgs, nil
105115
}

pkg/configs/db/postgres/postgres.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ func (d DB) GetRulesConfig(userID string) (configs.VersionedRulesConfig, error)
141141
if err != nil {
142142
return configs.VersionedRulesConfig{}, err
143143
}
144-
return current.GetVersionedRulesConfig(), nil
144+
cfg := current.GetVersionedRulesConfig()
145+
if cfg == nil {
146+
return configs.VersionedRulesConfig{}, sql.ErrNoRows
147+
}
148+
return *cfg, nil
145149
}
146150

147151
// SetRulesConfig sets the current alertmanager config for a user.

pkg/ruler/api_test.go

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
118119
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)
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+
}

pkg/ruler/configs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ func (c configsClient) GetConfigs(since configs.ID) (map[string]configs.Versione
7575
}
7676
configs := map[string]configs.VersionedRulesConfig{}
7777
for id, view := range response.Configs {
78-
configs[id] = view.GetVersionedRulesConfig()
78+
cfg := view.GetVersionedRulesConfig()
79+
if cfg != nil {
80+
configs[id] = *cfg
81+
}
7982
}
8083
return configs, nil
8184
}

0 commit comments

Comments
 (0)