github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/routes_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/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v7" 10 "code.cloudfoundry.org/cli/command/v7/v7fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("routes Command", func() { 20 var ( 21 cmd RoutesCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeSharedActor *commandfakes.FakeSharedActor 25 fakeActor *v7fakes.FakeRoutesActor 26 executeErr error 27 orglevel bool 28 args []string 29 binaryName string 30 ) 31 32 const tableHeaders = `space\s+host\s+domain\s+path` 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v7fakes.FakeRoutesActor) 39 args = nil 40 41 binaryName = "faceman" 42 fakeConfig.BinaryNameReturns(binaryName) 43 }) 44 45 JustBeforeEach(func() { 46 cmd = RoutesCommand{ 47 UI: testUI, 48 Config: fakeConfig, 49 SharedActor: fakeSharedActor, 50 Actor: fakeActor, 51 Orglevel: orglevel, 52 } 53 executeErr = cmd.Execute(args) 54 }) 55 56 When("the environment is not setup correctly", func() { 57 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: binaryName})) 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 When("when there is no org targeted", func() { 73 BeforeEach(func() { 74 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 75 }) 76 77 It("returns an error", func() { 78 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 79 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 80 Expect(checkTargetedOrg).To(BeTrue()) 81 Expect(checkTargetedSpace).To(BeTrue()) 82 }) 83 }) 84 }) 85 86 Context("When the environment is setup correctly", func() { 87 BeforeEach(func() { 88 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 89 }) 90 91 When("Orglevel is not passed", func() { 92 BeforeEach(func() { 93 orglevel = false 94 }) 95 When("GetRoutesBySpace returns an error", func() { 96 var expectedErr error 97 98 BeforeEach(func() { 99 warnings := v7action.Warnings{"warning-1", "warning-2"} 100 expectedErr = errors.New("some-error") 101 fakeActor.GetRoutesBySpaceReturns(nil, warnings, expectedErr) 102 }) 103 104 It("prints that error with warnings", func() { 105 Expect(executeErr).To(Equal(expectedErr)) 106 107 Expect(testUI.Err).To(Say("warning-1")) 108 Expect(testUI.Err).To(Say("warning-2")) 109 Expect(testUI.Out).ToNot(Say(tableHeaders)) 110 }) 111 }) 112 113 When("GetRoutesBySpace returns some routes", func() { 114 var routes []v7action.Route 115 116 BeforeEach(func() { 117 routes = []v7action.Route{ 118 {DomainName: "domain1", GUID: "route-guid-1", SpaceName: "space-1"}, 119 {DomainName: "domain2", GUID: "route-guid-2", SpaceName: "space-2", Host: "host-2", Path: "/path/2"}, 120 {DomainName: "domain3", GUID: "route-guid-3", SpaceName: "space-3", Host: "host-3"}, 121 } 122 123 fakeActor.GetRoutesBySpaceReturns( 124 routes, 125 v7action.Warnings{"actor-warning-1", "actor-warning-2", "actor-warning-3"}, 126 nil, 127 ) 128 129 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 130 GUID: "some-org-guid", 131 Name: "some-org", 132 }) 133 134 fakeConfig.TargetedSpaceReturns(configv3.Space{ 135 GUID: "some-space-guid", 136 Name: "some-space", 137 }) 138 }) 139 140 It("asks the RoutesActor for a list of routes", func() { 141 Expect(fakeActor.GetRoutesBySpaceCallCount()).To(Equal(1)) 142 }) 143 144 It("prints warnings", func() { 145 Expect(testUI.Err).To(Say("actor-warning-1")) 146 Expect(testUI.Err).To(Say("actor-warning-2")) 147 Expect(testUI.Err).To(Say("actor-warning-3")) 148 }) 149 150 It("prints the list of routes", func() { 151 Expect(executeErr).NotTo(HaveOccurred()) 152 Expect(testUI.Out).To(Say(tableHeaders)) 153 Expect(testUI.Out).To(Say(`space-1\s+domain1`)) 154 Expect(testUI.Out).To(Say(`space-2\s+host-2\s+domain2\s+\/path\/2`)) 155 Expect(testUI.Out).To(Say(`space-3\s+host-3\s+domain3`)) 156 }) 157 158 It("prints the flavor text", func() { 159 Expect(testUI.Out).To(Say("Getting routes for org some-org / space some-space as banana...\n\n")) 160 }) 161 }) 162 163 When("GetRoutesBySpace returns no routes", func() { 164 var routes []v7action.Route 165 166 BeforeEach(func() { 167 routes = []v7action.Route{} 168 169 fakeActor.GetRoutesBySpaceReturns( 170 routes, 171 v7action.Warnings{"actor-warning-1", "actor-warning-2", "actor-warning-3"}, 172 nil, 173 ) 174 175 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 176 GUID: "some-org-guid", 177 Name: "some-org", 178 }) 179 fakeConfig.TargetedSpaceReturns(configv3.Space{ 180 GUID: "some-space-guid", 181 Name: "some-space", 182 }) 183 }) 184 185 It("asks the RoutesActor for a list of routes", func() { 186 Expect(fakeActor.GetRoutesBySpaceCallCount()).To(Equal(1)) 187 }) 188 189 It("prints warnings", func() { 190 Expect(testUI.Err).To(Say("actor-warning-1")) 191 Expect(testUI.Err).To(Say("actor-warning-2")) 192 Expect(testUI.Err).To(Say("actor-warning-3")) 193 }) 194 195 It("does not print table headers", func() { 196 Expect(testUI.Out).NotTo(Say(tableHeaders)) 197 }) 198 199 It("prints a message indicating that no routes were found", func() { 200 Expect(executeErr).NotTo(HaveOccurred()) 201 Expect(testUI.Out).To(Say("No routes found.")) 202 }) 203 204 It("prints the flavor text", func() { 205 Expect(testUI.Out).To(Say("Getting routes for org some-org / space some-space as banana...\n\n")) 206 }) 207 }) 208 }) 209 210 When("Orglevel is passed", func() { 211 BeforeEach(func() { 212 orglevel = true 213 }) 214 215 When("GetRoutesByOrg returns an error", func() { 216 var expectedErr error 217 218 BeforeEach(func() { 219 warnings := v7action.Warnings{"warning-1", "warning-2"} 220 expectedErr = errors.New("some-error") 221 fakeActor.GetRoutesByOrgReturns(nil, warnings, expectedErr) 222 }) 223 224 It("prints that error with warnings", func() { 225 Expect(executeErr).To(Equal(expectedErr)) 226 Expect(testUI.Err).To(Say("warning-1")) 227 Expect(testUI.Err).To(Say("warning-2")) 228 Expect(testUI.Out).ToNot(Say(tableHeaders)) 229 }) 230 }) 231 232 When("GetRoutesByOrg returns some routes", func() { 233 var routes []v7action.Route 234 235 BeforeEach(func() { 236 routes = []v7action.Route{ 237 {DomainName: "domain1", GUID: "route-guid-1", SpaceName: "space-1"}, 238 {DomainName: "domain2", GUID: "route-guid-2", SpaceName: "space-2", Host: "host-2", Path: "/path/2"}, 239 {DomainName: "domain3", GUID: "route-guid-3", SpaceName: "space-3", Host: "host-3"}, 240 } 241 242 fakeActor.GetRoutesByOrgReturns( 243 routes, 244 v7action.Warnings{"actor-warning-1", "actor-warning-2", "actor-warning-3"}, 245 nil, 246 ) 247 248 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 249 GUID: "some-org-guid", 250 Name: "some-org", 251 }) 252 }) 253 254 It("asks the RoutesActor for a list of routes", func() { 255 Expect(fakeActor.GetRoutesByOrgCallCount()).To(Equal(1)) 256 }) 257 258 It("prints warnings", func() { 259 Expect(testUI.Err).To(Say("actor-warning-1")) 260 Expect(testUI.Err).To(Say("actor-warning-2")) 261 Expect(testUI.Err).To(Say("actor-warning-3")) 262 }) 263 264 It("prints the list of routes", func() { 265 Expect(executeErr).NotTo(HaveOccurred()) 266 Expect(testUI.Out).To(Say(tableHeaders)) 267 Expect(testUI.Out).To(Say(`space-1\s+domain1`)) 268 Expect(testUI.Out).To(Say(`space-2\s+host-2\s+domain2\s+\/path\/2`)) 269 Expect(testUI.Out).To(Say(`space-3\s+host-3\s+domain3`)) 270 }) 271 272 It("prints the flavor text", func() { 273 Expect(testUI.Out).To(Say("Getting routes for org some-org as banana...\n\n")) 274 }) 275 }) 276 277 When("GetRoutesByOrg returns no routes", func() { 278 var routes []v7action.Route 279 280 BeforeEach(func() { 281 routes = []v7action.Route{} 282 283 fakeActor.GetRoutesByOrgReturns( 284 routes, 285 v7action.Warnings{"actor-warning-1", "actor-warning-2", "actor-warning-3"}, 286 nil, 287 ) 288 289 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 290 GUID: "some-org-guid", 291 Name: "some-org", 292 }) 293 }) 294 295 It("asks the RoutesActor for a list of routes", func() { 296 Expect(fakeActor.GetRoutesByOrgCallCount()).To(Equal(1)) 297 }) 298 299 It("prints warnings", func() { 300 Expect(testUI.Err).To(Say("actor-warning-1")) 301 Expect(testUI.Err).To(Say("actor-warning-2")) 302 Expect(testUI.Err).To(Say("actor-warning-3")) 303 }) 304 305 It("does not print table headers", func() { 306 Expect(testUI.Out).NotTo(Say(tableHeaders)) 307 }) 308 309 It("prints a message indicating that no routes were found", func() { 310 Expect(executeErr).NotTo(HaveOccurred()) 311 Expect(testUI.Out).To(Say("No routes found.")) 312 }) 313 314 It("prints the flavor text", func() { 315 Expect(testUI.Out).To(Say("Getting routes for org some-org as banana...\n\n")) 316 }) 317 }) 318 }) 319 }) 320 })