github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/isolated/bind_service_command_test.go (about) 1 package isolated 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 8 "code.cloudfoundry.org/cli/integration/helpers" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 . "github.com/onsi/gomega/gexec" 13 ) 14 15 var _ = Describe("bind-service command", func() { 16 Describe("help", func() { 17 Context("when --help flag is set", func() { 18 It("Displays command usage to output", func() { 19 session := helpers.CF("bind-service", "--help") 20 Eventually(session).Should(Say("NAME:")) 21 Eventually(session).Should(Say("bind-service - Bind a service instance to an app")) 22 23 Eventually(session).Should(Say("USAGE:")) 24 Eventually(session).Should(Say("cf bind-service APP_NAME SERVICE_INSTANCE \\[-c PARAMETERS_AS_JSON\\]")) 25 Eventually(session).Should(Say("Optionally provide service-specific configuration parameters in a valid JSON object in-line:")) 26 Eventually(session).Should(Say("cf bind-service APP_NAME SERVICE_INSTANCE -c '{\"name\":\"value\",\"name\":\"value\"}'")) 27 Eventually(session).Should(Say("Optionally provide a file containing service-specific configuration parameters in a valid JSON object.")) 28 Eventually(session).Should(Say("The path to the parameters file can be an absolute or relative path to a file.")) 29 Eventually(session).Should(Say("cf bind-service APP_NAME SERVICE_INSTANCE -c PATH_TO_FILE")) 30 Eventually(session).Should(Say("Example of valid JSON object:")) 31 Eventually(session).Should(Say("{")) 32 Eventually(session).Should(Say("\"permissions\": \"read-only\"")) 33 Eventually(session).Should(Say("}")) 34 Eventually(session).Should(Say("EXAMPLES:")) 35 Eventually(session).Should(Say("Linux/Mac:")) 36 Eventually(session).Should(Say("cf bind-service myapp mydb -c '{\"permissions\":\"read-only\"}'")) 37 Eventually(session).Should(Say("Windows Command Line:")) 38 Eventually(session).Should(Say("cf bind-service myapp mydb -c \"{\\\\\"permissions\\\\\":\\\\\"read-only\\\\\"}\"")) 39 Eventually(session).Should(Say("Windows PowerShell:")) 40 Eventually(session).Should(Say("cf bind-service myapp mydb -c '{\\\\\"permissions\\\\\":\\\\\"read-only\\\\\"}'")) 41 Eventually(session).Should(Say("cf bind-service myapp mydb -c ~/workspace/tmp/instance_config.json")) 42 Eventually(session).Should(Say("ALIAS:")) 43 Eventually(session).Should(Say("bs")) 44 Eventually(session).Should(Say("OPTIONS:")) 45 Eventually(session).Should(Say("-c Valid JSON object containing service-specific configuration parameters, provided either in-line or in a file. For a list of supported configuration parameters, see documentation for the particular service offering.")) 46 Eventually(session).Should(Say("SEE ALSO:")) 47 Eventually(session).Should(Say("services")) 48 Eventually(session).Should(Exit(0)) 49 }) 50 }) 51 }) 52 53 var ( 54 serviceInstance string 55 appName string 56 ) 57 58 BeforeEach(func() { 59 serviceInstance = helpers.PrefixedRandomName("si") 60 appName = helpers.PrefixedRandomName("app") 61 }) 62 63 Context("when the environment is not setup correctly", func() { 64 It("fails with the appropriate errors", func() { 65 helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "bind-service", "app-name", "service-name") 66 }) 67 }) 68 69 Context("when the environment is setup correctly", func() { 70 var ( 71 org string 72 space string 73 service string 74 servicePlan string 75 domain string 76 username string 77 ) 78 79 BeforeEach(func() { 80 org = helpers.NewOrgName() 81 space = helpers.NewSpaceName() 82 service = helpers.PrefixedRandomName("SERVICE") 83 servicePlan = helpers.PrefixedRandomName("SERVICE-PLAN") 84 username, _ = helpers.GetCredentials() 85 86 setupCF(org, space) 87 domain = defaultSharedDomain() 88 }) 89 90 AfterEach(func() { 91 helpers.QuickDeleteOrg(org) 92 }) 93 94 Context("when the app does not exist", func() { 95 It("displays FAILED and app not found", func() { 96 session := helpers.CF("bind-service", "does-not-exist", serviceInstance) 97 Eventually(session).Should(Say("FAILED")) 98 Eventually(session.Err).Should(Say("App %s not found", "does-not-exist")) 99 Eventually(session).Should(Exit(1)) 100 }) 101 }) 102 103 Context("when the app exists", func() { 104 BeforeEach(func() { 105 helpers.WithHelloWorldApp(func(appDir string) { 106 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 107 }) 108 }) 109 110 Context("when the service does not exist", func() { 111 It("displays FAILED and service not found", func() { 112 session := helpers.CF("bind-service", appName, "does-not-exist") 113 Eventually(session).Should(Say("FAILED")) 114 Eventually(session.Err).Should(Say("Service instance %s not found", "does-not-exist")) 115 Eventually(session).Should(Exit(1)) 116 }) 117 }) 118 119 Context("when the service exists", func() { 120 BeforeEach(func() { 121 Eventually(helpers.CF("create-user-provided-service", serviceInstance, "-p", "{}")).Should(Exit(0)) 122 helpers.WithHelloWorldApp(func(appDir string) { 123 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 124 }) 125 }) 126 127 AfterEach(func() { 128 Eventually(helpers.CF("unbind-service", appName, serviceInstance)).Should(Exit(0)) 129 Eventually(helpers.CF("delete-service", serviceInstance, "-f")).Should(Exit(0)) 130 }) 131 132 It("binds the service to the app, displays OK and TIP", func() { 133 session := helpers.CF("bind-service", appName, serviceInstance) 134 Eventually(session).Should(Say("Binding service %s to app %s in org %s / space %s as %s...", serviceInstance, appName, org, space, username)) 135 136 Eventually(session).Should(Say("OK")) 137 Eventually(session).Should(Say("TIP: Use 'cf restage %s' to ensure your env variable changes take effect", appName)) 138 Eventually(session).Should(Exit(0)) 139 }) 140 141 Context("when the service is already bound to an app", func() { 142 BeforeEach(func() { 143 Eventually(helpers.CF("bind-service", appName, serviceInstance)).Should(Exit(0)) 144 }) 145 146 It("displays OK and that the app is already bound to the service", func() { 147 session := helpers.CF("bind-service", appName, serviceInstance) 148 149 Eventually(session).Should(Say("Binding service %s to app %s in org %s / space %s as %s...", serviceInstance, appName, org, space, username)) 150 Eventually(session).Should(Say("App %s is already bound to %s.", appName, serviceInstance)) 151 Eventually(session).Should(Say("OK")) 152 153 Eventually(session).Should(Exit(0)) 154 }) 155 }) 156 157 Context("when configuration parameters are provided in a file", func() { 158 var configurationFile *os.File 159 160 Context("when the file-path does not exist", func() { 161 It("displays FAILED and the invalid configuration error", func() { 162 session := helpers.CF("bind-service", appName, serviceInstance, "-c", "i-do-not-exist") 163 Eventually(session.Err).Should(Say("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object.")) 164 165 Eventually(session).Should(Exit(1)) 166 }) 167 }) 168 169 Context("when the file contians invalid json", func() { 170 BeforeEach(func() { 171 var err error 172 content := []byte("{i-am-very-bad-json") 173 configurationFile, err = ioutil.TempFile("", "CF_CLI") 174 Expect(err).ToNot(HaveOccurred()) 175 176 _, err = configurationFile.Write(content) 177 Expect(err).ToNot(HaveOccurred()) 178 179 err = configurationFile.Close() 180 Expect(err).ToNot(HaveOccurred()) 181 }) 182 183 AfterEach(func() { 184 os.Remove(configurationFile.Name()) 185 }) 186 187 It("displays FAILED and the invalid configuration error", func() { 188 session := helpers.CF("bind-service", appName, serviceInstance, "-c", configurationFile.Name()) 189 Eventually(session.Err).Should(Say("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object.")) 190 191 Eventually(session).Should(Exit(1)) 192 }) 193 }) 194 195 Context("when the file-path is relative", func() { 196 BeforeEach(func() { 197 var err error 198 content := []byte("{\"i-am-good-json\":\"good-boy\"}") 199 configurationFile, err = ioutil.TempFile("", "CF_CLI") 200 Expect(err).ToNot(HaveOccurred()) 201 202 _, err = configurationFile.Write(content) 203 Expect(err).ToNot(HaveOccurred()) 204 205 err = configurationFile.Close() 206 Expect(err).ToNot(HaveOccurred()) 207 }) 208 209 AfterEach(func() { 210 os.Remove(configurationFile.Name()) 211 }) 212 213 It("binds the service to the app, displays OK and TIP", func() { 214 session := helpers.CF("bind-service", appName, serviceInstance, "-c", configurationFile.Name()) 215 Eventually(session).Should(Say("Binding service %s to app %s in org %s / space %s as %s...", serviceInstance, appName, org, space, username)) 216 217 Eventually(session).Should(Say("OK")) 218 Eventually(session).Should(Say("TIP: Use 'cf restage %s' to ensure your env variable changes take effect", appName)) 219 Eventually(session).Should(Exit(0)) 220 }) 221 }) 222 223 Context("when the file-path is absolute", func() { 224 BeforeEach(func() { 225 var err error 226 content := []byte("{\"i-am-good-json\":\"good-boy\"}") 227 configurationFile, err = ioutil.TempFile("", "CF_CLI") 228 Expect(err).ToNot(HaveOccurred()) 229 230 _, err = configurationFile.Write(content) 231 Expect(err).ToNot(HaveOccurred()) 232 233 err = configurationFile.Close() 234 Expect(err).ToNot(HaveOccurred()) 235 }) 236 237 It("binds the service to the app, displays OK and TIP", func() { 238 absolutePath, err := filepath.Abs(configurationFile.Name()) 239 Expect(err).ToNot(HaveOccurred()) 240 session := helpers.CF("bind-service", appName, serviceInstance, "-c", absolutePath) 241 Eventually(session).Should(Say("Binding service %s to app %s in org %s / space %s as %s...", serviceInstance, appName, org, space, username)) 242 243 Eventually(session).Should(Say("OK")) 244 Eventually(session).Should(Say("TIP: Use 'cf restage %s' to ensure your env variable changes take effect", appName)) 245 Eventually(session).Should(Exit(0)) 246 }) 247 }) 248 }) 249 250 Context("when configuration paramters are provided as in-line JSON", func() { 251 Context("when the JSON is invalid", func() { 252 It("displays FAILED and the invalid configuration error", func() { 253 session := helpers.CF("bind-service", appName, serviceInstance, "-c", "i-am-invalid-json") 254 Eventually(session.Err).Should(Say("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object.")) 255 256 Eventually(session).Should(Exit(1)) 257 }) 258 }) 259 260 Context("when the JSON is valid", func() { 261 It("binds the service to the app, displays OK and TIP", func() { 262 session := helpers.CF("bind-service", appName, serviceInstance, "-c", "{\"i-am-valid-json\":\"dope dude\"}") 263 Eventually(session).Should(Say("Binding service %s to app %s in org %s / space %s as %s...", serviceInstance, appName, org, space, username)) 264 265 Eventually(session).Should(Say("OK")) 266 Eventually(session).Should(Say("TIP: Use 'cf restage %s' to ensure your env variable changes take effect", appName)) 267 Eventually(session).Should(Exit(0)) 268 }) 269 }) 270 }) 271 }) 272 273 Context("when the service is provided by a broker", func() { 274 var broker helpers.ServiceBroker 275 276 BeforeEach(func() { 277 broker = helpers.NewServiceBroker(helpers.NewServiceBrokerName(), helpers.NewAssets().ServiceBroker, domain, service, servicePlan) 278 broker.Push() 279 broker.Configure(true) 280 broker.Create() 281 282 Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0)) 283 284 Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0)) 285 }) 286 287 AfterEach(func() { 288 broker.Destroy() 289 }) 290 291 It("binds the service to the app, displays OK and TIP", func() { 292 session := helpers.CF("bind-service", appName, serviceInstance, "-c", `{"wheres":"waldo"}`) 293 Eventually(session).Should(Say("Binding service %s to app %s in org %s / space %s as %s...", serviceInstance, appName, org, space, username)) 294 295 Eventually(session).Should(Say("OK")) 296 Eventually(session).Should(Say("TIP: Use 'cf restage %s' to ensure your env variable changes take effect", appName)) 297 Eventually(session).Should(Exit(0)) 298 299 session = helpers.CF("service", serviceInstance) 300 Eventually(session).Should(Say(appName)) 301 Eventually(session).Should(Exit(0)) 302 }) 303 }) 304 }) 305 }) 306 })