Alerting: Add api client to integration tests (#50970)
This commit is contained in:
@@ -2,12 +2,22 @@ package alerting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/common/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
const defaultAlertmanagerConfigJSON = `
|
||||
@@ -74,3 +84,159 @@ func getBody(t *testing.T, body io.ReadCloser) string {
|
||||
require.NoError(t, err)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func AlertRuleGen() func() apimodels.PostableExtendedRuleNode {
|
||||
return func() apimodels.PostableExtendedRuleNode {
|
||||
return apimodels.PostableExtendedRuleNode{
|
||||
ApiRuleNode: &apimodels.ApiRuleNode{
|
||||
For: model.Duration(10 * time.Second),
|
||||
Labels: map[string]string{"label1": "val1"},
|
||||
Annotations: map[string]string{"annotation1": "val1"},
|
||||
},
|
||||
GrafanaManagedAlert: &apimodels.PostableGrafanaRule{
|
||||
Title: fmt.Sprintf("rule-%s", util.GenerateShortUID()),
|
||||
Condition: "A",
|
||||
Data: []ngmodels.AlertQuery{
|
||||
{
|
||||
RefID: "A",
|
||||
RelativeTimeRange: ngmodels.RelativeTimeRange{
|
||||
From: ngmodels.Duration(time.Duration(5) * time.Hour),
|
||||
To: ngmodels.Duration(time.Duration(3) * time.Hour),
|
||||
},
|
||||
DatasourceUID: "-100",
|
||||
Model: json.RawMessage(`{
|
||||
"type": "math",
|
||||
"expression": "2 + 3 > 1"
|
||||
}`),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateAlertRuleGroup(rulesCount int, gen func() apimodels.PostableExtendedRuleNode) apimodels.PostableRuleGroupConfig {
|
||||
rules := make([]apimodels.PostableExtendedRuleNode, 0, rulesCount)
|
||||
for i := 0; i < rulesCount; i++ {
|
||||
rules = append(rules, gen())
|
||||
}
|
||||
return apimodels.PostableRuleGroupConfig{
|
||||
Name: "arulegroup-" + util.GenerateShortUID(),
|
||||
Interval: model.Duration(10 * time.Second),
|
||||
Rules: rules,
|
||||
}
|
||||
}
|
||||
|
||||
func ConvertGettableRuleGroupToPostable(gettable apimodels.GettableRuleGroupConfig) apimodels.PostableRuleGroupConfig {
|
||||
rules := make([]apimodels.PostableExtendedRuleNode, 0, len(gettable.Rules))
|
||||
for _, rule := range gettable.Rules {
|
||||
rules = append(rules, ConvertGettableRuleToPostable(rule))
|
||||
}
|
||||
return apimodels.PostableRuleGroupConfig{
|
||||
Name: gettable.Name,
|
||||
Interval: gettable.Interval,
|
||||
Rules: rules,
|
||||
}
|
||||
}
|
||||
|
||||
func ConvertGettableRuleToPostable(gettable apimodels.GettableExtendedRuleNode) apimodels.PostableExtendedRuleNode {
|
||||
return apimodels.PostableExtendedRuleNode{
|
||||
ApiRuleNode: gettable.ApiRuleNode,
|
||||
GrafanaManagedAlert: ConvertGettableGrafanaRuleToPostable(gettable.GrafanaManagedAlert),
|
||||
}
|
||||
}
|
||||
|
||||
func ConvertGettableGrafanaRuleToPostable(gettable *apimodels.GettableGrafanaRule) *apimodels.PostableGrafanaRule {
|
||||
if gettable == nil {
|
||||
return nil
|
||||
}
|
||||
return &apimodels.PostableGrafanaRule{
|
||||
Title: gettable.Title,
|
||||
Condition: gettable.Condition,
|
||||
Data: gettable.Data,
|
||||
UID: gettable.UID,
|
||||
NoDataState: gettable.NoDataState,
|
||||
ExecErrState: gettable.ExecErrState,
|
||||
}
|
||||
}
|
||||
|
||||
type apiClient struct {
|
||||
url string
|
||||
}
|
||||
|
||||
func newAlertingApiClient(host, user, pass string) apiClient {
|
||||
if len(user) == 0 && len(pass) == 0 {
|
||||
return apiClient{url: fmt.Sprintf("http://%s", host)}
|
||||
}
|
||||
return apiClient{url: fmt.Sprintf("http://%s:%s@%s", user, pass, host)}
|
||||
}
|
||||
|
||||
// CreateFolder creates a folder for storing our alerts under.
|
||||
func (a apiClient) CreateFolder(t *testing.T, uID string, title string) {
|
||||
t.Helper()
|
||||
payload := fmt.Sprintf(`{"uid": "%s","title": "%s"}`, uID, title)
|
||||
u := fmt.Sprintf("%s/api/folders", a.url)
|
||||
r := strings.NewReader(payload)
|
||||
// nolint:gosec
|
||||
resp, err := http.Post(u, "application/json", r)
|
||||
defer func() {
|
||||
require.NoError(t, resp.Body.Close())
|
||||
}()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
func (a apiClient) PostRulesGroup(t *testing.T, folder string, group *apimodels.PostableRuleGroupConfig) (int, string) {
|
||||
t.Helper()
|
||||
buf := bytes.Buffer{}
|
||||
enc := json.NewEncoder(&buf)
|
||||
err := enc.Encode(group)
|
||||
require.NoError(t, err)
|
||||
|
||||
u := fmt.Sprintf("%s/api/ruler/grafana/api/v1/rules/%s", a.url, folder)
|
||||
// nolint:gosec
|
||||
resp, err := http.Post(u, "application/json", &buf)
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
return resp.StatusCode, string(b)
|
||||
}
|
||||
|
||||
func (a apiClient) GetRulesGroup(t *testing.T, folder string, group string) apimodels.RuleGroupConfigResponse {
|
||||
t.Helper()
|
||||
u := fmt.Sprintf("%s/api/ruler/grafana/api/v1/rules/%s/%s", a.url, folder, group)
|
||||
// nolint:gosec
|
||||
resp, err := http.Get(u)
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusAccepted, resp.StatusCode)
|
||||
|
||||
result := apimodels.RuleGroupConfigResponse{}
|
||||
require.NoError(t, json.Unmarshal(b, &result))
|
||||
return result
|
||||
}
|
||||
|
||||
func (a apiClient) GetAllRulesGroupInFolder(t *testing.T, folder string) apimodels.NamespaceConfigResponse {
|
||||
t.Helper()
|
||||
u := fmt.Sprintf("%s/api/ruler/grafana/api/v1/rules/%s", a.url, folder)
|
||||
// nolint:gosec
|
||||
resp, err := http.Get(u)
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusAccepted, resp.StatusCode)
|
||||
|
||||
result := apimodels.NamespaceConfigResponse{}
|
||||
require.NoError(t, json.Unmarshal(b, &result))
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user