@@ -17,11 +17,8 @@ package util
1717import (
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 ), "sample-fileshare-01" , "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 ), "sample-fileshare-05" , " 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