github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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/cf/util/testhelpers/commands"
    15  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    16  	testterm "code.cloudfoundry.org/cli/cf/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/cf/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  		It("does not fail when an org is not targeted and --orglevel is an argument", func() {
    68  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not logged in"})
    69  
    70  			Expect(runCommand("--orglevel")).To(BeTrue())
    71  		})
    72  
    73  		Context("when arguments are provided", func() {
    74  			var cmd commandregistry.Command
    75  			var flagContext flags.FlagContext
    76  
    77  			BeforeEach(func() {
    78  				cmd = &route.ListRoutes{}
    79  				cmd.SetDependency(deps, false)
    80  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    81  			})
    82  
    83  			It("should fail with usage", func() {
    84  				flagContext.Parse("blahblah")
    85  
    86  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    87  				Expect(err).NotTo(HaveOccurred())
    88  
    89  				err = testcmd.RunRequirements(reqs)
    90  				Expect(err).To(HaveOccurred())
    91  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    92  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    93  			})
    94  		})
    95  	})
    96  
    97  	Context("when there are routes", func() {
    98  		BeforeEach(func() {
    99  			cookieClickerGUID := "cookie-clicker-guid"
   100  
   101  			domainRepo.ListDomainsForOrgStub = func(_ string, cb func(models.DomainFields) bool) error {
   102  				tcpDomain := models.DomainFields{
   103  					GUID:            cookieClickerGUID,
   104  					RouterGroupType: "tcp",
   105  				}
   106  				cb(tcpDomain)
   107  				return nil
   108  			}
   109  
   110  			routeRepo.ListRoutesStub = func(cb func(models.Route) bool) error {
   111  				app1 := models.ApplicationFields{Name: "dora"}
   112  				app2 := models.ApplicationFields{Name: "bora"}
   113  
   114  				route := models.Route{
   115  					Space: models.SpaceFields{
   116  						Name: "my-space",
   117  					},
   118  					Host:   "hostname-1",
   119  					Domain: models.DomainFields{Name: "example.com"},
   120  					Apps:   []models.ApplicationFields{app1},
   121  					ServiceInstance: models.ServiceInstanceFields{
   122  						Name: "test-service",
   123  						GUID: "service-guid",
   124  					},
   125  				}
   126  
   127  				route2 := models.Route{
   128  					Space: models.SpaceFields{
   129  						Name: "my-space",
   130  					},
   131  					Host:   "hostname-2",
   132  					Path:   "/foo",
   133  					Domain: models.DomainFields{Name: "cookieclicker.co"},
   134  					Apps:   []models.ApplicationFields{app1, app2},
   135  				}
   136  
   137  				route3 := models.Route{
   138  					Space: models.SpaceFields{
   139  						Name: "my-space",
   140  					},
   141  					Domain: models.DomainFields{
   142  						GUID: cookieClickerGUID,
   143  						Name: "cookieclicker.co",
   144  					},
   145  					Apps: []models.ApplicationFields{app1, app2},
   146  					Port: 9090,
   147  				}
   148  
   149  				cb(route)
   150  				cb(route2)
   151  				cb(route3)
   152  
   153  				return nil
   154  			}
   155  		})
   156  
   157  		It("lists routes", func() {
   158  			runCommand()
   159  
   160  			Expect(ui.Outputs()).To(BeInDisplayOrder(
   161  				[]string{"Getting routes for org my-org / space my-space as my-user ..."},
   162  				[]string{"space", "host", "domain", "port", "path", "type", "apps", "service"},
   163  			))
   164  
   165  			Expect(terminal.Decolorize(ui.Outputs()[3])).To(MatchRegexp(`^my-space\s+hostname-1\s+example.com\s+dora\s+test-service\s*$`))
   166  			Expect(terminal.Decolorize(ui.Outputs()[4])).To(MatchRegexp(`^my-space\s+hostname-2\s+cookieclicker\.co\s+/foo\s+dora,bora\s*$`))
   167  			Expect(terminal.Decolorize(ui.Outputs()[5])).To(MatchRegexp(`^my-space\s+cookieclicker\.co\s+9090\s+tcp\s+dora,bora\s*$`))
   168  
   169  		})
   170  	})
   171  
   172  	Context("when there are routes in different spaces", func() {
   173  		BeforeEach(func() {
   174  			routeRepo.ListAllRoutesStub = func(cb func(models.Route) bool) error {
   175  				space1 := models.SpaceFields{Name: "space-1"}
   176  				space2 := models.SpaceFields{Name: "space-2"}
   177  
   178  				domain := models.DomainFields{Name: "example.com"}
   179  				domain2 := models.DomainFields{Name: "cookieclicker.co"}
   180  
   181  				app1 := models.ApplicationFields{Name: "dora"}
   182  				app2 := models.ApplicationFields{Name: "bora"}
   183  
   184  				route := models.Route{}
   185  				route.Host = "hostname-1"
   186  				route.Domain = domain
   187  				route.Apps = []models.ApplicationFields{app1}
   188  				route.Space = space1
   189  				route.ServiceInstance = models.ServiceInstanceFields{
   190  					Name: "test-service",
   191  					GUID: "service-guid",
   192  				}
   193  
   194  				route2 := models.Route{}
   195  				route2.Host = "hostname-2"
   196  				route2.Path = "/foo"
   197  				route2.Domain = domain2
   198  				route2.Apps = []models.ApplicationFields{app1, app2}
   199  				route2.Space = space2
   200  
   201  				cb(route)
   202  				cb(route2)
   203  
   204  				return nil
   205  			}
   206  		})
   207  
   208  		It("lists routes at orglevel", func() {
   209  			runCommand("--orglevel")
   210  
   211  			Expect(ui.Outputs()).To(ContainSubstrings(
   212  				[]string{"Getting routes for org", "my-org", "my-user"},
   213  				[]string{"space", "host", "domain", "apps", "service"},
   214  				[]string{"space-1", "hostname-1", "example.com", "dora", "test-service"},
   215  				[]string{"space-2", "hostname-2", "cookieclicker.co", "dora", "bora"},
   216  			))
   217  		})
   218  	})
   219  
   220  	Context("when there are not routes", func() {
   221  		It("tells the user when no routes were found", func() {
   222  			runCommand()
   223  
   224  			Expect(ui.Outputs()).To(ContainSubstrings(
   225  				[]string{"Getting routes"},
   226  				[]string{"No routes found"},
   227  			))
   228  		})
   229  	})
   230  
   231  	Context("when there is an error listing routes", func() {
   232  		BeforeEach(func() {
   233  			routeRepo.ListRoutesReturns(errors.New("an-error"))
   234  		})
   235  
   236  		It("returns an error to the user", func() {
   237  			runCommand()
   238  
   239  			Expect(ui.Outputs()).To(ContainSubstrings(
   240  				[]string{"Getting routes"},
   241  				[]string{"FAILED"},
   242  			))
   243  		})
   244  	})
   245  })