CloudMigrations: Query Grafana Migration Status for status while the snapshot is in the cloud (#90314)
* implement querying gms for snapshot status * add some documentation * provide snapshot resources after snapshot is created * add rate limiting to backend * fix compilation error * fix typo * add unit tests * finish merge * lint * swagger gen * more testing * remove duplicate test * address a couple PR comments * update switch statement to a map * add timeouts to gms client through the http client * remove extra whitespace * put method back where it was so the PR is less confusing * fix tests * add todo * fix final unit test
This commit is contained in:
@@ -31,12 +31,14 @@ import (
|
||||
)
|
||||
|
||||
func Test_NoopServiceDoesNothing(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := &NoopServiceImpl{}
|
||||
_, e := s.CreateToken(context.Background())
|
||||
assert.ErrorIs(t, e, cloudmigration.ErrFeatureDisabledError)
|
||||
}
|
||||
|
||||
func Test_CreateGetAndDeleteToken(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := setUpServiceTest(t, false)
|
||||
|
||||
createResp, err := s.CreateToken(context.Background())
|
||||
@@ -59,6 +61,7 @@ func Test_CreateGetAndDeleteToken(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_CreateGetRunMigrationsAndRuns(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := setUpServiceTest(t, true)
|
||||
|
||||
createTokenResp, err := s.CreateToken(context.Background())
|
||||
@@ -112,6 +115,245 @@ func Test_CreateGetRunMigrationsAndRuns(t *testing.T) {
|
||||
require.NotNil(t, createResp.UID, delMigResp.UID)
|
||||
}
|
||||
|
||||
func Test_GetSnapshotStatusFromGMS(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := setUpServiceTest(t, false).(*Service)
|
||||
|
||||
gmsClientMock := &gmsClientMock{}
|
||||
s.gmsClient = gmsClientMock
|
||||
|
||||
// Insert a session and snapshot into the database before we start
|
||||
sess, err := s.store.CreateMigrationSession(context.Background(), cloudmigration.CloudMigrationSession{})
|
||||
require.NoError(t, err)
|
||||
uid, err := s.store.CreateSnapshot(context.Background(), cloudmigration.CloudMigrationSnapshot{
|
||||
UID: "test uid",
|
||||
SessionUID: sess.UID,
|
||||
Status: cloudmigration.SnapshotStatusCreating,
|
||||
GMSSnapshotUID: "gms uid",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "test uid", uid)
|
||||
|
||||
// Make sure status is coming from the db only
|
||||
snapshot, err := s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusCreating, snapshot.Status)
|
||||
assert.Equal(t, 0, gmsClientMock.getStatusCalled)
|
||||
|
||||
// Make the status pending processing and ensure GMS gets called
|
||||
err = s.store.UpdateSnapshot(context.Background(), cloudmigration.UpdateSnapshotCmd{
|
||||
UID: uid,
|
||||
Status: cloudmigration.SnapshotStatusPendingProcessing,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
cleanupFunc := func() {
|
||||
gmsClientMock.getStatusCalled = 0
|
||||
err = s.store.UpdateSnapshot(context.Background(), cloudmigration.UpdateSnapshotCmd{
|
||||
UID: uid,
|
||||
Status: cloudmigration.SnapshotStatusPendingProcessing,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
t.Run("test case: gms snapshot initialized", func(t *testing.T) {
|
||||
gmsClientMock.getSnapshotResponse = &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateInitialized,
|
||||
}
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusPendingProcessing, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled)
|
||||
|
||||
t.Cleanup(cleanupFunc)
|
||||
})
|
||||
|
||||
t.Run("test case: gms snapshot processing", func(t *testing.T) {
|
||||
gmsClientMock.getSnapshotResponse = &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateProcessing,
|
||||
}
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusProcessing, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled)
|
||||
|
||||
t.Cleanup(cleanupFunc)
|
||||
})
|
||||
|
||||
t.Run("test case: gms snapshot finished", func(t *testing.T) {
|
||||
gmsClientMock.getSnapshotResponse = &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateFinished,
|
||||
}
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusFinished, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled)
|
||||
|
||||
t.Cleanup(cleanupFunc)
|
||||
})
|
||||
|
||||
t.Run("test case: gms snapshot canceled", func(t *testing.T) {
|
||||
gmsClientMock.getSnapshotResponse = &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateCanceled,
|
||||
}
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusCanceled, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled)
|
||||
|
||||
t.Cleanup(cleanupFunc)
|
||||
})
|
||||
|
||||
t.Run("test case: gms snapshot error", func(t *testing.T) {
|
||||
gmsClientMock.getSnapshotResponse = &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateError,
|
||||
}
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusError, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled)
|
||||
|
||||
t.Cleanup(cleanupFunc)
|
||||
})
|
||||
|
||||
t.Run("test case: gms snapshot unknown", func(t *testing.T) {
|
||||
gmsClientMock.getSnapshotResponse = &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateUnknown,
|
||||
}
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
// snapshot status should remain unchanged
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusPendingProcessing, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled)
|
||||
|
||||
t.Cleanup(cleanupFunc)
|
||||
})
|
||||
|
||||
t.Run("GMS results applied to local snapshot", func(t *testing.T) {
|
||||
gmsClientMock.getSnapshotResponse = &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateFinished,
|
||||
Results: []cloudmigration.CloudMigrationResource{
|
||||
{
|
||||
Type: cloudmigration.DatasourceDataType,
|
||||
RefID: "A",
|
||||
Status: cloudmigration.ItemStatusError,
|
||||
Error: "fake",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusFinished, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled)
|
||||
assert.Len(t, snapshot.Resources, 1)
|
||||
assert.Equal(t, gmsClientMock.getSnapshotResponse.Results[0], snapshot.Resources[0])
|
||||
|
||||
// ensure it is persisted
|
||||
snapshot, err = s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
ResultPage: 1,
|
||||
ResultLimit: 100,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cloudmigration.SnapshotStatusFinished, snapshot.Status)
|
||||
assert.Equal(t, 1, gmsClientMock.getStatusCalled) // shouldn't have queried GMS again now that it is finished
|
||||
assert.Len(t, snapshot.Resources, 1)
|
||||
assert.Equal(t, "A", snapshot.Resources[0].RefID)
|
||||
assert.Equal(t, "fake", snapshot.Resources[0].Error)
|
||||
|
||||
t.Cleanup(cleanupFunc)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_OnlyQueriesStatusFromGMSWhenRequired(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := setUpServiceTest(t, false).(*Service)
|
||||
|
||||
gmsClientMock := &gmsClientMock{
|
||||
getSnapshotResponse: &cloudmigration.GetSnapshotStatusResponse{
|
||||
State: cloudmigration.SnapshotStateFinished,
|
||||
},
|
||||
}
|
||||
s.gmsClient = gmsClientMock
|
||||
|
||||
// Insert a snapshot into the database before we start
|
||||
sess, err := s.store.CreateMigrationSession(context.Background(), cloudmigration.CloudMigrationSession{})
|
||||
require.NoError(t, err)
|
||||
uid, err := s.store.CreateSnapshot(context.Background(), cloudmigration.CloudMigrationSnapshot{
|
||||
UID: uuid.NewString(),
|
||||
SessionUID: sess.UID,
|
||||
Status: cloudmigration.SnapshotStatusCreating,
|
||||
GMSSnapshotUID: "gms uid",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// make sure GMS is not called when snapshot is creating, pending upload, uploading, finished, canceled, or errored
|
||||
for _, status := range []cloudmigration.SnapshotStatus{
|
||||
cloudmigration.SnapshotStatusCreating,
|
||||
cloudmigration.SnapshotStatusPendingUpload,
|
||||
cloudmigration.SnapshotStatusUploading,
|
||||
cloudmigration.SnapshotStatusFinished,
|
||||
cloudmigration.SnapshotStatusCanceled,
|
||||
cloudmigration.SnapshotStatusError,
|
||||
} {
|
||||
err = s.store.UpdateSnapshot(context.Background(), cloudmigration.UpdateSnapshotCmd{
|
||||
UID: uid,
|
||||
Status: status,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
_, err := s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, gmsClientMock.getStatusCalled)
|
||||
}
|
||||
|
||||
// make sure GMS is called when snapshot is pending processing or processing
|
||||
for i, status := range []cloudmigration.SnapshotStatus{
|
||||
cloudmigration.SnapshotStatusPendingProcessing,
|
||||
cloudmigration.SnapshotStatusProcessing,
|
||||
} {
|
||||
err = s.store.UpdateSnapshot(context.Background(), cloudmigration.UpdateSnapshotCmd{
|
||||
UID: uid,
|
||||
Status: status,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
_, err := s.GetSnapshot(context.Background(), cloudmigration.GetSnapshotsQuery{
|
||||
SnapshotUID: uid,
|
||||
SessionUID: sess.UID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, i+1, gmsClientMock.getStatusCalled)
|
||||
}
|
||||
}
|
||||
|
||||
func ctxWithSignedInUser() context.Context {
|
||||
c := &contextmodel.ReqContext{
|
||||
SignedInUser: &user.SignedInUser{OrgID: 1},
|
||||
@@ -136,8 +378,8 @@ func setUpServiceTest(t *testing.T, withDashboardMock bool) cloudmigration.Servi
|
||||
require.NoError(t, err)
|
||||
_, err = section.NewKey("domain", "localhost:1234")
|
||||
require.NoError(t, err)
|
||||
// dont know if this is the best, but dont want to refactor at the moment
|
||||
cfg.CloudMigration.IsDeveloperMode = true
|
||||
|
||||
cfg.CloudMigration.IsDeveloperMode = true // ensure local implementations are used
|
||||
cfg.CloudMigration.SnapshotFolder = filepath.Join(os.TempDir(), uuid.NewString())
|
||||
|
||||
dashboardService := dashboards.NewFakeDashboardService(t)
|
||||
@@ -176,3 +418,30 @@ func setUpServiceTest(t *testing.T, withDashboardMock bool) cloudmigration.Servi
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
type gmsClientMock struct {
|
||||
validateKeyCalled int
|
||||
startSnapshotCalled int
|
||||
getStatusCalled int
|
||||
|
||||
getSnapshotResponse *cloudmigration.GetSnapshotStatusResponse
|
||||
}
|
||||
|
||||
func (m *gmsClientMock) ValidateKey(_ context.Context, _ cloudmigration.CloudMigrationSession) error {
|
||||
m.validateKeyCalled++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *gmsClientMock) MigrateData(_ context.Context, _ cloudmigration.CloudMigrationSession, _ cloudmigration.MigrateDataRequest) (*cloudmigration.MigrateDataResponse, error) {
|
||||
panic("not implemented") // TODO: Implement
|
||||
}
|
||||
|
||||
func (m *gmsClientMock) StartSnapshot(_ context.Context, _ cloudmigration.CloudMigrationSession) (*cloudmigration.StartSnapshotResponse, error) {
|
||||
m.startSnapshotCalled++
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *gmsClientMock) GetSnapshotStatus(_ context.Context, _ cloudmigration.CloudMigrationSession, _ cloudmigration.CloudMigrationSnapshot) (*cloudmigration.GetSnapshotStatusResponse, error) {
|
||||
m.getStatusCalled++
|
||||
return m.getSnapshotResponse, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user