github.com/loafoe/cli@v7.1.0+incompatible/command/v7/restart_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/flag" 11 v7 "code.cloudfoundry.org/cli/command/v7" 12 "code.cloudfoundry.org/cli/command/v7/shared/sharedfakes" 13 "code.cloudfoundry.org/cli/command/v7/v7fakes" 14 "code.cloudfoundry.org/cli/resources" 15 "code.cloudfoundry.org/cli/util/configv3" 16 "code.cloudfoundry.org/cli/util/ui" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 ) 21 22 var _ = Describe("restart Command", func() { 23 var ( 24 cmd v7.RestartCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v7fakes.FakeActor 29 fakeAppStager *sharedfakes.FakeAppStager 30 31 binaryName string 32 executeErr error 33 app resources.Application 34 strategy constant.DeploymentStrategy 35 noWait bool 36 ) 37 38 BeforeEach(func() { 39 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 40 fakeConfig = new(commandfakes.FakeConfig) 41 fakeSharedActor = new(commandfakes.FakeSharedActor) 42 fakeActor = new(v7fakes.FakeActor) 43 fakeAppStager = new(sharedfakes.FakeAppStager) 44 45 binaryName = "faceman" 46 fakeConfig.BinaryNameReturns(binaryName) 47 app = resources.Application{Name: "app-name", GUID: "app-guid"} 48 strategy = constant.DeploymentStrategyDefault 49 noWait = false 50 51 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 52 Name: "some-org", 53 }) 54 fakeConfig.TargetedSpaceReturns(configv3.Space{ 55 Name: "some-space", 56 GUID: "some-space-guid", 57 }) 58 fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil) 59 fakeActor.GetApplicationByNameAndSpaceReturns(app, v7action.Warnings{"get-app-warning"}, nil) 60 61 cmd = v7.RestartCommand{ 62 RequiredArgs: flag.AppName{AppName: app.Name}, 63 Strategy: flag.DeploymentStrategy{Name: strategy}, 64 NoWait: noWait, 65 66 BaseCommand: v7.BaseCommand{ 67 UI: testUI, 68 Config: fakeConfig, 69 SharedActor: fakeSharedActor, 70 Actor: fakeActor, 71 }, 72 Stager: fakeAppStager, 73 } 74 }) 75 76 JustBeforeEach(func() { 77 executeErr = cmd.Execute(nil) 78 }) 79 80 When("checking target fails", func() { 81 BeforeEach(func() { 82 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 83 }) 84 85 It("returns an error", func() { 86 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 87 88 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 89 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 90 Expect(checkTargetedOrg).To(BeTrue()) 91 Expect(checkTargetedSpace).To(BeTrue()) 92 }) 93 }) 94 95 When("the user is not logged in", func() { 96 var expectedErr error 97 98 BeforeEach(func() { 99 expectedErr = errors.New("some current user error") 100 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 101 }) 102 103 It("return an error", func() { 104 Expect(executeErr).To(Equal(expectedErr)) 105 }) 106 }) 107 108 It("gets the application", func() { 109 Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 110 inputAppName, inputSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0) 111 Expect(inputAppName).To(Equal(app.Name)) 112 Expect(inputSpaceGUID).To(Equal("some-space-guid")) 113 }) 114 115 When("Getting the application fails", func() { 116 BeforeEach(func() { 117 fakeActor.GetApplicationByNameAndSpaceReturns( 118 resources.Application{}, 119 v7action.Warnings{"get-app-warning"}, 120 errors.New("get-app-error"), 121 ) 122 }) 123 124 It("displays all warnings and returns an error", func() { 125 Expect(executeErr).To(MatchError("get-app-error")) 126 Expect(testUI.Err).To(Say("get-app-warning")) 127 }) 128 }) 129 130 Context("a new package is available", func() { 131 BeforeEach(func() { 132 fakeActor.GetUnstagedNewestPackageGUIDReturns("package-guid", v7action.Warnings{}, nil) 133 }) 134 135 It("stages the new package and starts the app with the new droplet", func() { 136 Expect(executeErr).ToNot(HaveOccurred()) 137 Expect(fakeAppStager.StageAndStartCallCount()).To(Equal(1)) 138 139 inputApp, inputSpace, inputOrg, inputPkgGUID, inputStrategy, inputNoWait, inputAppAction := fakeAppStager.StageAndStartArgsForCall(0) 140 Expect(inputApp).To(Equal(app)) 141 Expect(inputSpace).To(Equal(cmd.Config.TargetedSpace())) 142 Expect(inputOrg).To(Equal(cmd.Config.TargetedOrganization())) 143 Expect(inputPkgGUID).To(Equal("package-guid")) 144 Expect(inputStrategy).To(Equal(strategy)) 145 Expect(inputNoWait).To(Equal(noWait)) 146 Expect(inputAppAction).To(Equal(constant.ApplicationRestarting)) 147 }) 148 149 Context("staging and starting the app returns an error", func() { 150 BeforeEach(func() { 151 fakeAppStager.StageAndStartReturns(errors.New("stage-and-start-error")) 152 }) 153 154 It("returns an error", func() { 155 Expect(executeErr).To(MatchError("stage-and-start-error")) 156 }) 157 }) 158 }) 159 160 Context("no new package is available", func() { 161 BeforeEach(func() { 162 fakeActor.GetUnstagedNewestPackageGUIDReturns("", v7action.Warnings{}, nil) 163 }) 164 165 It("starts the app with the current droplet", func() { 166 Expect(executeErr).ToNot(HaveOccurred()) 167 Expect(fakeAppStager.StartAppCallCount()).To(Equal(1)) 168 169 inputApp, inputDroplet, inputStrategy, inputNoWait, inputSpace, inputOrg, inputAppAction := fakeAppStager.StartAppArgsForCall(0) 170 Expect(inputApp).To(Equal(app)) 171 Expect(inputDroplet).To(Equal(resources.Droplet{})) 172 Expect(inputStrategy).To(Equal(strategy)) 173 Expect(inputNoWait).To(Equal(noWait)) 174 Expect(inputSpace).To(Equal(cmd.Config.TargetedSpace())) 175 Expect(inputOrg).To(Equal(cmd.Config.TargetedOrganization())) 176 Expect(inputAppAction).To(Equal(constant.ApplicationRestarting)) 177 }) 178 179 When("starting the app returns an error", func() { 180 BeforeEach(func() { 181 fakeAppStager.StartAppReturns(errors.New("start-error")) 182 }) 183 184 It("returns an error", func() { 185 Expect(executeErr).To(MatchError("start-error")) 186 }) 187 }) 188 }) 189 190 })