Files
velero/pkg/uploader/provider/block_test.go
T
b7d83a6f2b Cherry pick the in-place restore implementation PRs from feature branch to main (#10415)
* Update CRDs and CLI to support in-place restore (#10038)

Update CRDs(Restore, DataDownload, PodVolumeRestore) and restore create CLI to support in-place restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore (#10066)

Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Restore Exposer and PVC CSI to support in-place restore (#10104)

1. Update Restore Exposer to support exposing with existing PV for in-place restore
2. Update PVC CSI RIA to continue the restore process for in-place restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Block uploader to support increase restore (#10244)

Update Block uploader to support increase restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Exposer to recreate the target PV if the volume mode is different with the restore PVC (#10257)

Update Exposer to recreate the target PV if the volume mode is different with t
he restore PVC

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Preserve PVC selected-node annotation via carrier annotation for in-place restore

For in-place volume data restore, the existing PVC is deleted and
recreated. For StorageClasses with the WaitForFirstConsumer volume
binding mode, losing the volume.kubernetes.io/selected-node annotation
could let the scheduler place the recreated workload Pod in a different
zone than the original PV, leaving it stuck in ContainerCreating.

Instead of relying on RestoreItemAction execution order (the generic
PVC RIA unconditionally strips the selected-node annotation), the PVC
CSI RIA now captures the annotation from the existing PVC right before
deleting it and carries it on the target PVC via the Velero-internal
restore.velero.io/inplace-restore-selected-node annotation. The restore
engine translates the carrier back to the Kubernetes annotation after
all RestoreItemActions have run and always strips the carrier so it
never lands on the cluster.

This makes the behavior independent of RIA ordering: the Kubernetes
annotation is stripped by default on every path (including when the
target PVC does not exist and Velero falls back to provisioning a new
PVC), and preservation only happens when the CSI RIA explicitly
captured a value from the existing PVC.

Signed-off-by: chlins <chlins.zhang@gmail.com>

* Update the control path to make the in-place incremental restore with block data mover work E2E (#10410)

Update the control path to make the in-place incremental restore with block data mover work E2E

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

---------

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
Signed-off-by: chlins <chlins.zhang@gmail.com>
Co-authored-by: chlins <chlins.zhang@gmail.com>
2026-08-26 10:21:32 -04:00

544 lines
17 KiB
Go

/*
Copyright The Velero Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"context"
"testing"
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
corev1api "k8s.io/api/core/v1"
"github.com/vmware-tanzu/velero/internal/credentials"
"github.com/vmware-tanzu/velero/internal/credentials/mocks"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/cbtservice"
"github.com/vmware-tanzu/velero/pkg/repository"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
udmrepomocks "github.com/vmware-tanzu/velero/pkg/repository/udmrepo/mocks"
"github.com/vmware-tanzu/velero/pkg/uploader"
"github.com/vmware-tanzu/velero/pkg/uploader/block"
)
func TestNewBlockUploaderProvider(t *testing.T) {
requestorType := "testRequestor"
ctx := t.Context()
backupRepo := repository.NewBackupRepository(velerov1api.DefaultNamespace, repository.BackupRepositoryKey{VolumeNamespace: "fake-volume-ns-02", BackupLocation: "fake-bsl-02", RepositoryType: "fake-repository-type-02"})
mockLog := logrus.New()
testCases := []struct {
name string
mockCredGetter *mocks.SecretStore
mockBackupRepoService udmrepo.BackupRepoService
expectedError string
}{
{
name: "Success",
mockCredGetter: func() *mocks.SecretStore {
mockCredGetter := &mocks.SecretStore{}
mockCredGetter.On("Get", mock.Anything).Return("test", nil)
return mockCredGetter
}(),
mockBackupRepoService: func() udmrepo.BackupRepoService {
backupRepoService := &udmrepomocks.BackupRepoService{}
var backupRepo udmrepo.BackupRepo
backupRepoService.On("Open", t.Context(), mock.Anything).Return(backupRepo, nil)
return backupRepoService
}(),
expectedError: "",
},
{
name: "Error to get repo options",
mockCredGetter: func() *mocks.SecretStore {
mockCredGetter := &mocks.SecretStore{}
mockCredGetter.On("Get", mock.Anything).Return("test", errors.New("failed to get password"))
return mockCredGetter
}(),
mockBackupRepoService: func() udmrepo.BackupRepoService {
backupRepoService := &udmrepomocks.BackupRepoService{}
var backupRepo udmrepo.BackupRepo
backupRepoService.On("Open", t.Context(), mock.Anything).Return(backupRepo, nil)
return backupRepoService
}(),
expectedError: "error to get repo options",
},
{
name: "Error open repository service",
mockCredGetter: func() *mocks.SecretStore {
mockCredGetter := &mocks.SecretStore{}
mockCredGetter.On("Get", mock.Anything).Return("test", nil)
return mockCredGetter
}(),
mockBackupRepoService: func() udmrepo.BackupRepoService {
backupRepoService := &udmrepomocks.BackupRepoService{}
var backupRepo udmrepo.BackupRepo
backupRepoService.On("Open", t.Context(), mock.Anything).Return(backupRepo, errors.New("failed to init repository"))
return backupRepoService
}(),
expectedError: "Failed to find backup repository",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
credGetter := &credentials.CredentialGetter{FromSecret: tc.mockCredGetter}
BackupRepoServiceCreateFunc = func(string, logrus.FieldLogger) udmrepo.BackupRepoService {
return tc.mockBackupRepoService
}
_, err := NewBlockUploaderProvider(requestorType, ctx, credGetter, backupRepo, mockLog)
if tc.expectedError != "" {
require.ErrorContains(t, err, tc.expectedError)
} else {
require.NoError(t, err)
}
tc.mockCredGetter.AssertExpectations(t)
})
}
}
func TestBlockProviderClose(t *testing.T) {
mockBRepo := udmrepomocks.NewBackupRepo(t)
mockBRepo.On("Close", mock.Anything).Return(nil)
bp := &blockProvider{
bkRepo: mockBRepo,
}
err := bp.Close(t.Context())
require.NoError(t, err)
mockBRepo.AssertExpectations(t)
}
type blockMockProgressUpdater struct {
lastProgress *uploader.Progress
callCount int
}
func (u *blockMockProgressUpdater) UpdateProgress(p *uploader.Progress) {
u.lastProgress = p
u.callCount++
}
func TestBlockProviderGetPassword(t *testing.T) {
testCases := []struct {
name string
emptySecret bool
credGetterFunc func(*mocks.SecretStore, *corev1api.SecretKeySelector)
expectError bool
expectedPass string
}{
{
name: "valid credentials interface",
credGetterFunc: func(ss *mocks.SecretStore, selector *corev1api.SecretKeySelector) {
ss.On("Get", selector).Return("test", nil)
},
expectError: false,
expectedPass: "test",
},
{
name: "empty from secret",
emptySecret: true,
expectError: true,
expectedPass: "",
},
{
name: "ErrorGettingPassword",
credGetterFunc: func(ss *mocks.SecretStore, selector *corev1api.SecretKeySelector) {
ss.On("Get", selector).Return("", errors.New("error getting password"))
},
expectError: true,
expectedPass: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
credGetter := &credentials.CredentialGetter{}
mockCredGetter := &mocks.SecretStore{}
if !tc.emptySecret {
credGetter.FromSecret = mockCredGetter
}
repoKeySelector := &corev1api.SecretKeySelector{LocalObjectReference: corev1api.LocalObjectReference{Name: "velero-repo-credentials"}, Key: "repository-password"}
if tc.credGetterFunc != nil {
tc.credGetterFunc(mockCredGetter, repoKeySelector)
}
bp := &blockProvider{
credGetter: credGetter,
}
password, err := bp.GetPassword(nil)
if tc.expectError {
require.Error(t, err, "Expected an error")
} else {
require.NoError(t, err, "Expected no error")
}
assert.Equal(t, tc.expectedPass, password, "Expected password to match")
})
}
}
func TestBlockProviderRunBackup(t *testing.T) {
const requestorType = "test-requestor"
testCases := []struct {
name string
path string
realSource string
tags map[string]string
updater uploader.ProgressUpdater
mockBackupResult uploader.SnapshotInfo
mockBackupErr error
expectedID string
expectedSize int64
expectedIncrSize int64
expectError bool
expectedErrStr string
skipMock bool
checkCaptures func(*testing.T, string, map[string]string)
}{
{
name: "nil updater returns error",
path: "/dev/sda",
updater: nil,
expectError: true,
expectedErrStr: "backup progress updater is invalid",
skipMock: true,
},
{
name: "empty path returns error",
path: "",
updater: &FakeBackupProgressUpdater{},
expectError: true,
expectedErrStr: "path is empty",
skipMock: true,
},
{
name: "success returns correct snapshot info and updates progress",
path: "/dev/sda",
updater: &blockMockProgressUpdater{},
mockBackupResult: uploader.SnapshotInfo{
ID: "snap-001",
Size: 1024,
IncrementalSize: 512,
},
expectedID: "snap-001",
expectedSize: 1024,
expectedIncrSize: 512,
},
{
name: "canceled backup returns ErrorCanceled with partial snapshot info",
path: "/dev/sda",
updater: &FakeBackupProgressUpdater{},
mockBackupResult: uploader.SnapshotInfo{
ID: "snap-canceled",
Size: 2048,
IncrementalSize: 1024,
},
mockBackupErr: block.ErrCanceled,
expectedID: "snap-canceled",
expectedSize: 2048,
expectedIncrSize: 1024,
expectError: true,
expectedErrStr: "uploader is canceled",
},
{
name: "generic backup error is wrapped",
path: "/dev/sda",
updater: &FakeBackupProgressUpdater{},
mockBackupErr: errors.New("disk I/O error"),
expectError: true,
expectedErrStr: "Failed to run block backup",
},
{
name: "nil tags are initialized with required tags",
path: "/dev/sda",
tags: nil,
updater: &FakeBackupProgressUpdater{},
mockBackupResult: uploader.SnapshotInfo{ID: "snap-tags"},
expectedID: "snap-tags",
checkCaptures: func(t *testing.T, _ string, tags map[string]string) {
t.Helper()
assert.Equal(t, requestorType, tags[uploader.SnapshotRequesterTag])
assert.Equal(t, uploader.BlockType, tags[uploader.SnapshotUploaderTag])
},
},
{
name: "non-empty realSource is prefixed with requestorType and BlockType",
path: "/dev/sda",
realSource: "my-volume",
updater: &FakeBackupProgressUpdater{},
mockBackupResult: uploader.SnapshotInfo{ID: "snap-source"},
expectedID: "snap-source",
checkCaptures: func(t *testing.T, realSource string, _ map[string]string) {
t.Helper()
assert.Equal(t, requestorType+"/"+uploader.BlockType+"/my-volume", realSource)
},
},
{
name: "empty realSource is passed through unchanged",
path: "/dev/sda",
realSource: "",
updater: &FakeBackupProgressUpdater{},
mockBackupResult: uploader.SnapshotInfo{ID: "snap-nosource"},
expectedID: "snap-nosource",
checkCaptures: func(t *testing.T, realSource string, _ map[string]string) {
t.Helper()
assert.Empty(t, realSource)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockBRepo := udmrepomocks.NewBackupRepo(t)
var capturedRealSrc string
var capturedTags map[string]string
if !tc.skipMock {
blockBackupFunc = func(_ context.Context, _ block.Uploader, _ udmrepo.BackupRepo, _ string, realSource string, _ cbtservice.SourceInfo, _ bool, _ string, _ cbtservice.Service, _ map[string]string, tags map[string]string, _ logrus.FieldLogger) (uploader.SnapshotInfo, bool, error) {
capturedRealSrc = realSource
capturedTags = tags
return tc.mockBackupResult, false, tc.mockBackupErr
}
}
bp := &blockProvider{
requestorType: requestorType,
bkRepo: mockBRepo,
log: logrus.New(),
}
snapshotID, isEmpty, size, incrSize, err := bp.RunBackup(
t.Context(),
tc.path,
tc.realSource,
tc.tags,
false,
"",
CBTParam{},
uploader.PersistentVolumeBlock,
map[string]string{},
tc.updater,
)
assert.Equal(t, tc.expectedID, snapshotID)
assert.Equal(t, tc.expectedSize, size)
assert.Equal(t, tc.expectedIncrSize, incrSize)
if tc.expectError {
require.Error(t, err)
if tc.expectedErrStr != "" {
require.ErrorContains(t, err, tc.expectedErrStr)
}
} else {
require.NoError(t, err)
assert.False(t, isEmpty)
if mu, ok := tc.updater.(*blockMockProgressUpdater); ok {
assert.Equal(t, 1, mu.callCount)
require.NotNil(t, mu.lastProgress)
assert.Equal(t, tc.expectedSize, mu.lastProgress.TotalBytes)
assert.Equal(t, tc.expectedSize, mu.lastProgress.BytesDone)
}
}
if tc.checkCaptures != nil {
tc.checkCaptures(t, capturedRealSrc, capturedTags)
}
})
}
}
// TestBlockProviderCancelThroughWrappedError pins that cancellation is recognized
// after the sentinel has been wrapped, which is the only way it ever arrives in
// production: block/uploader.go wraps it with "error backing up bdev %s" and
// block/snapshot.go wraps that with "Failed to run uploader backup for si %v".
//
// Asserting on the message is useless here — provider.ErrorCanceled and
// block.ErrCanceled carry the *same* text ("uploader is canceled"), so a substring
// check passes whether or not the sentinel was actually recognized. The assertion
// has to be on identity.
func TestBlockProviderCancelThroughWrappedError(t *testing.T) {
t.Run("backup", func(t *testing.T) {
orig := blockBackupFunc
defer func() { blockBackupFunc = orig }()
blockBackupFunc = func(_ context.Context, _ block.Uploader, _ udmrepo.BackupRepo, _ string, _ string, _ cbtservice.SourceInfo, _ bool, _ string, _ cbtservice.Service, _ map[string]string, _ map[string]string, _ logrus.FieldLogger) (uploader.SnapshotInfo, bool, error) {
return uploader.SnapshotInfo{ID: "snap-cancel", Size: 2048, IncrementalSize: 1024}, false,
errors.Wrapf(
errors.Wrapf(block.ErrCanceled, "error backing up bdev %s", "ns/pvc"),
"Failed to run uploader backup for si %v", "si")
}
bp := &blockProvider{
requestorType: "test",
bkRepo: udmrepomocks.NewBackupRepo(t),
log: logrus.New(),
}
_, _, _, _, err := bp.RunBackup(
t.Context(), "/dev/sda", "ns/pvc", map[string]string{}, false, "",
CBTParam{}, uploader.PersistentVolumeBlock, map[string]string{},
&FakeBackupProgressUpdater{},
)
require.ErrorIs(t, err, ErrorCanceled,
"a wrapped block.ErrCanceled must surface as provider.ErrorCanceled; otherwise the "+
"DataUpload is marked Failed and the Backup PartiallyFailed for a user-requested cancel")
})
t.Run("restore", func(t *testing.T) {
orig := blockRestoreFunc
defer func() { blockRestoreFunc = orig }()
blockRestoreFunc = func(_ context.Context, _ block.Uploader, _ udmrepo.BackupRepo, _ string, _ string, _ bool, _ cbtservice.SourceInfo, _ cbtservice.Service, _ map[string]string, _ logrus.FieldLogger) (int64, error) {
return 0, errors.Wrap(block.ErrCanceled, "error restoring bdev")
}
bp := &blockProvider{
requestorType: "test",
bkRepo: udmrepomocks.NewBackupRepo(t),
log: logrus.New(),
}
_, err := bp.RunRestore(t.Context(), "snap-1", "/dev/sda", false, CBTParam{},
uploader.PersistentVolumeBlock, map[string]string{}, &blockMockProgressUpdater{})
require.ErrorIs(t, err, ErrorCanceled)
})
}
func TestBlockProviderRunRestore(t *testing.T) {
testCases := []struct {
name string
snapshotID string
volumePath string
updater uploader.ProgressUpdater
mockRestoreSize int64
mockRestoreErr error
expectedSize int64
expectError bool
expectedErrStr string
checkCaptures func(*testing.T, string, string)
}{
{
name: "nil updater returns error",
updater: nil,
expectError: true,
expectedErrStr: "restore progress updater is invalid",
},
{
name: "success returns size and updates progress",
snapshotID: "snap-001",
volumePath: "/dev/sdb",
updater: &blockMockProgressUpdater{},
mockRestoreSize: 4096,
expectedSize: 4096,
},
{
name: "canceled restore returns ErrorCanceled",
snapshotID: "snap-canceled",
volumePath: "/dev/sdb",
updater: &FakeRestoreProgressUpdater{},
mockRestoreErr: block.ErrCanceled,
expectError: true,
expectedErrStr: "uploader is canceled",
},
{
name: "generic restore error is wrapped",
snapshotID: "snap-error",
volumePath: "/dev/sdb",
updater: &FakeRestoreProgressUpdater{},
mockRestoreErr: errors.New("disk read error"),
expectError: true,
expectedErrStr: "Failed to run block restore",
},
{
name: "snapshotID and volumePath are forwarded to restore func",
snapshotID: "snap-fwd",
volumePath: "/dev/sdc",
updater: &FakeRestoreProgressUpdater{},
mockRestoreSize: 512,
expectedSize: 512,
checkCaptures: func(t *testing.T, snapshotID, volumePath string) {
t.Helper()
assert.Equal(t, "snap-fwd", snapshotID)
assert.Equal(t, "/dev/sdc", volumePath)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockBRepo := udmrepomocks.NewBackupRepo(t)
var capturedSnapshotID string
var capturedVolumePath string
blockRestoreFunc = func(ctx context.Context, blkUp block.Uploader, rep udmrepo.BackupRepo, snapshotID string, dest string, incremental bool, cbtSource cbtservice.SourceInfo, cbtService cbtservice.Service, uploaderCfg map[string]string, log logrus.FieldLogger) (int64, error) {
capturedSnapshotID = snapshotID
capturedVolumePath = dest
return tc.mockRestoreSize, tc.mockRestoreErr
}
bp := &blockProvider{
bkRepo: mockBRepo,
log: logrus.New(),
}
size, err := bp.RunRestore(
t.Context(),
tc.snapshotID,
tc.volumePath,
false,
CBTParam{},
uploader.PersistentVolumeBlock,
map[string]string{},
tc.updater,
)
if tc.expectError {
require.Error(t, err)
if tc.expectedErrStr != "" {
require.ErrorContains(t, err, tc.expectedErrStr)
}
assert.Equal(t, int64(0), size)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expectedSize, size)
if mu, ok := tc.updater.(*blockMockProgressUpdater); ok {
assert.Equal(t, 1, mu.callCount)
require.NotNil(t, mu.lastProgress)
assert.Equal(t, tc.expectedSize, mu.lastProgress.TotalBytes)
assert.Equal(t, tc.expectedSize, mu.lastProgress.BytesDone)
}
}
if tc.checkCaptures != nil {
tc.checkCaptures(t, capturedSnapshotID, capturedVolumePath)
}
})
}
}