Tests: Require GrafanaOpts when creating Grafana dir (#97825)

I don't see a reason to accept a variable amount here, as we never use it. The only use I can see is optionally
including the opts, which isn't necessary and only complicates matters when an empty struct would do just as well: the
options are all created to be assumed zero-values already, in case a test doesn't need that option set.
This commit is contained in:
Mariell Hoversholm
2024-12-12 15:13:06 +01:00
committed by GitHub
parent 47e58d5903
commit e6a6fc6f74
3 changed files with 134 additions and 134 deletions

View File

@ -124,7 +124,7 @@ func TestValidatePluginRepoConfig(t *testing.T) {
} }
// GrafanaComApiUrl is set to the default path https://grafana.com/api // GrafanaComApiUrl is set to the default path https://grafana.com/api
grafDir, cfgPath := testinfra.CreateGrafDir(t) grafDir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{})
// overriding the GrafanaComApiUrl to https://grafana-dev.com // overriding the GrafanaComApiUrl to https://grafana-dev.com
c, err := commandstest.NewCliContext(map[string]string{ c, err := commandstest.NewCliContext(map[string]string{

View File

@ -149,7 +149,7 @@ func StartGrafanaEnv(t *testing.T, grafDir, cfgPath string) (string, *server.Tes
// CreateGrafDir creates the Grafana directory. // CreateGrafDir creates the Grafana directory.
// The log by default is muted in the regression test, to activate it, pass option EnableLog = true // The log by default is muted in the regression test, to activate it, pass option EnableLog = true
func CreateGrafDir(t *testing.T, opts ...GrafanaOpts) (string, string) { func CreateGrafDir(t *testing.T, opts GrafanaOpts) (string, string) {
t.Helper() t.Helper()
tmpDir := t.TempDir() tmpDir := t.TempDir()
@ -319,89 +319,88 @@ func CreateGrafDir(t *testing.T, opts ...GrafanaOpts) (string, string) {
} }
queryRetries := 3 queryRetries := 3
for _, o := range opts { if opts.EnableCSP {
if o.EnableCSP {
securitySect, err := cfg.NewSection("security") securitySect, err := cfg.NewSection("security")
require.NoError(t, err) require.NoError(t, err)
_, err = securitySect.NewKey("content_security_policy", "true") _, err = securitySect.NewKey("content_security_policy", "true")
require.NoError(t, err) require.NoError(t, err)
} }
if len(o.EnableFeatureToggles) > 0 { if len(opts.EnableFeatureToggles) > 0 {
featureSection, err := cfg.NewSection("feature_toggles") featureSection, err := cfg.NewSection("feature_toggles")
require.NoError(t, err) require.NoError(t, err)
_, err = featureSection.NewKey("enable", strings.Join(o.EnableFeatureToggles, " ")) _, err = featureSection.NewKey("enable", strings.Join(opts.EnableFeatureToggles, " "))
require.NoError(t, err) require.NoError(t, err)
} }
if o.NGAlertAdminConfigPollInterval != 0 { if opts.NGAlertAdminConfigPollInterval != 0 {
ngalertingSection, err := cfg.NewSection("unified_alerting") ngalertingSection, err := cfg.NewSection("unified_alerting")
require.NoError(t, err) require.NoError(t, err)
_, err = ngalertingSection.NewKey("admin_config_poll_interval", o.NGAlertAdminConfigPollInterval.String()) _, err = ngalertingSection.NewKey("admin_config_poll_interval", opts.NGAlertAdminConfigPollInterval.String())
require.NoError(t, err) require.NoError(t, err)
} }
if o.NGAlertAlertmanagerConfigPollInterval != 0 { if opts.NGAlertAlertmanagerConfigPollInterval != 0 {
ngalertingSection, err := getOrCreateSection("unified_alerting") ngalertingSection, err := getOrCreateSection("unified_alerting")
require.NoError(t, err) require.NoError(t, err)
_, err = ngalertingSection.NewKey("alertmanager_config_poll_interval", o.NGAlertAlertmanagerConfigPollInterval.String()) _, err = ngalertingSection.NewKey("alertmanager_config_poll_interval", opts.NGAlertAlertmanagerConfigPollInterval.String())
require.NoError(t, err) require.NoError(t, err)
} }
if o.AppModeProduction { if opts.AppModeProduction {
_, err = dfltSect.NewKey("app_mode", "production") _, err = dfltSect.NewKey("app_mode", "production")
require.NoError(t, err) require.NoError(t, err)
} }
if o.AnonymousUserRole != "" { if opts.AnonymousUserRole != "" {
_, err = anonSect.NewKey("org_role", string(o.AnonymousUserRole)) _, err = anonSect.NewKey("org_role", string(opts.AnonymousUserRole))
require.NoError(t, err) require.NoError(t, err)
} }
if o.EnableQuota { if opts.EnableQuota {
quotaSection, err := cfg.NewSection("quota") quotaSection, err := cfg.NewSection("quota")
require.NoError(t, err) require.NoError(t, err)
_, err = quotaSection.NewKey("enabled", "true") _, err = quotaSection.NewKey("enabled", "true")
require.NoError(t, err) require.NoError(t, err)
dashboardQuota := int64(100) dashboardQuota := int64(100)
if o.DashboardOrgQuota != nil { if opts.DashboardOrgQuota != nil {
dashboardQuota = *o.DashboardOrgQuota dashboardQuota = *opts.DashboardOrgQuota
} }
_, err = quotaSection.NewKey("org_dashboard", strconv.FormatInt(dashboardQuota, 10)) _, err = quotaSection.NewKey("org_dashboard", strconv.FormatInt(dashboardQuota, 10))
require.NoError(t, err) require.NoError(t, err)
} }
if o.DisableAnonymous { if opts.DisableAnonymous {
anonSect, err := cfg.GetSection("auth.anonymous") anonSect, err := cfg.GetSection("auth.anonymous")
require.NoError(t, err) require.NoError(t, err)
_, err = anonSect.NewKey("enabled", "false") _, err = anonSect.NewKey("enabled", "false")
require.NoError(t, err) require.NoError(t, err)
} }
if o.PluginAdminEnabled { if opts.PluginAdminEnabled {
anonSect, err := cfg.NewSection("plugins") anonSect, err := cfg.NewSection("plugins")
require.NoError(t, err) require.NoError(t, err)
_, err = anonSect.NewKey("plugin_admin_enabled", "true") _, err = anonSect.NewKey("plugin_admin_enabled", "true")
require.NoError(t, err) require.NoError(t, err)
} }
if o.PluginAdminExternalManageEnabled { if opts.PluginAdminExternalManageEnabled {
anonSect, err := cfg.NewSection("plugins") anonSect, err := cfg.NewSection("plugins")
require.NoError(t, err) require.NoError(t, err)
_, err = anonSect.NewKey("plugin_admin_external_manage_enabled", "true") _, err = anonSect.NewKey("plugin_admin_external_manage_enabled", "true")
require.NoError(t, err) require.NoError(t, err)
} }
if o.ViewersCanEdit { if opts.ViewersCanEdit {
usersSection, err := cfg.NewSection("users") usersSection, err := cfg.NewSection("users")
require.NoError(t, err) require.NoError(t, err)
_, err = usersSection.NewKey("viewers_can_edit", "true") _, err = usersSection.NewKey("viewers_can_edit", "true")
require.NoError(t, err) require.NoError(t, err)
} }
if o.EnableUnifiedAlerting { if opts.EnableUnifiedAlerting {
unifiedAlertingSection, err := getOrCreateSection("unified_alerting") unifiedAlertingSection, err := getOrCreateSection("unified_alerting")
require.NoError(t, err) require.NoError(t, err)
_, err = unifiedAlertingSection.NewKey("enabled", "true") _, err = unifiedAlertingSection.NewKey("enabled", "true")
require.NoError(t, err) require.NoError(t, err)
} }
if len(o.UnifiedAlertingDisabledOrgs) > 0 { if len(opts.UnifiedAlertingDisabledOrgs) > 0 {
unifiedAlertingSection, err := getOrCreateSection("unified_alerting") unifiedAlertingSection, err := getOrCreateSection("unified_alerting")
require.NoError(t, err) require.NoError(t, err)
disableOrgStr := strings.Join(strings.Split(strings.Trim(fmt.Sprint(o.UnifiedAlertingDisabledOrgs), "[]"), " "), ",") disableOrgStr := strings.Join(strings.Split(strings.Trim(fmt.Sprint(opts.UnifiedAlertingDisabledOrgs), "[]"), " "), ",")
_, err = unifiedAlertingSection.NewKey("disabled_orgs", disableOrgStr) _, err = unifiedAlertingSection.NewKey("disabled_orgs", disableOrgStr)
require.NoError(t, err) require.NoError(t, err)
} }
if !o.EnableLog { if !opts.EnableLog {
logSection, err := getOrCreateSection("log") logSection, err := getOrCreateSection("log")
require.NoError(t, err) require.NoError(t, err)
_, err = logSection.NewKey("enabled", "false") _, err = logSection.NewKey("enabled", "false")
@ -413,54 +412,53 @@ func CreateGrafDir(t *testing.T, opts ...GrafanaOpts) (string, string) {
require.NoError(t, err) require.NoError(t, err)
} }
if o.APIServerStorageType != "" { if opts.APIServerStorageType != "" {
section, err := getOrCreateSection("grafana-apiserver") section, err := getOrCreateSection("grafana-apiserver")
require.NoError(t, err) require.NoError(t, err)
_, err = section.NewKey("storage_type", string(o.APIServerStorageType)) _, err = section.NewKey("storage_type", string(opts.APIServerStorageType))
require.NoError(t, err) require.NoError(t, err)
// Hardcoded local etcd until this is needed to run in CI // Hardcoded local etcd until this is needed to run in CI
if o.APIServerStorageType == "etcd" { if opts.APIServerStorageType == "etcd" {
_, err = section.NewKey("etcd_servers", "localhost:2379") _, err = section.NewKey("etcd_servers", "localhost:2379")
require.NoError(t, err) require.NoError(t, err)
} }
} }
if o.GRPCServerAddress != "" { if opts.GRPCServerAddress != "" {
logSection, err := getOrCreateSection("grpc_server") logSection, err := getOrCreateSection("grpc_server")
require.NoError(t, err) require.NoError(t, err)
_, err = logSection.NewKey("address", o.GRPCServerAddress) _, err = logSection.NewKey("address", opts.GRPCServerAddress)
require.NoError(t, err) require.NoError(t, err)
} }
// retry queries 3 times by default // retry queries 3 times by default
if o.QueryRetries != 0 { if opts.QueryRetries != 0 {
queryRetries = int(o.QueryRetries) queryRetries = int(opts.QueryRetries)
} }
if o.NGAlertSchedulerBaseInterval > 0 { if opts.NGAlertSchedulerBaseInterval > 0 {
unifiedAlertingSection, err := getOrCreateSection("unified_alerting") unifiedAlertingSection, err := getOrCreateSection("unified_alerting")
require.NoError(t, err) require.NoError(t, err)
_, err = unifiedAlertingSection.NewKey("scheduler_tick_interval", o.NGAlertSchedulerBaseInterval.String()) _, err = unifiedAlertingSection.NewKey("scheduler_tick_interval", opts.NGAlertSchedulerBaseInterval.String())
require.NoError(t, err) require.NoError(t, err)
_, err = unifiedAlertingSection.NewKey("min_interval", o.NGAlertSchedulerBaseInterval.String()) _, err = unifiedAlertingSection.NewKey("min_interval", opts.NGAlertSchedulerBaseInterval.String())
require.NoError(t, err) require.NoError(t, err)
} }
if o.GrafanaComAPIURL != "" { if opts.GrafanaComAPIURL != "" {
grafanaComSection, err := getOrCreateSection("grafana_com") grafanaComSection, err := getOrCreateSection("grafana_com")
require.NoError(t, err) require.NoError(t, err)
_, err = grafanaComSection.NewKey("api_url", o.GrafanaComAPIURL) _, err = grafanaComSection.NewKey("api_url", opts.GrafanaComAPIURL)
require.NoError(t, err) require.NoError(t, err)
} }
if o.UnifiedStorageConfig != nil { if opts.UnifiedStorageConfig != nil {
for k, v := range o.UnifiedStorageConfig { for k, v := range opts.UnifiedStorageConfig {
section, err := getOrCreateSection(fmt.Sprintf("unified_storage.%s", k)) section, err := getOrCreateSection(fmt.Sprintf("unified_storage.%s", k))
require.NoError(t, err) require.NoError(t, err)
_, err = section.NewKey("dualWriterMode", fmt.Sprintf("%d", v.DualWriterMode)) _, err = section.NewKey("dualWriterMode", fmt.Sprintf("%d", v.DualWriterMode))
require.NoError(t, err) require.NoError(t, err)
} }
} }
}
dbSection, err := getOrCreateSection("database") dbSection, err := getOrCreateSection("database")
require.NoError(t, err) require.NoError(t, err)

View File

@ -47,7 +47,9 @@ func TestIntegrationIndexView(t *testing.T) {
}) })
t.Run("CSP disabled", func(t *testing.T) { t.Run("CSP disabled", func(t *testing.T) {
grafDir, cfgPath := testinfra.CreateGrafDir(t) grafDir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
EnableCSP: false,
})
addr, _ := testinfra.StartGrafana(t, grafDir, cfgPath) addr, _ := testinfra.StartGrafana(t, grafDir, cfgPath)
// nolint:bodyclose // nolint:bodyclose