github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/application/restage_test.go (about) 1 package application_test 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api/applications/applicationsfakes" 5 "github.com/cloudfoundry/cli/cf/commandregistry" 6 "github.com/cloudfoundry/cli/cf/configuration/coreconfig" 7 "github.com/cloudfoundry/cli/cf/errors" 8 "github.com/cloudfoundry/cli/cf/flags" 9 "github.com/cloudfoundry/cli/cf/models" 10 "github.com/cloudfoundry/cli/cf/requirements" 11 "github.com/cloudfoundry/cli/cf/requirements/requirementsfakes" 12 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 13 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 14 . "github.com/cloudfoundry/cli/testhelpers/matchers" 15 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("restage command", func() { 21 var ( 22 ui *testterm.FakeUI 23 app models.Application 24 appRepo *applicationsfakes.FakeRepository 25 configRepo coreconfig.Repository 26 requirementsFactory *requirementsfakes.FakeFactory 27 stagingWatcher *fakeStagingWatcher 28 OriginalCommand commandregistry.Command 29 deps commandregistry.Dependency 30 ) 31 32 updateCommandDependency := func(pluginCall bool) { 33 deps.UI = ui 34 deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) 35 deps.Config = configRepo 36 37 //inject fake 'command dependency' into registry 38 commandregistry.Register(stagingWatcher) 39 40 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("restage").SetDependency(deps, pluginCall)) 41 } 42 43 BeforeEach(func() { 44 ui = &testterm.FakeUI{} 45 46 app = models.Application{} 47 app.Name = "my-app" 48 app.PackageState = "STAGED" 49 appRepo = new(applicationsfakes.FakeRepository) 50 appRepo.ReadReturns(app, nil) 51 52 configRepo = testconfig.NewRepositoryWithDefaults() 53 requirementsFactory = new(requirementsfakes.FakeFactory) 54 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 55 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 56 57 //save original command and restore later 58 OriginalCommand = commandregistry.Commands.FindCommand("start") 59 60 stagingWatcher = &fakeStagingWatcher{} 61 }) 62 63 AfterEach(func() { 64 commandregistry.Register(OriginalCommand) 65 }) 66 67 runCommand := func(args ...string) bool { 68 return testcmd.RunCLICommand("restage", args, requirementsFactory, updateCommandDependency, false, ui) 69 } 70 71 Describe("Requirements", func() { 72 It("fails when the user is not logged in", func() { 73 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 74 Expect(runCommand("my-app")).To(BeFalse()) 75 }) 76 77 It("fails with usage when no arguments are given", func() { 78 passed := runCommand() 79 Expect(ui.Outputs()).To(ContainSubstrings( 80 []string{"Incorrect Usage", "Requires", "argument"}, 81 )) 82 Expect(passed).To(BeFalse()) 83 }) 84 85 It("fails if a space is not targeted", func() { 86 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"}) 87 Expect(runCommand("my-app")).To(BeFalse()) 88 }) 89 }) 90 91 It("fails with usage when the app cannot be found", func() { 92 appRepo.ReadReturns(models.Application{}, errors.NewModelNotFoundError("app", "hocus-pocus")) 93 runCommand("hocus-pocus") 94 95 Expect(ui.Outputs()).To(ContainSubstrings( 96 []string{"FAILED"}, 97 []string{"not found"}, 98 )) 99 }) 100 101 Context("when the app is found", func() { 102 BeforeEach(func() { 103 app = models.Application{} 104 app.Name = "my-app" 105 app.GUID = "the-app-guid" 106 107 appRepo.ReadReturns(app, nil) 108 }) 109 110 It("sends a restage request", func() { 111 runCommand("my-app") 112 Expect(appRepo.CreateRestageRequestArgsForCall(0)).To(Equal("the-app-guid")) 113 Expect(ui.Outputs()).To(ContainSubstrings( 114 []string{"Restaging app", "my-app", "my-org", "my-space", "my-user"}, 115 )) 116 }) 117 118 It("resets app's PackageState", func() { 119 runCommand("my-app") 120 Expect(stagingWatcher.watched.PackageState).ToNot(Equal("STAGED")) 121 }) 122 123 It("watches the staging output", func() { 124 runCommand("my-app") 125 Expect(stagingWatcher.watched).To(Equal(app)) 126 Expect(stagingWatcher.orgName).To(Equal(configRepo.OrganizationFields().Name)) 127 Expect(stagingWatcher.spaceName).To(Equal(configRepo.SpaceFields().Name)) 128 }) 129 }) 130 }) 131 132 type fakeStagingWatcher struct { 133 watched models.Application 134 orgName string 135 spaceName string 136 } 137 138 func (f *fakeStagingWatcher) WatchStaging(app models.Application, orgName, spaceName string, start func(models.Application) (models.Application, error)) (updatedApp models.Application, err error) { 139 f.watched = app 140 f.orgName = orgName 141 f.spaceName = spaceName 142 return start(app) 143 } 144 func (cmd *fakeStagingWatcher) MetaData() commandregistry.CommandMetadata { 145 return commandregistry.CommandMetadata{Name: "start"} 146 } 147 148 func (cmd *fakeStagingWatcher) SetDependency(_ commandregistry.Dependency, _ bool) commandregistry.Command { 149 return cmd 150 } 151 152 func (cmd *fakeStagingWatcher) Requirements(_ requirements.Factory, _ flags.FlagContext) ([]requirements.Requirement, error) { 153 return []requirements.Requirement{}, nil 154 } 155 156 func (cmd *fakeStagingWatcher) Execute(_ flags.FlagContext) error { 157 return nil 158 }