github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/share_service_command_test.go (about) 1 package isolated 2 3 import ( 4 "code.cloudfoundry.org/cli/integration/helpers" 5 "code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 . "github.com/onsi/gomega/gexec" 11 ) 12 13 var _ = Describe("share-service command", func() { 14 var ( 15 shareServiceCommand = "share-service" 16 serviceInstanceName = "fake-service-instance-name" 17 shareToSpaceName = "fake-space-name" 18 shareToOrgName = "fake-org-name" 19 ) 20 21 Describe("help", func() { 22 23 matchHelpMessage := SatisfyAll( 24 Say("NAME:"), 25 Say("share-service - Share a service instance with another space"), 26 Say("USAGE:"), 27 Say(`cf share-service SERVICE_INSTANCE -s OTHER_SPACE \[-o OTHER_ORG\]`), 28 Say("OPTIONS:"), 29 Say(`-s\s+The space to share the service instance into`), 30 Say(`-o\s+Org of the other space \(Default: targeted org\)`), 31 Say("SEE ALSO:"), 32 Say("bind-service, service, services, unshare-service"), 33 ) 34 35 When("the -h flag is specified", func() { 36 It("succeeds and prints help", func() { 37 session := helpers.CF(shareServiceCommand, "-h") 38 Eventually(session).Should(Exit(0)) 39 Expect(session.Out).To(matchHelpMessage) 40 }) 41 }) 42 43 When("the service instance name is missing", func() { 44 It("fails with an error and prints help", func() { 45 session := helpers.CF(shareServiceCommand, "-s", shareToSpaceName) 46 Eventually(session).Should(Exit(1)) 47 Expect(session.Err).To(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided")) 48 Expect(session.Out).To(matchHelpMessage) 49 }) 50 }) 51 52 When("the space name flag is missing", func() { 53 It("fails with an error and prints help", func() { 54 session := helpers.CF(shareServiceCommand, serviceInstanceName) 55 Eventually(session).Should(Exit(1)) 56 Expect(session.Err).To(Say("Incorrect Usage: the required flag `-s' was not specified")) 57 Expect(session.Out).To(matchHelpMessage) 58 }) 59 }) 60 61 When("an extra parameter is specified", func() { 62 It("fails with an error and prints help", func() { 63 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName, "anotherRandomParameter") 64 Eventually(session).Should(Exit(1)) 65 Expect(session.Err).To(Say(`Incorrect Usage: unexpected argument "anotherRandomParameter"`)) 66 Expect(session.Out).To(SatisfyAll( 67 Say(`FAILED\n\n`), 68 matchHelpMessage, 69 )) 70 }) 71 }) 72 73 When("an extra flag is specified", func() { 74 It("fails with an error and prints help", func() { 75 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName, "--anotherRandomFlag") 76 Eventually(session).Should(Exit(1)) 77 Expect(session.Err).To(Say("Incorrect Usage: unknown flag `anotherRandomFlag'")) 78 Expect(session.Out).To(matchHelpMessage) 79 }) 80 }) 81 }) 82 83 When("the environment is not setup correctly", func() { 84 It("fails with the appropriate errors", func() { 85 helpers.CheckEnvironmentTargetedCorrectly( 86 true, 87 true, 88 ReadOnlyOrg, 89 shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName) 90 }) 91 }) 92 93 Context("share-service command is valid", func() { 94 var ( 95 orgName string 96 username string 97 ) 98 99 BeforeEach(func() { 100 orgName = helpers.NewOrgName() 101 spaceName := helpers.NewSpaceName() 102 helpers.SetupCF(orgName, spaceName) 103 104 username, _ = helpers.GetCredentials() 105 }) 106 107 AfterEach(func() { 108 helpers.QuickDeleteOrg(orgName) 109 }) 110 111 Describe("command parameters are invalid", func() { 112 113 Context("service instance cannot be retrieved", func() { 114 It("fails with an error", func() { 115 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName) 116 Eventually(session).Should(Exit(1)) 117 Expect(session.Out).To(SatisfyAll( 118 Say("Sharing service instance %s into org %s / space %s as %s...", serviceInstanceName, orgName, shareToSpaceName, username), 119 Say("FAILED"), 120 )) 121 Expect(session.Err).To(Say("Service instance '%s' not found", serviceInstanceName)) 122 }) 123 }) 124 125 Context("service instance exists", func() { 126 var broker *servicebrokerstub.ServiceBrokerStub 127 128 BeforeEach(func() { 129 broker = servicebrokerstub.New().Create().EnableServiceAccess() 130 131 serviceInstanceName = helpers.NewServiceInstanceName() 132 helpers.CreateManagedServiceInstance( 133 broker.FirstServiceOfferingName(), 134 broker.FirstServicePlanName(), 135 serviceInstanceName, 136 ) 137 138 shareToSpaceName = helpers.NewSpaceName() 139 shareToOrgName = helpers.NewOrgName() 140 }) 141 142 AfterEach(func() { 143 broker.Forget() 144 }) 145 146 Context("space cannot be retrieved in targeted org", func() { 147 It("fails with an error", func() { 148 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName) 149 Eventually(session).Should(Exit(1)) 150 Expect(session.Out).To(SatisfyAll( 151 Say("Sharing service instance %s into org %s / space %s as %s...", serviceInstanceName, orgName, shareToSpaceName, username), 152 Say("FAILED"), 153 )) 154 Eventually(session.Err).Should(Say("Space '%s' not found.", shareToSpaceName)) 155 }) 156 }) 157 158 Context("space cannot be retrieved in specified org", func() { 159 BeforeEach(func() { 160 helpers.CreateOrg(shareToOrgName) 161 }) 162 163 AfterEach(func() { 164 helpers.QuickDeleteOrg(shareToOrgName) 165 }) 166 167 It("fails with an error", func() { 168 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName, "-o", shareToOrgName) 169 Eventually(session).Should(Exit(1)) 170 Expect(session.Out).To(SatisfyAll( 171 Say("Sharing service instance %s into org %s / space %s as %s...", serviceInstanceName, shareToOrgName, shareToSpaceName, username), 172 Say("FAILED"), 173 )) 174 Eventually(session.Err).Should(Say("Space '%s' not found.", shareToSpaceName)) 175 }) 176 }) 177 178 Context("specified organization cannot be retrieved", func() { 179 It("fails with an error", func() { 180 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName, "-o", shareToOrgName) 181 Eventually(session).Should(Exit(1)) 182 Expect(session.Out).To(SatisfyAll( 183 Say("Sharing service instance %s into org %s / space %s as %s...", serviceInstanceName, shareToOrgName, shareToSpaceName, username), 184 Say("FAILED"), 185 )) 186 Eventually(session.Err).Should(Say("Organization '%s' not found.", shareToOrgName)) 187 }) 188 }) 189 }) 190 }) 191 192 Describe("when sharing the service instance succeeds", func() { 193 var broker *servicebrokerstub.ServiceBrokerStub 194 195 BeforeEach(func() { 196 broker = servicebrokerstub.New().Create().EnableServiceAccess() 197 198 serviceInstanceName = helpers.NewServiceInstanceName() 199 helpers.CreateManagedServiceInstance( 200 broker.FirstServiceOfferingName(), 201 broker.FirstServicePlanName(), 202 serviceInstanceName, 203 ) 204 }) 205 206 AfterEach(func() { 207 broker.Forget() 208 }) 209 210 When("space is in targeted org", func() { 211 BeforeEach(func() { 212 shareToSpaceName = helpers.NewSpaceName() 213 helpers.CreateSpace(shareToSpaceName) 214 }) 215 216 AfterEach(func() { 217 helpers.QuickDeleteSpace(shareToSpaceName) 218 }) 219 220 It("shares the service to space in targeted org", func() { 221 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName) 222 223 Eventually(session).Should(Exit(0)) 224 Expect(session.Out).To(SatisfyAll( 225 Say("Sharing service instance %s into org %s / space %s as %s...", serviceInstanceName, orgName, shareToSpaceName, username), 226 Say("OK"), 227 )) 228 229 By("validating the service is shared", func() { 230 session = helpers.CF("service", serviceInstanceName) 231 Eventually(session).Should(Exit(0)) 232 Expect(session.Out).To(SatisfyAll( 233 Say(`Shared with spaces`), 234 Say(`org\s+space\s+bindings`), 235 Say(`%s\s+%s\s+0`, orgName, shareToSpaceName), 236 )) 237 }) 238 }) 239 }) 240 241 When("the space to share is in specified org", func() { 242 BeforeEach(func() { 243 shareToOrgName = helpers.NewOrgName() 244 helpers.CreateOrg(shareToOrgName) 245 246 shareToSpaceName = helpers.NewSpaceName() 247 helpers.CreateSpaceInOrg(shareToSpaceName, shareToOrgName) 248 }) 249 250 AfterEach(func() { 251 helpers.QuickDeleteOrg(shareToOrgName) 252 }) 253 254 It("shares the service to space in specified org", func() { 255 session := helpers.CF(shareServiceCommand, serviceInstanceName, "-s", shareToSpaceName, "-o", shareToOrgName) 256 257 Eventually(session).Should(Exit(0)) 258 Expect(session.Out).To(SatisfyAll( 259 Say("Sharing service instance %s into org %s / space %s as %s...", serviceInstanceName, shareToOrgName, shareToSpaceName, username), 260 Say("OK"), 261 )) 262 263 By("validating the service is shared", func() { 264 session = helpers.CF("service", serviceInstanceName) 265 Eventually(session).Should(Exit(0)) 266 Expect(session.Out).To(SatisfyAll( 267 Say(`Shared with spaces`), 268 Say(`org\s+space\s+bindings`), 269 Say(`%s\s+%s\s+0`, shareToOrgName, shareToSpaceName), 270 )) 271 }) 272 }) 273 }) 274 }) 275 }) 276 })