github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/global/v3_share_service_command_test.go (about) 1 package global 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/integration/helpers" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 . "github.com/onsi/gomega/gexec" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("v3-share-service command", func() { 15 var ( 16 sourceOrgName string 17 sourceSpaceName string 18 sharedToOrgName string 19 sharedToSpaceName string 20 serviceInstance string 21 ) 22 23 BeforeEach(func() { 24 sourceOrgName = helpers.NewOrgName() 25 sourceSpaceName = helpers.NewSpaceName() 26 sharedToOrgName = helpers.NewOrgName() 27 sharedToSpaceName = helpers.NewSpaceName() 28 serviceInstance = helpers.PrefixedRandomName("svc-inst") 29 30 helpers.LoginCF() 31 session := helpers.CF("enable-feature-flag", "service_instance_sharing") 32 Eventually(session).Should(Exit(0)) 33 }) 34 35 Describe("help", func() { 36 Context("when --help flag is set", func() { 37 It("Displays command usage to output", func() { 38 session := helpers.CF("v3-share-service", "--help") 39 Eventually(session).Should(Say("NAME:")) 40 Eventually(session).Should(Say("v3-share-service - Share a service instance with another space")) 41 Eventually(session).Should(Say("USAGE:")) 42 Eventually(session).Should(Say("cf v3-share-service SERVICE_INSTANCE -s OTHER_SPACE \\[-o OTHER_ORG\\]")) 43 Eventually(session).Should(Say("OPTIONS:")) 44 Eventually(session).Should(Say("-o\\s+Org of the other space \\(Default: targeted org\\)")) 45 Eventually(session).Should(Say("-s\\s+Space to share the service instance into")) 46 Eventually(session).Should(Say("SEE ALSO:")) 47 Eventually(session).Should(Say("bind-service, service, services, v3-unshare-service")) 48 Eventually(session).Should(Exit(0)) 49 }) 50 }) 51 }) 52 53 Context("when the service instance name is not provided", func() { 54 It("tells the user that the service instance name is required, prints help text, and exits 1", func() { 55 session := helpers.CF("v3-share-service", "-s", sharedToSpaceName) 56 57 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided")) 58 Eventually(session).Should(Say("NAME:")) 59 Eventually(session).Should(Exit(1)) 60 }) 61 }) 62 63 Context("when the space name is not provided", func() { 64 It("tells the user that the space name is required, prints help text, and exits 1", func() { 65 session := helpers.CF("v3-share-service") 66 67 Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `-s' was not specified")) 68 Eventually(session).Should(Say("NAME:")) 69 Eventually(session).Should(Exit(1)) 70 }) 71 }) 72 73 It("displays the experimental warning", func() { 74 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName) 75 Eventually(session).Should(Say("This command is in EXPERIMENTAL stage and may change without notice")) 76 Eventually(session).Should(Exit()) 77 }) 78 79 Context("when the environment is not setup correctly", func() { 80 It("fails with the appropriate errors", func() { 81 helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "v3-share-service", serviceInstance, "-s", sharedToSpaceName) 82 }) 83 84 Context("when the v3 api does not exist", func() { 85 var server *Server 86 87 BeforeEach(func() { 88 server = helpers.StartAndTargetServerWithoutV3API() 89 }) 90 91 AfterEach(func() { 92 server.Close() 93 }) 94 95 It("fails with error message that the minimum version is not met", func() { 96 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName) 97 Eventually(session).Should(Say("FAILED")) 98 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.36\\.0 or higher\\.")) 99 Eventually(session).Should(Exit(1)) 100 }) 101 }) 102 103 Context("when the v3 api version is lower than the minimum version", func() { 104 var server *Server 105 106 BeforeEach(func() { 107 server = helpers.StartAndTargetServerWithV3Version("3.0.0") 108 }) 109 110 AfterEach(func() { 111 server.Close() 112 }) 113 114 It("fails with error message that the minimum version is not met", func() { 115 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName) 116 Eventually(session).Should(Say("FAILED")) 117 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.36\\.0 or higher\\.")) 118 Eventually(session).Should(Exit(1)) 119 }) 120 }) 121 }) 122 123 Context("when the environment is set up correctly", func() { 124 var ( 125 domain string 126 service string 127 servicePlan string 128 ) 129 130 BeforeEach(func() { 131 service = helpers.PrefixedRandomName("SERVICE") 132 servicePlan = helpers.PrefixedRandomName("SERVICE-PLAN") 133 134 helpers.CreateOrgAndSpace(sharedToOrgName, sharedToSpaceName) 135 setupCF(sourceOrgName, sourceSpaceName) 136 137 domain = defaultSharedDomain() 138 }) 139 140 AfterEach(func() { 141 helpers.QuickDeleteOrg(sharedToOrgName) 142 helpers.QuickDeleteOrg(sourceOrgName) 143 }) 144 145 Context("when there is a managed service instance in my current targeted space", func() { 146 var broker helpers.ServiceBroker 147 148 BeforeEach(func() { 149 broker = helpers.NewServiceBroker(helpers.NewServiceBrokerName(), helpers.NewAssets().ServiceBroker, domain, service, servicePlan) 150 broker.Push() 151 broker.Configure(true) 152 broker.Create() 153 154 Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0)) 155 Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0)) 156 }) 157 158 AfterEach(func() { 159 broker.Destroy() 160 }) 161 162 Context("when I want to share my service instance to a space in another org", func() { 163 AfterEach(func() { 164 Eventually(helpers.CF("v3-unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")).Should(Exit(0)) 165 }) 166 167 It("shares the service instance from my targeted space with the share-to org/space", func() { 168 username, _ := helpers.GetCredentials() 169 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName) 170 Eventually(session.Out).Should(Say("Sharing service instance %s into org %s / space %s as %s\\.\\.\\.", serviceInstance, sharedToOrgName, sharedToSpaceName, username)) 171 Eventually(session.Out).Should(Say("OK")) 172 Eventually(session).Should(Exit(0)) 173 }) 174 175 Context("when the service instance is already shared with that space", func() { 176 BeforeEach(func() { 177 Eventually(helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)).Should(Exit(0)) 178 }) 179 180 It("displays a warning and exits 0", func() { 181 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName) 182 Consistently(session.Out).ShouldNot(Say("FAILED")) 183 Eventually(session.Out).Should(Say("OK")) 184 185 Eventually(session.Err).Should(Say("Service instance %s is already shared with that space\\.", serviceInstance)) 186 Eventually(session).Should(Exit(0)) 187 }) 188 }) 189 }) 190 191 Context("when I want to share my service instance into another space in my targeted org", func() { 192 BeforeEach(func() { 193 helpers.CreateSpace(sharedToSpaceName) 194 }) 195 196 AfterEach(func() { 197 Eventually(helpers.CF("v3-unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")).Should(Exit(0)) 198 }) 199 200 It("shares the service instance from my targeted space with the share-to space", func() { 201 username, _ := helpers.GetCredentials() 202 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName) 203 Eventually(session.Out).Should(Say("Sharing service instance %s into org %s / space %s as %s\\.\\.\\.", serviceInstance, sourceOrgName, sharedToSpaceName, username)) 204 Eventually(session.Out).Should(Say("OK")) 205 Eventually(session).Should(Exit(0)) 206 }) 207 208 Context("when the service instance is already shared with that space", func() { 209 BeforeEach(func() { 210 Eventually(helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName)).Should(Exit(0)) 211 }) 212 213 It("displays a warning and exits 0", func() { 214 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName) 215 Consistently(session.Out).ShouldNot(Say("FAILED")) 216 Eventually(session.Out).Should(Say("OK")) 217 218 Eventually(session.Err).Should(Say("Service instance %s is already shared with that space\\.", serviceInstance)) 219 Eventually(session).Should(Exit(0)) 220 }) 221 }) 222 }) 223 224 Context("when the org I want to share into does not exist", func() { 225 It("fails with an org not found error", func() { 226 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", "missing-org") 227 Eventually(session).Should(Say("FAILED")) 228 Eventually(session.Err).Should(Say("Organization 'missing-org' not found")) 229 Eventually(session).Should(Exit(1)) 230 }) 231 }) 232 233 Context("when the space I want to share into does not exist", func() { 234 It("fails with a space not found error", func() { 235 session := helpers.CF("v3-share-service", serviceInstance, "-s", "missing-space") 236 Eventually(session).Should(Say("FAILED")) 237 Eventually(session.Err).Should(Say("Space 'missing-space' not found")) 238 Eventually(session).Should(Exit(1)) 239 }) 240 }) 241 242 Context("when I am a SpaceAuditor in the space I want to share into", func() { 243 var sharedToSpaceGUID string 244 BeforeEach(func() { 245 user := helpers.NewUsername() 246 password := helpers.NewPassword() 247 Eventually(helpers.CF("create-user", user, password)).Should(Exit(0)) 248 Eventually(helpers.CF("set-space-role", user, sourceOrgName, sourceSpaceName, "SpaceDeveloper")).Should(Exit(0)) 249 Eventually(helpers.CF("set-space-role", user, sharedToOrgName, sharedToSpaceName, "SpaceAuditor")).Should(Exit(0)) 250 Eventually(helpers.CF("auth", user, password)).Should(Exit(0)) 251 helpers.TargetOrgAndSpace(sharedToOrgName, sharedToSpaceName) 252 sharedToSpaceGUID = helpers.GetSpaceGUID(sharedToSpaceName) 253 helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName) 254 }) 255 256 AfterEach(func() { 257 setupCF(sourceOrgName, sourceSpaceName) 258 }) 259 260 It("fails with an unauthorized error", func() { 261 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName) 262 Eventually(session).Should(Say("FAILED")) 263 Eventually(session.Err).Should(Say("Unable to share service instance %s with spaces \\['%s'\\].", serviceInstance, sharedToSpaceGUID)) 264 Eventually(session.Err).Should(Say("Write permission is required in order to share a service instance with a space")) 265 Eventually(session).Should(Exit(1)) 266 }) 267 }) 268 269 Context("when my targeted space is the same as my share-to space", func() { 270 It("fails with a cannot share to self error", func() { 271 session := helpers.CF("v3-share-service", serviceInstance, "-s", sourceSpaceName) 272 Eventually(session).Should(Say("FAILED")) 273 Eventually(session.Err).Should(Say("Service instances cannot be shared into the space where they were created")) 274 Eventually(session).Should(Exit(1)) 275 }) 276 }) 277 278 Context("when a service instance with the same name exists in the shared-to space", func() { 279 BeforeEach(func() { 280 helpers.CreateSpace(sharedToSpaceName) 281 helpers.TargetOrgAndSpace(sourceOrgName, sharedToSpaceName) 282 Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0)) 283 helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName) 284 }) 285 286 It("fails with a name clash error", func() { 287 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName) 288 Eventually(session).Should(Say("FAILED")) 289 Eventually(session.Err).Should(Say(fmt.Sprintf("A service instance called %s already exists in %s", serviceInstance, sharedToSpaceName))) 290 Eventually(session).Should(Exit(1)) 291 }) 292 }) 293 294 Context("when the service instance is NOT shareable", func() { 295 Context("due to global settings", func() { 296 BeforeEach(func() { 297 helpers.DisableFeatureFlag("service_instance_sharing") 298 }) 299 300 AfterEach(func() { 301 helpers.EnableFeatureFlag("service_instance_sharing") 302 }) 303 304 It("should display that the service instance feature flag is disabled and exit 1", func() { 305 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName) 306 Eventually(session.Err).Should(Say(`The "service_instance_sharing" feature flag is disabled for this Cloud Foundry platform.`)) 307 Eventually(session).Should(Exit(1)) 308 }) 309 }) 310 311 Context("due to service broker settings", func() { 312 BeforeEach(func() { 313 broker.Configure(false) 314 broker.Update() 315 }) 316 317 It("should display that service instance sharing is disabled for this service and exit 1", func() { 318 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName) 319 Eventually(session.Err).Should(Say("Service instance sharing is disabled for this service.")) 320 Eventually(session).Should(Exit(1)) 321 }) 322 }) 323 324 Context("due to global settings AND service broker settings", func() { 325 BeforeEach(func() { 326 helpers.DisableFeatureFlag("service_instance_sharing") 327 broker.Configure(false) 328 broker.Update() 329 }) 330 331 AfterEach(func() { 332 helpers.EnableFeatureFlag("service_instance_sharing") 333 }) 334 335 It("should display that service instance sharing is disabled for this service and exit 1", func() { 336 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName) 337 Eventually(session.Err).Should(Say(`The "service_instance_sharing" feature flag is disabled for this Cloud Foundry platform. Also, service instance sharing is disabled for this service.`)) 338 Eventually(session).Should(Exit(1)) 339 }) 340 }) 341 }) 342 }) 343 344 Context("when the service instance does not exist", func() { 345 It("fails with a service instance not found error", func() { 346 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName) 347 Eventually(session).Should(Say("FAILED")) 348 Eventually(session.Err).Should(Say("Specified instance not found or not a managed service instance. Sharing is not supported for user provided services.")) 349 Eventually(session).Should(Exit(1)) 350 }) 351 }) 352 353 Context("when I try to share a user-provided-service", func() { 354 BeforeEach(func() { 355 helpers.CF("create-user-provided-service", serviceInstance, "-p", `{"username":"admin","password":"pa55woRD"}`) 356 }) 357 358 It("fails with only managed services can be shared", func() { 359 session := helpers.CF("v3-share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName) 360 Eventually(session).Should(Say("FAILED")) 361 Eventually(session.Err).Should(Say("User-provided services cannot be shared")) 362 Eventually(session).Should(Exit(1)) 363 }) 364 }) 365 }) 366 })