Skip to content

Commit 00f8b14

Browse files
authored
Merge pull request #1077 from Shruthi-1MN/Fix#1008
Fileshare name supports upto 255 characters name length
2 parents 7aefd14 + f0425db commit 00f8b14

3 files changed

Lines changed: 101 additions & 5 deletions

File tree

pkg/api/util/db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ func CreateFileShareDBEntry(ctx *c.Context, in *model.FileShareSpec) (*model.Fil
176176
log.Error(errMsg)
177177
return nil, errors.New(errMsg)
178178
}
179-
if len(in.Name) > 128 {
180-
errMsg := fmt.Sprintf("fileshare name length should not more than 128 characters. current length is : %d", len(in.Name))
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))
181181
log.Error(errMsg)
182182
return nil, errors.New(errMsg)
183183
}

pkg/api/util/db_test.go

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package util
1616

1717
import (
1818
"fmt"
19+
"github.com/opensds/opensds/pkg/utils"
1920
"reflect"
2021
"strconv"
2122
"testing"
@@ -431,14 +432,99 @@ func TestCreateFileShareDBEntry(t *testing.T) {
431432
assertTestResult(t, err.Error(), expectedError)
432433
})
433434

434-
t.Run("File share name length more than 128 characters are not allowed", func(t *testing.T) {
435-
in.Size, in.Name, in.ProfileId = int64(1), "kjxdpvfuenecpyhbxdumwibipjkigcjykabuhghjghjgjgjgjgjghhkkkkkkkkkkkkkkklsccdesnkgxnjnhfjusigceorogdgjlyciemscgvncerucokfekdiviuyplbly", "b3585ebe-c42c-120g-b28e-f373746a71ca"
435+
t.Run("File share name length equal to 0 character are not allowed", func(t *testing.T) {
436+
in.Name = utils.RandomString(0)
437+
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
436438
mockClient := new(dbtest.Client)
437439
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
438440
db.C = mockClient
439441

440442
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
441-
expectedError := "fileshare name length should not more than 128 characters. current length is : " + strconv.Itoa(len(in.Name))
443+
expectedError := "empty fileshare name is not allowed. Please give valid name."
444+
assertTestResult(t, err.Error(), expectedError)
445+
})
446+
447+
t.Run("File share name length equal to 1 character are allowed", func(t *testing.T) {
448+
in.Name = utils.RandomString(1)
449+
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
450+
mockClient := new(dbtest.Client)
451+
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
452+
db.C = mockClient
453+
454+
var expected = &SampleFileShares[0]
455+
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
456+
if err != nil {
457+
t.Errorf("failed to create fileshare err is %v\n", err)
458+
}
459+
assertTestResult(t, result, expected)
460+
})
461+
462+
t.Run("File share name length equal to 10 characters are allowed", func(t *testing.T) {
463+
in.Name = utils.RandomString(10)
464+
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
465+
mockClient := new(dbtest.Client)
466+
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
467+
db.C = mockClient
468+
469+
var expected = &SampleFileShares[0]
470+
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
471+
if err != nil {
472+
t.Errorf("failed to create fileshare err is %v\n", err)
473+
}
474+
assertTestResult(t, result, expected)
475+
})
476+
477+
t.Run("File share name length equal to 254 characters are allowed", func(t *testing.T) {
478+
in.Name = utils.RandomString(254)
479+
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
480+
mockClient := new(dbtest.Client)
481+
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
482+
db.C = mockClient
483+
484+
var expected = &SampleFileShares[0]
485+
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
486+
if err != nil {
487+
t.Errorf("failed to create fileshare err is %v\n", err)
488+
}
489+
assertTestResult(t, result, expected)
490+
})
491+
492+
t.Run("File share name length equal to 255 characters are allowed", func(t *testing.T) {
493+
in.Name = utils.RandomString(255)
494+
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
495+
mockClient := new(dbtest.Client)
496+
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
497+
db.C = mockClient
498+
499+
var expected = &SampleFileShares[0]
500+
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
501+
if err != nil {
502+
t.Errorf("failed to create fileshare err is %v\n", err)
503+
}
504+
assertTestResult(t, result, expected)
505+
})
506+
507+
t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) {
508+
in.Name = utils.RandomString(256)
509+
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
510+
mockClient := new(dbtest.Client)
511+
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
512+
db.C = mockClient
513+
514+
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
515+
expectedError := "fileshare name length should not be more than 255 characters. input name length is : "+strconv.Itoa(len(in.Name))
516+
assertTestResult(t, err.Error(), expectedError)
517+
})
518+
519+
t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) {
520+
in.Name = utils.RandomString(257)
521+
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
522+
mockClient := new(dbtest.Client)
523+
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
524+
db.C = mockClient
525+
526+
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
527+
expectedError := "fileshare name length should not be more than 255 characters. input name length is : "+strconv.Itoa(len(in.Name))
442528
assertTestResult(t, err.Error(), expectedError)
443529
})
444530

pkg/utils/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,13 @@ func WaitForCondition(f func() (bool, error), interval, timeout time.Duration) e
250250
}
251251
return fmt.Errorf("wait for condition timeout")
252252
}
253+
254+
func RandomString(n int) string {
255+
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
256+
257+
b := make([]rune, n)
258+
for i := range b {
259+
b[i] = letter[rand.Intn(len(letter))]
260+
}
261+
return string(b)
262+
}

0 commit comments

Comments
 (0)