Skip to content

Commit 336002b

Browse files
authored
Change the bar filename if identical bar names given (#168)
1 parent 4c25083 commit 336002b

File tree

2 files changed

+127
-4
lines changed

2 files changed

+127
-4
lines changed

cmd/runaceserver/integrationserver.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939

4040
var osMkdir = os.Mkdir
4141
var osCreate = os.Create
42+
var osStat = os.Stat
4243
var ioutilReadFile = ioutil.ReadFile
4344
var ioCopy = io.Copy
4445
var contentserverGetBAR = contentserver.GetBAR
@@ -567,13 +568,33 @@ func getConfigurationFromContentServer() error {
567568
log.Errorf("Error parsing content server url : %v", err)
568569
return err
569570
}
570-
filename := "/home/aceuser/initial-config/bars/" + path.Base(u.Path) + ".bar"
571-
572-
// temporarily override the bar name with "barfile.bar" if we only have ONE bar file until mq connector is fixed to support any bar name
571+
572+
var filename string
573573
if len(urlArray) == 1 {
574+
// temporarily override the bar name with "barfile.bar" if we only have ONE bar file until mq connector is fixed to support any bar name
574575
filename = "/home/aceuser/initial-config/bars/barfile.bar"
576+
} else {
577+
// Multiple bar support. Need to loop to check that the file does not already exist
578+
// (case where multiple bars have the same name)
579+
isAvailable := false
580+
count := 0
581+
for !isAvailable {
582+
if count == 0 {
583+
filename = "/home/aceuser/initial-config/bars/" + path.Base(u.Path) + ".bar"
584+
} else {
585+
filename = "/home/aceuser/initial-config/bars/" + path.Base(u.Path) + "-" + fmt.Sprint(count) + ".bar"
586+
log.Printf("Previous path already in use. Testing filename: " + filename)
587+
}
588+
589+
if _, err := osStat(filename); os.IsNotExist(err) {
590+
log.Printf("No existing file on that path so continuing")
591+
isAvailable = true
592+
}
593+
count++
594+
}
575595
}
576-
log.Printf("Will saving bar as: " + filename)
596+
597+
log.Printf("Will save bar as: " + filename)
577598

578599
file, err := osCreate(filename)
579600
if err != nil {

cmd/runaceserver/integrationserver_internal_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)