github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/service/create_user_provided_service_test.go (about) 1 package service_test 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "os" 7 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/commands/service" 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/flags" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 14 "github.com/blang/semver" 15 16 "code.cloudfoundry.org/cli/cf/api/apifakes" 17 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 18 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 19 20 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("CreateUserProvidedService", func() { 26 var ( 27 ui *testterm.FakeUI 28 configRepo coreconfig.Repository 29 serviceInstanceRepo *apifakes.FakeUserProvidedServiceInstanceRepository 30 31 cmd commandregistry.Command 32 deps commandregistry.Dependency 33 factory *requirementsfakes.FakeFactory 34 flagContext flags.FlagContext 35 36 loginRequirement requirements.Requirement 37 targetedSpaceRequirement requirements.Requirement 38 minAPIVersionRequirement requirements.Requirement 39 ) 40 41 BeforeEach(func() { 42 ui = new(testterm.FakeUI) 43 configRepo = testconfig.NewRepositoryWithDefaults() 44 serviceInstanceRepo = new(apifakes.FakeUserProvidedServiceInstanceRepository) 45 repoLocator := deps.RepoLocator.SetUserProvidedServiceInstanceRepository(serviceInstanceRepo) 46 47 deps = commandregistry.Dependency{ 48 UI: ui, 49 Config: configRepo, 50 RepoLocator: repoLocator, 51 } 52 53 cmd = &service.CreateUserProvidedService{} 54 cmd.SetDependency(deps, false) 55 56 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 57 factory = new(requirementsfakes.FakeFactory) 58 59 loginRequirement = &passingRequirement{Name: "login-requirement"} 60 factory.NewLoginRequirementReturns(loginRequirement) 61 62 minAPIVersionRequirement = &passingRequirement{Name: "min-api-version-requirement"} 63 factory.NewMinAPIVersionRequirementReturns(minAPIVersionRequirement) 64 65 targetedSpaceRequirement = &passingRequirement{Name: "targeted-space-requirement"} 66 factory.NewTargetedSpaceRequirementReturns(targetedSpaceRequirement) 67 }) 68 69 Describe("Requirements", func() { 70 Context("when not provided exactly one arg", func() { 71 BeforeEach(func() { 72 flagContext.Parse("service-instance", "extra-arg") 73 }) 74 75 It("fails with usage", func() { 76 _, err := cmd.Requirements(factory, flagContext) 77 Expect(err).To(HaveOccurred()) 78 Expect(ui.Outputs()).To(ContainSubstrings( 79 []string{"FAILED"}, 80 []string{"Incorrect Usage. Requires an argument"}, 81 )) 82 }) 83 }) 84 85 Context("when provided exactly one arg", func() { 86 BeforeEach(func() { 87 flagContext.Parse("service-instance") 88 }) 89 90 It("returns a LoginRequirement", func() { 91 actualRequirements, err := cmd.Requirements(factory, flagContext) 92 Expect(err).NotTo(HaveOccurred()) 93 Expect(factory.NewLoginRequirementCallCount()).To(Equal(1)) 94 Expect(actualRequirements).To(ContainElement(loginRequirement)) 95 }) 96 97 It("returns a TargetedSpaceRequirement", func() { 98 actualRequirements, err := cmd.Requirements(factory, flagContext) 99 Expect(err).NotTo(HaveOccurred()) 100 Expect(factory.NewTargetedSpaceRequirementCallCount()).To(Equal(1)) 101 Expect(actualRequirements).To(ContainElement(targetedSpaceRequirement)) 102 }) 103 }) 104 105 Context("when provided the -r flag", func() { 106 BeforeEach(func() { 107 flagContext.Parse("service-instance", "-r", "route-service-url") 108 }) 109 110 It("returns a MinAPIVersionRequirement", func() { 111 actualRequirements, err := cmd.Requirements(factory, flagContext) 112 Expect(err).NotTo(HaveOccurred()) 113 Expect(factory.NewMinAPIVersionRequirementCallCount()).To(Equal(1)) 114 Expect(actualRequirements).To(ContainElement(minAPIVersionRequirement)) 115 116 feature, requiredVersion := factory.NewMinAPIVersionRequirementArgsForCall(0) 117 Expect(feature).To(Equal("Option '-r'")) 118 expectedRequiredVersion, err := semver.Make("2.51.0") 119 Expect(err).NotTo(HaveOccurred()) 120 Expect(requiredVersion).To(Equal(expectedRequiredVersion)) 121 }) 122 }) 123 }) 124 125 Describe("Execute", func() { 126 var runCLIErr error 127 128 BeforeEach(func() { 129 err := flagContext.Parse("service-instance") 130 Expect(err).NotTo(HaveOccurred()) 131 cmd.Requirements(factory, flagContext) 132 }) 133 134 JustBeforeEach(func() { 135 runCLIErr = cmd.Execute(flagContext) 136 }) 137 138 It("tells the user it will create the user provided service", func() { 139 Expect(runCLIErr).NotTo(HaveOccurred()) 140 Expect(ui.Outputs()).To(ContainSubstrings( 141 []string{"Creating user provided service service-instance in org"}, 142 )) 143 }) 144 145 It("tries to create the user provided service instance", func() { 146 Expect(runCLIErr).NotTo(HaveOccurred()) 147 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 148 name, drainURL, routeServiceURL, credentialsMap := serviceInstanceRepo.CreateArgsForCall(0) 149 Expect(name).To(Equal("service-instance")) 150 Expect(drainURL).To(Equal("")) 151 Expect(routeServiceURL).To(Equal("")) 152 Expect(credentialsMap).To(Equal(map[string]interface{}{})) 153 }) 154 155 Context("when creating the user provided service instance succeeds", func() { 156 BeforeEach(func() { 157 serviceInstanceRepo.CreateReturns(nil) 158 }) 159 160 It("tells the user OK", func() { 161 Expect(runCLIErr).NotTo(HaveOccurred()) 162 Expect(ui.Outputs()).To(ContainSubstrings( 163 []string{"OK"}, 164 )) 165 }) 166 }) 167 168 Context("when creating the user provided service instance fails", func() { 169 BeforeEach(func() { 170 serviceInstanceRepo.CreateReturns(errors.New("create-err")) 171 }) 172 173 It("fails with error", func() { 174 Expect(runCLIErr).To(HaveOccurred()) 175 Expect(runCLIErr.Error()).To(Equal("create-err")) 176 }) 177 }) 178 179 Context("when the -l flag is passed", func() { 180 BeforeEach(func() { 181 flagContext.Parse("service-instance", "-l", "drain-url") 182 }) 183 184 It("tries to create the user provided service instance with the drain url", func() { 185 Expect(runCLIErr).NotTo(HaveOccurred()) 186 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 187 _, drainURL, _, _ := serviceInstanceRepo.CreateArgsForCall(0) 188 Expect(drainURL).To(Equal("drain-url")) 189 }) 190 }) 191 192 Context("when the -r flag is passed", func() { 193 BeforeEach(func() { 194 flagContext.Parse("service-instance", "-r", "route-service-url") 195 }) 196 197 It("tries to create the user provided service instance with the route service url", func() { 198 Expect(runCLIErr).NotTo(HaveOccurred()) 199 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 200 _, _, routeServiceURL, _ := serviceInstanceRepo.CreateArgsForCall(0) 201 Expect(routeServiceURL).To(Equal("route-service-url")) 202 }) 203 }) 204 205 Context("when the -p flag is passed with inline JSON", func() { 206 BeforeEach(func() { 207 flagContext.Parse("service-instance", "-p", `"{"some":"json"}"`) 208 }) 209 210 It("tries to create the user provided service instance with the credentials", func() { 211 Expect(runCLIErr).NotTo(HaveOccurred()) 212 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 213 _, _, _, credentialsMap := serviceInstanceRepo.CreateArgsForCall(0) 214 Expect(credentialsMap).To(Equal(map[string]interface{}{ 215 "some": "json", 216 })) 217 }) 218 }) 219 220 Context("when the -p flag is passed with a file containing JSON", func() { 221 var filename string 222 223 BeforeEach(func() { 224 tempfile, err := ioutil.TempFile("", "create-user-provided-service-test") 225 Expect(err).NotTo(HaveOccurred()) 226 Expect(tempfile.Close()).NotTo(HaveOccurred()) 227 filename = tempfile.Name() 228 229 jsonData := `{"some":"json"}` 230 ioutil.WriteFile(filename, []byte(jsonData), os.ModePerm) 231 flagContext.Parse("service-instance", "-p", filename) 232 }) 233 234 AfterEach(func() { 235 Expect(os.RemoveAll(filename)).NotTo(HaveOccurred()) 236 }) 237 238 It("tries to create the user provided service instance with the credentials", func() { 239 Expect(runCLIErr).NotTo(HaveOccurred()) 240 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 241 _, _, _, credentialsMap := serviceInstanceRepo.CreateArgsForCall(0) 242 Expect(credentialsMap).To(Equal(map[string]interface{}{ 243 "some": "json", 244 })) 245 }) 246 }) 247 248 Context("when the -p flag is passed with inline JSON", func() { 249 BeforeEach(func() { 250 flagContext.Parse("service-instance", "-p", `key1,key2`) 251 ui.Inputs = []string{"value1", "value2"} 252 }) 253 254 It("prompts the user for the values", func() { 255 Expect(runCLIErr).NotTo(HaveOccurred()) 256 Expect(ui.Prompts).To(ContainSubstrings( 257 []string{"key1"}, 258 []string{"key2"}, 259 )) 260 }) 261 262 It("tries to create the user provided service instance with the credentials", func() { 263 Expect(runCLIErr).NotTo(HaveOccurred()) 264 265 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 266 _, _, _, credentialsMap := serviceInstanceRepo.CreateArgsForCall(0) 267 Expect(credentialsMap).To(Equal(map[string]interface{}{ 268 "key1": "value1", 269 "key2": "value2", 270 })) 271 }) 272 }) 273 }) 274 })