Skip to content

Commit 866e3f8

Browse files
author
Shruthi-1MN
committed
snapshot fixes
1 parent f8581c3 commit 866e3f8

4 files changed

Lines changed: 15 additions & 81 deletions

File tree

openapi-spec/swagger.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,7 @@ components:
27512751
minLength: 1
27522752
maxLength: 255
27532753
type: string
2754+
pattern: '^[\w\- ]+$'
27542755
description:
27552756
type: string
27562757
FileShareSnapshotRespSpec:

pkg/api/util/db.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,6 @@ func CreateFileShareDBEntry(ctx *c.Context, in *model.FileShareSpec) (*model.Fil
170170
if in.UpdatedAt == "" {
171171
in.UpdatedAt = time.Now().Format(constants.TimeFormat)
172172
}
173-
//validate the name
174-
if in.Name == "" {
175-
errMsg := fmt.Sprintf("empty fileshare name is not allowed. Please give valid name.")
176-
log.Error(errMsg)
177-
return nil, errors.New(errMsg)
178-
}
179-
if len(in.Name) > 255 {
180-
errMsg := fmt.Sprintf("fileshare name length should not be more than 255 characters. input name length is : %d", len(in.Name))
181-
log.Error(errMsg)
182-
return nil, errors.New(errMsg)
183-
}
184173

185174
reg, err := regexp.Compile("^[a-zA-Z0-9_-]+$")
186175
if err != nil {
@@ -281,12 +270,6 @@ func CreateFileShareSnapshotDBEntry(ctx *c.Context, in *model.FileShareSnapshotS
281270
in.CreatedAt = time.Now().Format(constants.TimeFormat)
282271
}
283272

284-
//validate the snapshot name
285-
if in.Name == "" {
286-
errMsg := fmt.Sprintf("snapshot name can not be empty. Please give valid snapshot name")
287-
log.Error(errMsg)
288-
return nil, errors.New(errMsg)
289-
}
290273
if strings.HasPrefix(in.Name, "snapshot") {
291274
errMsg := fmt.Sprintf("names starting 'snapshot' are reserved. Please choose a different snapshot name.")
292275
log.Error(errMsg)

pkg/api/util/db_test.go

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package util
1717
import (
1818
"fmt"
1919
"reflect"
20-
"strconv"
2120
"testing"
2221

2322
"github.com/opensds/opensds/pkg/utils"
@@ -375,6 +374,19 @@ func TestCreateFileShareSnapshotDBEntry(t *testing.T) {
375374
assertTestResult(t, result, expected)
376375
})
377376

377+
t.Run("names starting 'snapshot' are reserved", func(t *testing.T) {
378+
req.Name = "snapshotknow"
379+
req.Description = "test snapshot"
380+
mockClient := new(dbtest.Client)
381+
mockClient.On("GetFileShare", context.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(fileshare, nil)
382+
mockClient.On("ListFileShareSnapshots", context.NewAdminContext()).Return(nil, nil)
383+
mockClient.On("CreateFileShareSnapshot", context.NewAdminContext(), req).Return(&SampleShareSnapshots[0], nil)
384+
db.C = mockClient
385+
386+
_, err := CreateFileShareSnapshotDBEntry(context.NewAdminContext(), req)
387+
expectedError := fmt.Sprintf("names starting 'snapshot' are reserved. Please choose a different snapshot name.")
388+
assertTestResult(t, err.Error(), expectedError)
389+
})
378390
}
379391

380392
func TestCreateFileShareDBEntry(t *testing.T) {
@@ -422,44 +434,6 @@ func TestCreateFileShareDBEntry(t *testing.T) {
422434
assertTestResult(t, err.Error(), expectedError)
423435
})
424436

425-
t.Run("Empty file share name is allowed", func(t *testing.T) {
426-
in.Size, in.Name, in.ProfileId = int64(1), "", "b3585ebe-c42c-120g-b28e-f373746a71ca"
427-
mockClient := new(dbtest.Client)
428-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
429-
db.C = mockClient
430-
431-
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
432-
expectedError := "empty fileshare name is not allowed. Please give valid name."
433-
assertTestResult(t, err.Error(), expectedError)
434-
})
435-
436-
t.Run("File share name length equal to 0 character are not allowed", func(t *testing.T) {
437-
in.Name = utils.RandSeqWithAlnum(0)
438-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
439-
mockClient := new(dbtest.Client)
440-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
441-
db.C = mockClient
442-
443-
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
444-
expectedError := "empty fileshare name is not allowed. Please give valid name."
445-
assertTestResult(t, err.Error(), expectedError)
446-
})
447-
448-
t.Run("File share name length equal to 1 character are allowed", func(t *testing.T) {
449-
in.Name = utils.RandSeqWithAlnum(1)
450-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
451-
mockClient := new(dbtest.Client)
452-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
453-
db.C = mockClient
454-
455-
var expected = &SampleFileShares[0]
456-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
457-
if err != nil {
458-
t.Errorf("failed to create fileshare err is %v\n", err)
459-
}
460-
assertTestResult(t, result, expected)
461-
})
462-
463437
t.Run("File share name length equal to 10 characters are allowed", func(t *testing.T) {
464438
in.Name = utils.RandSeqWithAlnum(10)
465439
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
@@ -504,30 +478,6 @@ func TestCreateFileShareDBEntry(t *testing.T) {
504478
}
505479
assertTestResult(t, result, expected)
506480
})
507-
508-
t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) {
509-
in.Name = utils.RandSeqWithAlnum(256)
510-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
511-
mockClient := new(dbtest.Client)
512-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
513-
db.C = mockClient
514-
515-
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
516-
expectedError := "fileshare name length should not be more than 255 characters. input name length is : " + strconv.Itoa(len(in.Name))
517-
assertTestResult(t, err.Error(), expectedError)
518-
})
519-
520-
t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) {
521-
in.Name = utils.RandSeqWithAlnum(257)
522-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
523-
mockClient := new(dbtest.Client)
524-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
525-
db.C = mockClient
526-
527-
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
528-
expectedError := "fileshare name length should not be more than 255 characters. input name length is : " + strconv.Itoa(len(in.Name))
529-
assertTestResult(t, err.Error(), expectedError)
530-
})
531481
}
532482

533483
func TestDeleteFileShareDBEntry(t *testing.T) {

pkg/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,4 @@ func ContainsIgnoreCase(a []string, x string) bool {
335335
}
336336
}
337337
return false
338-
}
338+
}

0 commit comments

Comments
 (0)