|
| 1 | +package file_based_service_bindings |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + . "github.com/cloudfoundry/cf-acceptance-tests/cats_suite_helpers" |
| 7 | + "github.com/cloudfoundry/cf-acceptance-tests/helpers/app_helpers" |
| 8 | + "github.com/cloudfoundry/cf-acceptance-tests/helpers/assets" |
| 9 | + "github.com/cloudfoundry/cf-acceptance-tests/helpers/random_name" |
| 10 | + "github.com/cloudfoundry/cf-acceptance-tests/services" |
| 11 | + "github.com/cloudfoundry/cf-test-helpers/v2/cf" |
| 12 | + "github.com/cloudfoundry/cf-test-helpers/v2/generator" |
| 13 | + "github.com/cloudfoundry/cf-test-helpers/v2/helpers" |
| 14 | + . "github.com/onsi/ginkgo/v2" |
| 15 | + . "github.com/onsi/gomega" |
| 16 | + . "github.com/onsi/gomega/gexec" |
| 17 | + "strings" |
| 18 | +) |
| 19 | + |
| 20 | +var _ = FileBasedServiceBindingsDescribe("Enabling file based service binding for a buildpack app", BuildpackLifecycle, func() { |
| 21 | + callback(BuildpackLifecycle) |
| 22 | +}) |
| 23 | + |
| 24 | +var _ = FileBasedServiceBindingsDescribe("Enabling file based service binding for a CNB app", CNBLifecycle, func() { |
| 25 | + callback(CNBLifecycle) |
| 26 | +}) |
| 27 | + |
| 28 | +var callback = func(lifecycle string) { |
| 29 | + var appName, serviceName string |
| 30 | + |
| 31 | + getEncodedFilepath := func(serviceName string, fileName string) string { |
| 32 | + path := fmt.Sprintf("/etc/cf-service-bindings/%s/%s", serviceName, fileName) |
| 33 | + return strings.Replace(path, "/", "%2F", -1) |
| 34 | + } |
| 35 | + |
| 36 | + checkFileContent := func(fileName string, content string) { |
| 37 | + curlResponse := helpers.CurlApp(Config, appName, "/file/"+getEncodedFilepath(serviceName, fileName)) |
| 38 | + Expect(curlResponse).Should(ContainSubstring(content)) |
| 39 | + } |
| 40 | + |
| 41 | + getServiceInstanceGuid := func(serviceName string) string { |
| 42 | + serviceGuidCmd := cf.Cf("service", serviceName, "--guid") |
| 43 | + Eventually(serviceGuidCmd).Should(Exit(0)) |
| 44 | + return strings.TrimSpace(string(serviceGuidCmd.Out.Contents())) |
| 45 | + } |
| 46 | + |
| 47 | + getServiceBindingGuid := func(appGuid string, instanceGuid string) string { |
| 48 | + jsonResults := services_test.Response{} |
| 49 | + bindingCurl := cf.Cf("curl", fmt.Sprintf("/v3/service_credential_bindings?app_guids=%s&service_instance_guids=%s", appGuid, instanceGuid)).Wait() |
| 50 | + Expect(bindingCurl).To(Exit(0)) |
| 51 | + Expect(json.Unmarshal(bindingCurl.Out.Contents(), &jsonResults)).NotTo(HaveOccurred()) |
| 52 | + Expect(len(jsonResults.Resources)).To(BeNumerically(">", 0), "Expected to find at least one service binding.") |
| 53 | + return jsonResults.Resources[0].GUID |
| 54 | + } |
| 55 | + |
| 56 | + BeforeEach(func() { |
| 57 | + appName = random_name.CATSRandomName("APP") |
| 58 | + serviceName = generator.PrefixedRandomName("cats", "svin") // uppercase characters are not valid |
| 59 | + }) |
| 60 | + |
| 61 | + AfterEach(func() { |
| 62 | + app_helpers.AppReport(appName) |
| 63 | + Eventually(cf.Cf("unbind-service", appName, serviceName).Wait()).Should(Exit(0)) |
| 64 | + Eventually(cf.Cf("delete", appName, "-f")).Should(Exit(0)) |
| 65 | + Eventually(cf.Cf("delete-service", serviceName, "-f").Wait()).Should(Exit(0)) |
| 66 | + }) |
| 67 | + |
| 68 | + It("creates the required files in the app container", func() { |
| 69 | + tags := "list, of, tags" |
| 70 | + creds := `{"username": "admin", "password":"pa55woRD"}` |
| 71 | + Expect(cf.Cf("create-user-provided-service", serviceName, "-p", creds, "-t", tags).Wait()).To(Exit(0)) |
| 72 | + serviceGuid := getServiceInstanceGuid(serviceName) |
| 73 | + |
| 74 | + if lifecycle == BuildpackLifecycle { |
| 75 | + Expect(cf.Cf("create-app", appName).Wait()).To(Exit(0)) |
| 76 | + } |
| 77 | + if lifecycle == CNBLifecycle { |
| 78 | + Expect(cf.Cf("create-app", appName, "--app-type", "cnb", "--buildpack", Config.GetGoBuildpackName()).Wait()).To(Exit(0)) |
| 79 | + } |
| 80 | + appGuid := app_helpers.GetAppGuid(appName) |
| 81 | + |
| 82 | + appFeatureUrl := fmt.Sprintf("/v3/apps/%s/features/file-based-service-bindings", appGuid) |
| 83 | + Expect(cf.Cf("curl", appFeatureUrl, "-X", "PATCH", "-d", `{"enabled": true}`).Wait()).To(Exit(0)) |
| 84 | + |
| 85 | + Expect(cf.Cf("bind-service", appName, serviceName).Wait()).To(Exit(0)) |
| 86 | + |
| 87 | + if lifecycle == BuildpackLifecycle { |
| 88 | + Expect(cf.Cf(app_helpers.CatnipWithArgs( |
| 89 | + appName, |
| 90 | + "-m", DEFAULT_MEMORY_LIMIT)..., |
| 91 | + ).Wait(Config.CfPushTimeoutDuration())).To(Exit(0)) |
| 92 | + } |
| 93 | + if lifecycle == CNBLifecycle { |
| 94 | + Expect(cf.Cf( |
| 95 | + "push", |
| 96 | + appName, |
| 97 | + "--lifecycle", "cnb", |
| 98 | + "--buildpack", Config.GetCNBGoBuildpackName(), |
| 99 | + "-m", DEFAULT_MEMORY_LIMIT, |
| 100 | + "-p", assets.NewAssets().CatnipSrc, |
| 101 | + ).Wait(Config.CfPushTimeoutDuration())).To(Exit(0)) |
| 102 | + } |
| 103 | + |
| 104 | + checkFileContent("binding-guid", getServiceBindingGuid(appGuid, serviceGuid)) |
| 105 | + checkFileContent("instance-guid", serviceGuid) |
| 106 | + checkFileContent("instance-name", serviceName) |
| 107 | + checkFileContent("label", "user-provided") |
| 108 | + checkFileContent("name", serviceName) |
| 109 | + checkFileContent("password", "pa55woRD") |
| 110 | + checkFileContent("provider", "user-provided") |
| 111 | + checkFileContent("tags", `["list","of","tags"]`) |
| 112 | + checkFileContent("type", "user-provided") |
| 113 | + checkFileContent("username", "admin") |
| 114 | + }) |
| 115 | +} |
0 commit comments