github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/command/v2/bind_service_command_test.go (about)

     1  package v2_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/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v2"
    11  	"code.cloudfoundry.org/cli/command/v2/v2fakes"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("bind-service Command", func() {
    20  	var (
    21  		cmd             BindServiceCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v2fakes.FakeBindServiceActor
    26  		binaryName      string
    27  		executeErr      error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    32  		fakeConfig = new(commandfakes.FakeConfig)
    33  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    34  		fakeActor = new(v2fakes.FakeBindServiceActor)
    35  
    36  		cmd = BindServiceCommand{
    37  			UI:          testUI,
    38  			Config:      fakeConfig,
    39  			SharedActor: fakeSharedActor,
    40  			Actor:       fakeActor,
    41  		}
    42  
    43  		cmd.RequiredArgs.AppName = "some-app"
    44  		cmd.RequiredArgs.ServiceInstanceName = "some-service"
    45  		cmd.ParametersAsJSON = map[string]interface{}{
    46  			"some-parameter": "some-value",
    47  		}
    48  
    49  		binaryName = "faceman"
    50  		fakeConfig.BinaryNameReturns("faceman")
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	Context("when a cloud controller API endpoint is set", func() {
    58  		BeforeEach(func() {
    59  			fakeConfig.TargetReturns("some-url")
    60  		})
    61  
    62  		Context("when checking target fails", func() {
    63  			BeforeEach(func() {
    64  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    65  			})
    66  
    67  			It("returns an error", func() {
    68  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    69  
    70  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    71  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    72  				Expect(checkTargetedOrg).To(BeTrue())
    73  				Expect(checkTargetedSpace).To(BeTrue())
    74  			})
    75  		})
    76  
    77  		Context("when the user is logged in, and an org and space are targeted", func() {
    78  			BeforeEach(func() {
    79  				fakeConfig.HasTargetedOrganizationReturns(true)
    80  				fakeConfig.HasTargetedSpaceReturns(true)
    81  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    82  					GUID: "some-org-guid",
    83  					Name: "some-org",
    84  				})
    85  				fakeConfig.TargetedSpaceReturns(configv3.Space{
    86  					GUID: "some-space-guid",
    87  					Name: "some-space",
    88  				})
    89  			})
    90  
    91  			Context("when getting the current user returns an error", func() {
    92  				var expectedErr error
    93  
    94  				BeforeEach(func() {
    95  					expectedErr = errors.New("got bananapants??")
    96  					fakeConfig.CurrentUserReturns(
    97  						configv3.User{},
    98  						expectedErr)
    99  				})
   100  
   101  				It("returns the error", func() {
   102  					Expect(executeErr).To(MatchError(expectedErr))
   103  				})
   104  			})
   105  
   106  			Context("when getting the current user does not return an error", func() {
   107  				BeforeEach(func() {
   108  					fakeConfig.CurrentUserReturns(
   109  						configv3.User{Name: "some-user"},
   110  						nil)
   111  				})
   112  
   113  				It("displays flavor text", func() {
   114  					Expect(testUI.Out).To(Say("Binding service some-service to app some-app in org some-org / space some-space as some-user..."))
   115  
   116  					Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   117  				})
   118  
   119  				Context("when the service was already bound", func() {
   120  					BeforeEach(func() {
   121  						fakeActor.BindServiceBySpaceReturns(
   122  							[]string{"foo", "bar"},
   123  							ccerror.ServiceBindingTakenError{})
   124  					})
   125  
   126  					It("displays warnings and 'OK'", func() {
   127  						Expect(executeErr).NotTo(HaveOccurred())
   128  
   129  						Expect(testUI.Err).To(Say("foo"))
   130  						Expect(testUI.Err).To(Say("bar"))
   131  						Expect(testUI.Out).To(Say("App some-app is already bound to some-service."))
   132  						Expect(testUI.Out).To(Say("OK"))
   133  					})
   134  				})
   135  
   136  				Context("when binding the service instance results in an error other than ServiceBindingTakenError", func() {
   137  					BeforeEach(func() {
   138  						fakeActor.BindServiceBySpaceReturns(
   139  							nil,
   140  							actionerror.ApplicationNotFoundError{Name: "some-app"})
   141  					})
   142  
   143  					It("should return the error", func() {
   144  						Expect(executeErr).To(MatchError(actionerror.ApplicationNotFoundError{
   145  							Name: "some-app",
   146  						}))
   147  					})
   148  				})
   149  
   150  				Context("when the service binding is successful", func() {
   151  					BeforeEach(func() {
   152  						fakeActor.BindServiceBySpaceReturns(
   153  							v2action.Warnings{"some-warning", "another-warning"},
   154  							nil,
   155  						)
   156  					})
   157  
   158  					It("displays OK and the TIP", func() {
   159  						Expect(executeErr).ToNot(HaveOccurred())
   160  
   161  						Expect(testUI.Out).To(Say("OK"))
   162  						Expect(testUI.Out).To(Say("TIP: Use 'faceman restage some-app' to ensure your env variable changes take effect"))
   163  						Expect(testUI.Err).To(Say("some-warning"))
   164  						Expect(testUI.Err).To(Say("another-warning"))
   165  
   166  						Expect(fakeActor.BindServiceBySpaceCallCount()).To(Equal(1))
   167  						appName, serviceInstanceName, spaceGUID, parameters := fakeActor.BindServiceBySpaceArgsForCall(0)
   168  						Expect(appName).To(Equal("some-app"))
   169  						Expect(serviceInstanceName).To(Equal("some-service"))
   170  						Expect(spaceGUID).To(Equal("some-space-guid"))
   171  						Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"}))
   172  					})
   173  				})
   174  			})
   175  		})
   176  	})
   177  })