add UT for progress message

Signed-off-by: Lyndon-Li <lyonghui@vmware.com>
This commit is contained in:
Lyndon-Li
2026-09-04 16:04:38 +08:00
parent 1048f26c20
commit 5146992b5b
7 changed files with 109 additions and 33 deletions
@@ -787,6 +787,15 @@ func TestOnDataDownloadProgress(t *testing.T) {
BytesDone: bytesDone,
},
},
{
name: "patch in progress phase with negative progress values and message",
dd: dataDownloadBuilder().Result(),
progress: uploader.Progress{
TotalBytes: -1,
BytesDone: -1,
Message: "some warning message",
},
},
{
name: "failed to get datadownload",
dd: dataDownloadBuilder().Result(),
@@ -815,20 +824,28 @@ func TestOnDataDownloadProgress(t *testing.T) {
require.NoError(t, r.client.Create(t.Context(), dd))
// Create a Progress object
progress := &uploader.Progress{
TotalBytes: totalBytes,
BytesDone: bytesDone,
}
progress := &test.progress
// Call the OnDataDownloadProgress function
r.OnDataDownloadProgress(ctx, namespace, duName, progress)
if len(test.needErrs) != 0 && !test.needErrs[0] {
// Get the updated DataDownload object from the fake client
updatedDu := &velerov2alpha1api.DataDownload{}
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
updatedDd := &velerov2alpha1api.DataDownload{}
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDd))
// Assert that the DataDownload object has been updated with the progress
assert.Equal(t, test.progress.TotalBytes, updatedDu.Status.Progress.TotalBytes)
assert.Equal(t, test.progress.BytesDone, updatedDu.Status.Progress.BytesDone)
if progress.TotalBytes != -1 {
assert.Equal(t, test.progress.TotalBytes, updatedDd.Status.Progress.TotalBytes)
} else {
assert.Equal(t, int64(0), updatedDd.Status.Progress.TotalBytes) // assuming default or original value
}
if progress.BytesDone != -1 {
assert.Equal(t, test.progress.BytesDone, updatedDd.Status.Progress.BytesDone)
} else {
assert.Equal(t, int64(0), updatedDd.Status.Progress.BytesDone) // assuming default or original value
}
if progress.Message != "" {
assert.Contains(t, updatedDd.Status.Message, progress.Message)
}
}
})
}
+23 -6
View File
@@ -809,6 +809,15 @@ func TestOnDataUploadProgress(t *testing.T) {
BytesDone: bytesDone,
},
},
{
name: "patch in progress phase with negative progress values and message",
du: dataUploadBuilder().Result(),
progress: uploader.Progress{
TotalBytes: -1,
BytesDone: -1,
Message: "some warning message",
},
},
{
name: "failed to get dataupload",
du: dataUploadBuilder().Result(),
@@ -837,10 +846,7 @@ func TestOnDataUploadProgress(t *testing.T) {
require.NoError(t, r.client.Create(t.Context(), du))
// Create a Progress object
progress := &uploader.Progress{
TotalBytes: totalBytes,
BytesDone: bytesDone,
}
progress := &test.progress
// Call the OnDataUploadProgress function
r.OnDataUploadProgress(ctx, namespace, duName, progress)
@@ -849,8 +855,19 @@ func TestOnDataUploadProgress(t *testing.T) {
updatedDu := &velerov2alpha1api.DataUpload{}
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
// Assert that the DataUpload object has been updated with the progress
assert.Equal(t, test.progress.TotalBytes, updatedDu.Status.Progress.TotalBytes)
assert.Equal(t, test.progress.BytesDone, updatedDu.Status.Progress.BytesDone)
if progress.TotalBytes != -1 {
assert.Equal(t, test.progress.TotalBytes, updatedDu.Status.Progress.TotalBytes)
} else {
assert.Equal(t, int64(0), updatedDu.Status.Progress.TotalBytes) // assuming default or original value
}
if progress.BytesDone != -1 {
assert.Equal(t, test.progress.BytesDone, updatedDu.Status.Progress.BytesDone)
} else {
assert.Equal(t, int64(0), updatedDu.Status.Progress.BytesDone) // assuming default or original value
}
if progress.Message != "" {
assert.Contains(t, updatedDu.Status.Message, progress.Message)
}
}
})
}
@@ -625,6 +625,15 @@ func TestOnPVBProgress(t *testing.T) {
BytesDone: bytesDone,
},
},
{
name: "patch in progress phase with negative progress values and message",
pvb: pvbBuilder().Result(),
progress: uploader.Progress{
TotalBytes: -1,
BytesDone: -1,
Message: "some warning message",
},
},
{
name: "failed to get pvb",
pvb: pvbBuilder().Result(),
@@ -653,17 +662,25 @@ func TestOnPVBProgress(t *testing.T) {
require.NoError(t, r.client.Create(t.Context(), pvb))
// Create a Progress object
progress := &uploader.Progress{
TotalBytes: totalBytes,
BytesDone: bytesDone,
}
progress := &test.progress
r.OnDataPathProgress(ctx, namespace, pvbName, progress)
if len(test.needErrs) != 0 && !test.needErrs[0] {
updatedPvb := &velerov1api.PodVolumeBackup{}
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: pvbName, Namespace: namespace}, updatedPvb))
assert.Equal(t, test.progress.TotalBytes, updatedPvb.Status.Progress.TotalBytes)
assert.Equal(t, test.progress.BytesDone, updatedPvb.Status.Progress.BytesDone)
if progress.TotalBytes != -1 {
assert.Equal(t, test.progress.TotalBytes, updatedPvb.Status.Progress.TotalBytes)
} else {
assert.Equal(t, int64(0), updatedPvb.Status.Progress.TotalBytes) // assuming default or original value
}
if progress.BytesDone != -1 {
assert.Equal(t, test.progress.BytesDone, updatedPvb.Status.Progress.BytesDone)
} else {
assert.Equal(t, int64(0), updatedPvb.Status.Progress.BytesDone) // assuming default or original value
}
if progress.Message != "" {
assert.Contains(t, updatedPvb.Status.Message, progress.Message)
}
}
})
}
@@ -1470,6 +1470,15 @@ func TestOnPodVolumeRestoreProgress(t *testing.T) {
BytesDone: bytesDone,
},
},
{
name: "patch in progress phase with negative progress values and message",
pvr: pvrBuilder().Result(),
progress: uploader.Progress{
TotalBytes: -1,
BytesDone: -1,
Message: "some warning message",
},
},
{
name: "failed to get pvr",
pvr: pvrBuilder().Result(),
@@ -1498,17 +1507,25 @@ func TestOnPodVolumeRestoreProgress(t *testing.T) {
require.NoError(t, r.client.Create(t.Context(), pvr))
// Create a Progress object
progress := &uploader.Progress{
TotalBytes: totalBytes,
BytesDone: bytesDone,
}
progress := &test.progress
r.OnDataPathProgress(ctx, namespace, pvrName, progress)
if len(test.needErrs) != 0 && !test.needErrs[0] {
updatedPVR := &velerov1api.PodVolumeRestore{}
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: pvrName, Namespace: namespace}, updatedPVR))
assert.Equal(t, test.progress.TotalBytes, updatedPVR.Status.Progress.TotalBytes)
assert.Equal(t, test.progress.BytesDone, updatedPVR.Status.Progress.BytesDone)
if progress.TotalBytes != -1 {
assert.Equal(t, test.progress.TotalBytes, updatedPVR.Status.Progress.TotalBytes)
} else {
assert.Equal(t, int64(0), updatedPVR.Status.Progress.TotalBytes) // assuming default or original value
}
if progress.BytesDone != -1 {
assert.Equal(t, test.progress.BytesDone, updatedPVR.Status.Progress.BytesDone)
} else {
assert.Equal(t, int64(0), updatedPVR.Status.Progress.BytesDone) // assuming default or original value
}
if progress.Message != "" {
assert.Contains(t, updatedPVR.Status.Message, progress.Message)
}
}
})
}
+8
View File
@@ -295,6 +295,8 @@ func TestBlockUploaderBackup(t *testing.T) {
var iterator cbt.Iterator
if !tc.nilBitmap {
iterMock := cbtmocks.NewIterator(t)
iterMock.On("Error").Return(nil).Maybe()
iterMock.On("Error").Return(nil).Maybe()
iterator = iterMock
backupMode := udmrepo.ObjectDataBackupModeInc
@@ -573,6 +575,7 @@ func TestRestoreData(t *testing.T) {
reader := bytes.NewReader(data)
iterMock := cbtmocks.NewIterator(t)
iterMock.On("Error").Return(nil).Maybe()
iterMock.On("Count").Return(uint64(1))
iterMock.On("Next").Return(uint64(0), true).Once()
iterMock.On("Next").Return(uint64(0), false)
@@ -603,6 +606,7 @@ func TestRestoreData(t *testing.T) {
reader := &errReader{err: errors.New("read error")}
iterMock := cbtmocks.NewIterator(t)
iterMock.On("Error").Return(nil).Maybe()
iterMock.On("Count").Return(uint64(1))
iterMock.On("Next").Return(uint64(0), true).Once()
iterMock.On("Next").Return(uint64(0), false)
@@ -623,6 +627,7 @@ func TestBlockUploaderRestore(t *testing.T) {
repoWriter.On("ReadMetadata", mock.Anything, udmrepo.ID("root-id")).Return(nil, errors.New("meta not found"))
iterMock := cbtmocks.NewIterator(t)
iterMock.On("Error").Return(nil).Maybe()
_, _, err := blkup.Restore(udmrepo.Snapshot{RootObject: udmrepo.ObjectMetadata{ID: "root-id"}}, destInfo{}, iterMock, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "meta not found")
@@ -680,6 +685,7 @@ func TestBlockUploaderRestore(t *testing.T) {
}
iterMock := cbtmocks.NewIterator(t)
iterMock.On("Error").Return(nil).Maybe()
iterMock.On("Count").Return(uint64(1))
iterMock.On("Next").Return(uint64(0), true).Once()
iterMock.On("Next").Return(uint64(0), false)
@@ -708,6 +714,7 @@ func TestBlockUploaderRestore(t *testing.T) {
}
dest := destInfo{size: 4194304, path: "/dev/target"}
iterMock := cbtmocks.NewIterator(t)
iterMock.On("Error").Return(nil).Maybe()
_, _, err := blkup.Restore(snap, dest, iterMock, nil)
require.Error(t, err)
@@ -732,6 +739,7 @@ func TestBlockUploaderRestore(t *testing.T) {
}
dest := destInfo{size: 512, path: "/dev/small"}
iterMock := cbtmocks.NewIterator(t)
iterMock.On("Error").Return(nil).Maybe()
_, _, err := blkup.Restore(snap, dest, iterMock, nil)
require.Error(t, err)
+3 -3
View File
@@ -200,7 +200,7 @@ func TestSnapshotSource(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
s := injectSnapshotFuncs()
MockFuncs(s, tc.args)
_, _, err = SnapshotSource(ctx, s.repoWriterMock, s.uploderMock, sourceInfo, rootDir, false, "/", nil, tc.uploaderCfg, log, "TestSnapshotSource")
_, _, err = SnapshotSource(ctx, s.repoWriterMock, s.uploderMock, sourceInfo, rootDir, false, "/", nil, tc.uploaderCfg, &fakeProgressUpdater{}, log, "TestSnapshotSource")
if tc.notError {
assert.NoError(t, err)
} else {
@@ -648,9 +648,9 @@ func TestBackup(t *testing.T) {
var snapshotInfo *uploader.SnapshotInfo
var err error
if tc.isEmptyUploader {
snapshotInfo, isSnapshotEmpty, err = Backup(t.Context(), nil, s.repoWriterMock, tc.sourcePath, "", tc.forceFull, tc.parentSnapshot, tc.volMode, map[string]string{}, tc.tags, &logrus.Logger{})
snapshotInfo, isSnapshotEmpty, err = Backup(t.Context(), nil, s.repoWriterMock, tc.sourcePath, "", tc.forceFull, tc.parentSnapshot, tc.volMode, map[string]string{}, tc.tags, &fakeProgressUpdater{}, &logrus.Logger{})
} else {
snapshotInfo, isSnapshotEmpty, err = Backup(t.Context(), s.uploderMock, s.repoWriterMock, tc.sourcePath, "", tc.forceFull, tc.parentSnapshot, tc.volMode, map[string]string{}, tc.tags, &logrus.Logger{})
snapshotInfo, isSnapshotEmpty, err = Backup(t.Context(), s.uploderMock, s.repoWriterMock, tc.sourcePath, "", tc.forceFull, tc.parentSnapshot, tc.volMode, map[string]string{}, tc.tags, &fakeProgressUpdater{}, &logrus.Logger{})
}
// Check if the returned error matches the expected error
if tc.expectedError != nil {
+4 -4
View File
@@ -65,27 +65,27 @@ func (f *FakeRestoreProgressUpdater) UpdateProgress(p *uploader.Progress) {}
func TestRunBackup(t *testing.T) {
testCases := []struct {
name string
hookBackupFunc func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error)
hookBackupFunc func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, updater uploader.ProgressUpdater, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error)
volMode uploader.PersistentVolumeMode
notError bool
}{
{
name: "success to backup",
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, updater uploader.ProgressUpdater, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
return &uploader.SnapshotInfo{}, false, nil
},
notError: true,
},
{
name: "get error to backup",
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, updater uploader.ProgressUpdater, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
return &uploader.SnapshotInfo{}, false, errors.New("failed to backup")
},
notError: false,
},
{
name: "success to backup block mode volume",
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
hookBackupFunc: func(ctx context.Context, fsUploader kopia.SnapshotUploader, repoWriter repo.RepositoryWriter, sourcePath string, realSource string, forceFull bool, parentSnapshot string, volMode uploader.PersistentVolumeMode, uploaderCfg map[string]string, tags map[string]string, updater uploader.ProgressUpdater, log logrus.FieldLogger) (*uploader.SnapshotInfo, bool, error) {
return &uploader.SnapshotInfo{}, false, nil
},
volMode: uploader.PersistentVolumeBlock,