Skip to content

Commit

Permalink
Merge pull request #945 from Shruthi-1MN/snapshot-fix#937
Browse files Browse the repository at this point in the history
Snapshot name existence check - Fix #931
  • Loading branch information
leonwanghui committed Jul 9, 2019
2 parents b5b0280 + fa8a98c commit a6b8da5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/api/util/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ func CreateFileShareSnapshotDBEntry(ctx *c.Context, in *model.FileShareSnapshotS
return nil, errors.New(errMsg)
}

// Check existence of fileshare snapshot name #931
filesnaps, err := db.C.ListFileShareSnapshots(ctx)
if err != nil {
errMsg := fmt.Sprintf("get list of fileshare snapshot failed: %s", err.Error())
log.Error(errMsg)
return nil, errors.New(errMsg)
} else {
for _, filesnap := range filesnaps {
if filesnap.Name == in.Name {
errMsg := fmt.Sprintf("file share snapshot name already exists")
log.Error(errMsg)
return nil, errors.New(errMsg)
}
}
}

if in.Id == "" {
in.Id = uuid.NewV4().String()
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/api/util/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,51 @@ func TestDeleteVolumeSnapshotDBEntry(t *testing.T) {
}
})
}

func TestCreateFileShareSnapshotDBEntry(t *testing.T) {
var fileshare = &model.FileShareSpec{
BaseModel: &model.BaseModel{
Id: "bd5b12a8-a101-11e7-941e-d77981b584d8",
},
Status: "available",
}
var req = &model.FileShareSnapshotSpec{
BaseModel: &model.BaseModel{
Id: "3769855c-a102-11e7-b772-17b880d2f537",
},
Name: "sample-snapshot-01",
Description: "This is the first sample snapshot for testing",
Status: "available",
ShareSize: int64(1),
FileShareId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
}

var sampleSnapshots = []*model.FileShareSnapshotSpec{&SampleShareSnapshots[0]}
t.Run("-ve test case - snapshot name already exists", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetFileShare", context.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(fileshare, nil)
mockClient.On("ListFileShareSnapshots", context.NewAdminContext()).Return(sampleSnapshots, nil)
db.C = mockClient

_, err := CreateFileShareSnapshotDBEntry(context.NewAdminContext(), req)
expectedError := "file share snapshot name already exists"
assertTestResult(t, err.Error(), expectedError)
})

t.Run("test +ve", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetFileShare", context.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(fileshare, nil)
mockClient.On("ListFileShareSnapshots", context.NewAdminContext()).Return(nil, nil)
mockClient.On("CreateFileShareSnapshot", context.NewAdminContext(), req).Return(&SampleShareSnapshots[0], nil)
db.C = mockClient

var expected = &SampleShareSnapshots[0]
result, err := CreateFileShareSnapshotDBEntry(context.NewAdminContext(), req)
if err != nil {
t.Errorf("failed to create fileshare snapshot, err is %v\n", err)
}
assertTestResult(t, result, expected)
})

}

0 comments on commit a6b8da5

Please sign in to comment.