github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/purge_service_instance_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/v7" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 11 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 12 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("purge-service-instance command", func() { 19 const ( 20 serviceInstanceName = "service-instance-name" 21 orgName = "fake-org-name" 22 spaceName = "fake-space-name" 23 spaceGUID = "fake-space-guid" 24 username = "fake-username" 25 ) 26 27 var ( 28 input *Buffer 29 testUI *ui.UI 30 fakeConfig *commandfakes.FakeConfig 31 fakeSharedActor *commandfakes.FakeSharedActor 32 fakeActor *v7fakes.FakeActor 33 cmd PurgeServiceInstanceCommand 34 executeErr error 35 ) 36 37 testActorInteractions := func() { 38 It("delegates to the actor", func() { 39 Expect(fakeActor.PurgeServiceInstanceCallCount()).To(Equal(1)) 40 actualName, actualSpaceGUID := fakeActor.PurgeServiceInstanceArgsForCall(0) 41 Expect(actualName).To(Equal(serviceInstanceName)) 42 Expect(actualSpaceGUID).To(Equal(spaceGUID)) 43 }) 44 45 When("the service instance did not exist", func() { 46 BeforeEach(func() { 47 fakeActor.PurgeServiceInstanceReturns( 48 v7action.Warnings{"purge warning"}, 49 actionerror.ServiceInstanceNotFoundError{}, 50 ) 51 }) 52 53 It("succeeds with a message", func() { 54 Expect(executeErr).NotTo(HaveOccurred()) 55 Expect(testUI.Err).To(Say("purge warning")) 56 Expect(testUI.Out).To(SatisfyAll( 57 Say("\n"), 58 Say(`Service instance %s did not exist\.\n`, serviceInstanceName), 59 Say("OK\n"), 60 )) 61 }) 62 }) 63 64 When("the service instance is successfully purged", func() { 65 BeforeEach(func() { 66 fakeActor.PurgeServiceInstanceReturns( 67 v7action.Warnings{"purge warning"}, 68 nil, 69 ) 70 }) 71 72 It("succeeds with a message", func() { 73 Expect(executeErr).NotTo(HaveOccurred()) 74 Expect(testUI.Err).To(Say("purge warning")) 75 Expect(testUI.Out).To(SatisfyAll( 76 Say("\n"), 77 Say(`Service instance %s purged\.\n`, serviceInstanceName), 78 Say("OK\n"), 79 )) 80 }) 81 }) 82 83 When("the actor returns an error", func() { 84 BeforeEach(func() { 85 fakeActor.PurgeServiceInstanceReturns( 86 v7action.Warnings{"purge warning"}, 87 errors.New("bang"), 88 ) 89 }) 90 91 It("fails with warnings", func() { 92 Expect(executeErr).To(MatchError("bang")) 93 Expect(testUI.Err).To(Say("purge warning")) 94 Expect(testUI.Out).NotTo(Say("OK")) 95 }) 96 }) 97 } 98 99 confirmYes := func() { 100 _, err := input.Write([]byte("y\n")) 101 Expect(err).NotTo(HaveOccurred()) 102 } 103 104 BeforeEach(func() { 105 input = NewBuffer() 106 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 107 fakeConfig = new(commandfakes.FakeConfig) 108 fakeSharedActor = new(commandfakes.FakeSharedActor) 109 fakeActor = new(v7fakes.FakeActor) 110 111 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: orgName}) 112 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: spaceName, GUID: spaceGUID}) 113 fakeActor.GetCurrentUserReturns(configv3.User{Name: username}, nil) 114 115 cmd = PurgeServiceInstanceCommand{ 116 BaseCommand: BaseCommand{ 117 UI: testUI, 118 Config: fakeConfig, 119 SharedActor: fakeSharedActor, 120 Actor: fakeActor, 121 }, 122 } 123 124 setPositionalFlags(&cmd, serviceInstanceName) 125 126 _ = executeErr 127 }) 128 129 JustBeforeEach(func() { 130 executeErr = cmd.Execute(nil) 131 }) 132 133 It("checks the user is logged in, and targeting an org and space", func() { 134 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 135 orgChecked, spaceChecked := fakeSharedActor.CheckTargetArgsForCall(0) 136 Expect(orgChecked).To(BeTrue()) 137 Expect(spaceChecked).To(BeTrue()) 138 }) 139 140 It("prompts the user", func() { 141 Expect(testUI.Out).To(SatisfyAll( 142 Say(`WARNING: This operation assumes that the service broker responsible for this service instance is no longer available or is not responding with a 200 or 410, and the service instance has been deleted, leaving orphan records in Cloud Foundry's database. All knowledge of the service instance will be removed from Cloud Foundry, including service bindings and service keys.\n`), 143 Say(`\n`), 144 Say(`Really purge service instance %s from Cloud Foundry\? \[yN\]:`, serviceInstanceName), 145 )) 146 }) 147 148 When("the user says yes", func() { 149 BeforeEach(func() { 150 confirmYes() 151 }) 152 153 It("outputs the attempted operation", func() { 154 Expect(testUI.Out).To(SatisfyAll( 155 Say(`Purging service instance %s in org %s / space %s as %s\.\.\.\n`, serviceInstanceName, orgName, spaceName, username), 156 Say(`\n`), 157 )) 158 }) 159 160 testActorInteractions() 161 }) 162 163 When("the user says no", func() { 164 BeforeEach(func() { 165 _, err := input.Write([]byte("n\n")) 166 Expect(err).NotTo(HaveOccurred()) 167 }) 168 169 It("does not call the actor", func() { 170 Expect(fakeActor.PurgeServiceInstanceCallCount()).To(BeZero()) 171 }) 172 173 It("says the delete was cancelled", func() { 174 Expect(executeErr).NotTo(HaveOccurred()) 175 Expect(testUI.Out).To(Say("Purge cancelled\n")) 176 }) 177 }) 178 179 When("the -f flag is specified", func() { 180 BeforeEach(func() { 181 setFlag(&cmd, "-f") 182 }) 183 184 It("does not prompt the user", func() { 185 Expect(testUI.Out).NotTo(Say("Really purge")) 186 }) 187 188 testActorInteractions() 189 }) 190 191 Context("errors", func() { 192 When("checking the target returns an error", func() { 193 BeforeEach(func() { 194 fakeSharedActor.CheckTargetReturns(errors.New("explode")) 195 }) 196 197 It("returns the error", func() { 198 Expect(executeErr).To(MatchError("explode")) 199 }) 200 }) 201 202 When("getting the username fails", func() { 203 BeforeEach(func() { 204 fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("boom")) 205 confirmYes() 206 }) 207 208 It("returns the error", func() { 209 Expect(executeErr).To(MatchError("boom")) 210 }) 211 }) 212 }) 213 })