From 2db8ed94410936c9856cee7626fd1747a9ea2ebe Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 9 Dec 2022 08:11:56 +0100 Subject: [PATCH] Chore: All tests under pkg/tests should be integration tests (#59521) * Chore: All tests under pkg/tests should be integrationtests * run alerting integration tests only for sqlite --- pkg/infra/db/db.go | 8 +++++ .../alerting/api_admin_configuration_test.go | 4 ++- .../api_alertmanager_configuration_test.go | 8 +++-- .../api/alerting/api_alertmanager_test.go | 32 ++++++++++++++----- .../alerting/api_available_channel_test.go | 4 ++- .../alerting/api_notification_channel_test.go | 12 +++++-- pkg/tests/api/alerting/api_prometheus_test.go | 12 +++++-- .../api/alerting/api_provisioning_test.go | 8 +++-- pkg/tests/api/alerting/api_ruler_test.go | 20 +++++++++--- .../api/dashboards/api_dashboards_test.go | 12 +++++-- pkg/tests/api/plugins/api_plugins_test.go | 6 +++- pkg/tests/testinfra/testinfra.go | 8 +++++ pkg/tests/web/index_view_test.go | 8 +++-- 13 files changed, 111 insertions(+), 31 deletions(-) diff --git a/pkg/infra/db/db.go b/pkg/infra/db/db.go index d85b7db05e9..14e3f654504 100644 --- a/pkg/infra/db/db.go +++ b/pkg/infra/db/db.go @@ -31,6 +31,14 @@ var InitTestDBwithCfg = sqlstore.InitTestDBWithCfg var ProvideService = sqlstore.ProvideService var NewSqlBuilder = sqlstore.NewSqlBuilder +func IsTestDbSQLite() bool { + if db, present := os.LookupEnv("GRAFANA_TEST_DB"); !present || db == "sqlite" { + return true + } + + return !IsTestDbMySQL() && !IsTestDbPostgres() +} + func IsTestDbMySQL() bool { if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present { return db == migrator.MySQL diff --git a/pkg/tests/api/alerting/api_admin_configuration_test.go b/pkg/tests/api/alerting/api_admin_configuration_test.go index 1633da33995..aa26e9ef568 100644 --- a/pkg/tests/api/alerting/api_admin_configuration_test.go +++ b/pkg/tests/api/alerting/api_admin_configuration_test.go @@ -25,7 +25,9 @@ import ( "github.com/grafana/grafana/pkg/tests/testinfra" ) -func TestAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) { +func TestIntegrationAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + const disableOrgID int64 = 3 dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, diff --git a/pkg/tests/api/alerting/api_alertmanager_configuration_test.go b/pkg/tests/api/alerting/api_alertmanager_configuration_test.go index e3daa09d5ea..59b06869c94 100644 --- a/pkg/tests/api/alerting/api_alertmanager_configuration_test.go +++ b/pkg/tests/api/alerting/api_alertmanager_configuration_test.go @@ -20,7 +20,9 @@ import ( "github.com/stretchr/testify/require" ) -func TestAlertmanagerConfigurationIsTransactional(t *testing.T) { +func TestIntegrationAlertmanagerConfigurationIsTransactional(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, @@ -137,7 +139,9 @@ func TestAlertmanagerConfigurationIsTransactional(t *testing.T) { } } -func TestAlertmanagerConfigurationPersistSecrets(t *testing.T) { +func TestIntegrationAlertmanagerConfigurationPersistSecrets(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, diff --git a/pkg/tests/api/alerting/api_alertmanager_test.go b/pkg/tests/api/alerting/api_alertmanager_test.go index b6815a3278f..d7aff484a71 100644 --- a/pkg/tests/api/alerting/api_alertmanager_test.go +++ b/pkg/tests/api/alerting/api_alertmanager_test.go @@ -34,7 +34,9 @@ type Response struct { TraceID string `json:"traceID"` } -func TestAMConfigAccess(t *testing.T) { +func TestIntegrationAMConfigAccess(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, @@ -392,7 +394,9 @@ func TestAMConfigAccess(t *testing.T) { }) } -func TestAlertAndGroupsQuery(t *testing.T) { +func TestIntegrationAlertAndGroupsQuery(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, @@ -549,7 +553,9 @@ func TestAlertAndGroupsQuery(t *testing.T) { } } -func TestRulerAccess(t *testing.T) { +func TestIntegrationRulerAccess(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -664,7 +670,9 @@ func TestRulerAccess(t *testing.T) { } } -func TestDeleteFolderWithRules(t *testing.T) { +func TestIntegrationDeleteFolderWithRules(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -824,7 +832,9 @@ func TestDeleteFolderWithRules(t *testing.T) { } } -func TestAlertRuleCRUD(t *testing.T) { +func TestIntegrationAlertRuleCRUD(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -1802,7 +1812,9 @@ func TestAlertRuleCRUD(t *testing.T) { } } -func TestAlertmanagerStatus(t *testing.T) { +func TestIntegrationAlertmanagerStatus(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -1865,7 +1877,9 @@ func TestAlertmanagerStatus(t *testing.T) { } } -func TestQuota(t *testing.T) { +func TestIntegrationQuota(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -2064,7 +2078,9 @@ func TestQuota(t *testing.T) { }) } -func TestEval(t *testing.T) { +func TestIntegrationEval(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, diff --git a/pkg/tests/api/alerting/api_available_channel_test.go b/pkg/tests/api/alerting/api_available_channel_test.go index bbf605de55c..bd1ba992612 100644 --- a/pkg/tests/api/alerting/api_available_channel_test.go +++ b/pkg/tests/api/alerting/api_available_channel_test.go @@ -15,7 +15,9 @@ import ( "github.com/grafana/grafana/pkg/tests/testinfra" ) -func TestAvailableChannels(t *testing.T) { +func TestIntegrationAvailableChannels(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index d743bb4123d..3fc09ffba95 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -31,7 +31,9 @@ import ( "github.com/grafana/grafana/pkg/tests/testinfra" ) -func TestTestReceivers(t *testing.T) { +func TestIntegrationTestReceivers(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + t.Run("assert no receivers returns 400 Bad Request", func(t *testing.T) { // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ @@ -423,7 +425,9 @@ func TestTestReceivers(t *testing.T) { }) } -func TestTestReceiversAlertCustomization(t *testing.T) { +func TestIntegrationTestReceiversAlertCustomization(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + t.Run("assert custom annotations and labels are sent", func(t *testing.T) { // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ @@ -693,7 +697,9 @@ func TestTestReceiversAlertCustomization(t *testing.T) { }) } -func TestNotificationChannels(t *testing.T) { +func TestIntegrationNotificationChannels(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, diff --git a/pkg/tests/api/alerting/api_prometheus_test.go b/pkg/tests/api/alerting/api_prometheus_test.go index 2f5672756f3..64dbfe9387f 100644 --- a/pkg/tests/api/alerting/api_prometheus_test.go +++ b/pkg/tests/api/alerting/api_prometheus_test.go @@ -24,7 +24,9 @@ import ( "github.com/grafana/grafana/pkg/tests/testinfra" ) -func TestPrometheusRules(t *testing.T) { +func TestIntegrationPrometheusRules(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, @@ -319,7 +321,9 @@ func TestPrometheusRules(t *testing.T) { } } -func TestPrometheusRulesFilterByDashboard(t *testing.T) { +func TestIntegrationPrometheusRulesFilterByDashboard(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ EnableFeatureToggles: []string{"ngalert"}, DisableAnonymous: true, @@ -612,7 +616,9 @@ func TestPrometheusRulesFilterByDashboard(t *testing.T) { } } -func TestPrometheusRulesPermissions(t *testing.T) { +func TestIntegrationPrometheusRulesPermissions(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, diff --git a/pkg/tests/api/alerting/api_provisioning_test.go b/pkg/tests/api/alerting/api_provisioning_test.go index 33c4495a608..ce115c647ee 100644 --- a/pkg/tests/api/alerting/api_provisioning_test.go +++ b/pkg/tests/api/alerting/api_provisioning_test.go @@ -13,7 +13,9 @@ import ( "github.com/stretchr/testify/require" ) -func TestProvisioning(t *testing.T) { +func TestIntegrationProvisioning(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, EnableUnifiedAlerting: true, @@ -59,7 +61,7 @@ func TestProvisioning(t *testing.T) { "name": "test-receiver", "type": "slack", "settings": { - "recipient": "value_recipient", + "recipient": "value_recipient", "token": "value_token" } }` @@ -157,7 +159,7 @@ func TestProvisioning(t *testing.T) { "name": "my-contact-point", "type": "slack", "settings": { - "recipient": "value_recipient", + "recipient": "value_recipient", "token": "value_token" } }` diff --git a/pkg/tests/api/alerting/api_ruler_test.go b/pkg/tests/api/alerting/api_ruler_test.go index 4e3186c9f99..3aa8b53617b 100644 --- a/pkg/tests/api/alerting/api_ruler_test.go +++ b/pkg/tests/api/alerting/api_ruler_test.go @@ -22,7 +22,9 @@ import ( "github.com/grafana/grafana/pkg/util" ) -func TestAlertRulePermissions(t *testing.T) { +func TestIntegrationAlertRulePermissions(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -311,7 +313,9 @@ func createRule(t *testing.T, client apiClient, folder string) { require.JSONEq(t, `{"message":"rule group updated successfully"}`, body) } -func TestAlertRuleConflictingTitle(t *testing.T) { +func TestIntegrationAlertRuleConflictingTitle(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -382,7 +386,9 @@ func TestAlertRuleConflictingTitle(t *testing.T) { }) } -func TestRulerRulesFilterByDashboard(t *testing.T) { +func TestIntegrationRulerRulesFilterByDashboard(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ EnableFeatureToggles: []string{"ngalert"}, DisableAnonymous: true, @@ -719,7 +725,9 @@ func TestRulerRulesFilterByDashboard(t *testing.T) { } } -func TestRuleGroupSequence(t *testing.T) { +func TestIntegrationRuleGroupSequence(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, @@ -815,7 +823,9 @@ func TestRuleGroupSequence(t *testing.T) { }) } -func TestRuleUpdate(t *testing.T) { +func TestIntegrationRuleUpdate(t *testing.T) { + testinfra.SQLiteIntegrationTest(t) + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableLegacyAlerting: true, diff --git a/pkg/tests/api/dashboards/api_dashboards_test.go b/pkg/tests/api/dashboards/api_dashboards_test.go index 5eefc5c15f5..65bd8f4e4cc 100644 --- a/pkg/tests/api/dashboards/api_dashboards_test.go +++ b/pkg/tests/api/dashboards/api_dashboards_test.go @@ -29,7 +29,11 @@ import ( "github.com/grafana/grafana/pkg/tests/testinfra" ) -func TestDashboardQuota(t *testing.T) { +func TestIntegrationDashboardQuota(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + // enable quota and set low dashboard quota // Setup Grafana and its Database dashboardQuota := int64(1) @@ -114,7 +118,11 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma return u.ID } -func TestUpdatingProvisionionedDashboards(t *testing.T) { +func TestIntegrationUpdatingProvisionionedDashboards(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableAnonymous: true, diff --git a/pkg/tests/api/plugins/api_plugins_test.go b/pkg/tests/api/plugins/api_plugins_test.go index 0ebffe901b3..251c9389310 100644 --- a/pkg/tests/api/plugins/api_plugins_test.go +++ b/pkg/tests/api/plugins/api_plugins_test.go @@ -30,7 +30,11 @@ const ( var updateSnapshotFlag = false -func TestPlugins(t *testing.T) { +func TestIntegrationPlugins(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + dir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ PluginAdminEnabled: true, }) diff --git a/pkg/tests/testinfra/testinfra.go b/pkg/tests/testinfra/testinfra.go index eb565baa74b..9d1550f56eb 100644 --- a/pkg/tests/testinfra/testinfra.go +++ b/pkg/tests/testinfra/testinfra.go @@ -345,6 +345,14 @@ func CreateGrafDir(t *testing.T, opts ...GrafanaOpts) (string, string) { return tmpDir, cfgPath } +func SQLiteIntegrationTest(t *testing.T) { + t.Helper() + + if testing.Short() || !db.IsTestDbSQLite() { + t.Skip("skipping integration test") + } +} + type GrafanaOpts struct { EnableCSP bool EnableFeatureToggles []string diff --git a/pkg/tests/web/index_view_test.go b/pkg/tests/web/index_view_test.go index 72cf4376b30..e2ef840ede6 100644 --- a/pkg/tests/web/index_view_test.go +++ b/pkg/tests/web/index_view_test.go @@ -12,8 +12,12 @@ import ( "github.com/stretchr/testify/require" ) -// TestIndexView tests the Grafana index view. -func TestIndexView(t *testing.T) { +// TestIntegrationIndexView tests the Grafana index view. +func TestIntegrationIndexView(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + t.Run("CSP enabled", func(t *testing.T) { grafDir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ EnableCSP: true,