From 866e3f810bac9bb846da62cf329402b6a5b31d7b Mon Sep 17 00:00:00 2001 From: Shruthi-1MN Date: Fri, 22 Nov 2019 16:22:50 +0530 Subject: [PATCH] snapshot fixes --- openapi-spec/swagger.yaml | 1 + pkg/api/util/db.go | 17 --------- pkg/api/util/db_test.go | 76 +++++++-------------------------------- pkg/utils/utils.go | 2 +- 4 files changed, 15 insertions(+), 81 deletions(-) diff --git a/openapi-spec/swagger.yaml b/openapi-spec/swagger.yaml index de628705e..c864572cd 100755 --- a/openapi-spec/swagger.yaml +++ b/openapi-spec/swagger.yaml @@ -2751,6 +2751,7 @@ components: minLength: 1 maxLength: 255 type: string + pattern: '^[\w\- ]+$' description: type: string FileShareSnapshotRespSpec: diff --git a/pkg/api/util/db.go b/pkg/api/util/db.go index 74ce0bee8..27808d805 100644 --- a/pkg/api/util/db.go +++ b/pkg/api/util/db.go @@ -170,17 +170,6 @@ func CreateFileShareDBEntry(ctx *c.Context, in *model.FileShareSpec) (*model.Fil if in.UpdatedAt == "" { in.UpdatedAt = time.Now().Format(constants.TimeFormat) } - //validate the name - if in.Name == "" { - errMsg := fmt.Sprintf("empty fileshare name is not allowed. Please give valid name.") - log.Error(errMsg) - return nil, errors.New(errMsg) - } - if len(in.Name) > 255 { - errMsg := fmt.Sprintf("fileshare name length should not be more than 255 characters. input name length is : %d", len(in.Name)) - log.Error(errMsg) - return nil, errors.New(errMsg) - } reg, err := regexp.Compile("^[a-zA-Z0-9_-]+$") if err != nil { @@ -281,12 +270,6 @@ func CreateFileShareSnapshotDBEntry(ctx *c.Context, in *model.FileShareSnapshotS in.CreatedAt = time.Now().Format(constants.TimeFormat) } - //validate the snapshot name - if in.Name == "" { - errMsg := fmt.Sprintf("snapshot name can not be empty. Please give valid snapshot name") - log.Error(errMsg) - return nil, errors.New(errMsg) - } if strings.HasPrefix(in.Name, "snapshot") { errMsg := fmt.Sprintf("names starting 'snapshot' are reserved. Please choose a different snapshot name.") log.Error(errMsg) diff --git a/pkg/api/util/db_test.go b/pkg/api/util/db_test.go index 7a095c80b..533f59c02 100644 --- a/pkg/api/util/db_test.go +++ b/pkg/api/util/db_test.go @@ -17,7 +17,6 @@ package util import ( "fmt" "reflect" - "strconv" "testing" "github.com/opensds/opensds/pkg/utils" @@ -375,6 +374,19 @@ func TestCreateFileShareSnapshotDBEntry(t *testing.T) { assertTestResult(t, result, expected) }) + t.Run("names starting 'snapshot' are reserved", func(t *testing.T) { + req.Name = "snapshotknow" + req.Description = "test snapshot" + 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 + + _, err := CreateFileShareSnapshotDBEntry(context.NewAdminContext(), req) + expectedError := fmt.Sprintf("names starting 'snapshot' are reserved. Please choose a different snapshot name.") + assertTestResult(t, err.Error(), expectedError) + }) } func TestCreateFileShareDBEntry(t *testing.T) { @@ -422,44 +434,6 @@ func TestCreateFileShareDBEntry(t *testing.T) { assertTestResult(t, err.Error(), expectedError) }) - t.Run("Empty file share name is allowed", func(t *testing.T) { - in.Size, in.Name, in.ProfileId = int64(1), "", "b3585ebe-c42c-120g-b28e-f373746a71ca" - mockClient := new(dbtest.Client) - mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil) - db.C = mockClient - - _, err := CreateFileShareDBEntry(context.NewAdminContext(), in) - expectedError := "empty fileshare name is not allowed. Please give valid name." - assertTestResult(t, err.Error(), expectedError) - }) - - t.Run("File share name length equal to 0 character are not allowed", func(t *testing.T) { - in.Name = utils.RandSeqWithAlnum(0) - in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca" - mockClient := new(dbtest.Client) - mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil) - db.C = mockClient - - _, err := CreateFileShareDBEntry(context.NewAdminContext(), in) - expectedError := "empty fileshare name is not allowed. Please give valid name." - assertTestResult(t, err.Error(), expectedError) - }) - - t.Run("File share name length equal to 1 character are allowed", func(t *testing.T) { - in.Name = utils.RandSeqWithAlnum(1) - in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca" - mockClient := new(dbtest.Client) - mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil) - db.C = mockClient - - var expected = &SampleFileShares[0] - result, err := CreateFileShareDBEntry(context.NewAdminContext(), in) - if err != nil { - t.Errorf("failed to create fileshare err is %v\n", err) - } - assertTestResult(t, result, expected) - }) - t.Run("File share name length equal to 10 characters are allowed", func(t *testing.T) { in.Name = utils.RandSeqWithAlnum(10) in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca" @@ -504,30 +478,6 @@ func TestCreateFileShareDBEntry(t *testing.T) { } assertTestResult(t, result, expected) }) - - t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) { - in.Name = utils.RandSeqWithAlnum(256) - in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca" - mockClient := new(dbtest.Client) - mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil) - db.C = mockClient - - _, err := CreateFileShareDBEntry(context.NewAdminContext(), in) - expectedError := "fileshare name length should not be more than 255 characters. input name length is : " + strconv.Itoa(len(in.Name)) - assertTestResult(t, err.Error(), expectedError) - }) - - t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) { - in.Name = utils.RandSeqWithAlnum(257) - in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca" - mockClient := new(dbtest.Client) - mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil) - db.C = mockClient - - _, err := CreateFileShareDBEntry(context.NewAdminContext(), in) - expectedError := "fileshare name length should not be more than 255 characters. input name length is : " + strconv.Itoa(len(in.Name)) - assertTestResult(t, err.Error(), expectedError) - }) } func TestDeleteFileShareDBEntry(t *testing.T) { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index b3a35fe39..8b2310c42 100755 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -335,4 +335,4 @@ func ContainsIgnoreCase(a []string, x string) bool { } } return false -} +} \ No newline at end of file