github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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/cf/util/testhelpers/configuration" 18 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 19 20 . "code.cloudfoundry.org/cli/cf/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 -t flag", func() { 106 BeforeEach(func() { 107 flagContext.Parse("service-instance", "-t", "tag,a,service") 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 '-t'")) 118 expectedRequiredVersion, err := semver.Make("2.104.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, tags := 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 Expect(tags).To(Equal([]string{})) 154 }) 155 156 Context("when creating the user provided service instance succeeds", func() { 157 BeforeEach(func() { 158 serviceInstanceRepo.CreateReturns(nil) 159 }) 160 161 It("tells the user OK", func() { 162 Expect(runCLIErr).NotTo(HaveOccurred()) 163 Expect(ui.Outputs()).To(ContainSubstrings( 164 []string{"OK"}, 165 )) 166 }) 167 }) 168 169 Context("when creating the user provided service instance fails", func() { 170 BeforeEach(func() { 171 serviceInstanceRepo.CreateReturns(errors.New("create-err")) 172 }) 173 174 It("fails with error", func() { 175 Expect(runCLIErr).To(HaveOccurred()) 176 Expect(runCLIErr.Error()).To(Equal("create-err")) 177 }) 178 }) 179 180 Context("when the -l flag is passed", func() { 181 BeforeEach(func() { 182 flagContext.Parse("service-instance", "-l", "drain-url") 183 }) 184 185 It("tries to create the user provided service instance with the drain url", func() { 186 Expect(runCLIErr).NotTo(HaveOccurred()) 187 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 188 _, drainURL, _, _, _ := serviceInstanceRepo.CreateArgsForCall(0) 189 Expect(drainURL).To(Equal("drain-url")) 190 }) 191 }) 192 193 Context("when the -r flag is passed", func() { 194 BeforeEach(func() { 195 flagContext.Parse("service-instance", "-r", "route-service-url") 196 }) 197 198 It("tries to create the user provided service instance with the route service url", func() { 199 Expect(runCLIErr).NotTo(HaveOccurred()) 200 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 201 _, _, routeServiceURL, _, _ := serviceInstanceRepo.CreateArgsForCall(0) 202 Expect(routeServiceURL).To(Equal("route-service-url")) 203 }) 204 }) 205 206 Context("when the -p flag is passed with inline JSON", func() { 207 BeforeEach(func() { 208 flagContext.Parse("service-instance", "-p", `"{"some":"json"}"`) 209 }) 210 211 It("tries to create the user provided service instance with the credentials", func() { 212 Expect(runCLIErr).NotTo(HaveOccurred()) 213 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 214 _, _, _, credentialsMap, _ := serviceInstanceRepo.CreateArgsForCall(0) 215 Expect(credentialsMap).To(Equal(map[string]interface{}{ 216 "some": "json", 217 })) 218 }) 219 }) 220 221 Context("when the -p flag is passed with a file containing JSON", func() { 222 var filename string 223 224 BeforeEach(func() { 225 tempfile, err := ioutil.TempFile("", "create-user-provided-service-test") 226 Expect(err).NotTo(HaveOccurred()) 227 Expect(tempfile.Close()).NotTo(HaveOccurred()) 228 filename = tempfile.Name() 229 230 jsonData := `{"some":"json"}` 231 ioutil.WriteFile(filename, []byte(jsonData), os.ModePerm) 232 flagContext.Parse("service-instance", "-p", filename) 233 }) 234 235 AfterEach(func() { 236 Expect(os.RemoveAll(filename)).NotTo(HaveOccurred()) 237 }) 238 239 It("tries to create the user provided service instance with the credentials", func() { 240 Expect(runCLIErr).NotTo(HaveOccurred()) 241 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 242 _, _, _, credentialsMap, _ := serviceInstanceRepo.CreateArgsForCall(0) 243 Expect(credentialsMap).To(Equal(map[string]interface{}{ 244 "some": "json", 245 })) 246 }) 247 }) 248 249 Context("when the -p flag is passed with inline JSON", func() { 250 BeforeEach(func() { 251 flagContext.Parse("service-instance", "-p", `key1,key2`) 252 ui.Inputs = []string{"value1", "value2"} 253 }) 254 255 It("prompts the user for the values", func() { 256 Expect(runCLIErr).NotTo(HaveOccurred()) 257 Expect(ui.Prompts).To(ContainSubstrings( 258 []string{"key1"}, 259 []string{"key2"}, 260 )) 261 }) 262 263 It("tries to create the user provided service instance with the credentials", func() { 264 Expect(runCLIErr).NotTo(HaveOccurred()) 265 266 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 267 _, _, _, credentialsMap, _ := serviceInstanceRepo.CreateArgsForCall(0) 268 Expect(credentialsMap).To(Equal(map[string]interface{}{ 269 "key1": "value1", 270 "key2": "value2", 271 })) 272 }) 273 }) 274 275 Context("when passing in tags", func() { 276 BeforeEach(func() { 277 flagContext.Parse("service-instance", "-t", "tag1, tag2, tag3, tag4") 278 }) 279 280 It("sucessfully creates a service instance and passes the tags as json", func() { 281 Expect(runCLIErr).NotTo(HaveOccurred()) 282 Expect(serviceInstanceRepo.CreateCallCount()).To(Equal(1)) 283 _, _, _, _, tags := serviceInstanceRepo.CreateArgsForCall(0) 284 Expect(tags).To(ConsistOf("tag1", "tag2", "tag3", "tag4")) 285 }) 286 }) 287 }) 288 })