Skip to content

Commit 7917f86

Browse files
committed
Tests for getting all configs
Fixes earlier commit
1 parent b7c8465 commit 7917f86

File tree

2 files changed

+72
-77
lines changed

2 files changed

+72
-77
lines changed

pkg/configs/db/postgres/postgres.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ func (d DB) SetRulesConfig(userID string, oldConfig, newConfig configs.RulesConf
169169
}
170170

171171
func (d DB) findRulesConfigs(filter squirrel.Sqlizer) (map[string]configs.VersionedRulesConfig, error) {
172-
rows, err := d.Select("id", "owner_id", "config ->> 'alertmanager_config'").
172+
rows, err := d.Select("id", "owner_id", "config ->> 'rules_files'").
173173
Options("DISTINCT ON (owner_id)").
174174
From("configs").
175175
Where(filter).
176-
Where("config @> 'alertmanager_config' AND config ->> 'alertmanager_config' <> ''").
176+
Where("config ->> 'rules_files' <> '{}'").
177177
OrderBy("owner_id, id DESC").
178178
Query()
179179
if err != nil {
@@ -184,7 +184,12 @@ func (d DB) findRulesConfigs(filter squirrel.Sqlizer) (map[string]configs.Versio
184184
for rows.Next() {
185185
var cfg configs.VersionedRulesConfig
186186
var userID string
187-
err = rows.Scan(&cfg.ID, &userID, &cfg.Config)
187+
var cfgBytes []byte
188+
err = rows.Scan(&cfg.ID, &userID, &cfgBytes)
189+
if err != nil {
190+
return nil, err
191+
}
192+
err = json.Unmarshal(cfgBytes, &cfg.Config)
188193
if err != nil {
189194
return nil, err
190195
}

pkg/ruler/api_test.go

Lines changed: 64 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ const (
2323
)
2424

2525
var (
26-
app *API
27-
database db.DB
28-
counter int
26+
app *API
27+
database db.DB
28+
counter int
29+
privateAPI RulesAPI
2930
)
3031

3132
// setup sets up the environment for the tests.
3233
func setup(t *testing.T) {
3334
database = dbtest.Setup(t)
3435
app = NewAPI(database)
3536
counter = 0
37+
privateAPI = dbStore{db: database}
3638
}
3739

3840
// cleanup cleans up the environment after a test.
@@ -178,74 +180,62 @@ func Test_PostConfig_MultipleUsers(t *testing.T) {
178180
assert.True(t, config2.ID > config1.ID, "%v > %v", config2.ID, config1.ID)
179181
}
180182

181-
// // GetAllConfigs returns an empty list of configs if there aren't any.
182-
// func Test_GetAllConfigs_Empty(t *testing.T) {
183-
// setup(t)
184-
// defer cleanup(t)
185-
186-
// w := request(t, "GET", c.PrivateEndpoint, nil)
187-
// assert.Equal(t, http.StatusOK, w.Code)
188-
// var found api.ConfigsView
189-
// err := json.Unmarshal(w.Body.Bytes(), &found)
190-
// assert.NoError(t, err, "Could not unmarshal JSON")
191-
// assert.Equal(t, api.ConfigsView{Configs: map[string]configs.View{}}, found)
192-
// }
193-
194-
// // GetAllConfigs returns all created configs.
195-
// func Test_GetAllConfigs(t *testing.T) {
196-
// setup(t)
197-
// defer cleanup(t)
198-
199-
// userID := makeUserID()
200-
// config := makeRulerConfig()
201-
202-
// view := c.post(t, userID, config)
203-
// w := request(t, "GET", c.PrivateEndpoint, nil)
204-
// assert.Equal(t, http.StatusOK, w.Code)
205-
// var found api.ConfigsView
206-
// err := json.Unmarshal(w.Body.Bytes(), &found)
207-
// assert.NoError(t, err, "Could not unmarshal JSON")
208-
// assert.Equal(t, api.ConfigsView{Configs: map[string]configs.View{
209-
// userID: view,
210-
// }}, found)
211-
// }
212-
213-
// // GetAllConfigs returns the *newest* versions of all created configs.
214-
// func Test_GetAllConfigs_Newest(t *testing.T) {
215-
// setup(t)
216-
// defer cleanup(t)
217-
218-
// userID := makeUserID()
219-
220-
// c.post(t, userID, makeRulerConfig())
221-
// c.post(t, userID, makeRulerConfig())
222-
// lastCreated := c.post(t, userID, makeRulerConfig())
223-
224-
// w := request(t, "GET", c.PrivateEndpoint, nil)
225-
// assert.Equal(t, http.StatusOK, w.Code)
226-
// var found api.ConfigsView
227-
// err := json.Unmarshal(w.Body.Bytes(), &found)
228-
// assert.NoError(t, err, "Could not unmarshal JSON")
229-
// assert.Equal(t, api.ConfigsView{Configs: map[string]configs.View{
230-
// userID: lastCreated,
231-
// }}, found)
232-
// }
233-
234-
// func Test_GetConfigs_IncludesNewerConfigsAndExcludesOlder(t *testing.T) {
235-
// setup(t)
236-
// defer cleanup(t)
237-
238-
// c.post(t, makeUserID(), makeRulerConfig())
239-
// config2 := c.post(t, makeUserID(), makeRulerConfig())
240-
// userID3 := makeUserID()
241-
// config3 := c.post(t, userID3, makeRulerConfig())
242-
243-
// w := request(t, "GET", fmt.Sprintf("%s?since=%d", c.PrivateEndpoint, config2.ID), nil)
244-
// assert.Equal(t, http.StatusOK, w.Code)
245-
// var found api.ConfigsView
246-
// err := json.Unmarshal(w.Body.Bytes(), &found)
247-
// assert.NoError(t, err, "Could not unmarshal JSON")
248-
// assert.Equal(t, api.ConfigsView{Configs: map[string]configs.View{
249-
// userID3: config3,
250-
// }}, found)
251-
// }
183+
// GetAllConfigs returns an empty list of configs if there aren't any.
184+
func Test_GetAllConfigs_Empty(t *testing.T) {
185+
setup(t)
186+
defer cleanup(t)
187+
188+
configs, err := privateAPI.GetConfigs(0)
189+
assert.NoError(t, err, "error getting configs")
190+
assert.Equal(t, 0, len(configs))
191+
}
192+
193+
// GetAllConfigs returns all created configs.
194+
func Test_GetAllConfigs(t *testing.T) {
195+
setup(t)
196+
defer cleanup(t)
197+
198+
userID := makeUserID()
199+
config := makeRulerConfig()
200+
view := post(t, userID, nil, config)
201+
202+
found, err := privateAPI.GetConfigs(0)
203+
assert.NoError(t, err, "error getting configs")
204+
assert.Equal(t, map[string]configs.VersionedRulesConfig{
205+
userID: view,
206+
}, found)
207+
}
208+
209+
// GetAllConfigs returns the *newest* versions of all created configs.
210+
func Test_GetAllConfigs_Newest(t *testing.T) {
211+
setup(t)
212+
defer cleanup(t)
213+
214+
userID := makeUserID()
215+
216+
config1 := post(t, userID, nil, makeRulerConfig())
217+
config2 := post(t, userID, config1.Config, makeRulerConfig())
218+
lastCreated := post(t, userID, config2.Config, makeRulerConfig())
219+
220+
found, err := privateAPI.GetConfigs(0)
221+
assert.NoError(t, err, "error getting configs")
222+
assert.Equal(t, map[string]configs.VersionedRulesConfig{
223+
userID: lastCreated,
224+
}, found)
225+
}
226+
227+
func Test_GetConfigs_IncludesNewerConfigsAndExcludesOlder(t *testing.T) {
228+
setup(t)
229+
defer cleanup(t)
230+
231+
post(t, makeUserID(), nil, makeRulerConfig())
232+
config2 := post(t, makeUserID(), nil, makeRulerConfig())
233+
userID3 := makeUserID()
234+
config3 := post(t, userID3, nil, makeRulerConfig())
235+
236+
found, err := privateAPI.GetConfigs(config2.ID)
237+
assert.NoError(t, err, "error getting configs")
238+
assert.Equal(t, map[string]configs.VersionedRulesConfig{
239+
userID3: config3,
240+
}, found)
241+
}

0 commit comments

Comments
 (0)