Alerting: Add endpoint to revert to a previous alertmanager configuration (#65751)

* Alerting: Add endpoint to revert to a previous alertmanager configuration

This endpoint is meant to be used in conjunction with /api/alertmanager/grafana/config/history to
revert to a previously applied alertmanager configuration. This is done by ID instead of raw config
string in order to avoid secure field complications.
This commit is contained in:
Matthew Jacobson
2023-04-05 14:10:03 -04:00
committed by GitHub
parent 6c6427e63f
commit 85f738cdf9
16 changed files with 393 additions and 9 deletions

View File

@@ -425,6 +425,61 @@ func TestIntegrationGetAppliedConfigurations(t *testing.T) {
})
}
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)
})
}
func setupConfig(t *testing.T, config string, store *DBstore) (string, string) {
t.Helper()
return setupConfigInOrg(t, config, 1, store)