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