Alerting: Move some tests to full integration tests (#108747)

* Alerting: Move some tests to full integration tests

* add back two error handling tests

* imports

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
Stephanie Hingtgen
2025-07-29 07:15:26 -05:00
committed by GitHub
parent 0c10600f95
commit 1716173f71
5 changed files with 656 additions and 511 deletions
@@ -1184,3 +1184,128 @@ func TestIntegrationExportFileProvisionContactPoints(t *testing.T) {
})
})
}
func TestIntegrationFullpath(t *testing.T) {
dir, p := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
DisableLegacyAlerting: true,
EnableUnifiedAlerting: true,
DisableAnonymous: true,
AppModeProduction: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, p)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "grafana", "password")
namespaceUID := "my-namespace"
namespaceTitle := namespaceUID
apiClient.CreateFolder(t, namespaceUID, namespaceTitle)
t.Run("for a rule under a root folder should set the right fullpath", func(t *testing.T) {
interval, err := model.ParseDuration("1m")
require.NoError(t, err)
doubleInterval := 2 * interval
rules := definitions.PostableRuleGroupConfig{
Name: "group",
Interval: interval,
Rules: []definitions.PostableExtendedRuleNode{
{
ApiRuleNode: &definitions.ApiRuleNode{
For: &doubleInterval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
GrafanaManagedAlert: &definitions.PostableGrafanaRule{
Title: "rule",
Condition: "A",
Data: []definitions.AlertQuery{
{
RefID: "A",
RelativeTimeRange: definitions.RelativeTimeRange{
From: definitions.Duration(time.Duration(5) * time.Hour),
To: definitions.Duration(time.Duration(3) * time.Hour),
},
DatasourceUID: expr.DatasourceUID,
Model: json.RawMessage(`{
"type": "math",
"expression": "2 + 3 > 1"
}`),
},
},
},
},
},
}
resp, status, _ := apiClient.PostRulesGroupWithStatus(t, namespaceUID, &rules, false)
require.Equal(t, http.StatusAccepted, status)
require.Len(t, resp.Created, 1)
ruleUID := resp.Created[0]
status, response := apiClient.GetProvisioningAlertRuleExport(t, ruleUID, &definitions.ExportQueryParams{Format: "json"})
require.Equal(t, http.StatusOK, status)
var export definitions.AlertingFileExport
require.NoError(t, json.Unmarshal([]byte(response), &export))
require.Len(t, export.Groups, 1)
assert.Equal(t, "", export.Groups[0].Folder)
})
t.Run("for a rule under a subfolder should set the right fullpath", func(t *testing.T) {
otherNamespaceUID := "my-other-namespace"
otherNamespaceTitle := "my-other-namespace containing multiple //"
apiClient.CreateFolder(t, otherNamespaceUID, otherNamespaceTitle, namespaceUID)
interval, err := model.ParseDuration("1m")
require.NoError(t, err)
doubleInterval := 2 * interval
rules := definitions.PostableRuleGroupConfig{
Name: "group-2",
Interval: interval,
Rules: []definitions.PostableExtendedRuleNode{
{
ApiRuleNode: &definitions.ApiRuleNode{
For: &doubleInterval,
Labels: map[string]string{"label1": "val1"},
Annotations: map[string]string{"annotation1": "val1"},
},
GrafanaManagedAlert: &definitions.PostableGrafanaRule{
Title: "rule-2",
Condition: "A",
Data: []definitions.AlertQuery{
{
RefID: "A",
RelativeTimeRange: definitions.RelativeTimeRange{
From: definitions.Duration(time.Duration(5) * time.Hour),
To: definitions.Duration(time.Duration(3) * time.Hour),
},
DatasourceUID: expr.DatasourceUID,
Model: json.RawMessage(`{
"type": "math",
"expression": "2 + 3 > 1"
}`),
},
},
},
},
},
}
resp, status, _ := apiClient.PostRulesGroupWithStatus(t, otherNamespaceUID, &rules, false)
require.Equal(t, http.StatusAccepted, status)
require.Len(t, resp.Created, 1)
ruleUID := resp.Created[0]
status, response := apiClient.GetProvisioningAlertRuleExport(t, ruleUID, &definitions.ExportQueryParams{Format: "json"})
require.Equal(t, http.StatusOK, status)
var export definitions.AlertingFileExport
require.NoError(t, json.Unmarshal([]byte(response), &export))
require.Len(t, export.Groups, 1)
assert.Equal(t, "my-namespace/my-other-namespace containing multiple //", export.Groups[0].Folder)
})
}