Hackaton: Add more unit tests, take 4 (#101704)

* cloudmigration/cloudmigrationimpl: run integration tests in parallel

* tsdb/mysql: run tests with actual service and instance manager

* pluginsintegration/angulardetectorsprovider: reduce job interval in test

* util: extract test that should only be ran with -race enabled and unskip it
This commit is contained in:
Matheus Macabu
2025-03-07 14:06:47 +01:00
committed by GitHub
parent cc80681beb
commit 5917ed8227
7 changed files with 266 additions and 154 deletions
@@ -20,7 +20,6 @@ import (
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/infra/kvstore"
"github.com/grafana/grafana/pkg/infra/tracing"
@@ -47,6 +46,7 @@ import (
"github.com/grafana/grafana/pkg/services/quota/quotatest"
secretsfakes "github.com/grafana/grafana/pkg/services/secrets/fakes"
secretskv "github.com/grafana/grafana/pkg/services/secrets/kvstore"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/usertest"
"github.com/grafana/grafana/pkg/setting"
@@ -60,6 +60,8 @@ func Test_NoopServiceDoesNothing(t *testing.T) {
}
func Test_CreateGetAndDeleteToken(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false)
createResp, err := s.CreateToken(context.Background())
@@ -82,6 +84,8 @@ func Test_CreateGetAndDeleteToken(t *testing.T) {
}
func Test_GetSnapshotStatusFromGMS(t *testing.T) {
t.Parallel()
setupTest := func(ctx context.Context) (service *Service, snapshotUID string, sessionUID string) {
s := setUpServiceTest(t, false).(*Service)
@@ -150,6 +154,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
}
t.Run("test case: gms snapshot initialized", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -172,6 +178,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
})
t.Run("test case: gms snapshot processing", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -194,6 +202,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
})
t.Run("test case: gms snapshot finished", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -216,6 +226,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
})
t.Run("test case: gms snapshot canceled", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -238,6 +250,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
})
t.Run("test case: gms snapshot error", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -260,6 +274,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
})
t.Run("test case: gms snapshot unknown", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -292,6 +308,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
})
t.Run("GMS results applied to local snapshot", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -335,6 +353,8 @@ func Test_GetSnapshotStatusFromGMS(t *testing.T) {
}
func Test_OnlyQueriesStatusFromGMSWhenRequired(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
gmsClientMock := &gmsClientMock{
@@ -407,6 +427,8 @@ func Test_OnlyQueriesStatusFromGMSWhenRequired(t *testing.T) {
}
func Test_DeletedDashboardsNotMigrated(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
// modify what the mock returns for just this test case
@@ -454,6 +476,8 @@ func Test_SortFolders(t *testing.T) {
}
func TestDeleteSession(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
user := &user.SignedInUser{UserUID: "user123"}
@@ -496,7 +520,11 @@ func TestDeleteSession(t *testing.T) {
}
func TestReportEvent(t *testing.T) {
t.Parallel()
t.Run("when the session is nil, it does not report the event", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -513,6 +541,8 @@ func TestReportEvent(t *testing.T) {
})
t.Run("when the session is not nil, it reports the event", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -530,6 +560,8 @@ func TestReportEvent(t *testing.T) {
}
func TestGetFolderNamesForFolderUIDs(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -594,6 +626,8 @@ func TestGetFolderNamesForFolderUIDs(t *testing.T) {
}
func TestGetParentNames(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@@ -654,6 +688,8 @@ func TestGetParentNames(t *testing.T) {
}
func TestGetLibraryElementsCommands(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
ctx, cancel := context.WithCancel(context.Background())
@@ -718,6 +754,8 @@ func TestIsPublicSignatureType(t *testing.T) {
}
func TestGetPlugins(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
ctx, cancel := context.WithCancel(context.Background())
@@ -817,7 +855,6 @@ func TestGetPlugins(t *testing.T) {
type configOverrides func(c *setting.Cfg)
func setUpServiceTest(t *testing.T, withDashboardMock bool, cfgOverrides ...configOverrides) cloudmigration.Service {
sqlStore := db.InitTestDB(t)
secretsService := secretsfakes.NewFakeSecretsService()
rr := routing.NewRouteRegister()
tracer := tracing.InitializeTracerForTest()
@@ -862,6 +899,14 @@ func setUpServiceTest(t *testing.T, withDashboardMock bool, cfgOverrides ...conf
featuremgmt.FlagDashboardRestore, // needed for skipping creating soft-deleted dashboards in the snapshot.
)
sqlStore := sqlstore.NewTestStore(t,
sqlstore.WithCfg(cfg),
sqlstore.WithFeatureFlags(
featuremgmt.FlagOnPremToCloudMigrations,
featuremgmt.FlagDashboardRestore, // needed for skipping creating soft-deleted dashboards in the snapshot.
),
)
kvStore := kvstore.ProvideService(sqlStore)
bus := bus.ProvideBus(tracer)
@@ -22,10 +22,14 @@ import (
)
func TestGetAlertMuteTimings(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("it returns the mute timings", func(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
s.features = featuremgmt.WithFeatures(featuremgmt.FlagOnPremToCloudMigrations)
@@ -42,10 +46,14 @@ func TestGetAlertMuteTimings(t *testing.T) {
}
func TestGetNotificationTemplates(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("it returns the notification templates", func(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
user := &user.SignedInUser{OrgID: 1}
@@ -61,10 +69,14 @@ func TestGetNotificationTemplates(t *testing.T) {
}
func TestGetContactPoints(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("it returns the contact points", func(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
user := &user.SignedInUser{
@@ -89,10 +101,14 @@ func TestGetContactPoints(t *testing.T) {
}
func TestGetNotificationPolicies(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("it returns the contact points", func(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
user := &user.SignedInUser{OrgID: 1}
@@ -113,10 +129,14 @@ func TestGetNotificationPolicies(t *testing.T) {
}
func TestGetAlertRules(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("it returns the alert rules", func(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
user := &user.SignedInUser{OrgID: 1}
@@ -130,6 +150,8 @@ func TestGetAlertRules(t *testing.T) {
})
t.Run("when the alert_rules_state config is `paused`, then the alert rules are all returned in `paused` state", func(t *testing.T) {
t.Parallel()
alertRulesState := func(c *setting.Cfg) {
c.CloudMigration.AlertRulesState = setting.GMSAlertRulesPaused
}
@@ -153,10 +175,14 @@ func TestGetAlertRules(t *testing.T) {
}
func TestGetAlertRuleGroups(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("it returns the alert rule groups", func(t *testing.T) {
t.Parallel()
s := setUpServiceTest(t, false).(*Service)
user := &user.SignedInUser{OrgID: 1}
@@ -190,6 +216,8 @@ func TestGetAlertRuleGroups(t *testing.T) {
})
t.Run("with the alert rules state set to paused, it returns the alert rule groups with alert rules paused", func(t *testing.T) {
t.Parallel()
alertRulesState := func(c *setting.Cfg) {
c.CloudMigration.AlertRulesState = setting.GMSAlertRulesPaused
}
@@ -6,21 +6,17 @@ import (
"strconv"
"testing"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/cloudmigration"
fakeSecrets "github.com/grafana/grafana/pkg/services/secrets/fakes"
secretskv "github.com/grafana/grafana/pkg/services/secrets/kvstore"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/tests/testsuite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func Test_GetAllCloudMigrationSessions(t *testing.T) {
t.Parallel()
_, s := setUpTest(t)
ctx := context.Background()
@@ -47,6 +43,8 @@ func Test_GetAllCloudMigrationSessions(t *testing.T) {
}
func Test_CreateMigrationSession(t *testing.T) {
t.Parallel()
_, s := setUpTest(t)
ctx := context.Background()
@@ -77,8 +75,11 @@ func Test_CreateMigrationSession(t *testing.T) {
}
func Test_GetMigrationSessionByUID(t *testing.T) {
t.Parallel()
_, s := setUpTest(t)
ctx := context.Background()
t.Run("find session by uid", func(t *testing.T) {
uid := "qwerty"
orgId := int64(1)
@@ -114,6 +115,8 @@ func Test_DeleteMigrationSession(t *testing.T) {
*/
func Test_SnapshotManagement(t *testing.T) {
t.Parallel()
_, s := setUpTest(t)
ctx := context.Background()
@@ -166,6 +169,8 @@ func Test_SnapshotManagement(t *testing.T) {
}
func Test_SnapshotResources(t *testing.T) {
t.Parallel()
_, s := setUpTest(t)
ctx := context.Background()
@@ -242,6 +247,8 @@ func Test_SnapshotResources(t *testing.T) {
}
func TestGetSnapshotList(t *testing.T) {
t.Parallel()
_, s := setUpTest(t)
// Taken from setUpTest
sessionUID := "qwerty"
@@ -367,7 +374,8 @@ func TestDecryptToken(t *testing.T) {
}
func setUpTest(t *testing.T) (*sqlstore.SQLStore, *sqlStore) {
testDB := db.InitTestDB(t)
testDB := sqlstore.NewTestStore(t)
s := &sqlStore{
db: testDB,
secretsService: fakeSecrets.FakeSecretsService{},