github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/create_service_broker_command_test.go (about) 1 package isolated 2 3 import ( 4 "io" 5 6 "code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub" 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 ) 15 16 var _ = Describe("create-service-broker command", func() { 17 var brokerName string 18 19 BeforeEach(func() { 20 helpers.SkipIfV7AndVersionLessThan(ccversion.MinVersionCreateServiceBrokerV3) 21 22 brokerName = helpers.NewServiceBrokerName() 23 24 helpers.LoginCF() 25 }) 26 27 Describe("help", func() { 28 When("--help flag is set", func() { 29 It("displays command usage to output", func() { 30 session := helpers.CF("create-service-broker", "--help") 31 Eventually(session).Should(Exit(0)) 32 33 expectToRenderCreateServiceBrokerHelp(session) 34 }) 35 }) 36 }) 37 38 When("not logged in", func() { 39 BeforeEach(func() { 40 helpers.LogoutCF() 41 }) 42 43 It("displays an informative error that the user must be logged in", func() { 44 session := helpers.CF("create-service-broker", brokerName, "user", "pass", "http://example.com") 45 Eventually(session).Should(Say("FAILED")) 46 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' or 'cf login --sso' to log in.")) 47 Eventually(session).Should(Exit(1)) 48 }) 49 }) 50 51 When("logged in", func() { 52 When("all arguments are provided", func() { 53 When("no org or space is targeted", func() { 54 var ( 55 username string 56 orgName string 57 spaceName string 58 broker *servicebrokerstub.ServiceBrokerStub 59 ) 60 61 BeforeEach(func() { 62 username, _ = helpers.GetCredentials() 63 orgName = helpers.NewOrgName() 64 spaceName = helpers.NewSpaceName() 65 helpers.SetupCF(orgName, spaceName) 66 broker = servicebrokerstub.Create() 67 helpers.ClearTarget() 68 }) 69 70 AfterEach(func() { 71 Eventually(helpers.CF("delete-service-broker", brokerName, "-f")).Should(Exit(0)) 72 helpers.QuickDeleteOrg(orgName) 73 broker.Forget() 74 }) 75 76 It("registers the broker and service offerings and plans are available", func() { 77 session := helpers.CF("create-service-broker", brokerName, broker.Username, broker.Password, broker.URL) 78 Eventually(session).Should(Say("Creating service broker %s as %s...", brokerName, username)) 79 Eventually(session).Should(Say("OK")) 80 Eventually(session).Should(Exit(0)) 81 82 session = helpers.CF("service-access", "-b", brokerName) 83 Eventually(session).Should(Say(broker.FirstServiceOfferingName())) 84 Eventually(session).Should(Say(broker.FirstServicePlanName())) 85 86 session = helpers.CF("service-brokers") 87 Eventually(session).Should(Say(brokerName)) 88 }) 89 }) 90 91 When("the --space-scoped flag is passed", func() { 92 BeforeEach(func() { 93 helpers.SkipIfV7AndVersionLessThan(ccversion.MinVersionCreateSpaceScopedServiceBrokerV3) 94 }) 95 96 When("no org or space is targeted", func() { 97 BeforeEach(func() { 98 helpers.ClearTarget() 99 }) 100 101 It("displays an informative error that a space must be targeted", func() { 102 session := helpers.CF("create-service-broker", "space-scoped-broker", "username", "password", "http://example.com", "--space-scoped") 103 Eventually(session).Should(Say("FAILED")) 104 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 105 Eventually(session).Should(Exit(1)) 106 }) 107 }) 108 109 When("both org and space are targeted", func() { 110 var ( 111 username string 112 orgName string 113 spaceName string 114 broker *servicebrokerstub.ServiceBrokerStub 115 ) 116 117 BeforeEach(func() { 118 username, _ = helpers.GetCredentials() 119 orgName = helpers.NewOrgName() 120 spaceName = helpers.NewSpaceName() 121 helpers.SetupCF(orgName, spaceName) 122 broker = servicebrokerstub.Create() 123 }) 124 125 AfterEach(func() { 126 Eventually(helpers.CF("delete-service-broker", brokerName, "-f")).Should(Exit(0)) 127 helpers.QuickDeleteOrg(orgName) 128 broker.Forget() 129 }) 130 131 It("registers the broker and exposes its services only to the targeted space", func() { 132 session := helpers.CF("create-service-broker", brokerName, broker.Username, broker.Password, broker.URL, "--space-scoped") 133 Eventually(session).Should(Say( 134 "Creating service broker %s in org %s / space %s as %s...", brokerName, orgName, spaceName, username)) 135 Eventually(session).Should(Say("OK")) 136 Eventually(session).Should(Exit(0)) 137 138 session = helpers.CF("service-brokers") 139 Eventually(session).Should(Say(brokerName)) 140 141 Eventually(func() io.Reader { 142 session := helpers.CF("marketplace") 143 Eventually(session).Should(Exit(0)) 144 145 return session.Out 146 }).Should(Say(broker.FirstServicePlanName())) 147 148 helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace) 149 session = helpers.CF("marketplace") 150 Eventually(session).ShouldNot(Say(broker.FirstServicePlanName())) 151 }) 152 }) 153 }) 154 155 When("the broker already exists", func() { 156 var ( 157 org string 158 cfUsername string 159 broker *servicebrokerstub.ServiceBrokerStub 160 newBroker *servicebrokerstub.ServiceBrokerStub 161 ) 162 163 BeforeEach(func() { 164 org = helpers.SetupCFWithGeneratedOrgAndSpaceNames() 165 cfUsername, _ = helpers.GetCredentials() 166 broker = servicebrokerstub.Register() 167 newBroker = servicebrokerstub.Create() 168 }) 169 170 AfterEach(func() { 171 broker.Forget() 172 newBroker.Forget() 173 helpers.QuickDeleteOrg(org) 174 }) 175 176 It("fails", func() { 177 session := helpers.CF("create-service-broker", broker.Name, newBroker.Username, newBroker.Password, newBroker.URL) 178 Eventually(session).Should(Exit(1), "expected duplicate create-service-broker to fail") 179 180 Expect(session.Out).To(SatisfyAll( 181 Say(`Creating service broker %s as %s...\n`, broker.Name, cfUsername), 182 Say(`FAILED\n`), 183 )) 184 Expect(session.Err).To(Say("Name must be unique")) 185 }) 186 187 When("the --update-if-exists flag is passed", func() { 188 It("updates the existing broker", func() { 189 session := helpers.CF("create-service-broker", broker.Name, newBroker.Username, newBroker.Password, newBroker.URL, "--update-if-exists") 190 Eventually(session).Should(Exit(0)) 191 192 Expect(session.Out).To(SatisfyAll( 193 Say("Updating service broker %s as %s...", broker.Name, cfUsername), 194 Say("OK"), 195 )) 196 197 By("checking the URL has been updated") 198 session = helpers.CF("service-brokers") 199 Eventually(session.Out).Should(Say("%s[[:space:]]+%s", broker.Name, newBroker.URL)) 200 }) 201 }) 202 }) 203 }) 204 }) 205 206 When("the broker URL is invalid", func() { 207 BeforeEach(func() { 208 // TODO: replace skip with versioned skip when 209 // https://www.pivotaltracker.com/story/show/166215494 is resolved. 210 helpers.SkipIfV7() 211 }) 212 213 It("displays a relevant error", func() { 214 session := helpers.CF("create-service-broker", brokerName, "user", "pass", "not-a-valid-url") 215 Eventually(session).Should(Say("FAILED")) 216 Eventually(session.Err).Should(Say("not-a-valid-url is not a valid URL")) 217 Eventually(session).Should(Exit(1)) 218 }) 219 }) 220 221 When("no arguments are provided", func() { 222 It("displays an error, naming each of the missing args and the help text", func() { 223 session := helpers.CF("create-service-broker") 224 Eventually(session).Should(Exit(1)) 225 226 expectToRenderCreateServiceBrokerHelp(session) 227 }) 228 }) 229 }) 230 231 func expectToRenderCreateServiceBrokerHelp(s *Session) { 232 Expect(s).To(SatisfyAll( 233 Say(`NAME:`), 234 Say(`\s+create-service-broker - Create a service broker`), 235 236 Say(`USAGE:`), 237 Say(`\s+cf create-service-broker SERVICE_BROKER USERNAME PASSWORD URL \[--space-scoped\]`), 238 Say(`\s+cf create-service-broker SERVICE_BROKER USERNAME URL \[--space-scoped\]`), 239 240 Say(`WARNING:`), 241 Say(`\s+Providing your password as a command line option is highly discouraged`), 242 Say(`\s+Your password may be visible to others and may be recorded in your shell history`), 243 244 Say(`ALIAS:`), 245 Say(`\s+csb`), 246 247 Say(`OPTIONS:`), 248 Say(`\s+--space-scoped\s+Make the broker's service plans only visible within the targeted space`), 249 Say(`\s+--update-if-exists\s+If the broker already exists, update it rather than failing. Ignores --space-scoped.`), 250 251 Say(`ENVIRONMENT:`), 252 Say(`\s+CF_BROKER_PASSWORD=password\s+Password associated with user. Overridden if PASSWORD argument is provided`), 253 254 Say(`SEE ALSO:`), 255 Say(`\s+enable-service-access, service-brokers, target`), 256 )) 257 }