github.com/cloudfoundry/cli@v7.1.0+incompatible/cf/commands/route/map_route_test.go (about)

     1  package route_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/commands/route"
     8  	"code.cloudfoundry.org/cli/cf/commands/route/routefakes"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    14  
    15  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    16  
    17  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    18  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    19  
    20  	"strings"
    21  
    22  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  )
    26  
    27  var _ = Describe("MapRoute", func() {
    28  	var (
    29  		ui         *testterm.FakeUI
    30  		configRepo coreconfig.Repository
    31  		routeRepo  *apifakes.FakeRouteRepository
    32  
    33  		cmd         commandregistry.Command
    34  		deps        commandregistry.Dependency
    35  		factory     *requirementsfakes.FakeFactory
    36  		flagContext flags.FlagContext
    37  
    38  		loginRequirement       requirements.Requirement
    39  		applicationRequirement *requirementsfakes.FakeApplicationRequirement
    40  		domainRequirement      *requirementsfakes.FakeDomainRequirement
    41  
    42  		originalCreateRouteCmd commandregistry.Command
    43  		fakeCreateRouteCmd     commandregistry.Command
    44  
    45  		fakeDomain models.DomainFields
    46  	)
    47  
    48  	BeforeEach(func() {
    49  		ui = &testterm.FakeUI{}
    50  		configRepo = testconfig.NewRepositoryWithDefaults()
    51  		routeRepo = new(apifakes.FakeRouteRepository)
    52  		repoLocator := deps.RepoLocator.SetRouteRepository(routeRepo)
    53  
    54  		deps = commandregistry.Dependency{
    55  			UI:          ui,
    56  			Config:      configRepo,
    57  			RepoLocator: repoLocator,
    58  		}
    59  
    60  		originalCreateRouteCmd = commandregistry.Commands.FindCommand("create-route")
    61  		fakeCreateRouteCmd = new(routefakes.OldFakeRouteCreator)
    62  		commandregistry.Register(fakeCreateRouteCmd)
    63  
    64  		cmd = &route.MapRoute{}
    65  		cmd.SetDependency(deps, false)
    66  
    67  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    68  
    69  		factory = new(requirementsfakes.FakeFactory)
    70  
    71  		loginRequirement = &passingRequirement{Name: "login-requirement"}
    72  		factory.NewLoginRequirementReturns(loginRequirement)
    73  
    74  		applicationRequirement = new(requirementsfakes.FakeApplicationRequirement)
    75  		factory.NewApplicationRequirementReturns(applicationRequirement)
    76  
    77  		fakeApplication := models.Application{}
    78  		fakeApplication.GUID = "fake-app-guid"
    79  		applicationRequirement.GetApplicationReturns(fakeApplication)
    80  
    81  		domainRequirement = new(requirementsfakes.FakeDomainRequirement)
    82  		factory.NewDomainRequirementReturns(domainRequirement)
    83  
    84  		fakeDomain = models.DomainFields{
    85  			GUID: "fake-domain-guid",
    86  			Name: "fake-domain-name",
    87  		}
    88  		domainRequirement.GetDomainReturns(fakeDomain)
    89  	})
    90  
    91  	AfterEach(func() {
    92  		commandregistry.Register(originalCreateRouteCmd)
    93  	})
    94  
    95  	Describe("Help text", func() {
    96  		var usage []string
    97  
    98  		BeforeEach(func() {
    99  			cmd := &route.MapRoute{}
   100  			up := commandregistry.CLICommandUsagePresenter(cmd)
   101  
   102  			usage = strings.Split(up.Usage(), "\n")
   103  		})
   104  
   105  		It("contains an example", func() {
   106  			Expect(usage).To(ContainElement("   cf map-route my-app example.com --port 50000                 # example.com:50000"))
   107  		})
   108  
   109  		It("contains the options", func() {
   110  			Expect(usage).To(ContainElement("   --hostname, -n      Hostname for the HTTP route (required for shared domains)"))
   111  			Expect(usage).To(ContainElement("   --path              Path for the HTTP route"))
   112  			Expect(usage).To(ContainElement("   --port              Port for the TCP route"))
   113  			Expect(usage).To(ContainElement("   --random-port       Create a random port for the TCP route"))
   114  		})
   115  
   116  		It("shows the usage", func() {
   117  			Expect(usage).To(ContainElement("   Map an HTTP route:"))
   118  			Expect(usage).To(ContainElement("      cf map-route APP_NAME DOMAIN [--hostname HOSTNAME] [--path PATH]"))
   119  
   120  			Expect(usage).To(ContainElement("   Map a TCP route:"))
   121  			Expect(usage).To(ContainElement("      cf map-route APP_NAME DOMAIN (--port PORT | --random-port)"))
   122  		})
   123  	})
   124  
   125  	Describe("Requirements", func() {
   126  		Context("when not provided exactly two args", func() {
   127  			BeforeEach(func() {
   128  				flagContext.Parse("app-name")
   129  			})
   130  
   131  			It("fails with usage", func() {
   132  				_, err := cmd.Requirements(factory, flagContext)
   133  				Expect(err).To(HaveOccurred())
   134  				Expect(ui.Outputs()).To(ContainSubstrings(
   135  					[]string{"Incorrect Usage. Requires APP_NAME and DOMAIN as arguments"},
   136  					[]string{"NAME"},
   137  					[]string{"USAGE"},
   138  				))
   139  			})
   140  		})
   141  
   142  		Context("when provided exactly two args", func() {
   143  			BeforeEach(func() {
   144  				flagContext.Parse("app-name", "domain-name")
   145  			})
   146  
   147  			It("returns a LoginRequirement", func() {
   148  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   149  				Expect(err).NotTo(HaveOccurred())
   150  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   151  
   152  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   153  			})
   154  
   155  			It("returns an ApplicationRequirement", func() {
   156  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   157  				Expect(err).NotTo(HaveOccurred())
   158  				Expect(factory.NewApplicationRequirementCallCount()).To(Equal(1))
   159  
   160  				Expect(factory.NewApplicationRequirementArgsForCall(0)).To(Equal("app-name"))
   161  				Expect(actualRequirements).To(ContainElement(applicationRequirement))
   162  			})
   163  
   164  			It("returns a DomainRequirement", func() {
   165  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   166  				Expect(err).NotTo(HaveOccurred())
   167  				Expect(factory.NewDomainRequirementCallCount()).To(Equal(1))
   168  
   169  				Expect(factory.NewDomainRequirementArgsForCall(0)).To(Equal("domain-name"))
   170  				Expect(actualRequirements).To(ContainElement(domainRequirement))
   171  			})
   172  
   173  			Context("when passing port with a hostname", func() {
   174  				BeforeEach(func() {
   175  					flagContext.Parse("app-name", "example.com", "--port", "8080", "--hostname", "something-else")
   176  				})
   177  
   178  				It("fails", func() {
   179  					_, err := cmd.Requirements(factory, flagContext)
   180  					Expect(err).To(HaveOccurred())
   181  					Expect(ui.Outputs()).To(ContainSubstrings(
   182  						[]string{"FAILED"},
   183  						[]string{"Cannot specify port together with hostname and/or path."},
   184  					))
   185  				})
   186  			})
   187  
   188  			Context("when passing port with a path", func() {
   189  				BeforeEach(func() {
   190  					flagContext.Parse("app-name", "example.com", "--port", "8080", "--path", "something-else")
   191  				})
   192  
   193  				It("fails", func() {
   194  					_, err := cmd.Requirements(factory, flagContext)
   195  					Expect(err).To(HaveOccurred())
   196  					Expect(ui.Outputs()).To(ContainSubstrings(
   197  						[]string{"FAILED"},
   198  						[]string{"Cannot specify port together with hostname and/or path."},
   199  					))
   200  				})
   201  			})
   202  
   203  			Context("when both --port and --random-port are given", func() {
   204  				BeforeEach(func() {
   205  					err := flagContext.Parse("app-name", "domain-name", "--port", "9090", "--random-port")
   206  					Expect(err).NotTo(HaveOccurred())
   207  				})
   208  
   209  				It("fails with error", func() {
   210  					_, err := cmd.Requirements(factory, flagContext)
   211  					Expect(err).To(HaveOccurred())
   212  					Expect(ui.Outputs()).To(ContainSubstrings(
   213  						[]string{"FAILED"},
   214  						[]string{"Cannot specify random-port together with port, hostname and/or path."},
   215  					))
   216  				})
   217  			})
   218  
   219  			Context("when both --random-port and --hostname are given", func() {
   220  				BeforeEach(func() {
   221  					err := flagContext.Parse("app-name", "domain-name", "--hostname", "host", "--random-port")
   222  					Expect(err).NotTo(HaveOccurred())
   223  				})
   224  
   225  				It("fails with error", func() {
   226  					_, err := cmd.Requirements(factory, flagContext)
   227  					Expect(err).To(HaveOccurred())
   228  					Expect(ui.Outputs()).To(ContainSubstrings(
   229  						[]string{"FAILED"},
   230  						[]string{"Cannot specify random-port together with port, hostname and/or path."},
   231  					))
   232  				})
   233  			})
   234  
   235  			Context("when --random-port and --path are given", func() {
   236  				BeforeEach(func() {
   237  					err := flagContext.Parse("app-name", "domain-name", "--path", "path", "--random-port")
   238  					Expect(err).NotTo(HaveOccurred())
   239  				})
   240  
   241  				It("fails with error", func() {
   242  					_, err := cmd.Requirements(factory, flagContext)
   243  					Expect(err).To(HaveOccurred())
   244  					Expect(ui.Outputs()).To(ContainSubstrings(
   245  						[]string{"FAILED"},
   246  						[]string{"Cannot specify random-port together with port, hostname and/or path."},
   247  					))
   248  				})
   249  			})
   250  		})
   251  	})
   252  
   253  	Describe("Execute", func() {
   254  		var err error
   255  
   256  		BeforeEach(func() {
   257  			err := flagContext.Parse("app-name", "domain-name")
   258  			Expect(err).NotTo(HaveOccurred())
   259  			cmd.Requirements(factory, flagContext)
   260  		})
   261  
   262  		JustBeforeEach(func() {
   263  			err = cmd.Execute(flagContext)
   264  		})
   265  
   266  		It("tries to create the route", func() {
   267  			Expect(err).ToNot(HaveOccurred())
   268  			fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   269  			Expect(ok).To(BeTrue())
   270  
   271  			Expect(fakeRouteCreator.CreateRouteCallCount()).To(Equal(1))
   272  			host, path, port, randomPort, domain, space := fakeRouteCreator.CreateRouteArgsForCall(0)
   273  			Expect(host).To(Equal(""))
   274  			Expect(path).To(Equal(""))
   275  			Expect(port).To(Equal(0))
   276  			Expect(randomPort).To(BeFalse())
   277  			Expect(domain).To(Equal(fakeDomain))
   278  			Expect(space).To(Equal(models.SpaceFields{
   279  				Name: "my-space",
   280  				GUID: "my-space-guid",
   281  			}))
   282  		})
   283  
   284  		Context("when a port is passed", func() {
   285  			BeforeEach(func() {
   286  				err := flagContext.Parse("app-name", "domain-name", "--port", "60000")
   287  				Expect(err).NotTo(HaveOccurred())
   288  				cmd.Requirements(factory, flagContext)
   289  			})
   290  
   291  			It("tries to create the route with the port", func() {
   292  				fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   293  				Expect(ok).To(BeTrue())
   294  
   295  				Expect(err).ToNot(HaveOccurred())
   296  				Expect(fakeRouteCreator.CreateRouteCallCount()).To(Equal(1))
   297  				_, _, port, _, _, _ := fakeRouteCreator.CreateRouteArgsForCall(0)
   298  				Expect(port).To(Equal(60000))
   299  			})
   300  		})
   301  
   302  		Context("when a random-port is passed", func() {
   303  			BeforeEach(func() {
   304  				err := flagContext.Parse("app-name", "domain-name", "--random-port")
   305  				Expect(err).NotTo(HaveOccurred())
   306  				cmd.Requirements(factory, flagContext)
   307  			})
   308  
   309  			It("tries to create the route with a random port", func() {
   310  				fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   311  				Expect(ok).To(BeTrue())
   312  
   313  				Expect(err).ToNot(HaveOccurred())
   314  				Expect(fakeRouteCreator.CreateRouteCallCount()).To(Equal(1))
   315  				_, _, _, randomPort, _, _ := fakeRouteCreator.CreateRouteArgsForCall(0)
   316  				Expect(randomPort).To(BeTrue())
   317  			})
   318  		})
   319  
   320  		Context("when creating the route fails", func() {
   321  			BeforeEach(func() {
   322  				fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   323  				Expect(ok).To(BeTrue())
   324  				fakeRouteCreator.CreateRouteReturns(models.Route{}, errors.New("create-route-err"))
   325  			})
   326  
   327  			It("returns an error", func() {
   328  				Expect(err).To(HaveOccurred())
   329  				Expect(err.Error()).To(ContainSubstring("create-route-err"))
   330  			})
   331  		})
   332  
   333  		Context("when creating the route succeeds", func() {
   334  			BeforeEach(func() {
   335  				fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   336  				Expect(ok).To(BeTrue())
   337  				fakeRouteCreator.CreateRouteReturns(models.Route{GUID: "fake-route-guid"}, nil)
   338  			})
   339  
   340  			It("tells the user that it is adding the route", func() {
   341  				Expect(err).ToNot(HaveOccurred())
   342  				Expect(ui.Outputs()).To(ContainSubstrings(
   343  					[]string{"Adding route", "to app", "in org"},
   344  				))
   345  			})
   346  
   347  			It("tries to bind the route", func() {
   348  				Expect(err).ToNot(HaveOccurred())
   349  				Expect(routeRepo.BindCallCount()).To(Equal(1))
   350  				routeGUID, appGUID := routeRepo.BindArgsForCall(0)
   351  				Expect(routeGUID).To(Equal("fake-route-guid"))
   352  				Expect(appGUID).To(Equal("fake-app-guid"))
   353  			})
   354  
   355  			Context("when binding the route succeeds", func() {
   356  				BeforeEach(func() {
   357  					routeRepo.BindReturns(nil)
   358  				})
   359  
   360  				It("tells the user that it succeeded", func() {
   361  					Expect(err).ToNot(HaveOccurred())
   362  					Expect(ui.Outputs()).To(ContainSubstrings(
   363  						[]string{"OK"},
   364  					))
   365  				})
   366  			})
   367  
   368  			Context("when binding the route fails", func() {
   369  				BeforeEach(func() {
   370  					routeRepo.BindReturns(errors.New("bind-error"))
   371  				})
   372  
   373  				It("returns an error", func() {
   374  					Expect(err).To(HaveOccurred())
   375  					Expect(err.Error()).To(Equal("bind-error"))
   376  				})
   377  			})
   378  		})
   379  
   380  		Context("when a hostname is passed", func() {
   381  			BeforeEach(func() {
   382  				err := flagContext.Parse("app-name", "domain-name", "-n", "the-hostname")
   383  				Expect(err).NotTo(HaveOccurred())
   384  				cmd.Requirements(factory, flagContext)
   385  			})
   386  
   387  			It("tries to create the route with the hostname", func() {
   388  				Expect(err).ToNot(HaveOccurred())
   389  				fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   390  				Expect(ok).To(BeTrue())
   391  				Expect(fakeRouteCreator.CreateRouteCallCount()).To(Equal(1))
   392  				hostName, _, _, _, _, _ := fakeRouteCreator.CreateRouteArgsForCall(0)
   393  				Expect(hostName).To(Equal("the-hostname"))
   394  			})
   395  		})
   396  
   397  		Context("when a hostname is not passed", func() {
   398  			BeforeEach(func() {
   399  				err := flagContext.Parse("app-name", "domain-name")
   400  				Expect(err).NotTo(HaveOccurred())
   401  				cmd.Requirements(factory, flagContext)
   402  			})
   403  
   404  			It("tries to create the route without a hostname", func() {
   405  				Expect(err).ToNot(HaveOccurred())
   406  				fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   407  				Expect(ok).To(BeTrue())
   408  				Expect(fakeRouteCreator.CreateRouteCallCount()).To(Equal(1))
   409  				hostName, _, _, _, _, _ := fakeRouteCreator.CreateRouteArgsForCall(0)
   410  				Expect(hostName).To(Equal(""))
   411  			})
   412  		})
   413  
   414  		Context("when a path is passed", func() {
   415  			BeforeEach(func() {
   416  				err := flagContext.Parse("app-name", "domain-name", "--path", "the-path")
   417  				Expect(err).NotTo(HaveOccurred())
   418  				cmd.Requirements(factory, flagContext)
   419  			})
   420  
   421  			It("tries to create the route with the path", func() {
   422  				Expect(err).ToNot(HaveOccurred())
   423  				fakeRouteCreator, ok := fakeCreateRouteCmd.(*routefakes.OldFakeRouteCreator)
   424  				Expect(ok).To(BeTrue())
   425  				Expect(fakeRouteCreator.CreateRouteCallCount()).To(Equal(1))
   426  				_, path, _, _, _, _ := fakeRouteCreator.CreateRouteArgsForCall(0)
   427  				Expect(path).To(Equal("the-path"))
   428  			})
   429  		})
   430  	})
   431  })