github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/route_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     5  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/cf/errors"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
     9  	v7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    12  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    13  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("route Command", func() {
    20  	var (
    21  		cmd             v7.RouteCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		binaryName      string
    27  		executeErr      error
    28  		domainName      string
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		binaryName = "faceman"
    38  		fakeConfig.BinaryNameReturns(binaryName)
    39  
    40  		domainName = "some-domain.com"
    41  
    42  		cmd = v7.RouteCommand{
    43  			BaseCommand: v7.BaseCommand{
    44  				UI:          testUI,
    45  				Config:      fakeConfig,
    46  				SharedActor: fakeSharedActor,
    47  				Actor:       fakeActor,
    48  			},
    49  			RequiredArgs: flag.Domain{Domain: domainName},
    50  		}
    51  
    52  		fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    53  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    54  		fakeActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    55  
    56  		fakeActor.GetDomainByNameReturns(
    57  			resources.Domain{Name: domainName, GUID: "domain-guid"},
    58  			v7action.Warnings{"get-domain-warnings"},
    59  			nil,
    60  		)
    61  
    62  		fakeActor.GetRouteByAttributesReturns(
    63  			resources.Route{GUID: "route-guid"},
    64  			v7action.Warnings{"get-route-warnings"},
    65  			nil,
    66  		)
    67  		fakeActor.GetApplicationMapForRouteReturns(
    68  			map[string]resources.Application{"app-guid": {GUID: "app-guid", Name: "app-name"}},
    69  			v7action.Warnings{"get-route-warnings"},
    70  			nil,
    71  		)
    72  
    73  	})
    74  
    75  	JustBeforeEach(func() {
    76  		executeErr = cmd.Execute(nil)
    77  	})
    78  
    79  	It("checks the target", func() {
    80  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    81  		checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    82  		Expect(checkTargetedOrg).To(BeTrue())
    83  		Expect(checkTargetedSpace).To(BeFalse())
    84  	})
    85  
    86  	When("checking target fails", func() {
    87  		BeforeEach(func() {
    88  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    89  		})
    90  
    91  		It("returns an error", func() {
    92  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    93  		})
    94  	})
    95  
    96  	It("checks if the user is logged in", func() {
    97  		Expect(fakeActor.GetCurrentUserCallCount()).To(Equal(1))
    98  	})
    99  
   100  	When("the user is not logged in", func() {
   101  		BeforeEach(func() {
   102  			fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("no current user"))
   103  		})
   104  
   105  		It("returns an error", func() {
   106  			Expect(executeErr).To(MatchError("no current user"))
   107  		})
   108  	})
   109  
   110  	When("getting the domain errors", func() {
   111  		BeforeEach(func() {
   112  			fakeActor.GetDomainByNameReturns(resources.Domain{}, v7action.Warnings{"get-domain-warnings"}, errors.New("get-domain-error"))
   113  		})
   114  
   115  		It("returns the error and displays warnings", func() {
   116  			Expect(testUI.Err).To(Say("get-domain-warnings"))
   117  			Expect(executeErr).To(MatchError(errors.New("get-domain-error")))
   118  			Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   119  		})
   120  	})
   121  
   122  	It("gets the domain and displays warnings", func() {
   123  		Expect(testUI.Err).To(Say("get-domain-warnings"))
   124  
   125  		Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   126  		Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   127  	})
   128  
   129  	Describe("getting the routes", func() {
   130  		It("calls GetRouteByAttributes and displaying warnings", func() {
   131  			Expect(testUI.Err).To(Say("route-warning"))
   132  
   133  			Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   134  			domain, host, path, port := fakeActor.GetRouteByAttributesArgsForCall(0)
   135  			Expect(domain.Name).To(Equal(domainName))
   136  			Expect(domain.GUID).To(Equal("domain-guid"))
   137  			Expect(host).To(Equal(cmd.Hostname))
   138  			Expect(path).To(Equal(cmd.Path.Path))
   139  			Expect(port).To(Equal(0))
   140  		})
   141  
   142  		When("getting the route errors", func() {
   143  			BeforeEach(func() {
   144  				fakeActor.GetRouteByAttributesReturns(
   145  					resources.Route{},
   146  					v7action.Warnings{"get-route-warnings"},
   147  					errors.New("get-route-error"),
   148  				)
   149  			})
   150  
   151  			It("returns the error and displays warnings", func() {
   152  				Expect(testUI.Err).To(Say("get-route-warnings"))
   153  				Expect(executeErr).To(MatchError(errors.New("get-route-error")))
   154  			})
   155  		})
   156  	})
   157  
   158  	Describe("getting the apps", func() {
   159  		It("calls GetApplicationMapForRoute and displaying warnings", func() {
   160  			Expect(testUI.Err).To(Say("route-warning"))
   161  
   162  			Expect(fakeActor.GetApplicationMapForRouteCallCount()).To(Equal(1))
   163  			route := fakeActor.GetApplicationMapForRouteArgsForCall(0)
   164  			Expect(route.GUID).To(Equal("route-guid"))
   165  		})
   166  
   167  		When("getting the Application mapping errors", func() {
   168  			BeforeEach(func() {
   169  				fakeActor.GetApplicationMapForRouteReturns(
   170  					map[string]resources.Application{},
   171  					v7action.Warnings{"get-app-map-warnings"},
   172  					errors.New("get-app-map-error"),
   173  				)
   174  			})
   175  
   176  			It("returns the error and displays warnings", func() {
   177  				Expect(testUI.Err).To(Say("get-app-map-warnings"))
   178  				Expect(executeErr).To(MatchError(errors.New("get-app-map-error")))
   179  			})
   180  		})
   181  	})
   182  
   183  	When("passing hostname and path flags", func() {
   184  		BeforeEach(func() {
   185  			cmd.Path.Path = "/some-path"
   186  			cmd.Hostname = "some-host"
   187  
   188  			destAppA := resources.RouteDestinationApp{GUID: "abc", Process: struct{ Type string }{"web"}}
   189  			destinationA := resources.RouteDestination{App: destAppA, Port: 8080, Protocol: "http1"}
   190  
   191  			destAppB := resources.RouteDestinationApp{GUID: "123", Process: struct{ Type string }{"web"}}
   192  			destinationB := resources.RouteDestination{App: destAppB, Port: 1337, Protocol: "http2"}
   193  
   194  			destinations := []resources.RouteDestination{destinationA, destinationB}
   195  			route := resources.Route{GUID: "route-guid", Host: cmd.Hostname, Path: cmd.Path.Path, Protocol: "http", Destinations: destinations}
   196  
   197  			fakeActor.GetRouteByAttributesReturns(
   198  				route,
   199  				v7action.Warnings{"get-route-warnings"},
   200  				nil,
   201  			)
   202  
   203  			appA := resources.Application{GUID: "abc", Name: "app-name"}
   204  			appB := resources.Application{GUID: "123", Name: "other-app-name"}
   205  
   206  			fakeActor.GetApplicationMapForRouteReturns(
   207  				map[string]resources.Application{"abc": appA, "123": appB},
   208  				v7action.Warnings{"get-apps-error"},
   209  				nil,
   210  			)
   211  		})
   212  
   213  		It("displays the summary", func() {
   214  			Expect(executeErr).NotTo(HaveOccurred())
   215  
   216  			Expect(testUI.Out).To(Say(`Showing route %s\.%s/some-path in org some-org / space some-space as some-user\.\.\.`, cmd.Hostname, domainName))
   217  			Expect(testUI.Out).To(Say(`domain:\s+%s`, domainName))
   218  			Expect(testUI.Out).To(Say(`host:\s+%s`, cmd.Hostname))
   219  			Expect(testUI.Out).To(Say(`path:\s+%s`, cmd.Path.Path))
   220  			Expect(testUI.Out).To(Say(`protocol:\s+http`))
   221  			Expect(testUI.Out).To(Say(`\n`))
   222  			Expect(testUI.Out).To(Say(`Destinations:`))
   223  			Expect(testUI.Out).To(Say(`\s+app\s+process\s+port\s+app-protocol`))
   224  			Expect(testUI.Out).To(Say(`\s+app-name\s+web\s+8080\s+http1`))
   225  			Expect(testUI.Out).To(Say(`\s+other-app-name\s+web\s+1337\s+http2`))
   226  
   227  			Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   228  			givenDomain, givenHostname, givenPath, givenPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   229  			Expect(givenDomain.Name).To(Equal(domainName))
   230  			Expect(givenHostname).To(Equal("some-host"))
   231  			Expect(givenPath).To(Equal("/some-path"))
   232  			Expect(givenPort).To(Equal(0))
   233  		})
   234  	})
   235  	Describe("RouteRetrieval display logic", func() {
   236  		When("passing in just a domain", func() {
   237  			BeforeEach(func() {
   238  				cmd.Hostname = ""
   239  				cmd.Path.Path = ""
   240  			})
   241  			It(" displays the right stuff", func() {
   242  				Expect(testUI.Out).To(Say(`Showing route %s in org some-org / space some-space as some-user\.\.\.`, domainName))
   243  			})
   244  		})
   245  		When("passing in a domain and hostname", func() {
   246  			BeforeEach(func() {
   247  				cmd.Hostname = "some-host"
   248  				cmd.Path.Path = ""
   249  			})
   250  			It(" displays the right stuff", func() {
   251  				Expect(testUI.Out).To(Say(`Showing route some-host\.%s in org some-org / space some-space as some-user\.\.\.`, domainName))
   252  			})
   253  		})
   254  
   255  		When("passing in a domain, a hostname, and a path", func() {
   256  			BeforeEach(func() {
   257  				cmd.Hostname = "some-host"
   258  				cmd.Path.Path = "/some-path"
   259  			})
   260  			It(" displays the right stuff", func() {
   261  				Expect(testUI.Out).To(Say(`Showing route some-host\.%s\/some-path in org some-org / space some-space as some-user\.\.\.`, domainName))
   262  			})
   263  		})
   264  		When("passing in a domain and a port", func() {
   265  			BeforeEach(func() {
   266  				cmd.Hostname = ""
   267  				cmd.Path.Path = ""
   268  				cmd.Port = 8080
   269  			})
   270  			It(" displays the right stuff", func() {
   271  				Expect(testUI.Out).To(Say(`Showing route %s:8080 in org some-org / space some-space as some-user\.\.\.`, domainName))
   272  			})
   273  		})
   274  	})
   275  })