Add UT for pkg/discovery

Signed-off-by: Ming <mqiu@vmware.com>
This commit is contained in:
Ming
2023-06-15 02:45:34 +00:00
parent 78025a09b6
commit e2bb5b3fe8
11 changed files with 619 additions and 38 deletions
+2 -2
View File
@@ -268,12 +268,12 @@ func findPreviousSnapshotManifest(ctx context.Context, rep repo.Repository, sour
for _, p := range man {
log.Debugf("Found one snapshot %s, start time %v, incomplete %s, tags %v", p.ID, p.StartTime.ToTime(), p.IncompleteReason, p.Tags)
requestor, found := p.Tags[uploader.SnapshotRequestorTag]
requester, found := p.Tags[uploader.SnapshotRequesterTag]
if !found {
continue
}
if requestor != snapshotTags[uploader.SnapshotRequestorTag] {
if requester != snapshotTags[uploader.SnapshotRequesterTag] {
continue
}
+21 -21
View File
@@ -264,7 +264,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
Path: "/path/to/dir1",
}
snapshotTags := map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
}
noLaterThan := fs.UTCTimestampFromTime(time.Now())
@@ -299,7 +299,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value",
"anotherCustomTag": "123",
@@ -312,7 +312,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
expectedSnapshots: []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value",
"anotherCustomTag": "123",
@@ -330,7 +330,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value1",
"snapshotRequestor": "user1",
@@ -339,7 +339,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
},
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value2",
"snapshotRequestor": "user1",
@@ -351,7 +351,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
expectedSnapshots: []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value1",
"snapshotRequestor": "user1",
@@ -361,14 +361,14 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
},
expectedError: nil,
},
// Snapshot with different requestor
// Snapshot with different requester
{
name: "Snapshot with different requestor",
name: "Snapshot with different requester",
listSnapshotsFunc: func(ctx context.Context, rep repo.Repository, si snapshot.SourceInfo) ([]*snapshot.Manifest, error) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user2",
uploader.SnapshotRequesterTag: "user2",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value",
"snapshotRequestor": "user2",
@@ -387,7 +387,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader2",
"otherTag": "value",
"snapshotRequestor": "user1",
@@ -406,7 +406,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value",
"snapshotRequestor": "user1",
@@ -426,7 +426,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value",
"snapshotRequestor": "user1",
@@ -446,7 +446,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value1",
"snapshotRequestor": "user1",
@@ -455,7 +455,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
},
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value2",
"snapshotRequestor": "user1",
@@ -466,7 +466,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
},
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value3",
"snapshotRequestor": "user1",
@@ -479,7 +479,7 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
expectedSnapshots: []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value3",
"snapshotRequestor": "user1",
@@ -490,14 +490,14 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
},
expectedError: nil,
},
// Snapshot with manifest SnapshotRequestorTag not found
// Snapshot with manifest SnapshotRequesterTag not found
{
name: "Snapshot with manifest SnapshotRequestorTag not found",
name: "Snapshot with manifest SnapshotRequesterTag not found",
listSnapshotsFunc: func(ctx context.Context, rep repo.Repository, si snapshot.SourceInfo) ([]*snapshot.Manifest, error) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
"requestor": "user1",
"requester": "user1",
uploader.SnapshotUploaderTag: "uploader1",
"otherTag": "value",
"snapshotRequestor": "user1",
@@ -510,14 +510,14 @@ func TestFindPreviousSnapshotManifest(t *testing.T) {
expectedSnapshots: []*snapshot.Manifest{},
expectedError: nil,
},
// Snapshot with manifest SnapshotRequestorTag not found
// Snapshot with manifest SnapshotRequesterTag not found
{
name: "Snapshot with manifest SnapshotUploaderTag not found",
listSnapshotsFunc: func(ctx context.Context, rep repo.Repository, si snapshot.SourceInfo) ([]*snapshot.Manifest, error) {
return []*snapshot.Manifest{
{
Tags: map[string]string{
uploader.SnapshotRequestorTag: "user1",
uploader.SnapshotRequesterTag: "user1",
"uploader": "uploader1",
"otherTag": "value",
"snapshotRequestor": "user1",
+1 -1
View File
@@ -150,7 +150,7 @@ func (kp *kopiaProvider) RunBackup(
if tags == nil {
tags = make(map[string]string)
}
tags[uploader.SnapshotRequestorTag] = kp.requestorType
tags[uploader.SnapshotRequesterTag] = kp.requestorType
tags[uploader.SnapshotUploaderTag] = uploader.KopiaType
snapshotInfo, isSnapshotEmpty, err := BackupFunc(ctx, kpUploader, repoWriter, path, realSource, forceFull, parentSnapshot, tags, log)
+5 -5
View File
@@ -65,7 +65,7 @@ func NewUploaderProvider(
ctx context.Context,
client client.Client,
uploaderType string,
requestorType string,
requesterType string,
repoIdentifier string,
bsl *velerov1api.BackupStorageLocation,
backupRepo *velerov1api.BackupRepository,
@@ -73,15 +73,15 @@ func NewUploaderProvider(
repoKeySelector *v1.SecretKeySelector,
log logrus.FieldLogger,
) (Provider, error) {
if requestorType == "" {
return nil, errors.New("requestor type is empty")
if requesterType == "" {
return nil, errors.New("requester type is empty")
}
if credGetter.FromFile == nil {
return nil, errors.New("uninitialized FileStore credentail is not supported")
return nil, errors.New("uninitialized FileStore credential is not supported")
}
if uploaderType == uploader.KopiaType {
return NewKopiaUploaderProvider(requestorType, ctx, credGetter, backupRepo, log)
return NewKopiaUploaderProvider(requesterType, ctx, credGetter, backupRepo, log)
} else {
return NewResticUploaderProvider(repoIdentifier, bsl, credGetter, repoKeySelector, log)
}
+5 -5
View File
@@ -55,25 +55,25 @@ func TestNewUploaderProvider(t *testing.T) {
Description: "When requestorType is empty, it should return an error",
UploaderType: "kopia",
RequestorType: "",
ExpectedError: "requestor type is empty",
ExpectedError: "requester type is empty",
},
{
Description: "When FileStore credential is uninitialized, it should return an error",
UploaderType: "kopia",
RequestorType: "requestor",
ExpectedError: "uninitialized FileStore credentail",
RequestorType: "requester",
ExpectedError: "uninitialized FileStore credential",
},
{
Description: "When uploaderType is kopia, it should return a KopiaUploaderProvider",
UploaderType: "kopia",
RequestorType: "requestor",
RequestorType: "requester",
needFromFile: true,
ExpectedError: "invalid credentials interface",
},
{
Description: "When uploaderType is not kopia, it should return a ResticUploaderProvider",
UploaderType: "restic",
RequestorType: "requestor",
RequestorType: "requester",
needFromFile: true,
ExpectedError: "",
},
+1 -1
View File
@@ -24,7 +24,7 @@ import (
const (
ResticType = "restic"
KopiaType = "kopia"
SnapshotRequestorTag = "snapshot-requestor"
SnapshotRequesterTag = "snapshot-requester"
SnapshotUploaderTag = "snapshot-uploader"
)