@@ -238,6 +238,7 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
238238 barDirPath := "/home/aceuser/initial-config/bars"
239239 var osMkdirRestore = osMkdir
240240 var osCreateRestore = osCreate
241+ var osStatRestore = osStat
241242 var ioutilReadFileRestore = ioutilReadFile
242243 var ioCopyRestore = ioCopy
243244 var contentserverGetBARRestore = contentserverGetBAR
@@ -270,6 +271,9 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
270271 osCreate = func (file string ) (* os.File , error ) {
271272 panic ("should be mocked" )
272273 }
274+ osStat = func (file string ) (os.FileInfo , error ) {
275+ panic ("should be mocked" )
276+ }
273277 ioCopy = func (target io.Writer , source io.Reader ) (int64 , error ) {
274278 panic ("should be mocked" )
275279 }
@@ -284,6 +288,7 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
284288 var restore = func () {
285289 osMkdir = osMkdirRestore
286290 osCreate = osCreateRestore
291+ osStat = osStatRestore
287292 ioutilReadFile = ioutilReadFileRestore
288293 ioCopy = ioCopyRestore
289294 contentserverGetBAR = contentserverGetBARRestore
@@ -359,6 +364,12 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
359364 return nil , nil
360365 }
361366
367+ osStat = func (file string ) (os.FileInfo , error ) {
368+ // Should not be called
369+ t .Errorf ("Should not check if file exist when only single bar URL" )
370+ return nil , nil
371+ }
372+
362373 ioutilReadFile = func (cafile string ) ([]byte , error ) {
363374 assert .Equal (t , "/home/aceuser/ssl/cacert.pem" , cafile )
364375 return []byte (dummyCert ), nil
@@ -441,6 +452,12 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
441452 return nil , nil
442453 }
443454
455+ osStat = func (file string ) (os.FileInfo , error ) {
456+ // Should not be called
457+ t .Errorf ("Should not check if file exist when only single bar URL" )
458+ return nil , nil
459+ }
460+
444461 ioutilReadFile = func (cafile string ) ([]byte , error ) {
445462 assert .Equal (t , "/home/aceuser/ssl/mycustom.pem" , cafile )
446463 return []byte (dummyCert ), nil
@@ -494,6 +511,12 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
494511 return nil , nil
495512 }
496513
514+ osStat = func (file string ) (os.FileInfo , error ) {
515+ // Should not be called
516+ t .Errorf ("Should not check if file exist when only single bar URL" )
517+ return nil , nil
518+ }
519+
497520 ioutilReadFile = func (cafile string ) ([]byte , error ) {
498521 assert .Equal (t , "/home/aceuser/ssl/mycustom.pem" , cafile )
499522 return []byte (dummyCert ), nil
@@ -588,6 +611,10 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
588611 return nil , nil
589612 }
590613
614+ osStat = func (file string ) (os.FileInfo , error ) {
615+ return nil , os .ErrNotExist
616+ }
617+
591618 ioutilReadFile = func (cafile string ) ([]byte , error ) {
592619 assert .Equal (t , "/home/aceuser/ssl/cacert.pem" , cafile )
593620 return []byte (dummyCert ), nil
@@ -622,4 +649,79 @@ func TestGetConfigurationFromContentServer(t *testing.T) {
622649
623650 assert .Nil (t , errorReturned )
624651 })
652+
653+ t .Run ("Creates multiple files with different names when using multi bar support and the bar file names are all the same" , func (t * testing.T ) {
654+
655+ //https://alexdash-ibm-ace-dashboard-prod:3443/v1/directories/CustomerDatabaseV1?userid=fsdjfhksdjfhsd
656+ var barName = "CustomerDatabaseV1"
657+ var contentServerName = "alexdash-ibm-ace-dashboard-prod"
658+ var barAuth = "userid=fsdjfhksdjfhsd"
659+ var barUrlBase = "https://" + contentServerName + ":3443/v1/directories/" + barName
660+ var barUrlFull = barUrlBase + "?" + barAuth
661+
662+ var barUrl = barUrlFull + "," + barUrlFull + "," + barUrlFull
663+
664+ testReadCloser := ioutil .NopCloser (strings .NewReader ("test" ))
665+
666+ os .Unsetenv ("DEFAULT_CONTENT_SERVER" )
667+ os .Setenv ("ACE_CONTENT_SERVER_URL" , barUrl )
668+ os .Unsetenv ("ACE_CONTENT_SERVER_NAME" )
669+ os .Unsetenv ("ACE_CONTENT_SERVER_TOKEN" )
670+ os .Setenv ("CONTENT_SERVER_CERT" , "cacert" )
671+ os .Setenv ("CONTENT_SERVER_KEY" , "cakey" )
672+
673+ osMkdir = func (dirPath string , mode os.FileMode ) error {
674+ assert .Equal (t , barDirPath , dirPath )
675+ assert .Equal (t , os .ModePerm , mode )
676+ return nil
677+ }
678+
679+ createdFiles := map [string ]bool {}
680+ osCreateCall := 1
681+ osCreate = func (file string ) (* os.File , error ) {
682+ createdFiles [file ] = true
683+ if osCreateCall == 1 {
684+ assert .Equal (t , "/home/aceuser/initial-config/bars/" + barName + ".bar" , file )
685+ } else if osCreateCall == 2 {
686+ assert .Equal (t , "/home/aceuser/initial-config/bars/" + barName + "-1.bar" , file )
687+ } else if osCreateCall == 3 {
688+ assert .Equal (t , "/home/aceuser/initial-config/bars/" + barName + "-2.bar" , file )
689+ }
690+ osCreateCall = osCreateCall + 1
691+ return nil , nil
692+ }
693+
694+ osStat = func (file string ) (os.FileInfo , error ) {
695+ if createdFiles [file ] {
696+ return nil , os .ErrExist
697+ } else {
698+ return nil , os .ErrNotExist
699+ }
700+ }
701+
702+ ioutilReadFile = func (cafile string ) ([]byte , error ) {
703+ assert .Equal (t , "/home/aceuser/ssl/cacert.pem" , cafile )
704+ return []byte (dummyCert ), nil
705+ }
706+
707+ getBarCall := 1
708+ contentserverGetBAR = func (url string , serverName string , token string , contentServerCACert []byte , contentServerCert string , contentServerKey string , log logger.LoggerInterface ) (io.ReadCloser , error ) {
709+ assert .Equal (t , barUrlBase + "?archive=true" , url )
710+ assert .Equal (t , contentServerName , serverName )
711+ assert .Equal (t , barAuth , token )
712+ assert .Equal (t , []byte (dummyCert ), contentServerCACert )
713+ assert .Equal (t , "cacert" , contentServerCert )
714+ assert .Equal (t , "cakey" , contentServerKey )
715+ getBarCall = getBarCall + 1
716+ return testReadCloser , nil
717+ }
718+
719+ ioCopy = func (target io.Writer , source io.Reader ) (int64 , error ) {
720+ return 0 , nil
721+ }
722+
723+ errorReturned := getConfigurationFromContentServer ()
724+
725+ assert .Nil (t , errorReturned )
726+ })
625727}
0 commit comments