Skip to content

Commit

Permalink
fix: ParseVcapServices stops searching after the first service offering
Browse files Browse the repository at this point in the history
The for loop on L103 was written to return after the checking the first entry in vcapMap, regardless of whether findMySQLTag found a suitable service or returned an error. If services from multiple service offerings are bound to the application and the service tagged with "mysql" is not in the first offering, the function will not find it. The new test case fails when run against the unmodified code.

To fix this, flatten the map[string][]VcapService into a []VCapService, and pass the flat slice to findMySQLTag.
  • Loading branch information
jameshochadel committed Oct 8, 2024
1 parent f95bde1 commit 3c32520
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
15 changes: 9 additions & 6 deletions dbservice/vcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ func ParseVcapServices(vcapServicesData string) (VcapService, error) {
return VcapService{}, fmt.Errorf("error unmarshalling VCAP_SERVICES: %s", err)
}

for _, vcapArray := range vcapMap {
vcapService, err := findMySQLTag(vcapArray, "mysql")
if err != nil {
return VcapService{}, fmt.Errorf("error finding MySQL tag: %s", err)
flatVCAPServices := []VcapService{}
for _, v := range vcapMap {
for _, i := range v {

Check failure on line 105 in dbservice/vcap.go

View workflow job for this annotation

GitHub Actions / Go test

should replace loop with flatVCAPServices = append(flatVCAPServices, v...) (S1011)
flatVCAPServices = append(flatVCAPServices, i)
}
return vcapService, nil
}

return VcapService{}, fmt.Errorf("error parsing VCAP_SERVICES")
service, err := findMySQLTag(flatVCAPServices, "mysql")
if err != nil {
return VcapService{}, fmt.Errorf("error finding MySQL tag: %s", err)
}
return service, nil
}

// whether a given string array arr contains string key
Expand Down
73 changes: 73 additions & 0 deletions dbservice/vcap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,79 @@ var _ = Describe("VCAP", func() {
`)
Expect(err).To(MatchError("error finding MySQL tag: the variable VCAP_SERVICES must have one VCAP service with a tag of 'mysql'. There are currently 0 VCAP services with the tag 'mysql'"))
})

It("succeeds when VCAP_SERVICES has multiple lists but only one with a MySQL tag", func() {
_, err := ParseVcapServices(`{
"google-cloudsql-mysql": [
{
"binding_name": "testbinding",
"instance_name": "testinstance",
"name": "kf-binding-tt2-mystorage",
"label": "google-storage",
"tags": [
"gcp",
"cloudsql"
],
"plan": "nearline",
"credentials": {
"CaCert": "-truncated-",
"ClientCert": "-truncated-",
"ClientKey": "-truncated-",
"Email": "[email protected]",
"Name": "pcf-binding-testbind",
"Password": "PASSWORD",
"PrivateKeyData": "PRIVATEKEY",
"ProjectId": "test-gsb",
"Sha1Fingerprint": "aa3bade266136f733642ebdb4992b89eb05f83c4",
"UniqueId": "108868434450972082663",
"UriPrefix": "",
"Username": "newuseraccount",
"database_name": "service_broker",
"host": "127.0.0.1",
"instance_name": "pcf-sb-1-1561406852899716453",
"last_master_operation_id": "",
"region": "",
"uri": "mysql://newuseraccount:[email protected]/service_broker?ssl_mode=required"
}
}
],
"user-provided": [
{
"binding_name": "testbinding",
"instance_name": "testinstance",
"name": "kf-binding-tt2-mystorage",
"label": "google-storage",
"tags": [
"gcp",
"cloudsql",
"mysql"
],
"plan": "nearline",
"credentials": {
"CaCert": "-truncated-",
"ClientCert": "-truncated-",
"ClientKey": "-truncated-",
"Email": "[email protected]",
"Name": "pcf-binding-testbind",
"Password": "PASSWORD",
"PrivateKeyData": "PRIVATEKEY",
"ProjectId": "test-gsb",
"Sha1Fingerprint": "aa3bade266136f733642ebdb4992b89eb05f83c4",
"UniqueId": "108868434450972082663",
"UriPrefix": "",
"Username": "newuseraccount",
"database_name": "service_broker",
"host": "127.0.0.1",
"instance_name": "pcf-sb-1-1561406852899716453",
"last_master_operation_id": "",
"region": "",
"uri": "mysql://newuseraccount:[email protected]/service_broker?ssl_mode=required"
}
}
]
}`)
Expect(err).NotTo(HaveOccurred())
})
})

Describe("setting database credentials", func() {
Expand Down

0 comments on commit 3c32520

Please sign in to comment.