github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/update_destination_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("update Command", func() {
    20  	var (
    21  		cmd             v7.UpdateDestinationCommand
    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  		appName         string
    30  		appProtocol     string
    31  		orgGUID         string
    32  		spaceGUID       string
    33  		hostname        string
    34  		path            string
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    39  		fakeConfig = new(commandfakes.FakeConfig)
    40  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    41  		fakeActor = new(v7fakes.FakeActor)
    42  
    43  		binaryName = "faceman"
    44  		fakeConfig.BinaryNameReturns(binaryName)
    45  
    46  		domainName = "some-domain.com"
    47  		appName = "super-app"
    48  		appProtocol = "http2"
    49  		orgGUID = "some-org-guid"
    50  		spaceGUID = "some-space-guid"
    51  		hostname = "hostname"
    52  		path = "path"
    53  
    54  		cmd = v7.UpdateDestinationCommand{
    55  			BaseCommand: v7.BaseCommand{
    56  				UI:          testUI,
    57  				Config:      fakeConfig,
    58  				SharedActor: fakeSharedActor,
    59  				Actor:       fakeActor,
    60  			},
    61  			RequiredArgs: flag.AppDomain{App: appName, Domain: domainName},
    62  			AppProtocol:  appProtocol,
    63  			Hostname:     hostname,
    64  			Path:         flag.V7RoutePath{Path: path},
    65  		}
    66  
    67  		fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: spaceGUID})
    68  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org", GUID: orgGUID})
    69  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    70  
    71  	})
    72  
    73  	JustBeforeEach(func() {
    74  		executeErr = cmd.Execute(nil)
    75  	})
    76  
    77  	It("checks the target", func() {
    78  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    79  		checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    80  		Expect(checkTargetedOrg).To(BeTrue())
    81  		Expect(checkTargetedSpace).To(BeTrue())
    82  	})
    83  
    84  	When("checking target fails", func() {
    85  		BeforeEach(func() {
    86  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    87  		})
    88  		It("returns an error", func() {
    89  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    90  
    91  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    92  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    93  			Expect(checkTargetedOrg).To(BeTrue())
    94  			Expect(checkTargetedSpace).To(BeTrue())
    95  		})
    96  	})
    97  
    98  	When("the user is not logged in", func() {
    99  		var expectedErr error
   100  
   101  		BeforeEach(func() {
   102  			expectedErr = errors.New("some current user error")
   103  			fakeActor.GetCurrentUserReturns(configv3.User{}, expectedErr)
   104  		})
   105  
   106  		It("return an error", func() {
   107  			Expect(executeErr).To(Equal(expectedErr))
   108  		})
   109  	})
   110  
   111  	When("the user is logged in and targeted", func() {
   112  		When("getting the domain errors", func() {
   113  			BeforeEach(func() {
   114  				fakeActor.GetDomainByNameReturns(resources.Domain{}, v7action.Warnings{"get-domain-warnings"}, errors.New("get-domain-error"))
   115  			})
   116  			It("returns the error and displays warnings", func() {
   117  				Expect(testUI.Err).To(Say("get-domain-warnings"))
   118  				Expect(executeErr).To(MatchError(errors.New("get-domain-error")))
   119  
   120  				Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   121  				Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   122  
   123  				Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(0))
   124  
   125  				Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(0))
   126  
   127  				Expect(fakeActor.GetRouteDestinationByAppGUIDCallCount()).To(Equal(0))
   128  
   129  				Expect(fakeActor.UpdateDestinationCallCount()).To(Equal(0))
   130  			})
   131  		})
   132  
   133  		When("getting the domain succeeds", func() {
   134  			BeforeEach(func() {
   135  				fakeActor.GetDomainByNameReturns(
   136  					resources.Domain{Name: domainName, GUID: "domain-guid"},
   137  					v7action.Warnings{"get-domain-warnings"},
   138  					nil,
   139  				)
   140  			})
   141  
   142  			When("getting the app errors", func() {
   143  				BeforeEach(func() {
   144  					fakeActor.GetApplicationByNameAndSpaceReturns(
   145  						resources.Application{},
   146  						v7action.Warnings{"get-app-warnings"},
   147  						errors.New("get-app-error"),
   148  					)
   149  				})
   150  				It("returns the error and displays warnings", func() {
   151  					Expect(testUI.Err).To(Say("get-domain-warnings"))
   152  					Expect(testUI.Err).To(Say("get-app-warnings"))
   153  					Expect(executeErr).To(MatchError(errors.New("get-app-error")))
   154  
   155  					Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   156  					Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   157  
   158  					Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   159  					actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   160  					Expect(actualAppName).To(Equal(appName))
   161  					Expect(actualSpaceGUID).To(Equal(spaceGUID))
   162  
   163  					Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(0))
   164  
   165  					Expect(fakeActor.CreateRouteCallCount()).To(Equal(0))
   166  
   167  					Expect(fakeActor.MapRouteCallCount()).To(Equal(0))
   168  				})
   169  			})
   170  
   171  			When("getting the app succeeds", func() {
   172  				BeforeEach(func() {
   173  					fakeActor.GetApplicationByNameAndSpaceReturns(
   174  						resources.Application{GUID: "app-guid", Name: appName},
   175  						v7action.Warnings{"get-app-warnings"},
   176  						nil,
   177  					)
   178  				})
   179  
   180  				When("getting the route errors", func() {
   181  					BeforeEach(func() {
   182  						fakeActor.GetRouteByAttributesReturns(
   183  							resources.Route{},
   184  							v7action.Warnings{"get-route-warnings"},
   185  							errors.New("get-route-error"),
   186  						)
   187  					})
   188  
   189  					It("returns the error and displays warnings", func() {
   190  						Expect(testUI.Err).To(Say("get-domain-warnings"))
   191  						Expect(testUI.Err).To(Say("get-app-warnings"))
   192  						Expect(testUI.Err).To(Say("get-route-warnings"))
   193  						Expect(executeErr).To(MatchError(errors.New("get-route-error")))
   194  
   195  						Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   196  						Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   197  
   198  						Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   199  						actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   200  						Expect(actualAppName).To(Equal(appName))
   201  						Expect(actualSpaceGUID).To(Equal(spaceGUID))
   202  
   203  						Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   204  						actualDomain, actualHostname, actualPath, actualPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   205  						Expect(actualDomain.Name).To(Equal(domainName))
   206  						Expect(actualDomain.GUID).To(Equal("domain-guid"))
   207  						Expect(actualHostname).To(Equal(hostname))
   208  						Expect(actualPath).To(Equal(path))
   209  						Expect(actualPort).To(Equal(0))
   210  
   211  						Expect(fakeActor.UpdateDestinationCallCount()).To(Equal(0))
   212  
   213  					})
   214  				})
   215  
   216  				When("the requested route does not exist", func() {
   217  					BeforeEach(func() {
   218  						fakeActor.GetRouteByAttributesReturns(
   219  							resources.Route{},
   220  							v7action.Warnings{"get-route-warnings"},
   221  							actionerror.RouteNotFoundError{},
   222  						)
   223  					})
   224  
   225  					It("displays error message", func() {
   226  						Expect(testUI.Err).To(Say("get-domain-warnings"))
   227  						Expect(testUI.Err).To(Say("get-route-warnings"))
   228  						Expect(executeErr).To(HaveOccurred())
   229  
   230  						Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   231  						Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   232  
   233  						Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   234  						actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   235  						Expect(actualAppName).To(Equal(appName))
   236  						Expect(actualSpaceGUID).To(Equal(spaceGUID))
   237  
   238  						Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   239  						actualDomain, actualHostname, actualPath, actualPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   240  						Expect(actualDomain.Name).To(Equal(domainName))
   241  						Expect(actualDomain.GUID).To(Equal("domain-guid"))
   242  						Expect(actualHostname).To(Equal(hostname))
   243  						Expect(actualPath).To(Equal(path))
   244  						Expect(actualPort).To(Equal(0))
   245  					})
   246  				})
   247  
   248  				When("the requested route exists", func() {
   249  					BeforeEach(func() {
   250  						fakeActor.GetRouteByAttributesReturns(
   251  							resources.Route{GUID: "route-guid"},
   252  							v7action.Warnings{"get-route-warnings"},
   253  							nil,
   254  						)
   255  					})
   256  					When("getting the destination errors", func() {
   257  						BeforeEach(func() {
   258  							fakeActor.GetRouteDestinationByAppGUIDReturns(
   259  								resources.RouteDestination{},
   260  								errors.New("get-destination-error"),
   261  							)
   262  						})
   263  						It("returns the error and warnings", func() {
   264  							Expect(executeErr).To(MatchError(errors.New("get-destination-error")))
   265  
   266  							Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   267  							Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   268  
   269  							Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   270  							actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   271  							Expect(actualAppName).To(Equal(appName))
   272  							Expect(actualSpaceGUID).To(Equal(spaceGUID))
   273  
   274  							Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   275  							actualDomain, actualHostname, actualPath, actualPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   276  							Expect(actualDomain.Name).To(Equal(domainName))
   277  							Expect(actualDomain.GUID).To(Equal("domain-guid"))
   278  							Expect(actualHostname).To(Equal(hostname))
   279  							Expect(actualPath).To(Equal(path))
   280  							Expect(actualPort).To(Equal(0))
   281  
   282  							Expect(fakeActor.GetRouteDestinationByAppGUIDCallCount()).To(Equal(1))
   283  							actualRoute, actualAppGUID := fakeActor.GetRouteDestinationByAppGUIDArgsForCall(0)
   284  							Expect(actualRoute.GUID).To(Equal("route-guid"))
   285  							Expect(actualAppGUID).To(Equal("app-guid"))
   286  
   287  							Expect(fakeActor.UpdateDestinationCallCount()).To(Equal(0))
   288  						})
   289  					})
   290  					When("the destination already exists", func() {
   291  						BeforeEach(func() {
   292  							fakeActor.GetRouteDestinationByAppGUIDReturns(
   293  								resources.RouteDestination{
   294  									GUID: "route-dst-guid",
   295  									App: resources.RouteDestinationApp{
   296  										GUID: "existing-app-guid",
   297  									},
   298  								},
   299  								nil,
   300  							)
   301  						})
   302  
   303  						It("exits 0 with a helpful message that the destination protocol was changed", func() {
   304  							Expect(executeErr).ShouldNot(HaveOccurred())
   305  
   306  							Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   307  							Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   308  
   309  							Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   310  							actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   311  							Expect(actualAppName).To(Equal(appName))
   312  							Expect(actualSpaceGUID).To(Equal(spaceGUID))
   313  
   314  							Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   315  							actualDomain, actualHostname, actualPath, actualPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   316  							Expect(actualDomain.Name).To(Equal(domainName))
   317  							Expect(actualDomain.GUID).To(Equal("domain-guid"))
   318  							Expect(actualHostname).To(Equal(hostname))
   319  							Expect(actualPath).To(Equal(path))
   320  							Expect(actualPort).To(Equal(0))
   321  
   322  							Expect(fakeActor.GetRouteDestinationByAppGUIDCallCount()).To(Equal(1))
   323  							actualRoute, actualAppGUID := fakeActor.GetRouteDestinationByAppGUIDArgsForCall(0)
   324  							Expect(actualRoute.GUID).To(Equal("route-guid"))
   325  							Expect(actualAppGUID).To(Equal("app-guid"))
   326  							Expect(fakeActor.UpdateDestinationCallCount()).To(Equal(1))
   327  						})
   328  					})
   329  
   330  					When("the destination is not found", func() {
   331  						When("updating the destination errors", func() {
   332  							BeforeEach(func() {
   333  								fakeActor.UpdateDestinationReturns(v7action.Warnings{"update-dest-warnings"}, errors.New("map-route-error"))
   334  								fakeActor.GetRouteDestinationByAppGUIDReturns(
   335  									resources.RouteDestination{
   336  										GUID: "route-dst-guid",
   337  										App: resources.RouteDestinationApp{
   338  											GUID: "existing-app-guid",
   339  										},
   340  									},
   341  									nil,
   342  								)
   343  							})
   344  
   345  							It("returns the error and displays warnings", func() {
   346  								Expect(testUI.Err).To(Say("get-domain-warnings"))
   347  								Expect(testUI.Err).To(Say("get-app-warnings"))
   348  								Expect(testUI.Err).To(Say("get-route-warnings"))
   349  								Expect(testUI.Err).To(Say("update-dest-warnings"))
   350  								Expect(executeErr).To(MatchError(errors.New("map-route-error")))
   351  
   352  								Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   353  								Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domainName))
   354  
   355  								Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   356  								actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   357  								Expect(actualAppName).To(Equal(appName))
   358  								Expect(actualSpaceGUID).To(Equal(spaceGUID))
   359  
   360  								Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   361  								actualDomain, actualHostname, actualPath, actualPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   362  								Expect(actualDomain.Name).To(Equal(domainName))
   363  								Expect(actualDomain.GUID).To(Equal("domain-guid"))
   364  								Expect(actualHostname).To(Equal(hostname))
   365  								Expect(actualPath).To(Equal(path))
   366  								Expect(actualPort).To(Equal(0))
   367  
   368  								Expect(fakeActor.UpdateDestinationCallCount()).To(Equal(1))
   369  								actualRouteGUID, destinationGUID, protocol := fakeActor.UpdateDestinationArgsForCall(0)
   370  								Expect(actualRouteGUID).To(Equal("route-guid"))
   371  								Expect(destinationGUID).To(Equal("route-dst-guid"))
   372  								Expect(protocol).To(Equal("http2"))
   373  							})
   374  						})
   375  					})
   376  				})
   377  			})
   378  		})
   379  	})
   380  })