github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/actor/v7pushaction/route_test.go (about)

     1  package v7pushaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  	. "code.cloudfoundry.org/cli/actor/v7pushaction"
    10  	"code.cloudfoundry.org/cli/actor/v7pushaction/v7pushactionfakes"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Routes", func() {
    17  	var (
    18  		actor       *Actor
    19  		fakeV2Actor *v7pushactionfakes.FakeV2Actor
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		actor, fakeV2Actor, _, _ = getTestPushActor()
    24  	})
    25  
    26  	Describe("CreateAndMapDefaultApplicationRoute", func() {
    27  		var (
    28  			orgGUID     string
    29  			spaceGUID   string
    30  			application v7action.Application
    31  
    32  			warnings   Warnings
    33  			executeErr error
    34  		)
    35  
    36  		BeforeEach(func() {
    37  			orgGUID = "some-org-guid"
    38  			spaceGUID = "some-space-guid"
    39  			application = v7action.Application{Name: "some-app", GUID: "some-app-guid"}
    40  		})
    41  
    42  		JustBeforeEach(func() {
    43  			warnings, executeErr = actor.CreateAndMapDefaultApplicationRoute(orgGUID, spaceGUID, application)
    44  		})
    45  
    46  		When("getting organization domains errors", func() {
    47  			BeforeEach(func() {
    48  				fakeV2Actor.GetOrganizationDomainsReturns(
    49  					[]v2action.Domain{},
    50  					v2action.Warnings{"domain-warning"},
    51  					errors.New("some-error"))
    52  			})
    53  
    54  			It("returns the error", func() {
    55  				Expect(executeErr).To(MatchError("some-error"))
    56  				Expect(warnings).To(ConsistOf("domain-warning"))
    57  			})
    58  		})
    59  
    60  		When("getting organization domains succeeds", func() {
    61  			BeforeEach(func() {
    62  				fakeV2Actor.GetOrganizationDomainsReturns(
    63  					[]v2action.Domain{
    64  						{
    65  							GUID: "some-domain-guid",
    66  							Name: "some-domain",
    67  						},
    68  					},
    69  					v2action.Warnings{"domain-warning"},
    70  					nil,
    71  				)
    72  			})
    73  
    74  			When("getting the application routes errors", func() {
    75  				BeforeEach(func() {
    76  					fakeV2Actor.GetApplicationRoutesReturns(
    77  						[]v2action.Route{},
    78  						v2action.Warnings{"route-warning"},
    79  						errors.New("some-error"),
    80  					)
    81  				})
    82  
    83  				It("returns the error", func() {
    84  					Expect(executeErr).To(MatchError("some-error"))
    85  					Expect(warnings).To(ConsistOf("domain-warning", "route-warning"))
    86  				})
    87  			})
    88  
    89  			When("getting the application routes succeeds", func() {
    90  				When("the route is already bound to the app", func() {
    91  					BeforeEach(func() {
    92  						fakeV2Actor.GetApplicationRoutesReturns(
    93  							[]v2action.Route{
    94  								{
    95  									Host: "some-app",
    96  									Domain: v2action.Domain{
    97  										GUID: "some-domain-guid",
    98  										Name: "some-domain",
    99  									},
   100  									GUID:      "some-route-guid",
   101  									SpaceGUID: "some-space-guid",
   102  								},
   103  							},
   104  							v2action.Warnings{"route-warning"},
   105  							nil,
   106  						)
   107  					})
   108  
   109  					It("returns any warnings", func() {
   110  						Expect(executeErr).ToNot(HaveOccurred())
   111  						Expect(warnings).To(ConsistOf("domain-warning", "route-warning"))
   112  
   113  						Expect(fakeV2Actor.GetOrganizationDomainsCallCount()).To(Equal(1), "Expected GetOrganizationDomains to be called once, but it was not")
   114  						orgGUID := fakeV2Actor.GetOrganizationDomainsArgsForCall(0)
   115  						Expect(orgGUID).To(Equal("some-org-guid"))
   116  
   117  						Expect(fakeV2Actor.GetApplicationRoutesCallCount()).To(Equal(1), "Expected GetApplicationRoutes to be called once, but it was not")
   118  						appGUID := fakeV2Actor.GetApplicationRoutesArgsForCall(0)
   119  						Expect(appGUID).To(Equal("some-app-guid"))
   120  
   121  						Expect(fakeV2Actor.CreateRouteCallCount()).To(Equal(0), "Expected CreateRoute to not be called but it was")
   122  						Expect(fakeV2Actor.MapRouteToApplicationCallCount()).To(Equal(0), "Expected MapRouteToApplication to not be called but it was")
   123  					})
   124  				})
   125  
   126  				When("the route isn't bound to the app", func() {
   127  					When("finding route in space errors", func() {
   128  						BeforeEach(func() {
   129  							fakeV2Actor.FindRouteBoundToSpaceWithSettingsReturns(
   130  								v2action.Route{},
   131  								v2action.Warnings{"route-warning"},
   132  								errors.New("some-error"),
   133  							)
   134  						})
   135  
   136  						It("returns the error", func() {
   137  							Expect(executeErr).To(MatchError("some-error"))
   138  							Expect(warnings).To(ConsistOf("domain-warning", "route-warning"))
   139  						})
   140  					})
   141  
   142  					When("the route exists", func() {
   143  						BeforeEach(func() {
   144  							fakeV2Actor.FindRouteBoundToSpaceWithSettingsReturns(
   145  								v2action.Route{
   146  									GUID: "some-route-guid",
   147  									Host: "some-app",
   148  									Domain: v2action.Domain{
   149  										Name: "some-domain",
   150  										GUID: "some-domain-guid",
   151  									},
   152  									SpaceGUID: "some-space-guid",
   153  								},
   154  								v2action.Warnings{"route-warning"},
   155  								nil,
   156  							)
   157  						})
   158  
   159  						When("the map command returns an error", func() {
   160  							BeforeEach(func() {
   161  								fakeV2Actor.MapRouteToApplicationReturns(
   162  									v2action.Warnings{"map-warning"},
   163  									errors.New("some-error"),
   164  								)
   165  							})
   166  
   167  							It("returns the error", func() {
   168  								Expect(executeErr).To(MatchError("some-error"))
   169  								Expect(warnings).To(ConsistOf("domain-warning", "route-warning", "map-warning"))
   170  							})
   171  						})
   172  
   173  						When("the map command succeeds", func() {
   174  							BeforeEach(func() {
   175  								fakeV2Actor.MapRouteToApplicationReturns(
   176  									v2action.Warnings{"map-warning"},
   177  									nil,
   178  								)
   179  							})
   180  
   181  							It("maps the route to the app and returns any warnings", func() {
   182  								Expect(executeErr).ToNot(HaveOccurred())
   183  								Expect(warnings).To(ConsistOf("domain-warning", "route-warning", "map-warning"))
   184  
   185  								Expect(fakeV2Actor.FindRouteBoundToSpaceWithSettingsCallCount()).To(Equal(1), "Expected FindRouteBoundToSpaceWithSettings to be called once, but it was not")
   186  								spaceRoute := fakeV2Actor.FindRouteBoundToSpaceWithSettingsArgsForCall(0)
   187  								Expect(spaceRoute).To(Equal(v2action.Route{
   188  									Host: "some-app",
   189  									Domain: v2action.Domain{
   190  										Name: "some-domain",
   191  										GUID: "some-domain-guid",
   192  									},
   193  									SpaceGUID: "some-space-guid",
   194  								}))
   195  
   196  								Expect(fakeV2Actor.MapRouteToApplicationCallCount()).To(Equal(1), "Expected MapRouteToApplication to be called once, but it was not")
   197  								routeGUID, appGUID := fakeV2Actor.MapRouteToApplicationArgsForCall(0)
   198  								Expect(routeGUID).To(Equal("some-route-guid"))
   199  								Expect(appGUID).To(Equal("some-app-guid"))
   200  							})
   201  						})
   202  					})
   203  
   204  					When("the route does not exist", func() {
   205  						BeforeEach(func() {
   206  							fakeV2Actor.FindRouteBoundToSpaceWithSettingsReturns(
   207  								v2action.Route{},
   208  								v2action.Warnings{"route-warning"},
   209  								actionerror.RouteNotFoundError{},
   210  							)
   211  						})
   212  
   213  						When("the create route command errors", func() {
   214  							BeforeEach(func() {
   215  								fakeV2Actor.CreateRouteReturns(
   216  									v2action.Route{},
   217  									v2action.Warnings{"route-create-warning"},
   218  									errors.New("some-error"),
   219  								)
   220  							})
   221  
   222  							It("returns the error", func() {
   223  								Expect(executeErr).To(MatchError("some-error"))
   224  								Expect(warnings).To(ConsistOf("domain-warning", "route-warning", "route-create-warning"))
   225  							})
   226  						})
   227  
   228  						When("the create route command succeeds", func() {
   229  							BeforeEach(func() {
   230  								fakeV2Actor.CreateRouteReturns(
   231  									v2action.Route{
   232  										GUID: "some-route-guid",
   233  										Host: "some-app",
   234  										Domain: v2action.Domain{
   235  											Name: "some-domain",
   236  											GUID: "some-domain-guid",
   237  										},
   238  										SpaceGUID: "some-space-guid",
   239  									},
   240  									v2action.Warnings{"route-create-warning"},
   241  									nil,
   242  								)
   243  							})
   244  
   245  							When("the map command errors", func() {
   246  								BeforeEach(func() {
   247  									fakeV2Actor.MapRouteToApplicationReturns(
   248  										v2action.Warnings{"map-warning"},
   249  										errors.New("some-error"),
   250  									)
   251  								})
   252  
   253  								It("returns the error", func() {
   254  									Expect(executeErr).To(MatchError("some-error"))
   255  									Expect(warnings).To(ConsistOf("domain-warning", "route-warning", "route-create-warning", "map-warning"))
   256  								})
   257  							})
   258  							When("the map command succeeds", func() {
   259  								BeforeEach(func() {
   260  									fakeV2Actor.MapRouteToApplicationReturns(
   261  										v2action.Warnings{"map-warning"},
   262  										nil,
   263  									)
   264  								})
   265  
   266  								It("creates the route, maps it to the app, and returns any warnings", func() {
   267  									Expect(executeErr).ToNot(HaveOccurred())
   268  									Expect(warnings).To(ConsistOf("domain-warning", "route-warning", "route-create-warning", "map-warning"))
   269  
   270  									Expect(fakeV2Actor.CreateRouteCallCount()).To(Equal(1), "Expected CreateRoute to be called once, but it was not")
   271  									defaultRoute, shouldGeneratePort := fakeV2Actor.CreateRouteArgsForCall(0)
   272  									Expect(defaultRoute).To(Equal(v2action.Route{
   273  										Host: "some-app",
   274  										Domain: v2action.Domain{
   275  											Name: "some-domain",
   276  											GUID: "some-domain-guid",
   277  										},
   278  										SpaceGUID: "some-space-guid",
   279  									}))
   280  									Expect(shouldGeneratePort).To(BeFalse())
   281  
   282  									Expect(fakeV2Actor.FindRouteBoundToSpaceWithSettingsCallCount()).To(Equal(1), "Expected FindRouteBoundToSpaceWithSettings to be called once, but it was not")
   283  									spaceRoute := fakeV2Actor.FindRouteBoundToSpaceWithSettingsArgsForCall(0)
   284  									Expect(spaceRoute).To(Equal(v2action.Route{
   285  										Host: "some-app",
   286  										Domain: v2action.Domain{
   287  											Name: "some-domain",
   288  											GUID: "some-domain-guid",
   289  										},
   290  										SpaceGUID: "some-space-guid",
   291  									}))
   292  
   293  									Expect(fakeV2Actor.MapRouteToApplicationCallCount()).To(Equal(1), "Expected MapRouteToApplication to be called once, but it was not")
   294  									routeGUID, appGUID := fakeV2Actor.MapRouteToApplicationArgsForCall(0)
   295  									Expect(routeGUID).To(Equal("some-route-guid"))
   296  									Expect(appGUID).To(Equal("some-app-guid"))
   297  								})
   298  							})
   299  						})
   300  					})
   301  				})
   302  			})
   303  		})
   304  	})
   305  })