github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/unmap_route_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("unmap-route Command", func() {
    22  	var (
    23  		cmd             UnmapRouteCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		input           *Buffer
    29  		binaryName      string
    30  		executeErr      error
    31  		domain          string
    32  		appName         string
    33  		hostname        string
    34  		path            string
    35  		orgGUID         string
    36  		orgName         string
    37  		spaceGUID       string
    38  		spaceName       string
    39  		userName        string
    40  		port            int
    41  	)
    42  
    43  	BeforeEach(func() {
    44  		input = NewBuffer()
    45  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    46  		fakeConfig = new(commandfakes.FakeConfig)
    47  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    48  		fakeActor = new(v7fakes.FakeActor)
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  		appName = "my-app"
    53  		domain = "some-domain.com"
    54  		hostname = "host"
    55  		path = "/path"
    56  		orgGUID = "some-org-guid"
    57  		orgName = "some-org"
    58  		spaceGUID = "some-space-guid"
    59  		spaceName = "some-space"
    60  		userName = "steve"
    61  		port = 0
    62  
    63  		cmd = UnmapRouteCommand{
    64  			RequiredArgs: flag.AppDomain{App: appName, Domain: domain},
    65  			Hostname:     hostname,
    66  			Path:         flag.V7RoutePath{Path: path},
    67  			Port:         port,
    68  			BaseCommand: BaseCommand{
    69  				UI:          testUI,
    70  				Config:      fakeConfig,
    71  				SharedActor: fakeSharedActor,
    72  				Actor:       fakeActor,
    73  			},
    74  		}
    75  
    76  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    77  			Name: orgName,
    78  			GUID: orgGUID,
    79  		})
    80  
    81  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    82  			Name: spaceName,
    83  			GUID: spaceGUID,
    84  		})
    85  
    86  		fakeConfig.CurrentUserReturns(configv3.User{Name: userName}, nil)
    87  
    88  		fakeActor.GetDomainByNameReturns(
    89  			resources.Domain{Name: "some-domain.com", GUID: "domain-guid"},
    90  			v7action.Warnings{"get-domain-warnings"},
    91  			nil,
    92  		)
    93  
    94  		fakeActor.GetApplicationByNameAndSpaceReturns(
    95  			resources.Application{Name: "app", GUID: "app-guid"},
    96  			v7action.Warnings{"get-app-warnings"},
    97  			nil,
    98  		)
    99  
   100  		fakeActor.GetRouteByAttributesReturns(
   101  			resources.Route{GUID: "route-guid"},
   102  			v7action.Warnings{"get-route-warnings"},
   103  			nil,
   104  		)
   105  
   106  		fakeActor.GetRouteDestinationByAppGUIDReturns(
   107  			resources.RouteDestination{GUID: "destination-guid"},
   108  			nil,
   109  		)
   110  
   111  		fakeActor.UnmapRouteReturns(
   112  			v7action.Warnings{"unmap-route-warnings"},
   113  			nil,
   114  		)
   115  	})
   116  
   117  	JustBeforeEach(func() {
   118  		executeErr = cmd.Execute(nil)
   119  	})
   120  
   121  	When("checking target fails", func() {
   122  		BeforeEach(func() {
   123  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
   124  		})
   125  
   126  		It("returns an error", func() {
   127  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
   128  
   129  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   130  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   131  			Expect(checkTargetedOrg).To(BeTrue())
   132  			Expect(checkTargetedSpace).To(BeTrue())
   133  		})
   134  	})
   135  
   136  	When("the user is not logged in", func() {
   137  		var expectedErr error
   138  
   139  		BeforeEach(func() {
   140  			expectedErr = errors.New("some current user error")
   141  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   142  		})
   143  
   144  		It("return an error", func() {
   145  			Expect(executeErr).To(Equal(expectedErr))
   146  		})
   147  	})
   148  
   149  	When("getting the domain errors", func() {
   150  		BeforeEach(func() {
   151  			fakeActor.GetDomainByNameReturns(resources.Domain{}, v7action.Warnings{"get-domain-warnings"}, errors.New("get-domain-error"))
   152  		})
   153  
   154  		It("returns the error and displays warnings", func() {
   155  			Expect(testUI.Err).To(Say("get-domain-warnings"))
   156  			Expect(executeErr).To(MatchError(errors.New("get-domain-error")))
   157  			Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   158  		})
   159  	})
   160  
   161  	It("gets the domain and displays warnings", func() {
   162  		Expect(testUI.Err).To(Say("get-domain-warnings"))
   163  
   164  		Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   165  		Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domain))
   166  	})
   167  
   168  	When("getting the app errors", func() {
   169  		BeforeEach(func() {
   170  			fakeActor.GetApplicationByNameAndSpaceReturns(
   171  				resources.Application{},
   172  				v7action.Warnings{"get-app-warnings"},
   173  				errors.New("get-app-error"),
   174  			)
   175  		})
   176  
   177  		It("returns the error and displays warnings", func() {
   178  			Expect(testUI.Err).To(Say("get-app-warnings"))
   179  			Expect(executeErr).To(MatchError(errors.New("get-app-error")))
   180  			Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   181  		})
   182  	})
   183  
   184  	It("gets the app and displays the warnings", func() {
   185  		Expect(testUI.Err).To(Say("get-app-warnings"))
   186  
   187  		Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   188  		actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   189  		Expect(actualAppName).To(Equal(appName))
   190  		Expect(actualSpaceGUID).To(Equal(spaceGUID))
   191  	})
   192  
   193  	When("getting the route errors", func() {
   194  		BeforeEach(func() {
   195  			fakeActor.GetRouteByAttributesReturns(
   196  				resources.Route{},
   197  				v7action.Warnings{"get-route-warnings"},
   198  				errors.New("get-route-error"),
   199  			)
   200  		})
   201  
   202  		It("returns the error and displays warnings", func() {
   203  			Expect(testUI.Err).To(Say("get-route-warnings"))
   204  			Expect(executeErr).To(MatchError(errors.New("get-route-error")))
   205  
   206  			Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   207  		})
   208  	})
   209  
   210  	When("the route is TCP", func() {
   211  		BeforeEach(func() {
   212  			cmd.Hostname = ""
   213  			cmd.Path = flag.V7RoutePath{Path: ""}
   214  			cmd.Port = 1024
   215  		})
   216  
   217  		It("gets the routes and displays warnings", func() {
   218  			Expect(testUI.Err).To(Say("get-route-warnings"))
   219  
   220  			Expect(testUI.Out).To(Say(
   221  				`Removing route %s from app %s in org %s / space %s as %s\.\.\.`,
   222  				"some-domain.com:1024",
   223  				appName,
   224  				orgName,
   225  				spaceName,
   226  				userName,
   227  			))
   228  
   229  			Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   230  			actualDomain, _, _, actualPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   231  			Expect(actualDomain.Name).To(Equal("some-domain.com"))
   232  			Expect(actualDomain.GUID).To(Equal("domain-guid"))
   233  			Expect(actualPort).To(Equal(1024))
   234  		})
   235  	})
   236  
   237  	It("gets the routes and displays warnings", func() {
   238  		Expect(testUI.Err).To(Say("get-route-warnings"))
   239  
   240  		Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   241  		actualDomain, actualHostname, actualPath, actualPort := fakeActor.GetRouteByAttributesArgsForCall(0)
   242  		Expect(actualDomain.Name).To(Equal("some-domain.com"))
   243  		Expect(actualDomain.GUID).To(Equal("domain-guid"))
   244  		Expect(actualHostname).To(Equal(hostname))
   245  		Expect(actualPath).To(Equal(path))
   246  		Expect(actualPort).To(Equal(0))
   247  	})
   248  
   249  	It("prints flavor text", func() {
   250  		Expect(testUI.Out).To(Say(
   251  			`Removing route %s from app %s in org %s / space %s as %s\.\.\.`,
   252  			"host.some-domain.com/path",
   253  			appName,
   254  			orgName,
   255  			spaceName,
   256  			userName,
   257  		))
   258  	})
   259  
   260  	When("getting the route destination fails because the app is not mapped", func() {
   261  		BeforeEach(func() {
   262  			fakeActor.GetRouteDestinationByAppGUIDReturns(
   263  				resources.RouteDestination{},
   264  				actionerror.RouteDestinationNotFoundError{},
   265  			)
   266  		})
   267  
   268  		It("prints a message and returns without an error", func() {
   269  			Expect(executeErr).NotTo(HaveOccurred())
   270  			Expect(testUI.Out).To(Say("Route to be unmapped is not currently mapped to the application."))
   271  			Expect(testUI.Out).To(Say("OK"))
   272  		})
   273  	})
   274  
   275  	It("gets the route destination and prints the warnings", func() {
   276  		Expect(fakeActor.GetRouteDestinationByAppGUIDCallCount()).To(Equal(1))
   277  		givenRoute, givenAppGUID := fakeActor.GetRouteDestinationByAppGUIDArgsForCall(0)
   278  		Expect(givenRoute.GUID).To(Equal("route-guid"))
   279  		Expect(givenAppGUID).To(Equal("app-guid"))
   280  	})
   281  
   282  	When("unmapping the route fails", func() {
   283  		BeforeEach(func() {
   284  			fakeActor.UnmapRouteReturns(
   285  				v7action.Warnings{"unmap-route-warnings"},
   286  				errors.New("failed to unmap route"),
   287  			)
   288  		})
   289  
   290  		It("prints warnings and returns the error", func() {
   291  			Expect(fakeActor.UnmapRouteCallCount()).To(Equal(1))
   292  			givenRouteGUID, givenDestinationGUID := fakeActor.UnmapRouteArgsForCall(0)
   293  			Expect(givenRouteGUID).To(Equal("route-guid"))
   294  			Expect(givenDestinationGUID).To(Equal("destination-guid"))
   295  
   296  			Expect(executeErr).To(MatchError("failed to unmap route"))
   297  		})
   298  	})
   299  
   300  	It("prints warnings and does not return an error", func() {
   301  		Expect(executeErr).NotTo(HaveOccurred())
   302  		Expect(testUI.Out).To(Say("OK"))
   303  
   304  		Expect(fakeActor.UnmapRouteCallCount()).To(Equal(1))
   305  		givenRouteGUID, givenDestinationGUID := fakeActor.UnmapRouteArgsForCall(0)
   306  		Expect(givenRouteGUID).To(Equal("route-guid"))
   307  		Expect(givenDestinationGUID).To(Equal("destination-guid"))
   308  	})
   309  })