Alerting: Refactor integration tests (#99519)

---------

Signed-off-by: Yuri Tseretyan <yuriy.tseretyan@grafana.com>
(cherry picked from commit af663dadc7)
pull/100514/head
Yuri Tseretyan 6 months ago committed by Moustafa Baiou
parent 822e5fb1a1
commit 2728e5cf14
  1. 2041
      pkg/tests/api/alerting/api_alertmanager_test.go
  2. 9
      pkg/tests/api/alerting/api_backtesting_test.go
  3. 2260
      pkg/tests/api/alerting/api_ruler_test.go
  4. 0
      pkg/tests/api/alerting/test-data/api_backtesting_data.json
  5. 34
      pkg/tests/api/alerting/testing.go

File diff suppressed because it is too large Load Diff

@ -4,8 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"os"
"path/filepath"
"path"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/data"
@ -23,7 +22,7 @@ import (
)
func TestBacktesting(t *testing.T) {
dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
dir, grafanaPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
DisableLegacyAlerting: true,
EnableUnifiedAlerting: true,
DisableAnonymous: true,
@ -34,7 +33,7 @@ func TestBacktesting(t *testing.T) {
EnableLog: false,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, grafanaPath)
userId := createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
@ -44,7 +43,7 @@ func TestBacktesting(t *testing.T) {
apiCli := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
input, err := os.ReadFile(filepath.Join("api_backtesting_data.json"))
input, err := testData.ReadFile(path.Join("test-data", "api_backtesting_data.json"))
require.NoError(t, err)
var testData map[string]apimodels.BacktestConfig
require.NoError(t, json.Unmarshal(input, &testData))

File diff suppressed because it is too large Load Diff

@ -2,6 +2,7 @@ package alerting
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
@ -20,11 +21,19 @@ import (
"github.com/grafana/grafana/pkg/api"
"github.com/grafana/grafana/pkg/expr"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/folder"
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/services/org/orgimpl"
"github.com/grafana/grafana/pkg/services/quota"
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
@ -53,6 +62,11 @@ const defaultAlertmanagerConfigJSON = `
}
`
type Response struct {
Message string `json:"message"`
TraceID string `json:"traceID"`
}
func getRequest(t *testing.T, url string, expStatusCode int) *http.Response {
t.Helper()
// nolint:gosec
@ -1008,3 +1022,23 @@ func requireStatusCode(t *testing.T, expected, actual int, response string) {
t.Helper()
require.Equalf(t, expected, actual, "Unexpected status. Response: %s", response)
}
func createUser(t *testing.T, db db.DB, cfg *setting.Cfg, cmd user.CreateUserCommand) int64 {
t.Helper()
cfg.AutoAssignOrg = true
cfg.AutoAssignOrgId = 1
quotaService := quotaimpl.ProvideService(db, cfg)
orgService, err := orgimpl.ProvideService(db, cfg, quotaService)
require.NoError(t, err)
usrSvc, err := userimpl.ProvideService(
db, orgService, cfg, nil, nil, tracing.InitializeTracerForTest(),
quotaService, supportbundlestest.NewFakeBundleService(),
)
require.NoError(t, err)
u, err := usrSvc.Create(context.Background(), &cmd)
require.NoError(t, err)
return u.ID
}

Loading…
Cancel
Save