github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/copy_source_test.go (about) 1 package application_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes" 5 "code.cloudfoundry.org/cli/cf/api/authentication/authenticationfakes" 6 "code.cloudfoundry.org/cli/cf/api/copyapplicationsource/copyapplicationsourcefakes" 7 "code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes" 8 "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes" 9 "code.cloudfoundry.org/cli/cf/commands/application/applicationfakes" 10 "code.cloudfoundry.org/cli/cf/models" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 13 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 14 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 15 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 16 17 "code.cloudfoundry.org/cli/cf/commandregistry" 18 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 19 "code.cloudfoundry.org/cli/cf/errors" 20 21 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 22 . "github.com/onsi/ginkgo" 23 . "github.com/onsi/gomega" 24 ) 25 26 var _ = Describe("CopySource", func() { 27 28 var ( 29 ui *testterm.FakeUI 30 config coreconfig.Repository 31 requirementsFactory *requirementsfakes.FakeFactory 32 authRepo *authenticationfakes.FakeRepository 33 appRepo *applicationsfakes.FakeRepository 34 copyAppSourceRepo *copyapplicationsourcefakes.FakeRepository 35 spaceRepo *spacesfakes.FakeSpaceRepository 36 orgRepo *organizationsfakes.FakeOrganizationRepository 37 appRestarter *applicationfakes.FakeRestarter 38 OriginalCommand commandregistry.Command 39 deps commandregistry.Dependency 40 ) 41 42 updateCommandDependency := func(pluginCall bool) { 43 deps.UI = ui 44 deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(authRepo) 45 deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) 46 deps.RepoLocator = deps.RepoLocator.SetCopyApplicationSourceRepository(copyAppSourceRepo) 47 deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo) 48 deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo) 49 deps.Config = config 50 51 //inject fake 'command dependency' into registry 52 commandregistry.Register(appRestarter) 53 54 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("copy-source").SetDependency(deps, pluginCall)) 55 } 56 57 BeforeEach(func() { 58 ui = &testterm.FakeUI{} 59 requirementsFactory = new(requirementsfakes.FakeFactory) 60 authRepo = new(authenticationfakes.FakeRepository) 61 appRepo = new(applicationsfakes.FakeRepository) 62 copyAppSourceRepo = new(copyapplicationsourcefakes.FakeRepository) 63 spaceRepo = new(spacesfakes.FakeSpaceRepository) 64 orgRepo = new(organizationsfakes.FakeOrganizationRepository) 65 config = testconfig.NewRepositoryWithDefaults() 66 67 //save original command and restore later 68 OriginalCommand = commandregistry.Commands.FindCommand("restart") 69 70 appRestarter = new(applicationfakes.FakeRestarter) 71 //setup fakes to correctly interact with commandregistry 72 appRestarter.SetDependencyStub = func(_ commandregistry.Dependency, _ bool) commandregistry.Command { 73 return appRestarter 74 } 75 appRestarter.MetaDataReturns(commandregistry.CommandMetadata{Name: "restart"}) 76 }) 77 78 AfterEach(func() { 79 commandregistry.Register(OriginalCommand) 80 }) 81 82 runCommand := func(args ...string) bool { 83 return testcmd.RunCLICommand("copy-source", args, requirementsFactory, updateCommandDependency, false, ui) 84 } 85 86 Describe("requirement failures", func() { 87 It("when not logged in", func() { 88 requirementsFactory.NewUsageRequirementReturns(requirements.Passing{}) 89 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 90 Expect(runCommand("source-app", "target-app")).ToNot(HavePassedRequirements()) 91 }) 92 93 It("when a space is not targeted", func() { 94 requirementsFactory.NewUsageRequirementReturns(requirements.Passing{}) 95 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 96 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"}) 97 Expect(runCommand("source-app", "target-app")).ToNot(HavePassedRequirements()) 98 }) 99 100 It("when provided too many args", func() { 101 requirementsFactory.NewUsageRequirementReturns(requirements.Failing{}) 102 Expect(runCommand("source-app", "target-app", "too-much", "app-name")).ToNot(HavePassedRequirements()) 103 }) 104 }) 105 106 Describe("Passing requirements", func() { 107 BeforeEach(func() { 108 requirementsFactory.NewUsageRequirementReturns(requirements.Passing{}) 109 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 110 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 111 }) 112 113 Context("refreshing the auth token", func() { 114 It("makes a call for the app token", func() { 115 runCommand("source-app", "target-app") 116 Expect(authRepo.RefreshAuthTokenCallCount()).To(Equal(1)) 117 }) 118 119 Context("when refreshing the auth token fails", func() { 120 BeforeEach(func() { 121 authRepo.RefreshAuthTokenReturns("", errors.New("I accidentally the UAA")) 122 }) 123 124 It("it displays an error", func() { 125 runCommand("source-app", "target-app") 126 Expect(ui.Outputs()).To(ContainSubstrings( 127 []string{"FAILED"}, 128 []string{"accidentally the UAA"}, 129 )) 130 }) 131 }) 132 133 Describe("when retrieving the app token succeeds", func() { 134 var ( 135 sourceApp, targetApp models.Application 136 ) 137 138 BeforeEach(func() { 139 sourceApp = models.Application{ 140 ApplicationFields: models.ApplicationFields{ 141 Name: "source-app", 142 GUID: "source-app-guid", 143 }, 144 } 145 appRepo.ReadReturns(sourceApp, nil) 146 147 targetApp = models.Application{ 148 ApplicationFields: models.ApplicationFields{ 149 Name: "target-app", 150 GUID: "target-app-guid", 151 }, 152 } 153 appRepo.ReadFromSpaceReturns(targetApp, nil) 154 }) 155 156 Describe("when no parameters are passed", func() { 157 It("obtains both the source and target application from the same space", func() { 158 runCommand("source-app", "target-app") 159 160 targetAppName, spaceGUID := appRepo.ReadFromSpaceArgsForCall(0) 161 Expect(targetAppName).To(Equal("target-app")) 162 Expect(spaceGUID).To(Equal("my-space-guid")) 163 164 Expect(appRepo.ReadArgsForCall(0)).To(Equal("source-app")) 165 166 sourceAppGUID, targetAppGUID := copyAppSourceRepo.CopyApplicationArgsForCall(0) 167 Expect(sourceAppGUID).To(Equal("source-app-guid")) 168 Expect(targetAppGUID).To(Equal("target-app-guid")) 169 170 appArg, orgName, spaceName := appRestarter.ApplicationRestartArgsForCall(0) 171 Expect(appArg).To(Equal(targetApp)) 172 Expect(orgName).To(Equal(config.OrganizationFields().Name)) 173 Expect(spaceName).To(Equal(config.SpaceFields().Name)) 174 175 Expect(ui.Outputs()).To(ContainSubstrings( 176 []string{"Copying source from app", "source-app", "to target app", "target-app", "in org my-org / space my-space as my-user..."}, 177 []string{"Note: this may take some time"}, 178 []string{"OK"}, 179 )) 180 }) 181 182 Context("Failures", func() { 183 It("if we cannot obtain the source application", func() { 184 appRepo.ReadReturns(models.Application{}, errors.New("could not find source app")) 185 runCommand("source-app", "target-app") 186 187 Expect(ui.Outputs()).To(ContainSubstrings( 188 []string{"FAILED"}, 189 []string{"could not find source app"}, 190 )) 191 }) 192 193 It("fails if we cannot obtain the target application", func() { 194 appRepo.ReadFromSpaceReturns(models.Application{}, errors.New("could not find target app")) 195 runCommand("source-app", "target-app") 196 197 Expect(ui.Outputs()).To(ContainSubstrings( 198 []string{"FAILED"}, 199 []string{"could not find target app"}, 200 )) 201 }) 202 }) 203 }) 204 205 Describe("when a space is provided, but not an org", func() { 206 It("send the correct target appplication for the current org and target space", func() { 207 space := models.Space{} 208 space.Name = "space-name" 209 space.GUID = "model-space-guid" 210 spaceRepo.FindByNameReturns(space, nil) 211 212 runCommand("-s", "space-name", "source-app", "target-app") 213 214 targetAppName, spaceGUID := appRepo.ReadFromSpaceArgsForCall(0) 215 Expect(targetAppName).To(Equal("target-app")) 216 Expect(spaceGUID).To(Equal("model-space-guid")) 217 218 Expect(ui.Outputs()).To(ContainSubstrings( 219 []string{"Copying source from app", "source-app", "to target app", "target-app", "in org my-org / space space-name as my-user..."}, 220 []string{"Note: this may take some time"}, 221 []string{"OK"}, 222 )) 223 }) 224 225 Context("Failures", func() { 226 It("when we cannot find the provided space", func() { 227 spaceRepo.FindByNameReturns(models.Space{}, errors.New("Error finding space by name.")) 228 229 runCommand("-s", "space-name", "source-app", "target-app") 230 Expect(ui.Outputs()).To(ContainSubstrings( 231 []string{"FAILED"}, 232 []string{"Error finding space by name."}, 233 )) 234 }) 235 }) 236 }) 237 238 Describe("when an org and space name are passed as parameters", func() { 239 It("send the correct target application for the space and org", func() { 240 orgRepo.FindByNameReturns(models.Organization{ 241 Spaces: []models.SpaceFields{ 242 { 243 Name: "space-name", 244 GUID: "space-guid", 245 }, 246 }, 247 }, nil) 248 249 runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app") 250 251 targetAppName, spaceGUID := appRepo.ReadFromSpaceArgsForCall(0) 252 Expect(targetAppName).To(Equal("target-app")) 253 Expect(spaceGUID).To(Equal("space-guid")) 254 255 sourceAppGUID, targetAppGUID := copyAppSourceRepo.CopyApplicationArgsForCall(0) 256 Expect(sourceAppGUID).To(Equal("source-app-guid")) 257 Expect(targetAppGUID).To(Equal("target-app-guid")) 258 259 appArg, orgName, spaceName := appRestarter.ApplicationRestartArgsForCall(0) 260 Expect(appArg).To(Equal(targetApp)) 261 Expect(orgName).To(Equal("org-name")) 262 Expect(spaceName).To(Equal("space-name")) 263 264 Expect(ui.Outputs()).To(ContainSubstrings( 265 []string{"Copying source from app source-app to target app target-app in org org-name / space space-name as my-user..."}, 266 []string{"Note: this may take some time"}, 267 []string{"OK"}, 268 )) 269 }) 270 271 Context("failures", func() { 272 It("cannot just accept an organization and no space", func() { 273 runCommand("-o", "org-name", "source-app", "target-app") 274 275 Expect(ui.Outputs()).To(ContainSubstrings( 276 []string{"FAILED"}, 277 []string{"Please provide the space within the organization containing the target application"}, 278 )) 279 }) 280 281 It("when we cannot find the provided org", func() { 282 orgRepo.FindByNameReturns(models.Organization{}, errors.New("Could not find org")) 283 runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app") 284 285 Expect(ui.Outputs()).To(ContainSubstrings( 286 []string{"FAILED"}, 287 []string{"Could not find org"}, 288 )) 289 }) 290 291 It("when the org does not contain the space name provide", func() { 292 orgRepo.FindByNameReturns(models.Organization{}, nil) 293 runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app") 294 295 Expect(ui.Outputs()).To(ContainSubstrings( 296 []string{"FAILED"}, 297 []string{"Could not find space space-name in organization org-name"}, 298 )) 299 }) 300 301 It("when the targeted app does not exist in the targeted org and space", func() { 302 orgRepo.FindByNameReturns(models.Organization{ 303 Spaces: []models.SpaceFields{ 304 { 305 Name: "space-name", 306 GUID: "space-guid", 307 }, 308 }, 309 }, nil) 310 311 appRepo.ReadFromSpaceReturns(models.Application{}, errors.New("could not find app")) 312 runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app") 313 314 Expect(ui.Outputs()).To(ContainSubstrings( 315 []string{"FAILED"}, 316 []string{"could not find app"}, 317 )) 318 }) 319 }) 320 }) 321 322 Describe("when the --no-restart flag is passed", func() { 323 It("does not restart the target application", func() { 324 runCommand("--no-restart", "source-app", "target-app") 325 Expect(appRestarter.ApplicationRestartCallCount()).To(Equal(0)) 326 }) 327 }) 328 }) 329 }) 330 }) 331 })