github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/command/v2/delete_orphaned_routes_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 "code.cloudfoundry.org/cli/command/v2" 10 "code.cloudfoundry.org/cli/command/v2/v2fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("deleted-orphaned-routes Command", func() { 19 var ( 20 cmd v2.DeleteOrphanedRoutesCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v2fakes.FakeDeleteOrphanedRoutesActor 25 input *Buffer 26 binaryName string 27 executeErr error 28 ) 29 30 BeforeEach(func() { 31 input = NewBuffer() 32 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 33 fakeConfig = new(commandfakes.FakeConfig) 34 fakeSharedActor = new(commandfakes.FakeSharedActor) 35 fakeActor = new(v2fakes.FakeDeleteOrphanedRoutesActor) 36 37 cmd = v2.DeleteOrphanedRoutesCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 } 43 44 binaryName = "faceman" 45 fakeConfig.BinaryNameReturns(binaryName) 46 }) 47 48 JustBeforeEach(func() { 49 executeErr = cmd.Execute(nil) 50 }) 51 52 Context("when a cloud controller API endpoint is set", func() { 53 BeforeEach(func() { 54 fakeConfig.TargetReturns("some-url") 55 }) 56 57 Context("when checking target fails", func() { 58 BeforeEach(func() { 59 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 60 }) 61 62 It("returns an error", func() { 63 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"})) 64 65 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 66 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 67 Expect(checkTargetedOrg).To(BeTrue()) 68 Expect(checkTargetedSpace).To(BeTrue()) 69 }) 70 }) 71 72 Context("when the user is logged in, and org and space are targeted", func() { 73 BeforeEach(func() { 74 fakeConfig.HasTargetedOrganizationReturns(true) 75 fakeConfig.HasTargetedSpaceReturns(true) 76 fakeConfig.TargetedSpaceReturns(configv3.Space{ 77 GUID: "some-space-guid", 78 Name: "some-space", 79 }) 80 }) 81 82 Context("when getting the current user returns an error", func() { 83 var expectedErr error 84 85 BeforeEach(func() { 86 expectedErr = errors.New("getting current user error") 87 fakeConfig.CurrentUserReturns( 88 configv3.User{}, 89 expectedErr) 90 }) 91 92 It("returns the error", func() { 93 Expect(executeErr).To(MatchError(expectedErr)) 94 }) 95 }) 96 97 Context("when getting the current user does not return an error", func() { 98 BeforeEach(func() { 99 fakeConfig.CurrentUserReturns( 100 configv3.User{Name: "some-user"}, 101 nil) 102 }) 103 104 Context("when the '-f' flag is provided", func() { 105 BeforeEach(func() { 106 cmd.Force = true 107 }) 108 109 It("does not prompt for user confirmation", func() { 110 Expect(executeErr).ToNot(HaveOccurred()) 111 112 Expect(testUI.Out).ToNot(Say("Really delete orphaned routes\\? \\[yN\\]:")) 113 }) 114 }) 115 116 Context("when the '-f' flag is not provided", func() { 117 Context("when user is prompted for confirmation", func() { 118 BeforeEach(func() { 119 _, err := input.Write([]byte("\n")) 120 Expect(err).NotTo(HaveOccurred()) 121 }) 122 123 It("displays the interactive prompt", func() { 124 Expect(executeErr).ToNot(HaveOccurred()) 125 126 Expect(testUI.Out).To(Say("Really delete orphaned routes\\? \\[yN\\]:")) 127 }) 128 }) 129 130 Context("when the user inputs no", func() { 131 BeforeEach(func() { 132 _, err := input.Write([]byte("n\n")) 133 Expect(err).NotTo(HaveOccurred()) 134 }) 135 136 It("does not delete orphaned routes", func() { 137 Expect(executeErr).ToNot(HaveOccurred()) 138 139 Expect(fakeActor.GetOrphanedRoutesBySpaceCallCount()).To(Equal(0)) 140 Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0)) 141 }) 142 }) 143 144 Context("when the user input is invalid", func() { 145 BeforeEach(func() { 146 _, err := input.Write([]byte("e\n")) 147 Expect(err).NotTo(HaveOccurred()) 148 }) 149 150 It("returns an error", func() { 151 Expect(executeErr).To(HaveOccurred()) 152 153 Expect(fakeActor.GetOrphanedRoutesBySpaceCallCount()).To(Equal(0)) 154 Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0)) 155 }) 156 }) 157 158 Context("when the user inputs yes", func() { 159 var routes []v2action.Route 160 161 BeforeEach(func() { 162 _, err := input.Write([]byte("y\n")) 163 Expect(err).NotTo(HaveOccurred()) 164 165 routes = []v2action.Route{ 166 { 167 GUID: "route-1-guid", 168 Host: "route-1", 169 Domain: v2action.Domain{ 170 Name: "bosh-lite.com", 171 }, 172 Path: "/path", 173 }, 174 { 175 GUID: "route-2-guid", 176 Host: "route-2", 177 Domain: v2action.Domain{ 178 Name: "bosh-lite.com", 179 }, 180 }, 181 } 182 183 fakeActor.GetOrphanedRoutesBySpaceReturns(routes, nil, nil) 184 }) 185 186 It("displays getting routes message", func() { 187 Expect(executeErr).ToNot(HaveOccurred()) 188 189 Expect(testUI.Out).To(Say("Getting routes as some-user ...\n")) 190 }) 191 192 It("deletes the routes and displays that they are deleted", func() { 193 Expect(executeErr).ToNot(HaveOccurred()) 194 195 Expect(fakeActor.GetOrphanedRoutesBySpaceCallCount()).To(Equal(1)) 196 Expect(fakeActor.GetOrphanedRoutesBySpaceArgsForCall(0)).To(Equal("some-space-guid")) 197 Expect(fakeActor.DeleteRouteCallCount()).To(Equal(2)) 198 Expect(fakeActor.DeleteRouteArgsForCall(0)).To(Equal(routes[0].GUID)) 199 Expect(fakeActor.DeleteRouteArgsForCall(1)).To(Equal(routes[1].GUID)) 200 201 Expect(testUI.Out).To(Say("Deleting route route-1.bosh-lite.com/path...")) 202 Expect(testUI.Out).To(Say("Deleting route route-2.bosh-lite.com...")) 203 Expect(testUI.Out).To(Say("OK")) 204 }) 205 206 Context("when there are warnings", func() { 207 BeforeEach(func() { 208 fakeActor.GetOrphanedRoutesBySpaceReturns( 209 []v2action.Route{{GUID: "some-route-guid"}}, 210 []string{"foo", "bar"}, 211 nil) 212 fakeActor.DeleteRouteReturns([]string{"baz"}, nil) 213 }) 214 215 It("displays the warnings", func() { 216 Expect(executeErr).ToNot(HaveOccurred()) 217 218 Expect(testUI.Err).To(Say("foo")) 219 Expect(testUI.Err).To(Say("bar")) 220 Expect(testUI.Err).To(Say("baz")) 221 }) 222 }) 223 224 Context("when getting the routes returns an error", func() { 225 var expectedErr error 226 227 Context("when the error is a DomainNotFoundError", func() { 228 BeforeEach(func() { 229 fakeActor.GetOrphanedRoutesBySpaceReturns( 230 nil, 231 nil, 232 actionerror.DomainNotFoundError{ 233 Name: "some-domain", 234 GUID: "some-domain-guid", 235 }, 236 ) 237 }) 238 239 It("returns translatableerror.DomainNotFoundError", func() { 240 Expect(executeErr).To(MatchError(actionerror.DomainNotFoundError{ 241 Name: "some-domain", 242 GUID: "some-domain-guid", 243 })) 244 }) 245 }) 246 247 Context("when the error is an OrphanedRoutesNotFoundError", func() { 248 BeforeEach(func() { 249 expectedErr = actionerror.OrphanedRoutesNotFoundError{} 250 fakeActor.GetOrphanedRoutesBySpaceReturns(nil, nil, expectedErr) 251 }) 252 253 It("should not return an error and only display 'OK'", func() { 254 Expect(executeErr).ToNot(HaveOccurred()) 255 256 Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0)) 257 }) 258 }) 259 260 Context("when there is a generic error", func() { 261 BeforeEach(func() { 262 expectedErr = errors.New("getting orphaned routes error") 263 fakeActor.GetOrphanedRoutesBySpaceReturns(nil, nil, expectedErr) 264 }) 265 266 It("returns the error", func() { 267 Expect(executeErr).To(MatchError(expectedErr)) 268 }) 269 }) 270 }) 271 272 Context("when deleting a route returns an error", func() { 273 var expectedErr error 274 275 BeforeEach(func() { 276 expectedErr = errors.New("deleting route error") 277 fakeActor.GetOrphanedRoutesBySpaceReturns( 278 []v2action.Route{{GUID: "some-route-guid"}}, 279 nil, 280 nil) 281 fakeActor.DeleteRouteReturns(nil, expectedErr) 282 }) 283 284 It("returns the error", func() { 285 Expect(executeErr).To(MatchError(expectedErr)) 286 }) 287 }) 288 }) 289 }) 290 }) 291 }) 292 }) 293 })