github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/route/create_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/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  
    13  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    14  
    15  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    16  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    17  
    18  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    19  
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("CreateRoute", func() {
    25  	var (
    26  		ui         *testterm.FakeUI
    27  		routeRepo  *apifakes.FakeRouteRepository
    28  		configRepo coreconfig.Repository
    29  
    30  		cmd         commandregistry.Command
    31  		deps        commandregistry.Dependency
    32  		factory     *requirementsfakes.FakeFactory
    33  		flagContext flags.FlagContext
    34  
    35  		spaceRequirement  *requirementsfakes.FakeSpaceRequirement
    36  		domainRequirement *requirementsfakes.FakeDomainRequirement
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		ui = &testterm.FakeUI{}
    41  		configRepo = testconfig.NewRepositoryWithDefaults()
    42  		routeRepo = new(apifakes.FakeRouteRepository)
    43  		repoLocator := deps.RepoLocator.SetRouteRepository(routeRepo)
    44  
    45  		deps = commandregistry.Dependency{
    46  			UI:          ui,
    47  			Config:      configRepo,
    48  			RepoLocator: repoLocator,
    49  		}
    50  
    51  		cmd = &route.CreateRoute{}
    52  		cmd.SetDependency(deps, false)
    53  
    54  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    55  
    56  		factory = new(requirementsfakes.FakeFactory)
    57  
    58  		spaceRequirement = new(requirementsfakes.FakeSpaceRequirement)
    59  		space := models.Space{}
    60  		space.GUID = "space-guid"
    61  		space.Name = "space-name"
    62  		spaceRequirement.GetSpaceReturns(space)
    63  		factory.NewSpaceRequirementReturns(spaceRequirement)
    64  
    65  		domainRequirement = new(requirementsfakes.FakeDomainRequirement)
    66  		domainRequirement.GetDomainReturns(models.DomainFields{
    67  			GUID: "domain-guid",
    68  			Name: "domain-name",
    69  		})
    70  		factory.NewDomainRequirementReturns(domainRequirement)
    71  	})
    72  
    73  	Describe("Requirements", func() {
    74  		Context("when not provided exactly two args", func() {
    75  			BeforeEach(func() {
    76  				err := flagContext.Parse("space-name")
    77  				Expect(err).NotTo(HaveOccurred())
    78  			})
    79  
    80  			It("fails with usage", func() {
    81  				_, err := cmd.Requirements(factory, flagContext)
    82  				Expect(err).To(HaveOccurred())
    83  				Expect(ui.Outputs()).To(ContainSubstrings(
    84  					[]string{"Incorrect Usage. Requires SPACE and DOMAIN as arguments"},
    85  					[]string{"NAME"},
    86  					[]string{"USAGE"},
    87  				))
    88  			})
    89  		})
    90  
    91  		Context("when provided exactly two args", func() {
    92  			BeforeEach(func() {
    93  				err := flagContext.Parse("space-name", "domain-name")
    94  				Expect(err).NotTo(HaveOccurred())
    95  			})
    96  
    97  			It("returns a SpaceRequirement", func() {
    98  				actualRequirements, err := cmd.Requirements(factory, flagContext)
    99  				Expect(err).NotTo(HaveOccurred())
   100  				Expect(factory.NewSpaceRequirementCallCount()).To(Equal(1))
   101  				Expect(factory.NewSpaceRequirementArgsForCall(0)).To(Equal("space-name"))
   102  
   103  				Expect(actualRequirements).To(ContainElement(spaceRequirement))
   104  			})
   105  
   106  			It("returns a DomainRequirement", func() {
   107  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   108  				Expect(err).NotTo(HaveOccurred())
   109  				Expect(factory.NewDomainRequirementCallCount()).To(Equal(1))
   110  				Expect(factory.NewDomainRequirementArgsForCall(0)).To(Equal("domain-name"))
   111  
   112  				Expect(actualRequirements).To(ContainElement(domainRequirement))
   113  			})
   114  		})
   115  
   116  		Context("when both --port and --hostname are given", func() {
   117  			BeforeEach(func() {
   118  				err := flagContext.Parse("space-name", "domain-name", "--port", "9090", "--hostname", "host")
   119  				Expect(err).NotTo(HaveOccurred())
   120  			})
   121  
   122  			It("fails with error", func() {
   123  				_, err := cmd.Requirements(factory, flagContext)
   124  				Expect(err).To(HaveOccurred())
   125  				Expect(ui.Outputs()).To(ContainSubstrings(
   126  					[]string{"FAILED"},
   127  					[]string{"Cannot specify port together with hostname and/or path."},
   128  				))
   129  			})
   130  		})
   131  
   132  		Context("when both --port and --path are given", func() {
   133  			BeforeEach(func() {
   134  				err := flagContext.Parse("space-name", "domain-name", "--port", "9090", "--path", "path")
   135  				Expect(err).NotTo(HaveOccurred())
   136  			})
   137  
   138  			It("fails with error", func() {
   139  				_, err := cmd.Requirements(factory, flagContext)
   140  				Expect(err).To(HaveOccurred())
   141  				Expect(ui.Outputs()).To(ContainSubstrings(
   142  					[]string{"FAILED"},
   143  					[]string{"Cannot specify port together with hostname and/or path."},
   144  				))
   145  			})
   146  		})
   147  
   148  		Context("when both --port and --random-port are given", func() {
   149  			BeforeEach(func() {
   150  				err := flagContext.Parse("space-name", "domain-name", "--port", "9090", "--random-port")
   151  				Expect(err).NotTo(HaveOccurred())
   152  			})
   153  
   154  			It("fails with error", func() {
   155  				_, err := cmd.Requirements(factory, flagContext)
   156  				Expect(err).To(HaveOccurred())
   157  				Expect(ui.Outputs()).To(ContainSubstrings(
   158  					[]string{"FAILED"},
   159  					[]string{"Cannot specify random-port together with port, hostname and/or path."},
   160  				))
   161  			})
   162  		})
   163  
   164  		Context("when both --random-port and --hostname are given", func() {
   165  			BeforeEach(func() {
   166  				err := flagContext.Parse("space-name", "domain-name", "--hostname", "host", "--random-port")
   167  				Expect(err).NotTo(HaveOccurred())
   168  			})
   169  
   170  			It("fails with error", func() {
   171  				_, err := cmd.Requirements(factory, flagContext)
   172  				Expect(err).To(HaveOccurred())
   173  				Expect(ui.Outputs()).To(ContainSubstrings(
   174  					[]string{"FAILED"},
   175  					[]string{"Cannot specify random-port together with port, hostname and/or path."},
   176  				))
   177  			})
   178  		})
   179  
   180  		Context("when --random-port and --path are given", func() {
   181  			BeforeEach(func() {
   182  				err := flagContext.Parse("space-name", "domain-name", "--path", "path", "--random-port")
   183  				Expect(err).NotTo(HaveOccurred())
   184  			})
   185  
   186  			It("fails with error", func() {
   187  				_, err := cmd.Requirements(factory, flagContext)
   188  				Expect(err).To(HaveOccurred())
   189  				Expect(ui.Outputs()).To(ContainSubstrings(
   190  					[]string{"FAILED"},
   191  					[]string{"Cannot specify random-port together with port, hostname and/or path."},
   192  				))
   193  			})
   194  		})
   195  	})
   196  
   197  	Describe("Execute", func() {
   198  		var err error
   199  
   200  		BeforeEach(func() {
   201  			err := flagContext.Parse("space-name", "domain-name")
   202  			Expect(err).NotTo(HaveOccurred())
   203  			cmd.Requirements(factory, flagContext)
   204  		})
   205  
   206  		JustBeforeEach(func() {
   207  			err = cmd.Execute(flagContext)
   208  		})
   209  
   210  		It("attempts to create a route in the space", func() {
   211  			Expect(err).NotTo(HaveOccurred())
   212  
   213  			Expect(routeRepo.CreateInSpaceCallCount()).To(Equal(1))
   214  			hostname, path, domain, space, port, randomPort := routeRepo.CreateInSpaceArgsForCall(0)
   215  			Expect(hostname).To(Equal(""))
   216  			Expect(path).To(Equal(""))
   217  			Expect(domain).To(Equal("domain-guid"))
   218  			Expect(space).To(Equal("space-guid"))
   219  			Expect(port).To(Equal(0))
   220  			Expect(randomPort).To(BeFalse())
   221  		})
   222  
   223  		Context("when the --path option is given", func() {
   224  			BeforeEach(func() {
   225  				err := flagContext.Parse("space-name", "domain-name", "--path", "some-path")
   226  				Expect(err).NotTo(HaveOccurred())
   227  			})
   228  
   229  			It("tries to create a route with the path", func() {
   230  				Expect(err).NotTo(HaveOccurred())
   231  
   232  				Expect(routeRepo.CreateInSpaceCallCount()).To(Equal(1))
   233  				_, path, _, _, _, _ := routeRepo.CreateInSpaceArgsForCall(0)
   234  				Expect(path).To(Equal("some-path"))
   235  			})
   236  		})
   237  
   238  		Context("when the --random-port option is given", func() {
   239  			BeforeEach(func() {
   240  				err := flagContext.Parse("space-name", "domain-name", "--random-port")
   241  				Expect(err).NotTo(HaveOccurred())
   242  			})
   243  
   244  			It("tries to create a route with a random port", func() {
   245  				Expect(err).NotTo(HaveOccurred())
   246  
   247  				Expect(routeRepo.CreateInSpaceCallCount()).To(Equal(1))
   248  				_, _, _, _, _, randomPort := routeRepo.CreateInSpaceArgsForCall(0)
   249  				Expect(randomPort).To(BeTrue())
   250  			})
   251  		})
   252  
   253  		Context("when the --port option is given", func() {
   254  			BeforeEach(func() {
   255  				err := flagContext.Parse("space-name", "domain-name", "--port", "9090")
   256  				Expect(err).NotTo(HaveOccurred())
   257  			})
   258  
   259  			It("tries to create a route with the port", func() {
   260  				Expect(err).NotTo(HaveOccurred())
   261  
   262  				Expect(routeRepo.CreateInSpaceCallCount()).To(Equal(1))
   263  				_, _, _, _, port, _ := routeRepo.CreateInSpaceArgsForCall(0)
   264  				Expect(port).To(Equal(9090))
   265  			})
   266  		})
   267  
   268  		Context("when the --hostname option is given", func() {
   269  			BeforeEach(func() {
   270  				err := flagContext.Parse("space-name", "domain-name", "--hostname", "host")
   271  				Expect(err).NotTo(HaveOccurred())
   272  			})
   273  
   274  			It("tries to create a route with the hostname", func() {
   275  				Expect(err).NotTo(HaveOccurred())
   276  
   277  				Expect(routeRepo.CreateInSpaceCallCount()).To(Equal(1))
   278  				host, _, _, _, _, _ := routeRepo.CreateInSpaceArgsForCall(0)
   279  				Expect(host).To(Equal("host"))
   280  			})
   281  		})
   282  
   283  		Context("when creating the route fails", func() {
   284  			BeforeEach(func() {
   285  				routeRepo.CreateInSpaceReturns(models.Route{}, errors.New("create-error"))
   286  
   287  				err := flagContext.Parse("space-name", "domain-name", "--port", "9090", "--hostname", "hostname", "--path", "/path")
   288  				Expect(err).NotTo(HaveOccurred())
   289  			})
   290  
   291  			It("attempts to find the route", func() {
   292  				Expect(err).To(HaveOccurred())
   293  				Expect(routeRepo.FindCallCount()).To(Equal(1))
   294  
   295  				host, domain, path, port := routeRepo.FindArgsForCall(0)
   296  				Expect(host).To(Equal("hostname"))
   297  				Expect(domain.Name).To(Equal("domain-name"))
   298  				Expect(path).To(Equal("/path"))
   299  				Expect(port).To(Equal(9090))
   300  			})
   301  
   302  			Context("when finding the route fails", func() {
   303  				BeforeEach(func() {
   304  					routeRepo.FindReturns(models.Route{}, errors.New("find-error"))
   305  				})
   306  
   307  				It("fails with the original error", func() {
   308  					Expect(err).To(HaveOccurred())
   309  					Expect(err.Error()).To(Equal("create-error"))
   310  				})
   311  			})
   312  
   313  			Context("when a route with the same space guid, but different domain guid is found", func() {
   314  				It("fails with the original error", func() {
   315  					Expect(err).To(HaveOccurred())
   316  					Expect(err.Error()).To(Equal("create-error"))
   317  				})
   318  			})
   319  
   320  			Context("when a route with the same domain guid, but different space guid is found", func() {
   321  				It("fails with the original error", func() {
   322  					Expect(err).To(HaveOccurred())
   323  					Expect(err.Error()).To(Equal("create-error"))
   324  				})
   325  			})
   326  
   327  			Context("when a route with the same domain and space guid is found", func() {
   328  				BeforeEach(func() {
   329  					routeRepo.FindReturns(models.Route{
   330  						Domain: models.DomainFields{
   331  							GUID: "domain-guid",
   332  							Name: "domain-name",
   333  						},
   334  						Space: models.SpaceFields{
   335  							GUID: "space-guid",
   336  						},
   337  					}, nil)
   338  				})
   339  
   340  				It("prints a message", func() {
   341  					Expect(err).NotTo(HaveOccurred())
   342  					Expect(ui.Outputs()).To(ContainSubstrings(
   343  						[]string{"OK"},
   344  						[]string{"Route domain-name already exists"},
   345  					))
   346  				})
   347  			})
   348  		})
   349  	})
   350  
   351  	Describe("CreateRoute", func() {
   352  		var domainFields models.DomainFields
   353  		var spaceFields models.SpaceFields
   354  		var rc route.Creator
   355  
   356  		BeforeEach(func() {
   357  			domainFields = models.DomainFields{
   358  				GUID: "domain-guid",
   359  				Name: "domain-name",
   360  			}
   361  			spaceFields = models.SpaceFields{
   362  				GUID: "space-guid",
   363  				Name: "space-name",
   364  			}
   365  
   366  			var ok bool
   367  			rc, ok = cmd.(route.Creator)
   368  			Expect(ok).To(BeTrue())
   369  		})
   370  
   371  		It("attempts to create a route in the space", func() {
   372  			rc.CreateRoute("hostname", "path", 9090, true, domainFields, spaceFields)
   373  
   374  			Expect(routeRepo.CreateInSpaceCallCount()).To(Equal(1))
   375  			hostname, path, domain, space, port, randomPort := routeRepo.CreateInSpaceArgsForCall(0)
   376  			Expect(hostname).To(Equal("hostname"))
   377  			Expect(path).To(Equal("path"))
   378  			Expect(domain).To(Equal(domainFields.GUID))
   379  			Expect(space).To(Equal(spaceFields.GUID))
   380  			Expect(port).To(Equal(9090))
   381  			Expect(randomPort).To(BeTrue())
   382  		})
   383  
   384  		Context("when creating the route fails", func() {
   385  			BeforeEach(func() {
   386  				routeRepo.CreateInSpaceReturns(models.Route{}, errors.New("create-error"))
   387  			})
   388  
   389  			It("attempts to find the route", func() {
   390  				rc.CreateRoute("hostname", "path", 0, false, domainFields, spaceFields)
   391  				Expect(routeRepo.FindCallCount()).To(Equal(1))
   392  			})
   393  
   394  			Context("when finding the route fails", func() {
   395  				BeforeEach(func() {
   396  					routeRepo.FindReturns(models.Route{}, errors.New("find-error"))
   397  				})
   398  
   399  				It("returns the original error", func() {
   400  					_, err := rc.CreateRoute("hostname", "path", 0, false, domainFields, spaceFields)
   401  					Expect(err).To(HaveOccurred())
   402  					Expect(err.Error()).To(Equal("create-error"))
   403  				})
   404  			})
   405  
   406  			Context("when a route with the same space guid, but different domain guid is found", func() {
   407  				It("returns the original error", func() {
   408  					_, err := rc.CreateRoute("hostname", "path", 0, false, domainFields, spaceFields)
   409  					Expect(err).To(HaveOccurred())
   410  					Expect(err.Error()).To(Equal("create-error"))
   411  				})
   412  			})
   413  
   414  			Context("when a route with the same domain guid, but different space guid is found", func() {
   415  				It("returns the original error", func() {
   416  					_, err := rc.CreateRoute("hostname", "path", 0, false, domainFields, spaceFields)
   417  					Expect(err).To(HaveOccurred())
   418  					Expect(err.Error()).To(Equal("create-error"))
   419  				})
   420  			})
   421  
   422  			Context("when a route with the same domain and space guid is found", func() {
   423  				BeforeEach(func() {
   424  					routeRepo.FindReturns(models.Route{
   425  						Host: "hostname",
   426  						Path: "path",
   427  						Domain: models.DomainFields{
   428  							GUID: "domain-guid",
   429  							Name: "domain-name",
   430  						},
   431  						Space: models.SpaceFields{
   432  							GUID: "space-guid",
   433  						},
   434  					}, nil)
   435  				})
   436  
   437  				It("prints a message that it already exists", func() {
   438  					rc.CreateRoute("hostname", "path", 0, false, domainFields, spaceFields)
   439  					Expect(ui.Outputs()).To(ContainSubstrings(
   440  						[]string{"OK"},
   441  						[]string{"Route hostname.domain-name/path already exists"}))
   442  				})
   443  			})
   444  		})
   445  
   446  		Context("when creating the route succeeds", func() {
   447  			var route models.Route
   448  
   449  			JustBeforeEach(func() {
   450  				routeRepo.CreateInSpaceReturns(route, nil)
   451  			})
   452  
   453  			It("prints a success message", func() {
   454  				rc.CreateRoute("hostname", "path", 0, false, domainFields, spaceFields)
   455  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   456  			})
   457  
   458  			Context("when --random-port is specified", func() {
   459  				BeforeEach(func() {
   460  					route = models.Route{
   461  						Host:   "some-host",
   462  						Domain: domainFields,
   463  						Port:   9090,
   464  					}
   465  				})
   466  
   467  				It("print a success message with created route", func() {
   468  					rc.CreateRoute("hostname", "path", 0, true, domainFields, spaceFields)
   469  					Expect(ui.Outputs()).To(ContainSubstrings(
   470  						[]string{"OK"},
   471  						[]string{"Route domain-name:9090 has been created"},
   472  					))
   473  				})
   474  			})
   475  		})
   476  	})
   477  })