Skip to content

Commit 38d81fc

Browse files
committed
Tests for invalid configs
1 parent 37c8b1a commit 38d81fc

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

pkg/ruler/api_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,32 @@ func Test_PostConfig_CreatesConfig(t *testing.T) {
150150
assert.Equal(t, config, result.Config)
151151
}
152152

153+
// Posting an invalid config when there's none set returns an error and leaves the config unset.
154+
func Test_PostConfig_InvalidNewConfig(t *testing.T) {
155+
setup(t)
156+
defer cleanup(t)
157+
158+
userID := makeUserID()
159+
invalidConfig := map[string]string{
160+
"some.rules": "invalid config",
161+
}
162+
updateRequest := configUpdateRequest{
163+
OldConfig: nil,
164+
NewConfig: invalidConfig,
165+
}
166+
b, err := json.Marshal(updateRequest)
167+
require.NoError(t, err)
168+
reader := bytes.NewReader(b)
169+
{
170+
w := requestAsUser(t, userID, "POST", endpoint, reader)
171+
require.Equal(t, http.StatusBadRequest, w.Code)
172+
}
173+
{
174+
w := requestAsUser(t, userID, "GET", endpoint, nil)
175+
require.Equal(t, http.StatusNotFound, w.Code)
176+
}
177+
}
178+
153179
// Posting to a configuration sets it so that you can get it again.
154180
func Test_PostConfig_UpdatesConfig(t *testing.T) {
155181
setup(t)
@@ -164,6 +190,32 @@ func Test_PostConfig_UpdatesConfig(t *testing.T) {
164190
assert.Equal(t, config2, view2.Config)
165191
}
166192

193+
// Posting an invalid config when there's one already set returns an error and leaves the config as is.
194+
func Test_PostConfig_InvalidChangedConfig(t *testing.T) {
195+
setup(t)
196+
defer cleanup(t)
197+
198+
userID := makeUserID()
199+
config := makeRulerConfig()
200+
post(t, userID, nil, config)
201+
invalidConfig := map[string]string{
202+
"some.rules": "invalid config",
203+
}
204+
updateRequest := configUpdateRequest{
205+
OldConfig: nil,
206+
NewConfig: invalidConfig,
207+
}
208+
b, err := json.Marshal(updateRequest)
209+
require.NoError(t, err)
210+
reader := bytes.NewReader(b)
211+
{
212+
w := requestAsUser(t, userID, "POST", endpoint, reader)
213+
require.Equal(t, http.StatusBadRequest, w.Code)
214+
}
215+
result := get(t, userID)
216+
assert.Equal(t, config, result.Config)
217+
}
218+
167219
// Different users can have different configurations.
168220
func Test_PostConfig_MultipleUsers(t *testing.T) {
169221
setup(t)
@@ -241,6 +293,5 @@ func Test_GetConfigs_IncludesNewerConfigsAndExcludesOlder(t *testing.T) {
241293
}
242294

243295
// Test user w/ only alertmanager config doesn't show up in getallconfigs
244-
// Test posting invalid config
245296
// Test setting ruler config doesn't change alertmanager config
246297
// Test setting alertmanager config doesn't change ruler config

0 commit comments

Comments
 (0)