2022-03-23 03:31:46 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2022-12-12 11:11:18 -06:00
|
|
|
"sort"
|
2022-03-23 03:31:46 -05:00
|
|
|
"testing"
|
2023-03-31 15:43:04 -05:00
|
|
|
"time"
|
2022-03-23 03:31:46 -05:00
|
|
|
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2022-07-20 09:54:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-03-23 03:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2024-02-09 08:35:39 -06:00
|
|
|
"github.com/grafana/grafana/pkg/tests/testsuite"
|
2022-03-23 03:31:46 -05:00
|
|
|
)
|
|
|
|
|
2024-02-09 08:35:39 -06:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
testsuite.Run(m)
|
|
|
|
}
|
|
|
|
|
2023-02-02 11:45:17 -06:00
|
|
|
func TestIntegrationAlertmanagerStore(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2022-10-19 08:02:15 -05:00
|
|
|
sqlStore := db.InitTestDB(t)
|
2022-03-23 03:31:46 -05:00
|
|
|
store := &DBstore{
|
|
|
|
SQLStore: sqlStore,
|
2022-07-20 09:54:18 -05:00
|
|
|
Logger: log.NewNopLogger(),
|
2022-03-23 03:31:46 -05:00
|
|
|
}
|
2022-07-20 09:54:18 -05:00
|
|
|
|
2022-12-12 11:11:18 -06:00
|
|
|
t.Run("GetLatestAlertmanagerConfiguration for org that doesn't exist returns error", func(t *testing.T) {
|
|
|
|
_, _ = setupConfig(t, "my-config", store)
|
2023-12-12 06:49:54 -06:00
|
|
|
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1234)
|
2022-12-12 11:11:18 -06:00
|
|
|
|
|
|
|
require.ErrorIs(t, err, ErrNoAlertmanagerConfiguration)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Nil(t, config)
|
2022-12-12 11:11:18 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetLatestAlertmanagerConfiguration return the right config", func(t *testing.T) {
|
2022-07-20 09:54:18 -05:00
|
|
|
_, configMD5 := setupConfig(t, "my-config", store)
|
2023-12-12 06:49:54 -06:00
|
|
|
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
|
2022-12-12 11:11:18 -06:00
|
|
|
|
2022-03-23 03:31:46 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.NotNil(t, config)
|
|
|
|
require.Equal(t, "my-config", config.AlertmanagerConfiguration)
|
|
|
|
require.Equal(t, configMD5, config.ConfigurationHash)
|
2022-03-23 03:31:46 -05:00
|
|
|
})
|
|
|
|
|
2022-12-12 11:11:18 -06:00
|
|
|
t.Run("GetLatestAlertmanagerConfiguration after saving multiple times should return the latest config", func(t *testing.T) {
|
|
|
|
_, _ = setupConfig(t, "my-config1", store)
|
|
|
|
_, _ = setupConfig(t, "my-config2", store)
|
|
|
|
_, configMD5 := setupConfig(t, "my-config3", store)
|
2023-12-12 06:49:54 -06:00
|
|
|
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
|
2022-12-12 11:11:18 -06:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.NotNil(t, config)
|
|
|
|
require.Equal(t, "my-config3", config.AlertmanagerConfiguration)
|
|
|
|
require.Equal(t, configMD5, config.ConfigurationHash)
|
2022-12-12 11:11:18 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetAllLatestAlertmanagerConfiguration gets latest config for all orgs", func(t *testing.T) {
|
|
|
|
_, _ = setupConfigInOrg(t, "my-config1", 1, store)
|
|
|
|
_, _ = setupConfigInOrg(t, "my-config2", 1, store)
|
|
|
|
_, _ = setupConfigInOrg(t, "my-config3", 1, store)
|
|
|
|
_, _ = setupConfigInOrg(t, "my-config1", 2, store)
|
|
|
|
_, _ = setupConfigInOrg(t, "my-config1", 3, store)
|
|
|
|
|
|
|
|
res, err := store.GetAllLatestAlertmanagerConfiguration(context.Background())
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, res, 3)
|
|
|
|
sort.Slice(res, func(i, j int) bool {
|
|
|
|
return res[i].OrgID < res[j].OrgID
|
|
|
|
})
|
|
|
|
require.Equal(t, int64(1), res[0].OrgID)
|
|
|
|
require.Equal(t, int64(2), res[1].OrgID)
|
|
|
|
require.Equal(t, int64(3), res[2].OrgID)
|
|
|
|
require.Equal(t, "my-config3", res[0].AlertmanagerConfiguration)
|
|
|
|
require.Equal(t, "my-config1", res[1].AlertmanagerConfiguration)
|
|
|
|
require.Equal(t, "my-config1", res[2].AlertmanagerConfiguration)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("SaveAlertmanagerConfigurationWithCallback calls callback", func(t *testing.T) {
|
|
|
|
called := false
|
|
|
|
callback := func() error { called = true; return nil }
|
|
|
|
cmd := buildSaveConfigCmd(t, "my-config", 1)
|
|
|
|
|
|
|
|
err := store.SaveAlertmanagerConfigurationWithCallback(context.Background(), &cmd, callback)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, called)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("SaveAlertmanagerConfigurationWithCallback rolls back if callback returns error", func(t *testing.T) {
|
|
|
|
_, _ = setupConfigInOrg(t, "my-config", 1, store)
|
|
|
|
callback := func() error { return fmt.Errorf("callback failed") }
|
|
|
|
cmd := buildSaveConfigCmd(t, "my-config-changed", 1)
|
|
|
|
|
|
|
|
err := store.SaveAlertmanagerConfigurationWithCallback(context.Background(), &cmd, callback)
|
|
|
|
|
|
|
|
require.ErrorContains(t, err, "callback failed")
|
|
|
|
// Assert that we rolled back the transaction.
|
2023-12-12 06:49:54 -06:00
|
|
|
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
|
2022-12-12 11:11:18 -06:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Equal(t, config.AlertmanagerConfiguration, "my-config")
|
2022-12-12 11:11:18 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UpdateAlertmanagerConfiguration returns error if existing config does not exist", func(t *testing.T) {
|
|
|
|
cmd := buildSaveConfigCmd(t, "my-config", 1234)
|
|
|
|
cmd.FetchedConfigurationHash = fmt.Sprintf("%x", md5.Sum([]byte("my-config")))
|
|
|
|
err := store.UpdateAlertmanagerConfiguration(context.Background(), &cmd)
|
|
|
|
|
|
|
|
require.ErrorIs(t, err, ErrVersionLockedObjectNotFound)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-02 11:45:17 -06:00
|
|
|
func TestIntegrationAlertmanagerHash(t *testing.T) {
|
2022-12-12 11:11:18 -06:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
|
|
|
sqlStore := db.InitTestDB(t)
|
|
|
|
store := &DBstore{
|
|
|
|
SQLStore: sqlStore,
|
|
|
|
Logger: log.NewNopLogger(),
|
|
|
|
}
|
|
|
|
|
2022-03-23 03:31:46 -05:00
|
|
|
t.Run("When passing the right hash the config should be updated", func(t *testing.T) {
|
2022-07-20 09:54:18 -05:00
|
|
|
_, configMD5 := setupConfig(t, "my-config", store)
|
2023-12-12 06:49:54 -06:00
|
|
|
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
|
2022-03-23 03:31:46 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Equal(t, configMD5, config.ConfigurationHash)
|
2022-03-23 03:31:46 -05:00
|
|
|
newConfig, newConfigMD5 := "my-config-new", fmt.Sprintf("%x", md5.Sum([]byte("my-config-new")))
|
2022-04-05 16:48:51 -05:00
|
|
|
err = store.UpdateAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
2022-03-23 03:31:46 -05:00
|
|
|
AlertmanagerConfiguration: newConfig,
|
|
|
|
FetchedConfigurationHash: configMD5,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: 1,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-12-12 06:49:54 -06:00
|
|
|
config, err = store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
|
2022-03-23 03:31:46 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Equal(t, newConfig, config.AlertmanagerConfiguration)
|
|
|
|
require.Equal(t, newConfigMD5, config.ConfigurationHash)
|
2022-03-23 03:31:46 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("When passing the wrong hash the update should error", func(t *testing.T) {
|
2022-07-20 09:54:18 -05:00
|
|
|
config, configMD5 := setupConfig(t, "my-config", store)
|
2023-12-12 06:49:54 -06:00
|
|
|
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
|
2022-03-23 03:31:46 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Equal(t, configMD5, amConfig.ConfigurationHash)
|
2022-04-05 16:48:51 -05:00
|
|
|
err = store.UpdateAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
2022-03-23 03:31:46 -05:00
|
|
|
AlertmanagerConfiguration: config,
|
|
|
|
FetchedConfigurationHash: "the-wrong-hash",
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: 1,
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.EqualError(t, ErrVersionLockedObjectNotFound, err.Error())
|
|
|
|
})
|
|
|
|
}
|
2022-07-20 09:54:18 -05:00
|
|
|
|
2023-02-02 11:45:17 -06:00
|
|
|
func TestIntegrationAlertmanagerConfigCleanup(t *testing.T) {
|
2022-07-20 09:54:18 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2022-10-19 08:02:15 -05:00
|
|
|
sqlStore := db.InitTestDB(t)
|
2022-07-20 09:54:18 -05:00
|
|
|
store := &DBstore{
|
|
|
|
SQLStore: sqlStore,
|
|
|
|
Logger: log.NewNopLogger(),
|
|
|
|
}
|
2023-02-02 11:45:17 -06:00
|
|
|
t.Run("when calling the cleanup with fewer records than the limit all records should stay", func(t *testing.T) {
|
2022-07-20 09:54:18 -05:00
|
|
|
var orgID int64 = 3
|
|
|
|
oldestConfig, _ := setupConfig(t, "oldest-record", store)
|
|
|
|
err := store.SaveAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
|
|
|
AlertmanagerConfiguration: oldestConfig,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
olderConfig, _ := setupConfig(t, "older-record", store)
|
|
|
|
err = store.SaveAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
|
|
|
AlertmanagerConfiguration: olderConfig,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
config, _ := setupConfig(t, "newest-record", store)
|
|
|
|
err = store.SaveAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
|
|
|
AlertmanagerConfiguration: config,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-02 11:45:17 -06:00
|
|
|
rowsAffected, err := store.deleteOldConfigurations(context.Background(), orgID, 100)
|
|
|
|
require.Equal(t, int64(0), rowsAffected)
|
2022-07-20 09:54:18 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-12 06:49:54 -06:00
|
|
|
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), orgID)
|
2022-07-20 09:54:18 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Equal(t, "newest-record", amConfig.AlertmanagerConfiguration)
|
2022-07-20 09:54:18 -05:00
|
|
|
})
|
|
|
|
t.Run("when calling the cleanup only the oldest records surpassing the limit should be deleted", func(t *testing.T) {
|
|
|
|
var orgID int64 = 2
|
|
|
|
oldestConfig, _ := setupConfig(t, "oldest-record", store)
|
|
|
|
err := store.SaveAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
|
|
|
AlertmanagerConfiguration: oldestConfig,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
olderConfig, _ := setupConfig(t, "older-record", store)
|
|
|
|
err = store.SaveAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
|
|
|
AlertmanagerConfiguration: olderConfig,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
config, _ := setupConfig(t, "newest-record", store)
|
|
|
|
err = store.SaveAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
|
|
|
AlertmanagerConfiguration: config,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rowsAffacted, err := store.deleteOldConfigurations(context.Background(), orgID, 1)
|
|
|
|
require.Equal(t, int64(2), rowsAffacted)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-12 06:49:54 -06:00
|
|
|
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), orgID)
|
2022-07-20 09:54:18 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Equal(t, "newest-record", amConfig.AlertmanagerConfiguration)
|
2022-07-20 09:54:18 -05:00
|
|
|
})
|
|
|
|
t.Run("limit set to 0 should fail", func(t *testing.T) {
|
|
|
|
_, err := store.deleteOldConfigurations(context.Background(), 1, 0)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("limit set to negative should fail", func(t *testing.T) {
|
|
|
|
_, err := store.deleteOldConfigurations(context.Background(), 1, -1)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-02 11:45:17 -06:00
|
|
|
func TestIntegrationMarkConfigurationAsApplied(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
|
|
|
sqlStore := db.InitTestDB(t)
|
|
|
|
store := &DBstore{
|
|
|
|
SQLStore: sqlStore,
|
|
|
|
Logger: log.NewNopLogger(),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("attempting to mark a non existent config as applied shouldn't fail", func(tt *testing.T) {
|
|
|
|
cmd := models.MarkConfigurationAsAppliedCmd{
|
|
|
|
OrgID: 10,
|
|
|
|
ConfigurationHash: "test",
|
|
|
|
}
|
|
|
|
err := store.MarkConfigurationAsApplied(context.Background(), &cmd)
|
|
|
|
require.NoError(tt, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("marking an existent config should succeed", func(tt *testing.T) {
|
|
|
|
const orgID = 1
|
2023-03-31 15:43:04 -05:00
|
|
|
limit := 10
|
2023-02-02 11:45:17 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
config, _ := setupConfig(t, "test", store)
|
|
|
|
err := store.SaveAlertmanagerConfiguration(ctx, &models.SaveAlertmanagerConfigurationCmd{
|
|
|
|
AlertmanagerConfiguration: config,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(tt, err)
|
|
|
|
|
|
|
|
// Config should be saved but not marked as applied yet.
|
2023-03-31 15:43:04 -05:00
|
|
|
configs, err := store.GetAppliedConfigurations(ctx, orgID, limit)
|
2023-02-02 11:45:17 -06:00
|
|
|
require.NoError(tt, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Len(tt, configs, 0)
|
2023-02-02 11:45:17 -06:00
|
|
|
|
2023-12-12 06:49:54 -06:00
|
|
|
amConfig, err := store.GetLatestAlertmanagerConfiguration(ctx, orgID)
|
2023-02-02 11:45:17 -06:00
|
|
|
require.NoError(tt, err)
|
|
|
|
|
|
|
|
cmd := models.MarkConfigurationAsAppliedCmd{
|
|
|
|
OrgID: orgID,
|
2023-03-28 03:34:35 -05:00
|
|
|
ConfigurationHash: amConfig.ConfigurationHash,
|
2023-02-02 11:45:17 -06:00
|
|
|
}
|
|
|
|
err = store.MarkConfigurationAsApplied(ctx, &cmd)
|
|
|
|
require.NoError(tt, err)
|
|
|
|
|
|
|
|
// Config should now be saved and marked as successfully applied.
|
2023-03-31 15:43:04 -05:00
|
|
|
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)
|
2023-02-02 11:45:17 -06:00
|
|
|
}
|
2023-03-31 15:43:04 -05:00
|
|
|
|
|
|
|
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)
|
2023-02-02 11:45:17 -06:00
|
|
|
require.NoError(tt, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Len(tt, configs, 1)
|
2023-03-31 15:43:04 -05:00
|
|
|
require.Equal(tt, configs[0].ID, highestID)
|
2023-02-02 11:45:17 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:10:03 -05:00
|
|
|
func TestIntegrationGetHistoricalConfiguration(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
|
|
|
sqlStore := db.InitTestDB(t)
|
|
|
|
store := &DBstore{
|
|
|
|
SQLStore: sqlStore,
|
|
|
|
Logger: log.NewNopLogger(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracks the autogenerated PK for the history table.
|
|
|
|
var historyTablePK int64 = 0
|
|
|
|
|
|
|
|
t.Run("no configurations = error", func(tt *testing.T) {
|
|
|
|
_, err := store.GetHistoricalConfiguration(context.Background(), 10, 10)
|
|
|
|
require.Error(tt, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("correct configurations should be returned", func(tt *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var org int64 = 1
|
|
|
|
setupConfigInOrg(t, "testa", org, store)
|
|
|
|
historyTablePK += 1
|
|
|
|
setupConfigInOrg(t, "testb", org, store)
|
|
|
|
historyTablePK += 1
|
|
|
|
|
|
|
|
cfg, err := store.GetHistoricalConfiguration(ctx, org, historyTablePK)
|
|
|
|
require.NoError(tt, err)
|
|
|
|
|
|
|
|
// Check that the returned configuration is the one that we're expecting.
|
|
|
|
require.Equal(tt, "testb", cfg.AlertmanagerConfiguration)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("configurations from other orgs should not be retrievable by id", func(tt *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var org int64 = 1
|
|
|
|
setupConfigInOrg(t, "test1", org, store)
|
|
|
|
historyTablePK += 1
|
|
|
|
|
|
|
|
// Create a config in a different org.
|
|
|
|
var otherOrg int64 = 2
|
|
|
|
setupConfigInOrg(t, "test2", otherOrg, store)
|
|
|
|
historyTablePK += 1
|
|
|
|
|
|
|
|
// Sanity check that config is retrievable with correct org and id.
|
|
|
|
cfg, err := store.GetHistoricalConfiguration(ctx, otherOrg, historyTablePK)
|
|
|
|
require.NoError(tt, err)
|
|
|
|
require.Equal(tt, "test2", cfg.AlertmanagerConfiguration)
|
|
|
|
|
|
|
|
// Verify that we cannot retrieve the config from org=2 when passing in org=1.
|
|
|
|
_, err = store.GetHistoricalConfiguration(ctx, org, historyTablePK)
|
|
|
|
require.Error(tt, err, ErrNoAlertmanagerConfiguration)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:54:18 -05:00
|
|
|
func setupConfig(t *testing.T, config string, store *DBstore) (string, string) {
|
2022-12-12 11:11:18 -06:00
|
|
|
t.Helper()
|
|
|
|
return setupConfigInOrg(t, config, 1, store)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupConfigInOrg(t *testing.T, config string, org int64, store *DBstore) (string, string) {
|
2022-07-20 09:54:18 -05:00
|
|
|
t.Helper()
|
|
|
|
config, configMD5 := config, fmt.Sprintf("%x", md5.Sum([]byte(config)))
|
2022-12-12 11:11:18 -06:00
|
|
|
cmd := buildSaveConfigCmd(t, config, org)
|
|
|
|
err := store.SaveAlertmanagerConfiguration(context.Background(), &cmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return config, configMD5
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildSaveConfigCmd(t *testing.T, config string, org int64) models.SaveAlertmanagerConfigurationCmd {
|
|
|
|
t.Helper()
|
|
|
|
return models.SaveAlertmanagerConfigurationCmd{
|
2022-07-20 09:54:18 -05:00
|
|
|
AlertmanagerConfiguration: config,
|
|
|
|
ConfigurationVersion: "v1",
|
|
|
|
Default: false,
|
2022-12-12 11:11:18 -06:00
|
|
|
OrgID: org,
|
|
|
|
}
|
2022-07-20 09:54:18 -05:00
|
|
|
}
|