Chore: Remove result fields from ngalert (#65410)
* remove result fields from ngalert * remove duplicate imports
This commit is contained in:
@@ -83,31 +83,33 @@ func (st DBstore) IncreaseVersionForAllRulesInNamespace(ctx context.Context, org
|
||||
|
||||
// GetAlertRuleByUID is a handler for retrieving an alert rule from that database by its UID and organisation ID.
|
||||
// It returns ngmodels.ErrAlertRuleNotFound if no alert rule is found for the provided ID.
|
||||
func (st DBstore) GetAlertRuleByUID(ctx context.Context, query *ngmodels.GetAlertRuleByUIDQuery) error {
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
func (st DBstore) GetAlertRuleByUID(ctx context.Context, query *ngmodels.GetAlertRuleByUIDQuery) (result *ngmodels.AlertRule, err error) {
|
||||
err = st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
alertRule, err := getAlertRuleByUID(sess, query.UID, query.OrgID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query.Result = alertRule
|
||||
result = alertRule
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
// GetAlertRulesGroupByRuleUID is a handler for retrieving a group of alert rules from that database by UID and organisation ID of one of rules that belong to that group.
|
||||
func (st DBstore) GetAlertRulesGroupByRuleUID(ctx context.Context, query *ngmodels.GetAlertRulesGroupByRuleUIDQuery) error {
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var result []*ngmodels.AlertRule
|
||||
func (st DBstore) GetAlertRulesGroupByRuleUID(ctx context.Context, query *ngmodels.GetAlertRulesGroupByRuleUIDQuery) (result []*ngmodels.AlertRule, err error) {
|
||||
err = st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var rules []*ngmodels.AlertRule
|
||||
err := sess.Table("alert_rule").Alias("a").Join(
|
||||
"INNER",
|
||||
"alert_rule AS b", "a.org_id = b.org_id AND a.namespace_uid = b.namespace_uid AND a.rule_group = b.rule_group AND b.uid = ?", query.UID,
|
||||
).Where("a.org_id = ?", query.OrgID).Select("a.*").Find(&result)
|
||||
).Where("a.org_id = ?", query.OrgID).Select("a.*").Find(&rules)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query.Result = result
|
||||
result = rules
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
// InsertAlertRules is a handler for creating/updating alert rules.
|
||||
@@ -243,8 +245,8 @@ func (st DBstore) CountAlertRulesInFolder(ctx context.Context, query *ngmodels.C
|
||||
}
|
||||
|
||||
// ListAlertRules is a handler for retrieving alert rules of specific organisation.
|
||||
func (st DBstore) ListAlertRules(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error {
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
func (st DBstore) ListAlertRules(ctx context.Context, query *ngmodels.ListAlertRulesQuery) (result ngmodels.RulesGroup, err error) {
|
||||
err = st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
q := sess.Table("alert_rule")
|
||||
|
||||
if query.OrgID >= 0 {
|
||||
@@ -295,9 +297,10 @@ func (st DBstore) ListAlertRules(ctx context.Context, query *ngmodels.ListAlertR
|
||||
alertRules = append(alertRules, rule)
|
||||
}
|
||||
|
||||
query.Result = alertRules
|
||||
result = alertRules
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
// Count returns either the number of the alert rules under a specific org (if orgID is not zero)
|
||||
|
||||
@@ -24,8 +24,8 @@ var (
|
||||
|
||||
// GetLatestAlertmanagerConfiguration returns the lastest version of the alertmanager configuration.
|
||||
// It returns ErrNoAlertmanagerConfiguration if no configuration is found.
|
||||
func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error {
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (result *models.AlertConfiguration, err error) {
|
||||
err = st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
c := &models.AlertConfiguration{}
|
||||
// The ID is already an auto incremental column, using the ID as an order should guarantee the latest.
|
||||
ok, err := sess.Table("alert_configuration").Where("org_id = ?", query.OrgID).Get(c)
|
||||
@@ -37,9 +37,10 @@ func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query
|
||||
return ErrNoAlertmanagerConfiguration
|
||||
}
|
||||
|
||||
query.Result = c
|
||||
result = c
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
// GetAllLatestAlertmanagerConfiguration returns the latest configuration of every organization
|
||||
@@ -162,8 +163,8 @@ func (st *DBstore) MarkConfigurationAsApplied(ctx context.Context, cmd *models.M
|
||||
}
|
||||
|
||||
// GetAppliedConfigurations returns all configurations that have been marked as applied, ordered newest -> oldest by id.
|
||||
func (st *DBstore) GetAppliedConfigurations(ctx context.Context, query *models.GetAppliedConfigurationsQuery) error {
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
func (st *DBstore) GetAppliedConfigurations(ctx context.Context, query *models.GetAppliedConfigurationsQuery) (result []*models.HistoricAlertConfiguration, err error) {
|
||||
err = st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
cfgs := []*models.HistoricAlertConfiguration{}
|
||||
err := sess.Table("alert_configuration_history").
|
||||
Desc("id").
|
||||
@@ -174,9 +175,10 @@ func (st *DBstore) GetAppliedConfigurations(ctx context.Context, query *models.G
|
||||
return err
|
||||
}
|
||||
|
||||
query.Result = cfgs
|
||||
result = cfgs
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (st *DBstore) deleteOldConfigurations(ctx context.Context, orgID int64, limit int) (int64, error) {
|
||||
|
||||
@@ -30,10 +30,10 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
|
||||
OrgID: 1234,
|
||||
}
|
||||
|
||||
err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
|
||||
require.ErrorIs(t, err, ErrNoAlertmanagerConfiguration)
|
||||
require.Nil(t, req.Result)
|
||||
require.Nil(t, config)
|
||||
})
|
||||
|
||||
t.Run("GetLatestAlertmanagerConfiguration return the right config", func(t *testing.T) {
|
||||
@@ -42,12 +42,12 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
|
||||
OrgID: 1,
|
||||
}
|
||||
|
||||
err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, req.Result)
|
||||
require.Equal(t, "my-config", req.Result.AlertmanagerConfiguration)
|
||||
require.Equal(t, configMD5, req.Result.ConfigurationHash)
|
||||
require.NotNil(t, config)
|
||||
require.Equal(t, "my-config", config.AlertmanagerConfiguration)
|
||||
require.Equal(t, configMD5, config.ConfigurationHash)
|
||||
})
|
||||
|
||||
t.Run("GetLatestAlertmanagerConfiguration after saving multiple times should return the latest config", func(t *testing.T) {
|
||||
@@ -58,12 +58,12 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
|
||||
OrgID: 1,
|
||||
}
|
||||
|
||||
err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, req.Result)
|
||||
require.Equal(t, "my-config3", req.Result.AlertmanagerConfiguration)
|
||||
require.Equal(t, configMD5, req.Result.ConfigurationHash)
|
||||
require.NotNil(t, config)
|
||||
require.Equal(t, "my-config3", config.AlertmanagerConfiguration)
|
||||
require.Equal(t, configMD5, config.ConfigurationHash)
|
||||
})
|
||||
|
||||
t.Run("GetAllLatestAlertmanagerConfiguration gets latest config for all orgs", func(t *testing.T) {
|
||||
@@ -109,9 +109,9 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
|
||||
require.ErrorContains(t, err, "callback failed")
|
||||
// Assert that we rolled back the transaction.
|
||||
get := &models.GetLatestAlertmanagerConfigurationQuery{OrgID: 1}
|
||||
err = store.GetLatestAlertmanagerConfiguration(context.Background(), get)
|
||||
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), get)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, get.Result.AlertmanagerConfiguration, "my-config")
|
||||
require.Equal(t, config.AlertmanagerConfiguration, "my-config")
|
||||
})
|
||||
|
||||
t.Run("UpdateAlertmanagerConfiguration returns error if existing config does not exist", func(t *testing.T) {
|
||||
@@ -138,9 +138,9 @@ func TestIntegrationAlertmanagerHash(t *testing.T) {
|
||||
req := &models.GetLatestAlertmanagerConfigurationQuery{
|
||||
OrgID: 1,
|
||||
}
|
||||
err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, configMD5, req.Result.ConfigurationHash)
|
||||
require.Equal(t, configMD5, config.ConfigurationHash)
|
||||
newConfig, newConfigMD5 := "my-config-new", fmt.Sprintf("%x", md5.Sum([]byte("my-config-new")))
|
||||
err = store.UpdateAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
||||
AlertmanagerConfiguration: newConfig,
|
||||
@@ -150,10 +150,10 @@ func TestIntegrationAlertmanagerHash(t *testing.T) {
|
||||
OrgID: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
config, err = store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, newConfig, req.Result.AlertmanagerConfiguration)
|
||||
require.Equal(t, newConfigMD5, req.Result.ConfigurationHash)
|
||||
require.Equal(t, newConfig, config.AlertmanagerConfiguration)
|
||||
require.Equal(t, newConfigMD5, config.ConfigurationHash)
|
||||
})
|
||||
|
||||
t.Run("When passing the wrong hash the update should error", func(t *testing.T) {
|
||||
@@ -161,9 +161,9 @@ func TestIntegrationAlertmanagerHash(t *testing.T) {
|
||||
req := &models.GetLatestAlertmanagerConfigurationQuery{
|
||||
OrgID: 1,
|
||||
}
|
||||
err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, configMD5, req.Result.ConfigurationHash)
|
||||
require.Equal(t, configMD5, amConfig.ConfigurationHash)
|
||||
err = store.UpdateAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
|
||||
AlertmanagerConfiguration: config,
|
||||
FetchedConfigurationHash: "the-wrong-hash",
|
||||
@@ -221,9 +221,9 @@ func TestIntegrationAlertmanagerConfigCleanup(t *testing.T) {
|
||||
req := &models.GetLatestAlertmanagerConfigurationQuery{
|
||||
OrgID: orgID,
|
||||
}
|
||||
err = store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "newest-record", req.Result.AlertmanagerConfiguration)
|
||||
require.Equal(t, "newest-record", amConfig.AlertmanagerConfiguration)
|
||||
})
|
||||
t.Run("when calling the cleanup only the oldest records surpassing the limit should be deleted", func(t *testing.T) {
|
||||
var orgID int64 = 2
|
||||
@@ -261,9 +261,9 @@ func TestIntegrationAlertmanagerConfigCleanup(t *testing.T) {
|
||||
req := &models.GetLatestAlertmanagerConfigurationQuery{
|
||||
OrgID: orgID,
|
||||
}
|
||||
err = store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "newest-record", req.Result.AlertmanagerConfiguration)
|
||||
require.Equal(t, "newest-record", amConfig.AlertmanagerConfiguration)
|
||||
})
|
||||
t.Run("limit set to 0 should fail", func(t *testing.T) {
|
||||
_, err := store.deleteOldConfigurations(context.Background(), 1, 0)
|
||||
@@ -311,19 +311,19 @@ func TestIntegrationMarkConfigurationAsApplied(t *testing.T) {
|
||||
appliedCfgsQuery := models.GetAppliedConfigurationsQuery{
|
||||
OrgID: orgID,
|
||||
}
|
||||
err = store.GetAppliedConfigurations(ctx, &appliedCfgsQuery)
|
||||
configs, err := store.GetAppliedConfigurations(ctx, &appliedCfgsQuery)
|
||||
require.NoError(tt, err)
|
||||
require.Len(tt, appliedCfgsQuery.Result, 0)
|
||||
require.Len(tt, configs, 0)
|
||||
|
||||
query := models.GetLatestAlertmanagerConfigurationQuery{
|
||||
OrgID: orgID,
|
||||
}
|
||||
err = store.GetLatestAlertmanagerConfiguration(ctx, &query)
|
||||
amConfig, err := store.GetLatestAlertmanagerConfiguration(ctx, &query)
|
||||
require.NoError(tt, err)
|
||||
|
||||
cmd := models.MarkConfigurationAsAppliedCmd{
|
||||
OrgID: orgID,
|
||||
ConfigurationHash: query.Result.ConfigurationHash,
|
||||
ConfigurationHash: amConfig.ConfigurationHash,
|
||||
}
|
||||
err = store.MarkConfigurationAsApplied(ctx, &cmd)
|
||||
require.NoError(tt, err)
|
||||
@@ -332,9 +332,9 @@ func TestIntegrationMarkConfigurationAsApplied(t *testing.T) {
|
||||
appliedCfgsQuery = models.GetAppliedConfigurationsQuery{
|
||||
OrgID: orgID,
|
||||
}
|
||||
err = store.GetAppliedConfigurations(ctx, &appliedCfgsQuery)
|
||||
configs, err = store.GetAppliedConfigurations(ctx, &appliedCfgsQuery)
|
||||
require.NoError(tt, err)
|
||||
require.Len(tt, appliedCfgsQuery.Result, 1)
|
||||
require.Len(tt, configs, 1)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ const AlertDefinitionMaxTitleLength = 190
|
||||
|
||||
// AlertingStore is the database interface used by the Alertmanager service.
|
||||
type AlertingStore interface {
|
||||
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error
|
||||
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
|
||||
GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error)
|
||||
SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
|
||||
SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error
|
||||
|
||||
@@ -32,8 +32,8 @@ func (c *GroupDelta) IsEmpty() bool {
|
||||
}
|
||||
|
||||
type RuleReader interface {
|
||||
ListAlertRules(ctx context.Context, query *models.ListAlertRulesQuery) error
|
||||
GetAlertRulesGroupByRuleUID(ctx context.Context, query *models.GetAlertRulesGroupByRuleUIDQuery) error
|
||||
ListAlertRules(ctx context.Context, query *models.ListAlertRulesQuery) (models.RulesGroup, error)
|
||||
GetAlertRulesGroupByRuleUID(ctx context.Context, query *models.GetAlertRulesGroupByRuleUIDQuery) ([]*models.AlertRule, error)
|
||||
}
|
||||
|
||||
// CalculateChanges calculates the difference between rules in the group in the database and the submitted rules. If a submitted rule has UID it tries to find it in the database (in other groups).
|
||||
@@ -45,10 +45,10 @@ func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey model
|
||||
NamespaceUIDs: []string{groupKey.NamespaceUID},
|
||||
RuleGroup: groupKey.RuleGroup,
|
||||
}
|
||||
if err := ruleReader.ListAlertRules(ctx, q); err != nil {
|
||||
existingGroupRules, err := ruleReader.ListAlertRules(ctx, q)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query database for rules in the group %s: %w", groupKey, err)
|
||||
}
|
||||
existingGroupRules := q.Result
|
||||
if len(existingGroupRules) > 0 {
|
||||
affectedGroups[groupKey] = existingGroupRules
|
||||
}
|
||||
@@ -76,10 +76,11 @@ func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey model
|
||||
} else if existing, ok = loadedRulesByUID[r.UID]; !ok { // check the "cache" and if there is no hit, query the database
|
||||
// Rule can be from other group or namespace
|
||||
q := &models.GetAlertRulesGroupByRuleUIDQuery{OrgID: groupKey.OrgID, UID: r.UID}
|
||||
if err := ruleReader.GetAlertRulesGroupByRuleUID(ctx, q); err != nil {
|
||||
ruleList, err := ruleReader.GetAlertRulesGroupByRuleUID(ctx, q)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query database for a group of alert rules: %w", err)
|
||||
}
|
||||
for _, rule := range q.Result {
|
||||
for _, rule := range ruleList {
|
||||
if rule.UID == r.UID {
|
||||
existing = rule
|
||||
}
|
||||
@@ -88,7 +89,7 @@ func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey model
|
||||
if existing == nil {
|
||||
return nil, fmt.Errorf("failed to update rule with UID %s because %w", r.UID, models.ErrAlertRuleNotFound)
|
||||
}
|
||||
affectedGroups[existing.GetGroupKey()] = q.Result
|
||||
affectedGroups[existing.GetGroupKey()] = ruleList
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
|
||||
// ListAlertInstances is a handler for retrieving alert instances within specific organisation
|
||||
// based on various filters.
|
||||
func (st DBstore) ListAlertInstances(ctx context.Context, cmd *models.ListAlertInstancesQuery) error {
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
func (st DBstore) ListAlertInstances(ctx context.Context, cmd *models.ListAlertInstancesQuery) (result []*models.AlertInstance, err error) {
|
||||
err = st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
alertInstances := make([]*models.AlertInstance, 0)
|
||||
|
||||
s := strings.Builder{}
|
||||
@@ -37,9 +37,10 @@ func (st DBstore) ListAlertInstances(ctx context.Context, cmd *models.ListAlertI
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.Result = alertInstances
|
||||
result = alertInstances
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
// SaveAlertInstances saves all the provided alert instances to the store.
|
||||
|
||||
@@ -99,9 +99,9 @@ func TestIntegrationAlertInstanceBulkWrite(t *testing.T) {
|
||||
q := &models.ListAlertInstancesQuery{
|
||||
RuleOrgID: id,
|
||||
}
|
||||
err = dbstore.ListAlertInstances(ctx, q)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, q)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, counts[i], len(q.Result), "Org %v: Expected %v instances but got %v", id, counts[i], len(q.Result))
|
||||
require.Equal(t, counts[i], len(alerts), "Org %v: Expected %v instances but got %v", id, counts[i], len(alerts))
|
||||
}
|
||||
t.Log("Finished database read")
|
||||
|
||||
@@ -113,9 +113,9 @@ func TestIntegrationAlertInstanceBulkWrite(t *testing.T) {
|
||||
q := &models.ListAlertInstancesQuery{
|
||||
RuleOrgID: id,
|
||||
}
|
||||
err = dbstore.ListAlertInstances(ctx, q)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, q)
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, len(q.Result), "Org %v: Deleted instances but still had %v", id, len(q.Result))
|
||||
require.Zero(t, len(alerts), "Org %v: Deleted instances but still had %v", id, len(alerts))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,14 +171,14 @@ func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
||||
RuleOrgID: instance.RuleOrgID,
|
||||
RuleUID: instance.RuleUID,
|
||||
}
|
||||
err = dbstore.ListAlertInstances(ctx, listCmd)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, listCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, listCmd.Result, 1)
|
||||
require.Equal(t, instance.Labels, listCmd.Result[0].Labels)
|
||||
require.Equal(t, alertRule1.OrgID, listCmd.Result[0].RuleOrgID)
|
||||
require.Equal(t, alertRule1.UID, listCmd.Result[0].RuleUID)
|
||||
require.Equal(t, instance.CurrentReason, listCmd.Result[0].CurrentReason)
|
||||
require.Len(t, alerts, 1)
|
||||
require.Equal(t, instance.Labels, alerts[0].Labels)
|
||||
require.Equal(t, alertRule1.OrgID, alerts[0].RuleOrgID)
|
||||
require.Equal(t, alertRule1.UID, alerts[0].RuleUID)
|
||||
require.Equal(t, instance.CurrentReason, alerts[0].CurrentReason)
|
||||
})
|
||||
|
||||
t.Run("can save and read new alert instance with no labels", func(t *testing.T) {
|
||||
@@ -201,13 +201,13 @@ func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
||||
RuleUID: instance.RuleUID,
|
||||
}
|
||||
|
||||
err = dbstore.ListAlertInstances(ctx, listCmd)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, listCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, listCmd.Result, 1)
|
||||
require.Equal(t, alertRule2.OrgID, listCmd.Result[0].RuleOrgID)
|
||||
require.Equal(t, alertRule2.UID, listCmd.Result[0].RuleUID)
|
||||
require.Equal(t, instance.Labels, listCmd.Result[0].Labels)
|
||||
require.Len(t, alerts, 1)
|
||||
require.Equal(t, alertRule2.OrgID, alerts[0].RuleOrgID)
|
||||
require.Equal(t, alertRule2.UID, alerts[0].RuleUID)
|
||||
require.Equal(t, instance.Labels, alerts[0].Labels)
|
||||
})
|
||||
|
||||
t.Run("can save two instances with same org_id, uid and different labels", func(t *testing.T) {
|
||||
@@ -245,10 +245,10 @@ func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
||||
RuleUID: instance1.RuleUID,
|
||||
}
|
||||
|
||||
err = dbstore.ListAlertInstances(ctx, listQuery)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, listQuery.Result, 2)
|
||||
require.Len(t, alerts, 2)
|
||||
})
|
||||
|
||||
t.Run("can list all added instances in org", func(t *testing.T) {
|
||||
@@ -256,10 +256,10 @@ func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
||||
RuleOrgID: orgID,
|
||||
}
|
||||
|
||||
err := dbstore.ListAlertInstances(ctx, listQuery)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, listQuery.Result, 4)
|
||||
require.Len(t, alerts, 4)
|
||||
})
|
||||
|
||||
t.Run("should ignore Normal state with no reason if feature flag is enabled", func(t *testing.T) {
|
||||
@@ -291,10 +291,10 @@ func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
||||
RuleOrgID: orgID,
|
||||
}
|
||||
|
||||
err = dbstore.ListAlertInstances(ctx, listQuery)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
||||
require.NoError(t, err)
|
||||
|
||||
containsHash(t, listQuery.Result, instance1.LabelsHash)
|
||||
containsHash(t, alerts, instance1.LabelsHash)
|
||||
|
||||
f := dbstore.FeatureToggles
|
||||
dbstore.FeatureToggles = featuremgmt.WithFeatures(featuremgmt.FlagAlertingNoNormalState)
|
||||
@@ -302,12 +302,12 @@ func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
||||
dbstore.FeatureToggles = f
|
||||
})
|
||||
|
||||
err = dbstore.ListAlertInstances(ctx, listQuery)
|
||||
alerts, err = dbstore.ListAlertInstances(ctx, listQuery)
|
||||
require.NoError(t, err)
|
||||
|
||||
containsHash(t, listQuery.Result, instance2.LabelsHash)
|
||||
containsHash(t, alerts, instance2.LabelsHash)
|
||||
|
||||
for _, instance := range listQuery.Result {
|
||||
for _, instance := range alerts {
|
||||
if instance.CurrentState == models.InstanceStateNormal && instance.CurrentReason == "" {
|
||||
require.Fail(t, "List operation expected to return all states except Normal but the result contains Normal states")
|
||||
}
|
||||
@@ -347,14 +347,14 @@ func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
||||
RuleUID: alertRule4.UID,
|
||||
}
|
||||
|
||||
err = dbstore.ListAlertInstances(ctx, listQuery)
|
||||
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, listQuery.Result, 1)
|
||||
require.Len(t, alerts, 1)
|
||||
|
||||
require.Equal(t, instance2.RuleOrgID, listQuery.Result[0].RuleOrgID)
|
||||
require.Equal(t, instance2.RuleUID, listQuery.Result[0].RuleUID)
|
||||
require.Equal(t, instance2.Labels, listQuery.Result[0].Labels)
|
||||
require.Equal(t, instance2.CurrentState, listQuery.Result[0].CurrentState)
|
||||
require.Equal(t, instance2.RuleOrgID, alerts[0].RuleOrgID)
|
||||
require.Equal(t, instance2.RuleUID, alerts[0].RuleUID)
|
||||
require.Equal(t, instance2.Labels, alerts[0].Labels)
|
||||
require.Equal(t, instance2.CurrentState, alerts[0].CurrentState)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user