github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/upgrade_service_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror" 7 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 8 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 9 "github.com/LukasHeimann/cloudfoundrycli/v8/command/translatableerror" 10 . "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 11 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 12 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 13 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("upgrade-service command", func() { 20 const ( 21 serviceInstanceName = "fake-service-instance-name" 22 spaceName = "fake-space-name" 23 spaceGUID = "fake-space-guid" 24 orgName = "fake-org-name" 25 username = "fake-username" 26 ) 27 28 var ( 29 input *Buffer 30 testUI *ui.UI 31 fakeConfig *commandfakes.FakeConfig 32 fakeSharedActor *commandfakes.FakeSharedActor 33 fakeActor *v7fakes.FakeActor 34 cmd UpgradeServiceCommand 35 executeErr error 36 ) 37 38 BeforeEach(func() { 39 input = NewBuffer() 40 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 41 fakeConfig = new(commandfakes.FakeConfig) 42 fakeSharedActor = new(commandfakes.FakeSharedActor) 43 fakeActor = new(v7fakes.FakeActor) 44 45 cmd = UpgradeServiceCommand{ 46 BaseCommand: BaseCommand{ 47 UI: testUI, 48 Config: fakeConfig, 49 SharedActor: fakeSharedActor, 50 Actor: fakeActor, 51 }, 52 } 53 54 setPositionalFlags(&cmd, serviceInstanceName) 55 56 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: orgName}) 57 fakeConfig.TargetedSpaceReturns(configv3.Space{ 58 Name: spaceName, 59 GUID: spaceGUID, 60 }) 61 fakeActor.GetCurrentUserReturns(configv3.User{Name: username}, nil) 62 }) 63 64 JustBeforeEach(func() { 65 executeErr = cmd.Execute(nil) 66 }) 67 68 testActorInteractions := func() { 69 It("delegates to the actor", func() { 70 Expect(fakeActor.UpgradeManagedServiceInstanceCallCount()).To(Equal(1)) 71 actualName, actualSpaceGUID := fakeActor.UpgradeManagedServiceInstanceArgsForCall(0) 72 Expect(actualName).To(Equal(serviceInstanceName)) 73 Expect(actualSpaceGUID).To(Equal(spaceGUID)) 74 }) 75 76 When("the service instance does not exist", func() { 77 BeforeEach(func() { 78 fakeActor.UpgradeManagedServiceInstanceReturns( 79 v7action.Warnings{"upgrade warning"}, 80 actionerror.ServiceInstanceNotFoundError{Name: serviceInstanceName}, 81 ) 82 }) 83 84 It("prints warnings and returns a translatable error", func() { 85 Expect(testUI.Err).To(Say("upgrade warning")) 86 Expect(executeErr).To(MatchError(translatableerror.ServiceInstanceNotFoundError{ 87 Name: serviceInstanceName, 88 })) 89 }) 90 }) 91 92 When("the service instance upgrade starts successfully", func() { 93 BeforeEach(func() { 94 fakeActor.UpgradeManagedServiceInstanceReturns( 95 v7action.Warnings{"upgrade warning"}, 96 nil, 97 ) 98 }) 99 100 It("succeeds with a message", func() { 101 Expect(executeErr).NotTo(HaveOccurred()) 102 Expect(testUI.Err).To(Say("upgrade warning")) 103 Expect(testUI.Out).To(SatisfyAll( 104 Say("\n"), 105 Say(`Upgrade in progress. Use 'cf services' or 'cf service %s' to check operation status\.\n`, serviceInstanceName), 106 Say("OK\n"), 107 )) 108 }) 109 }) 110 111 When("the actor returns an unexpected error", func() { 112 BeforeEach(func() { 113 fakeActor.UpgradeManagedServiceInstanceReturns( 114 v7action.Warnings{"upgrade warning"}, 115 errors.New("bang"), 116 ) 117 }) 118 119 It("fails with warnings", func() { 120 Expect(executeErr).To(MatchError("bang")) 121 Expect(testUI.Err).To(Say("upgrade warning")) 122 Expect(testUI.Out).NotTo(Say("OK")) 123 }) 124 }) 125 } 126 127 It("checks the user is logged in, and targeting an org and space", func() { 128 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 129 orgChecked, spaceChecked := fakeSharedActor.CheckTargetArgsForCall(0) 130 Expect(orgChecked).To(BeTrue()) 131 Expect(spaceChecked).To(BeTrue()) 132 }) 133 134 It("prompts the user for confirmation", func() { 135 Expect(testUI.Out).To(SatisfyAll( 136 Say(`Warning: This operation may be long running and will block further operations on the service instance until it's completed`), 137 Say(`Do you really want to upgrade the service instance %s\? \[yN\]:`, serviceInstanceName), 138 )) 139 }) 140 141 When("the user confirms when prompted", func() { 142 BeforeEach(func() { 143 _, err := input.Write([]byte("y\n")) 144 Expect(err).NotTo(HaveOccurred()) 145 }) 146 147 It("outputs the attempted operation", func() { 148 Expect(testUI.Out).To(SatisfyAll( 149 Say(`Upgrading service instance %s in org %s / space %s as %s\.\.\.\n`, serviceInstanceName, orgName, spaceName, username), 150 Say(`\n`), 151 )) 152 }) 153 154 testActorInteractions() 155 }) 156 157 When("the user cancels when prompted", func() { 158 BeforeEach(func() { 159 _, err := input.Write([]byte("n\n")) 160 Expect(err).NotTo(HaveOccurred()) 161 }) 162 163 It("does not call the actor", func() { 164 Expect(fakeActor.DeleteServiceInstanceCallCount()).To(BeZero()) 165 }) 166 167 It("outputs that the upgrade was cancelled", func() { 168 Expect(executeErr).NotTo(HaveOccurred()) 169 Expect(testUI.Out).To(Say("Upgrade cancelled\n")) 170 }) 171 }) 172 173 When("the -f flags is passed", func() { 174 BeforeEach(func() { 175 setFlag(&cmd, "-f") 176 }) 177 178 It("does not prompt the user", func() { 179 Expect(testUI.Out).NotTo(Say("Do you really want")) 180 }) 181 182 testActorInteractions() 183 }) 184 185 When("checking the target returns an error", func() { 186 It("returns the error", func() { 187 fakeSharedActor.CheckTargetReturns(errors.New("explode")) 188 executeErr := cmd.Execute(nil) 189 Expect(executeErr).To(MatchError("explode")) 190 }) 191 }) 192 })