mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Fetch all applied alerting configurations (#65728)
* WIP * skip invalid historic configurations instead of erroring * add warning log when bad historic config is found * remove unused custom marshaller for GettableHistoricUserConfig * add id to historic user config, move limit check to store, fix typo * swagger spec
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@@ -296,6 +297,7 @@ func TestIntegrationMarkConfigurationAsApplied(t *testing.T) {
|
||||
|
||||
t.Run("marking an existent config should succeed", func(tt *testing.T) {
|
||||
const orgID = 1
|
||||
limit := 10
|
||||
ctx := context.Background()
|
||||
|
||||
config, _ := setupConfig(t, "test", store)
|
||||
@@ -308,10 +310,7 @@ func TestIntegrationMarkConfigurationAsApplied(t *testing.T) {
|
||||
require.NoError(tt, err)
|
||||
|
||||
// Config should be saved but not marked as applied yet.
|
||||
appliedCfgsQuery := models.GetAppliedConfigurationsQuery{
|
||||
OrgID: orgID,
|
||||
}
|
||||
configs, err := store.GetAppliedConfigurations(ctx, &appliedCfgsQuery)
|
||||
configs, err := store.GetAppliedConfigurations(ctx, orgID, limit)
|
||||
require.NoError(tt, err)
|
||||
require.Len(tt, configs, 0)
|
||||
|
||||
@@ -329,15 +328,103 @@ func TestIntegrationMarkConfigurationAsApplied(t *testing.T) {
|
||||
require.NoError(tt, err)
|
||||
|
||||
// Config should now be saved and marked as successfully applied.
|
||||
appliedCfgsQuery = models.GetAppliedConfigurationsQuery{
|
||||
OrgID: orgID,
|
||||
}
|
||||
configs, err = store.GetAppliedConfigurations(ctx, &appliedCfgsQuery)
|
||||
configs, err = store.GetAppliedConfigurations(ctx, orgID, limit)
|
||||
require.NoError(tt, err)
|
||||
require.Len(tt, configs, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationGetAppliedConfigurations(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
sqlStore := db.InitTestDB(t)
|
||||
store := &DBstore{
|
||||
SQLStore: sqlStore,
|
||||
Logger: log.NewNopLogger(),
|
||||
}
|
||||
|
||||
t.Run("no configurations = empty slice", func(tt *testing.T) {
|
||||
configs, err := store.GetAppliedConfigurations(context.Background(), 10, 10)
|
||||
require.NoError(tt, err)
|
||||
require.NotNil(tt, configs)
|
||||
require.Len(tt, configs, 0)
|
||||
})
|
||||
|
||||
t.Run("saved configurations marked as applied should be returned", func(tt *testing.T) {
|
||||
ctx := context.Background()
|
||||
var org int64 = 1
|
||||
limit := 10
|
||||
unmarkedConfig, _ := setupConfig(t, "unmarked", store)
|
||||
|
||||
// Save four configurations for the same org.
|
||||
for i := 0; i < 4; i++ {
|
||||
config, _ := setupConfig(t, fmt.Sprintf("test-%d", i+1), store)
|
||||
cmd := &models.SaveAlertmanagerConfigurationCmd{
|
||||
AlertmanagerConfiguration: config,
|
||||
ConfigurationVersion: "v1",
|
||||
Default: false,
|
||||
OrgID: org,
|
||||
LastApplied: time.Now().UTC().Unix(),
|
||||
}
|
||||
|
||||
// Don't mark the third config, that way we have 2 marked, 1 unmarked, 1 marked.
|
||||
if i == 2 {
|
||||
cmd.LastApplied = 0
|
||||
cmd.AlertmanagerConfiguration = unmarkedConfig
|
||||
}
|
||||
|
||||
err := store.SaveAlertmanagerConfiguration(ctx, cmd)
|
||||
require.NoError(tt, err)
|
||||
}
|
||||
|
||||
// Save some configs for other orgs.
|
||||
for i := 0; i < 4; i++ {
|
||||
config, _ := setupConfig(t, fmt.Sprintf("test-%d", i+1), store)
|
||||
cmd := &models.SaveAlertmanagerConfigurationCmd{
|
||||
AlertmanagerConfiguration: config,
|
||||
ConfigurationVersion: "v1",
|
||||
Default: false,
|
||||
OrgID: int64(i) + org + 1, // This way we avoid saving more configs for the same org.
|
||||
LastApplied: time.Now().UTC().Unix(),
|
||||
}
|
||||
|
||||
err := store.SaveAlertmanagerConfiguration(ctx, cmd)
|
||||
require.NoError(tt, err)
|
||||
}
|
||||
|
||||
configs, err := store.GetAppliedConfigurations(ctx, org, limit)
|
||||
require.NoError(tt, err)
|
||||
require.Len(tt, configs, 3)
|
||||
|
||||
var lastID int64
|
||||
for _, config := range configs {
|
||||
// Check that the returned configurations are the ones that we're expecting.
|
||||
require.NotEqual(tt, config.AlertConfiguration.AlertmanagerConfiguration, unmarkedConfig)
|
||||
|
||||
// Configs should only belong to the queried org.
|
||||
require.Equal(tt, org, config.OrgID)
|
||||
|
||||
// LastApplied must not be zero.
|
||||
require.NotZero(tt, config.LastApplied)
|
||||
|
||||
// Configs should be returned in descending order (id).
|
||||
if lastID != 0 {
|
||||
require.LessOrEqual(tt, config.AlertConfiguration.ID, lastID)
|
||||
}
|
||||
lastID = config.AlertConfiguration.ID
|
||||
}
|
||||
|
||||
// The limit should be considered by the store.
|
||||
// The only record returned should be the latest one (highest id).
|
||||
highestID := configs[0].ID
|
||||
configs, err = store.GetAppliedConfigurations(ctx, org, 1)
|
||||
require.NoError(tt, err)
|
||||
require.Len(tt, configs, 1)
|
||||
require.Equal(tt, configs[0].ID, highestID)
|
||||
})
|
||||
}
|
||||
|
||||
func setupConfig(t *testing.T, config string, store *DBstore) (string, string) {
|
||||
t.Helper()
|
||||
return setupConfigInOrg(t, config, 1, store)
|
||||
|
||||
Reference in New Issue
Block a user