mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user