Skip to content

Commit 1e90bd1

Browse files
author
Shruthi-1MN
committed
File share name fixes
1 parent f8581c3 commit 1e90bd1

File tree

3 files changed

+30
-117
lines changed

3 files changed

+30
-117
lines changed

openapi-spec/swagger.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ components:
26772677
type: string
26782678
minLength: 1
26792679
maxLength: 255
2680-
pattern: '[A-Za-z0-9_-]'
2680+
pattern: '^[\w\- ]+$'
26812681
description:
26822682
type: string
26832683
size:

pkg/api/util/db.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"errors"
2323
"fmt"
2424
"net"
25-
"regexp"
2625
"strings"
2726
"time"
2827

@@ -170,28 +169,23 @@ func CreateFileShareDBEntry(ctx *c.Context, in *model.FileShareSpec) (*model.Fil
170169
if in.UpdatedAt == "" {
171170
in.UpdatedAt = time.Now().Format(constants.TimeFormat)
172171
}
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-
}
184172

185-
reg, err := regexp.Compile("^[a-zA-Z0-9_-]+$")
173+
shares, err := db.C.ListFileShares(ctx)
186174
if err != nil {
187-
errMsg := fmt.Sprintf("regex compilation for file name validation failed")
188-
log.Error(errMsg)
189-
return nil, errors.New(errMsg)
190-
}
191-
if reg.MatchString(in.Name) == false {
192-
errMsg := fmt.Sprintf("invalid fileshare name it only contain english char and number : %v", in.Name)
193-
log.Error(errMsg)
194-
return nil, errors.New(errMsg)
175+
return nil, err
176+
} else {
177+
for _, fshare := range shares {
178+
if fshare.Name == in.Name {
179+
errMsg := fmt.Sprintf("file share name already exists")
180+
log.Error(errMsg)
181+
return nil, errors.New(errMsg)
182+
}
183+
if fshare.Id == in.Id {
184+
errMsg := fmt.Sprintf("file share id already exists")
185+
log.Error(errMsg)
186+
return nil, errors.New(errMsg)
187+
}
188+
}
195189
}
196190

197191
in.UserId = ctx.UserId

pkg/api/util/db_test.go

Lines changed: 14 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ package util
1717
import (
1818
"fmt"
1919
"reflect"
20-
"strconv"
2120
"testing"
2221

23-
"github.com/opensds/opensds/pkg/utils"
24-
2522
"github.com/opensds/opensds/pkg/context"
2623
"github.com/opensds/opensds/pkg/db"
2724
"github.com/opensds/opensds/pkg/model"
@@ -390,6 +387,7 @@ func TestCreateFileShareDBEntry(t *testing.T) {
390387
t.Run("Everything should work well", func(t *testing.T) {
391388
mockClient := new(dbtest.Client)
392389
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
390+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(nil, nil)
393391
db.C = mockClient
394392

395393
var expected = &SampleFileShares[0]
@@ -404,6 +402,7 @@ func TestCreateFileShareDBEntry(t *testing.T) {
404402
in.Size = int64(-2)
405403
mockClient := new(dbtest.Client)
406404
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
405+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(nil, nil)
407406
db.C = mockClient
408407

409408
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
@@ -415,117 +414,37 @@ func TestCreateFileShareDBEntry(t *testing.T) {
415414
in.ProfileId = ""
416415
mockClient := new(dbtest.Client)
417416
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
417+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(nil, nil)
418418
db.C = mockClient
419419

420420
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
421421
expectedError := "profile id can not be empty when creating fileshare in db!"
422422
assertTestResult(t, err.Error(), expectedError)
423423
})
424424

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-
463-
t.Run("File share name length equal to 10 characters are allowed", func(t *testing.T) {
464-
in.Name = utils.RandSeqWithAlnum(10)
465-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
466-
mockClient := new(dbtest.Client)
467-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
468-
db.C = mockClient
469-
470-
var expected = &SampleFileShares[0]
471-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
472-
if err != nil {
473-
t.Errorf("failed to create fileshare err is %v\n", err)
474-
}
475-
assertTestResult(t, result, expected)
476-
})
477-
478-
t.Run("File share name length equal to 254 characters are allowed", func(t *testing.T) {
479-
in.Name = utils.RandSeqWithAlnum(254)
480-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
481-
mockClient := new(dbtest.Client)
482-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
483-
db.C = mockClient
484-
485-
var expected = &SampleFileShares[0]
486-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
487-
if err != nil {
488-
t.Errorf("failed to create fileshare err is %v\n", err)
489-
}
490-
assertTestResult(t, result, expected)
491-
})
492-
493-
t.Run("File share name length equal to 255 characters are allowed", func(t *testing.T) {
494-
in.Name = utils.RandSeqWithAlnum(255)
495-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
496-
mockClient := new(dbtest.Client)
497-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
498-
db.C = mockClient
499-
500-
var expected = &SampleFileShares[0]
501-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
502-
if err != nil {
503-
t.Errorf("failed to create fileshare err is %v\n", err)
504-
}
505-
assertTestResult(t, result, expected)
506-
})
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"
425+
t.Run("Duplicate file share name are not allowed", func(t *testing.T) {
426+
var sampleshares = []*model.FileShareSpec{&SampleFileShares[0]}
427+
in.Size, in.Name, in.ProfileId, in.Description = int64(1), "samplefileshare01", "b3585ebe-c42c-120g-b28e-f373746a71ca", "File share test"
511428
mockClient := new(dbtest.Client)
512429
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
430+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(sampleshares, nil)
513431
db.C = mockClient
514432

515433
_, 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))
434+
expectedError := "file share name already exists"
517435
assertTestResult(t, err.Error(), expectedError)
518436
})
519437

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"
438+
t.Run("Overwritting existing file share resource are not allowed", func(t *testing.T) {
439+
var sampleshares = []*model.FileShareSpec{&SampleFileShares[0]}
440+
in.Size, in.Name, in.ProfileId, in.Description, in.Id = int64(1), "samplefileshare05", "b3585ebe-c42c-120g-b28e-f373746a71ca", "File share test", "d2975ebe-d82c-430f-b28e-f373746a71ca"
523441
mockClient := new(dbtest.Client)
524442
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
443+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(sampleshares, nil)
525444
db.C = mockClient
526445

527446
_, 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))
447+
expectedError := "file share id already exists"
529448
assertTestResult(t, err.Error(), expectedError)
530449
})
531450
}
@@ -811,4 +730,4 @@ func TestDeleteFileShareSnapshotDBEntry(t *testing.T) {
811730
expectedError := fmt.Sprintf("only the fileshare snapshot with the status available, error, error_deleting can be deleted, the fileshare status is %s", in.Status)
812731
assertTestResult(t, err.Error(), expectedError)
813732
})
814-
}
733+
}

0 commit comments

Comments
 (0)