mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 01:12:09 +08:00

* Revert "chore: add replDB to team service (#91799)" This reverts commit c6ae2d7999aa6fc797db39e9d66c6fea70278f83. * Revert "experiment: use read replica for Get and Find Dashboards (#91706)" This reverts commit 54177ca619dbb5ded2dcb158405802d8dbdbc982. * Revert "QuotaService: refactor to use ReplDB for Get queries (#91333)" This reverts commit 299c142f6a6e8c5673cfdea9f87b56ac304f9834. * Revert "refactor replCfg to look more like plugins/plugin config (#91142)" This reverts commit ac0b4bb34d495914cbe8daad85b7c75c31e8070d. * Revert "chore (replstore): fix registration with multiple sql drivers, again (#90990)" This reverts commit daedb358dded00d349d9fac6106aaaa6bf18322e. * Revert "Chore (sqlstore): add validation and testing for repl config (#90683)" This reverts commit af19f039b62d9945377292a8e679ee258fd56b3d. * Revert "ReplStore: Add support for round robin load balancing between multiple read replicas (#90530)" This reverts commit 27b52b1507f5218a7b38046b4d96bc004d949d46. * Revert "DashboardStore: Use ReplDB and get dashboard quotas from the ReadReplica (#90235)" This reverts commit 8a6107cd35f6444c0674ee4230d3d6bcfbbd4a58. * Revert "accesscontrol service read replica (#89963)" This reverts commit 77a4869fcadf13827d76d5767d4de74812d6dd6d. * Revert "Fix: add mapping for the new mysqlRepl driver (#89551)" This reverts commit ab5a079bcc5b0f0a6929f0a3742eb2859d4a3498. * Revert "fix: sql instrumentation dual registration error (#89508)" This reverts commit d988f5c3b064fade6e96511e0024190c22d48e50. * Revert "Experimental Feature Toggle: databaseReadReplica (#89232)" This reverts commit 50244ed4a1435cbf3e3c87d4af34fd7937f7c259.
206 lines
5.0 KiB
Go
206 lines
5.0 KiB
Go
package correlations
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
|
"github.com/grafana/grafana/pkg/server"
|
|
"github.com/grafana/grafana/pkg/services/correlations"
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
|
"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/tests/testinfra"
|
|
"github.com/grafana/grafana/pkg/tests/testsuite"
|
|
)
|
|
|
|
type errorResponseBody struct {
|
|
Message string `json:"message"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
type TestContext struct {
|
|
env server.TestEnv
|
|
t *testing.T
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
testsuite.Run(m)
|
|
}
|
|
|
|
func NewTestEnv(t *testing.T) TestContext {
|
|
t.Helper()
|
|
dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
|
|
DisableAnonymous: true,
|
|
})
|
|
_, env := testinfra.StartGrafanaEnv(t, dir, path)
|
|
|
|
return TestContext{
|
|
env: *env,
|
|
t: t,
|
|
}
|
|
}
|
|
|
|
type User struct {
|
|
User user.User
|
|
password user.Password
|
|
}
|
|
|
|
type GetParams struct {
|
|
url string
|
|
user User
|
|
page string
|
|
}
|
|
|
|
func (c TestContext) Get(params GetParams) *http.Response {
|
|
c.t.Helper()
|
|
fmtUrl := fmt.Sprintf("%s?page=%s", params.url, params.page)
|
|
resp, err := http.Get(c.getURL(fmtUrl, params.user))
|
|
require.NoError(c.t, err)
|
|
|
|
return resp
|
|
}
|
|
|
|
type PostParams struct {
|
|
url string
|
|
body string
|
|
user User
|
|
}
|
|
|
|
func (c TestContext) Post(params PostParams) *http.Response {
|
|
c.t.Helper()
|
|
buf := bytes.NewReader([]byte(params.body))
|
|
|
|
// nolint:gosec
|
|
resp, err := http.Post(
|
|
c.getURL(params.url, params.user),
|
|
"application/json",
|
|
buf,
|
|
)
|
|
require.NoError(c.t, err)
|
|
|
|
return resp
|
|
}
|
|
|
|
type PatchParams struct {
|
|
url string
|
|
body string
|
|
user User
|
|
}
|
|
|
|
func (c TestContext) Patch(params PatchParams) *http.Response {
|
|
c.t.Helper()
|
|
|
|
req, err := http.NewRequest(http.MethodPatch, c.getURL(params.url, params.user), bytes.NewBuffer([]byte(params.body)))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
require.NoError(c.t, err)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
require.NoError(c.t, err)
|
|
require.NoError(c.t, err)
|
|
|
|
return resp
|
|
}
|
|
|
|
type DeleteParams struct {
|
|
url string
|
|
user User
|
|
}
|
|
|
|
func (c TestContext) Delete(params DeleteParams) *http.Response {
|
|
c.t.Helper()
|
|
|
|
req, err := http.NewRequest("DELETE", c.getURL(params.url, params.user), nil)
|
|
require.NoError(c.t, err)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
require.NoError(c.t, err)
|
|
|
|
return resp
|
|
}
|
|
|
|
func (c TestContext) getURL(url string, user User) string {
|
|
c.t.Helper()
|
|
|
|
baseUrl := fmt.Sprintf("http://%s", c.env.Server.HTTPServer.Listener.Addr())
|
|
if user.User.Login != "" && user.password != "" {
|
|
baseUrl = fmt.Sprintf("http://%s:%s@%s", user.User.Login, user.password, c.env.Server.HTTPServer.Listener.Addr())
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
"%s%s",
|
|
baseUrl,
|
|
url,
|
|
)
|
|
}
|
|
|
|
func (c TestContext) createOrg(name string) int64 {
|
|
c.t.Helper()
|
|
store := c.env.SQLStore
|
|
c.env.Cfg.AutoAssignOrg = false
|
|
quotaService := quotaimpl.ProvideService(store, c.env.Cfg)
|
|
orgService, err := orgimpl.ProvideService(store, c.env.Cfg, quotaService)
|
|
require.NoError(c.t, err)
|
|
orgId, err := orgService.GetOrCreate(context.Background(), name)
|
|
require.NoError(c.t, err)
|
|
return orgId
|
|
}
|
|
|
|
func (c TestContext) createUser(cmd user.CreateUserCommand) User {
|
|
c.t.Helper()
|
|
store := c.env.SQLStore
|
|
c.env.Cfg.AutoAssignOrg = true
|
|
c.env.Cfg.AutoAssignOrgId = 1
|
|
|
|
quotaService := quotaimpl.ProvideService(store, c.env.Cfg)
|
|
orgService, err := orgimpl.ProvideService(store, c.env.Cfg, quotaService)
|
|
require.NoError(c.t, err)
|
|
usrSvc, err := userimpl.ProvideService(
|
|
store, orgService, c.env.Cfg, nil, nil, tracing.InitializeTracerForTest(),
|
|
quotaService, supportbundlestest.NewFakeBundleService(),
|
|
)
|
|
require.NoError(c.t, err)
|
|
|
|
user, err := usrSvc.Create(context.Background(), &cmd)
|
|
require.NoError(c.t, err)
|
|
|
|
return User{
|
|
User: *user,
|
|
password: cmd.Password,
|
|
}
|
|
}
|
|
|
|
func (c TestContext) createDs(cmd *datasources.AddDataSourceCommand) *datasources.DataSource {
|
|
c.t.Helper()
|
|
|
|
dataSource, err := c.env.Server.HTTPServer.DataSourcesService.AddDataSource(context.Background(), cmd)
|
|
require.NoError(c.t, err)
|
|
return dataSource
|
|
}
|
|
|
|
func (c TestContext) createCorrelation(cmd correlations.CreateCorrelationCommand) correlations.Correlation {
|
|
c.t.Helper()
|
|
correlation, err := c.env.Server.HTTPServer.CorrelationsService.CreateCorrelation(context.Background(), cmd)
|
|
|
|
require.NoError(c.t, err)
|
|
return correlation
|
|
}
|
|
|
|
func (c TestContext) createCorrelationPassError(cmd correlations.CreateCorrelationCommand) (correlations.Correlation, error) {
|
|
c.t.Helper()
|
|
return c.env.Server.HTTPServer.CorrelationsService.CreateCorrelation(context.Background(), cmd)
|
|
}
|
|
|
|
func (c TestContext) createOrUpdateCorrelation(cmd correlations.CreateCorrelationCommand) {
|
|
c.t.Helper()
|
|
err := c.env.Server.HTTPServer.CorrelationsService.CreateOrUpdateCorrelation(context.Background(), cmd)
|
|
|
|
require.NoError(c.t, err)
|
|
}
|