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