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