-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Acceptance test for file based service bindings (#1374)
Acceptance tests for file based service bindings * pushes buildpack and CNB app, enables app feature "file-based-service-bindings" and binds a user-provided service * then checks service binding files via app /file endpoint
- Loading branch information
1 parent
2848e56
commit 99633fa
Showing
12 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: catnip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-chi/chi/v5" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
func FileHandler(res http.ResponseWriter, req *http.Request) { | ||
filename := chi.URLParam(req, "filename") | ||
decodedFilename, err := url.PathUnescape(filename) | ||
if err != nil { | ||
http.Error(res, fmt.Sprintf("Cannot unescape file name: %s", filename), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
_, err = os.Stat(decodedFilename) | ||
if err != nil { | ||
http.Error(res, http.StatusText(404) + ": " + decodedFilename, 404) | ||
return | ||
} | ||
|
||
content, err := os.ReadFile(decodedFilename) | ||
if err != nil { | ||
http.Error(res, http.StatusText(500) + ": " + err.Error(), 500) | ||
return | ||
} | ||
res.Write(append(content, '\n')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
file_based_service_bindings/file_based_service_bindings.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package file_based_service_bindings | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
. "github.com/cloudfoundry/cf-acceptance-tests/cats_suite_helpers" | ||
"github.com/cloudfoundry/cf-acceptance-tests/helpers/app_helpers" | ||
"github.com/cloudfoundry/cf-acceptance-tests/helpers/assets" | ||
"github.com/cloudfoundry/cf-acceptance-tests/helpers/random_name" | ||
"github.com/cloudfoundry/cf-acceptance-tests/services" | ||
"github.com/cloudfoundry/cf-test-helpers/v2/cf" | ||
"github.com/cloudfoundry/cf-test-helpers/v2/generator" | ||
"github.com/cloudfoundry/cf-test-helpers/v2/helpers" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/onsi/gomega/gexec" | ||
"strings" | ||
) | ||
|
||
var _ = FileBasedServiceBindingsDescribe("Enabling file based service binding for a buildpack app", BuildpackLifecycle, func() { | ||
callback(BuildpackLifecycle) | ||
}) | ||
|
||
var _ = FileBasedServiceBindingsDescribe("Enabling file based service binding for a CNB app", CNBLifecycle, func() { | ||
callback(CNBLifecycle) | ||
}) | ||
|
||
var callback = func(lifecycle string) { | ||
var appName, serviceName string | ||
|
||
getEncodedFilepath := func(serviceName string, fileName string) string { | ||
path := fmt.Sprintf("/etc/cf-service-bindings/%s/%s", serviceName, fileName) | ||
return strings.Replace(path, "/", "%2F", -1) | ||
} | ||
|
||
checkFileContent := func(fileName string, content string) { | ||
curlResponse := helpers.CurlApp(Config, appName, "/file/"+getEncodedFilepath(serviceName, fileName)) | ||
Expect(curlResponse).Should(ContainSubstring(content)) | ||
} | ||
|
||
getServiceInstanceGuid := func(serviceName string) string { | ||
serviceGuidCmd := cf.Cf("service", serviceName, "--guid") | ||
Eventually(serviceGuidCmd).Should(Exit(0)) | ||
return strings.TrimSpace(string(serviceGuidCmd.Out.Contents())) | ||
} | ||
|
||
getServiceBindingGuid := func(appGuid string, instanceGuid string) string { | ||
jsonResults := services_test.Response{} | ||
bindingCurl := cf.Cf("curl", fmt.Sprintf("/v3/service_credential_bindings?app_guids=%s&service_instance_guids=%s", appGuid, instanceGuid)).Wait() | ||
Expect(bindingCurl).To(Exit(0)) | ||
Expect(json.Unmarshal(bindingCurl.Out.Contents(), &jsonResults)).NotTo(HaveOccurred()) | ||
Expect(len(jsonResults.Resources)).To(BeNumerically(">", 0), "Expected to find at least one service binding.") | ||
return jsonResults.Resources[0].GUID | ||
} | ||
|
||
BeforeEach(func() { | ||
appName = random_name.CATSRandomName("APP") | ||
serviceName = generator.PrefixedRandomName("cats", "svin") // uppercase characters are not valid | ||
}) | ||
|
||
AfterEach(func() { | ||
app_helpers.AppReport(appName) | ||
Eventually(cf.Cf("unbind-service", appName, serviceName).Wait()).Should(Exit(0)) | ||
Eventually(cf.Cf("delete", appName, "-f")).Should(Exit(0)) | ||
Eventually(cf.Cf("delete-service", serviceName, "-f").Wait()).Should(Exit(0)) | ||
}) | ||
|
||
It("creates the required files in the app container", func() { | ||
tags := "list, of, tags" | ||
creds := `{"username": "admin", "password":"pa55woRD"}` | ||
Expect(cf.Cf("create-user-provided-service", serviceName, "-p", creds, "-t", tags).Wait()).To(Exit(0)) | ||
serviceGuid := getServiceInstanceGuid(serviceName) | ||
|
||
if lifecycle == BuildpackLifecycle { | ||
Expect(cf.Cf("create-app", appName).Wait()).To(Exit(0)) | ||
} | ||
if lifecycle == CNBLifecycle { | ||
Expect(cf.Cf("create-app", appName, "--app-type", "cnb", "--buildpack", Config.GetGoBuildpackName()).Wait()).To(Exit(0)) | ||
} | ||
appGuid := app_helpers.GetAppGuid(appName) | ||
|
||
appFeatureUrl := fmt.Sprintf("/v3/apps/%s/features/file-based-service-bindings", appGuid) | ||
Expect(cf.Cf("curl", appFeatureUrl, "-X", "PATCH", "-d", `{"enabled": true}`).Wait()).To(Exit(0)) | ||
|
||
Expect(cf.Cf("bind-service", appName, serviceName).Wait()).To(Exit(0)) | ||
|
||
if lifecycle == BuildpackLifecycle { | ||
Expect(cf.Cf(app_helpers.CatnipWithArgs( | ||
appName, | ||
"-m", DEFAULT_MEMORY_LIMIT)..., | ||
).Wait(Config.CfPushTimeoutDuration())).To(Exit(0)) | ||
} | ||
if lifecycle == CNBLifecycle { | ||
Expect(cf.Cf( | ||
"push", | ||
appName, | ||
"--lifecycle", "cnb", | ||
"--buildpack", Config.GetCNBGoBuildpackName(), | ||
"-m", DEFAULT_MEMORY_LIMIT, | ||
"-p", assets.NewAssets().CatnipSrc, | ||
).Wait(Config.CfPushTimeoutDuration())).To(Exit(0)) | ||
} | ||
|
||
checkFileContent("binding-guid", getServiceBindingGuid(appGuid, serviceGuid)) | ||
checkFileContent("instance-guid", serviceGuid) | ||
checkFileContent("instance-name", serviceName) | ||
checkFileContent("label", "user-provided") | ||
checkFileContent("name", serviceName) | ||
checkFileContent("password", "pa55woRD") | ||
checkFileContent("provider", "user-provided") | ||
checkFileContent("tags", `["list","of","tags"]`) | ||
checkFileContent("type", "user-provided") | ||
checkFileContent("username", "admin") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters