Chore: Use proper database in integration tests (#109119)
* Chore: Apply proper database settings to integration tests * add logging * join host and port in database config to override default ports * apply test fixes from the original pr * host might contain port already * increase timeout * increase timeout even more * even larger timeouts * Use eventually for stats * Use eventually also for listing settings as index takes some time --------- Co-authored-by: Roberto Jimenez Sanchez <roberto.jimenez@grafana.com>
This commit is contained in:
co-authored by
Roberto Jimenez Sanchez
parent
3a4dc10d3f
commit
f3ca49f2b3
@@ -94,6 +94,9 @@ func (dbCfg *DatabaseConfig) readConfig(cfg *setting.Cfg) error {
|
||||
} else {
|
||||
dbCfg.Type = sec.Key("type").String()
|
||||
dbCfg.Host = sec.Key("host").String()
|
||||
if port := sec.Key("port").String(); port != "" {
|
||||
dbCfg.Host = dbCfg.Host + ":" + port
|
||||
}
|
||||
dbCfg.Name = sec.Key("name").String()
|
||||
dbCfg.User = sec.Key("user").String()
|
||||
dbCfg.ConnectionString = sec.Key("connection_string").String()
|
||||
|
||||
@@ -22,6 +22,11 @@ type TestDB struct {
|
||||
DriverName string
|
||||
ConnStr string
|
||||
Path string
|
||||
Host string
|
||||
Port string
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
Cleanup func()
|
||||
}
|
||||
|
||||
@@ -132,6 +137,11 @@ func mySQLTestDB() (*TestDB, error) {
|
||||
return &TestDB{
|
||||
DriverName: "mysql",
|
||||
ConnStr: conn_str,
|
||||
Host: host,
|
||||
Port: port,
|
||||
User: "grafana",
|
||||
Password: "password",
|
||||
Database: "grafana_tests",
|
||||
Cleanup: func() {},
|
||||
}, nil
|
||||
}
|
||||
@@ -149,6 +159,11 @@ func postgresTestDB() (*TestDB, error) {
|
||||
return &TestDB{
|
||||
DriverName: "postgres",
|
||||
ConnStr: connStr,
|
||||
Host: host,
|
||||
Port: port,
|
||||
User: "grafanatest",
|
||||
Password: "grafanatest",
|
||||
Database: "grafanatest",
|
||||
Cleanup: func() {},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -115,50 +115,61 @@ func TestIntegrationProvisioning_CreatingAndGetting(t *testing.T) {
|
||||
|
||||
// Viewer can see settings listing
|
||||
t.Run("viewer has access to list", func(t *testing.T) {
|
||||
settings := &provisioning.RepositoryViewList{}
|
||||
rsp := helper.ViewerREST.Get().
|
||||
Namespace("default").
|
||||
Suffix("settings").
|
||||
Do(context.Background())
|
||||
require.NoError(t, rsp.Error())
|
||||
err := rsp.Into(settings)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, settings.Items, len(inputFiles))
|
||||
require.EventuallyWithT(t, func(collect *assert.CollectT) {
|
||||
settings := &provisioning.RepositoryViewList{}
|
||||
rsp := helper.ViewerREST.Get().
|
||||
Namespace("default").
|
||||
Suffix("settings").
|
||||
Do(context.Background())
|
||||
if !assert.NoError(collect, rsp.Error()) {
|
||||
return
|
||||
}
|
||||
|
||||
// FIXME: this should be an enterprise integration test
|
||||
if extensions.IsEnterprise {
|
||||
require.ElementsMatch(t, []provisioning.RepositoryType{
|
||||
provisioning.LocalRepositoryType,
|
||||
provisioning.GitHubRepositoryType,
|
||||
provisioning.GitRepositoryType,
|
||||
provisioning.BitbucketRepositoryType,
|
||||
provisioning.GitLabRepositoryType,
|
||||
}, settings.AvailableRepositoryTypes)
|
||||
} else {
|
||||
require.ElementsMatch(t, []provisioning.RepositoryType{
|
||||
provisioning.LocalRepositoryType,
|
||||
provisioning.GitHubRepositoryType,
|
||||
}, settings.AvailableRepositoryTypes)
|
||||
}
|
||||
err := rsp.Into(settings)
|
||||
if !assert.NoError(collect, err) {
|
||||
return
|
||||
}
|
||||
if !assert.Len(collect, settings.Items, len(inputFiles)) {
|
||||
return
|
||||
}
|
||||
|
||||
// FIXME: this should be an enterprise integration test
|
||||
if extensions.IsEnterprise {
|
||||
assert.ElementsMatch(collect, []provisioning.RepositoryType{
|
||||
provisioning.LocalRepositoryType,
|
||||
provisioning.GitHubRepositoryType,
|
||||
provisioning.GitRepositoryType,
|
||||
provisioning.BitbucketRepositoryType,
|
||||
provisioning.GitLabRepositoryType,
|
||||
}, settings.AvailableRepositoryTypes)
|
||||
} else {
|
||||
assert.ElementsMatch(collect, []provisioning.RepositoryType{
|
||||
provisioning.LocalRepositoryType,
|
||||
provisioning.GitHubRepositoryType,
|
||||
}, settings.AvailableRepositoryTypes)
|
||||
}
|
||||
}, time.Second*10, time.Millisecond*100, "Expected settings to match")
|
||||
})
|
||||
|
||||
t.Run("Repositories are reported in stats", func(t *testing.T) {
|
||||
report := apis.DoRequest(helper.K8sTestHelper, apis.RequestParams{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/admin/usage-report-preview",
|
||||
User: helper.Org1.Admin,
|
||||
}, &usagestats.Report{})
|
||||
require.EventuallyWithT(t, func(collect *assert.CollectT) {
|
||||
report := apis.DoRequest(helper.K8sTestHelper, apis.RequestParams{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/admin/usage-report-preview",
|
||||
User: helper.Org1.Admin,
|
||||
}, &usagestats.Report{})
|
||||
|
||||
stats := map[string]any{}
|
||||
for k, v := range report.Result.Metrics {
|
||||
if strings.HasPrefix(k, "stats.repository.") {
|
||||
stats[k] = v
|
||||
stats := map[string]any{}
|
||||
for k, v := range report.Result.Metrics {
|
||||
if strings.HasPrefix(k, "stats.repository.") {
|
||||
stats[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
require.Equal(t, map[string]any{
|
||||
"stats.repository.github.count": 1.0,
|
||||
"stats.repository.local.count": 1.0,
|
||||
}, stats)
|
||||
assert.Equal(collect, map[string]any{
|
||||
"stats.repository.github.count": 1.0,
|
||||
"stats.repository.local.count": 1.0,
|
||||
}, stats)
|
||||
}, time.Second*10, time.Millisecond*100, "Expected stats to match")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -85,6 +86,22 @@ func StartGrafanaEnv(t *testing.T, grafDir, cfgPath string) (string, *server.Tes
|
||||
|
||||
err = featuremgmt.InitOpenFeatureWithCfg(cfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Use proper database type based on the environment variable GRAFANA_TEST_DB in tests
|
||||
testDB, err := sqlutil.GetTestDB(sqlutil.GetTestDBType())
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(testDB.Cleanup)
|
||||
|
||||
dbCfg := cfg.Raw.Section("database")
|
||||
dbCfg.Key("type").SetValue(testDB.DriverName)
|
||||
dbCfg.Key("host").SetValue(testDB.Host)
|
||||
dbCfg.Key("port").SetValue(testDB.Port)
|
||||
dbCfg.Key("user").SetValue(testDB.User)
|
||||
dbCfg.Key("password").SetValue(testDB.Password)
|
||||
dbCfg.Key("name").SetValue(testDB.Database)
|
||||
|
||||
t.Log("Using test database", "type", testDB.DriverName, "host", testDB.Host, "port", testDB.Port, "user", testDB.User, "name", testDB.Database)
|
||||
|
||||
env, err := server.InitializeForTest(t, t, cfg, serverOpts, apiServerOpts)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user