github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/route/routes_test.go (about) 1 package route_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/api/apifakes" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/flags" 10 "code.cloudfoundry.org/cli/cf/models" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 15 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 16 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 20 "code.cloudfoundry.org/cli/cf/commands/route" 21 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 22 ) 23 24 var _ = Describe("routes command", func() { 25 var ( 26 ui *testterm.FakeUI 27 routeRepo *apifakes.FakeRouteRepository 28 domainRepo *apifakes.FakeDomainRepository 29 configRepo coreconfig.Repository 30 requirementsFactory *requirementsfakes.FakeFactory 31 deps commandregistry.Dependency 32 ) 33 34 updateCommandDependency := func(pluginCall bool) { 35 deps.UI = ui 36 deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo).SetDomainRepository(domainRepo) 37 deps.Config = configRepo 38 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("routes").SetDependency(deps, pluginCall)) 39 } 40 41 BeforeEach(func() { 42 ui = &testterm.FakeUI{} 43 configRepo = testconfig.NewRepositoryWithDefaults() 44 requirementsFactory = new(requirementsfakes.FakeFactory) 45 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 46 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 47 routeRepo = new(apifakes.FakeRouteRepository) 48 domainRepo = new(apifakes.FakeDomainRepository) 49 }) 50 51 runCommand := func(args ...string) bool { 52 return testcmd.RunCLICommand("routes", args, requirementsFactory, updateCommandDependency, false, ui) 53 } 54 55 Describe("login requirements", func() { 56 It("fails if the user is not logged in", func() { 57 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 58 Expect(runCommand()).To(BeFalse()) 59 }) 60 61 It("fails when an org and space is not targeted", func() { 62 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not logged in"}) 63 64 Expect(runCommand()).To(BeFalse()) 65 }) 66 67 Context("when arguments are provided", func() { 68 var cmd commandregistry.Command 69 var flagContext flags.FlagContext 70 71 BeforeEach(func() { 72 cmd = &route.ListRoutes{} 73 cmd.SetDependency(deps, false) 74 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 75 }) 76 77 It("should fail with usage", func() { 78 flagContext.Parse("blahblah") 79 80 reqs, err := cmd.Requirements(requirementsFactory, flagContext) 81 Expect(err).NotTo(HaveOccurred()) 82 83 err = testcmd.RunRequirements(reqs) 84 Expect(err).To(HaveOccurred()) 85 Expect(err.Error()).To(ContainSubstring("Incorrect Usage")) 86 Expect(err.Error()).To(ContainSubstring("No argument required")) 87 }) 88 }) 89 }) 90 91 Context("when there are routes", func() { 92 BeforeEach(func() { 93 cookieClickerGUID := "cookie-clicker-guid" 94 95 domainRepo.ListDomainsForOrgStub = func(_ string, cb func(models.DomainFields) bool) error { 96 tcpDomain := models.DomainFields{ 97 GUID: cookieClickerGUID, 98 RouterGroupType: "tcp", 99 } 100 cb(tcpDomain) 101 return nil 102 } 103 104 routeRepo.ListRoutesStub = func(cb func(models.Route) bool) error { 105 app1 := models.ApplicationFields{Name: "dora"} 106 app2 := models.ApplicationFields{Name: "bora"} 107 108 route := models.Route{ 109 Space: models.SpaceFields{ 110 Name: "my-space", 111 }, 112 Host: "hostname-1", 113 Domain: models.DomainFields{Name: "example.com"}, 114 Apps: []models.ApplicationFields{app1}, 115 ServiceInstance: models.ServiceInstanceFields{ 116 Name: "test-service", 117 GUID: "service-guid", 118 }, 119 } 120 121 route2 := models.Route{ 122 Space: models.SpaceFields{ 123 Name: "my-space", 124 }, 125 Host: "hostname-2", 126 Path: "/foo", 127 Domain: models.DomainFields{Name: "cookieclicker.co"}, 128 Apps: []models.ApplicationFields{app1, app2}, 129 } 130 131 route3 := models.Route{ 132 Space: models.SpaceFields{ 133 Name: "my-space", 134 }, 135 Domain: models.DomainFields{ 136 GUID: cookieClickerGUID, 137 Name: "cookieclicker.co", 138 }, 139 Apps: []models.ApplicationFields{app1, app2}, 140 Port: 9090, 141 } 142 143 cb(route) 144 cb(route2) 145 cb(route3) 146 147 return nil 148 } 149 }) 150 151 It("lists routes", func() { 152 runCommand() 153 154 Expect(ui.Outputs()).To(BeInDisplayOrder( 155 []string{"Getting routes for org my-org / space my-space as my-user ..."}, 156 []string{"space", "host", "domain", "port", "path", "type", "apps", "service"}, 157 )) 158 159 Expect(terminal.Decolorize(ui.Outputs()[3])).To(MatchRegexp(`^my-space\s+hostname-1\s+example.com\s+dora\s+test-service\s*$`)) 160 Expect(terminal.Decolorize(ui.Outputs()[4])).To(MatchRegexp(`^my-space\s+hostname-2\s+cookieclicker\.co\s+/foo\s+dora,bora\s*$`)) 161 Expect(terminal.Decolorize(ui.Outputs()[5])).To(MatchRegexp(`^my-space\s+cookieclicker\.co\s+9090\s+tcp\s+dora,bora\s*$`)) 162 163 }) 164 }) 165 166 Context("when there are routes in different spaces", func() { 167 BeforeEach(func() { 168 routeRepo.ListAllRoutesStub = func(cb func(models.Route) bool) error { 169 space1 := models.SpaceFields{Name: "space-1"} 170 space2 := models.SpaceFields{Name: "space-2"} 171 172 domain := models.DomainFields{Name: "example.com"} 173 domain2 := models.DomainFields{Name: "cookieclicker.co"} 174 175 app1 := models.ApplicationFields{Name: "dora"} 176 app2 := models.ApplicationFields{Name: "bora"} 177 178 route := models.Route{} 179 route.Host = "hostname-1" 180 route.Domain = domain 181 route.Apps = []models.ApplicationFields{app1} 182 route.Space = space1 183 route.ServiceInstance = models.ServiceInstanceFields{ 184 Name: "test-service", 185 GUID: "service-guid", 186 } 187 188 route2 := models.Route{} 189 route2.Host = "hostname-2" 190 route2.Path = "/foo" 191 route2.Domain = domain2 192 route2.Apps = []models.ApplicationFields{app1, app2} 193 route2.Space = space2 194 195 cb(route) 196 cb(route2) 197 198 return nil 199 } 200 }) 201 202 It("lists routes at orglevel", func() { 203 runCommand("--orglevel") 204 205 Expect(ui.Outputs()).To(ContainSubstrings( 206 []string{"Getting routes for org", "my-org", "my-user"}, 207 []string{"space", "host", "domain", "apps", "service"}, 208 []string{"space-1", "hostname-1", "example.com", "dora", "test-service"}, 209 []string{"space-2", "hostname-2", "cookieclicker.co", "dora", "bora"}, 210 )) 211 }) 212 }) 213 214 Context("when there are not routes", func() { 215 It("tells the user when no routes were found", func() { 216 runCommand() 217 218 Expect(ui.Outputs()).To(ContainSubstrings( 219 []string{"Getting routes"}, 220 []string{"No routes found"}, 221 )) 222 }) 223 }) 224 225 Context("when there is an error listing routes", func() { 226 BeforeEach(func() { 227 routeRepo.ListRoutesReturns(errors.New("an-error")) 228 }) 229 230 It("returns an error to the user", func() { 231 runCommand() 232 233 Expect(ui.Outputs()).To(ContainSubstrings( 234 []string{"Getting routes"}, 235 []string{"FAILED"}, 236 )) 237 }) 238 }) 239 })