Skip to content

Commit

Permalink
Rename update-route & add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dariquest committed Dec 13, 2024
1 parent f04a40c commit e4ed4f0
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (cmd *UpdateRoute) MetaData() commandregistry.CommandMetadata {
Name: "update-route",
Description: T("Update an existing route"),
Usage: []string{
fmt.Sprintf("%s:\n", T("Update an existing HTTP route")),
fmt.Sprintf("%s:\n", T("Update an existing route")),
" CF_NAME update-route ",
fmt.Sprintf("%s ", T("DOMAIN")),
fmt.Sprintf("[--hostname %s] ", T("HOSTNAME")),
Expand Down Expand Up @@ -104,7 +104,7 @@ func (cmd *UpdateRoute) Execute(c flags.FlagContext) error {
route, err := cmd.routeRepo.Find(host, domainFields, path, 0)
if err != nil {
if _, ok := err.(*errors.ModelNotFoundError); ok {
cmd.ui.Warn(T("Route with domain '{{.URL}}' does not exist.",
cmd.ui.Failed(T("Route with domain '{{.URL}}' does not exist.",
map[string]interface{}{"URL": url}))
return nil
}
Expand Down
210 changes: 210 additions & 0 deletions cf/commands/route/update_route_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package route_test

import (
"errors"

"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/commands/route"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/flags"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"

"code.cloudfoundry.org/cli/cf/api/apifakes"

testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"

"strings"

. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("UpdateRoute", func() {
var (
ui *testterm.FakeUI
configRepo coreconfig.Repository
routeRepo *apifakes.FakeRouteRepository

cmd commandregistry.Command
deps commandregistry.Dependency
factory *requirementsfakes.FakeFactory
flagContext flags.FlagContext

loginRequirement requirements.Requirement
domainRequirement *requirementsfakes.FakeDomainRequirement

fakeDomain models.DomainFields
)

BeforeEach(func() {
ui = &testterm.FakeUI{}
configRepo = testconfig.NewRepositoryWithDefaults()
routeRepo = new(apifakes.FakeRouteRepository)
repoLocator := deps.RepoLocator.SetRouteRepository(routeRepo)

deps = commandregistry.Dependency{
UI: ui,
Config: configRepo,
RepoLocator: repoLocator,
}

cmd = &route.UpdateRoute{}
cmd.SetDependency(deps, false)

flagContext = flags.NewFlagContext(cmd.MetaData().Flags)

factory = new(requirementsfakes.FakeFactory)

loginRequirement = &passingRequirement{Name: "login-requirement"}
factory.NewLoginRequirementReturns(loginRequirement)

domainRequirement = new(requirementsfakes.FakeDomainRequirement)
factory.NewDomainRequirementReturns(domainRequirement)

fakeDomain = models.DomainFields{
GUID: "fake-domain-guid",
Name: "fake-domain-name",
}
domainRequirement.GetDomainReturns(fakeDomain)
})

AfterEach(func() {

})

Describe("Help text", func() {
var usage []string

BeforeEach(func() {
cmd := &route.UpdateRoute{}
up := commandregistry.CLICommandUsagePresenter(cmd)

usage = strings.Split(up.Usage(), "\n")
})

It("contains an example", func() {
Expect(usage).To(ContainElement("CF_NAME update-route example.com -o loadbalancing=round-robin"))
})

It("contains the options", func() {
Expect(usage).To(ContainElement(" --hostname, -n Hostname for the HTTP route (required for shared domains)"))
Expect(usage).To(ContainElement(" --path Path for the HTTP route"))
Expect(usage).To(ContainElement(" --option, -o Set the value of a per-route option"))
Expect(usage).To(ContainElement(" --remove-option, -r Remove an option with the given name"))
})

It("shows the usage", func() {
Expect(usage).To(ContainElement(" Update an existing route:"))
Expect(usage).To(ContainElement(" cf update-route APP_NAME DOMAIN [--hostname HOSTNAME] [--path PATH] [--option OPTION=VALUE] [--remove-option OPTION]"))
})
})

Describe("Requirements", func() {
Context("when not provided exactly one arg", func() {
BeforeEach(func() {
flagContext.Parse("")
})

It("fails with usage", func() {
_, err := cmd.Requirements(factory, flagContext)
Expect(err).To(HaveOccurred())
Expect(ui.Outputs()).To(ContainSubstrings(
[]string{"Incorrect Usage. Requires DOMAIN as an argument"},
[]string{"NAME"},
[]string{"USAGE"},
))
})
})

Context("when provided exactly one arg", func() {
BeforeEach(func() {
flagContext.Parse("domain-name")
})

It("returns a LoginRequirement", func() {
actualRequirements, err := cmd.Requirements(factory, flagContext)
Expect(err).NotTo(HaveOccurred())
Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))

Expect(actualRequirements).To(ContainElement(loginRequirement))
})

It("returns a DomainRequirement", func() {
actualRequirements, err := cmd.Requirements(factory, flagContext)
Expect(err).NotTo(HaveOccurred())
Expect(factory.NewDomainRequirementCallCount()).To(Equal(1))

Expect(factory.NewDomainRequirementArgsForCall(0)).To(Equal("domain-name"))
Expect(actualRequirements).To(ContainElement(domainRequirement))
})
})
})

Describe("Execute", func() {
var err error

BeforeEach(func() {
err := flagContext.Parse("domain-name")
Expect(err).NotTo(HaveOccurred())
cmd.Requirements(factory, flagContext)
})

JustBeforeEach(func() {
err = cmd.Execute(flagContext)
})

It("tries to find the route", func() {
Expect(err).NotTo(HaveOccurred())
Expect(routeRepo.FindCallCount()).To(Equal(1))
hostname, domain, path, port := routeRepo.FindArgsForCall(0)
Expect(hostname).To(Equal(""))
Expect(domain).To(Equal(fakeDomain))
Expect(path).To(Equal(""))
Expect(port).To(Equal(0))
})

Context("when a hostname and a path are passed", func() {
BeforeEach(func() {
err := flagContext.Parse("domain-name", "--hostname", "the-hostname", "the-path", "--path", "the-path")
Expect(err).NotTo(HaveOccurred())
cmd.Requirements(factory, flagContext)
})

It("tries to find the route with the hostname and path", func() {
Expect(err).NotTo(HaveOccurred())
Expect(routeRepo.FindCallCount()).To(Equal(1))
hostname, _, path, _ := routeRepo.FindArgsForCall(0)
Expect(hostname).To(Equal("the-hostname"))
Expect(path).To(Equal("the-path"))
})
})

Context("when the route can be found", func() {
BeforeEach(func() {
routeRepo.FindReturns(models.Route{GUID: "route-guid"}, nil)
})

It("tells the user a route with the given domain does not exist", func() {
Expect(err).To(HaveOccurred())
Expect(ui.Outputs()).To(ContainSubstrings(
[]string{"Route with domain", "does not exist"},
))
})

Context("when the route cannot be found", func() {
BeforeEach(func() {
routeRepo.FindReturns(models.Route{}, errors.New("find-by-host-and-domain-err"))
})

It("returns an error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("find-by-host-and-domain-err"))
})
})
})
})
})

0 comments on commit e4ed4f0

Please sign in to comment.