github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/share_service_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	"code.cloudfoundry.org/cli/command/v3"
    12  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("share-service Command", func() {
    21  	var (
    22  		cmd             v3.ShareServiceCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v3fakes.FakeShareServiceActor
    27  		binaryName      string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v3fakes.FakeShareServiceActor)
    36  
    37  		cmd = v3.ShareServiceCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeActor,
    42  		}
    43  
    44  		cmd.RequiredArgs.ServiceInstance = "some-service-instance"
    45  		cmd.SpaceName = "some-space"
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  		fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinVersionShareServiceV3)
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	Context("when the API version is below the minimum", func() {
    57  		BeforeEach(func() {
    58  			fakeActor.CloudControllerV3APIVersionReturns("0.0.0")
    59  		})
    60  
    61  		It("returns a MinimumAPIVersionNotMetError", func() {
    62  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    63  				CurrentVersion: "0.0.0",
    64  				MinimumVersion: ccversion.MinVersionShareServiceV3,
    65  			}))
    66  		})
    67  	})
    68  
    69  	Context("when the environment is not correctly setup", func() {
    70  		BeforeEach(func() {
    71  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    72  		})
    73  
    74  		It("returns an error", func() {
    75  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    76  
    77  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    78  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    79  			Expect(checkTargetedOrg).To(BeTrue())
    80  			Expect(checkTargetedSpace).To(BeTrue())
    81  		})
    82  	})
    83  
    84  	Context("when the user is logged in, and a space and org are targeted", func() {
    85  		BeforeEach(func() {
    86  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    87  				GUID: "some-org-guid",
    88  				Name: "some-org",
    89  			})
    90  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    91  				GUID: "some-space-guid",
    92  			})
    93  		})
    94  
    95  		Context("when an error occurs getting the current user", func() {
    96  			var expectedErr error
    97  
    98  			BeforeEach(func() {
    99  				expectedErr = errors.New("get current user error")
   100  				fakeConfig.CurrentUserReturns(
   101  					configv3.User{},
   102  					expectedErr)
   103  			})
   104  
   105  			It("returns the error", func() {
   106  				Expect(executeErr).To(MatchError(expectedErr))
   107  			})
   108  		})
   109  
   110  		Context("when no errors occur getting the current user", func() {
   111  			BeforeEach(func() {
   112  				fakeConfig.CurrentUserReturns(
   113  					configv3.User{Name: "some-user"},
   114  					nil)
   115  			})
   116  
   117  			Context("when '-o' (org name) is not provided", func() {
   118  				Context("when no errors occur sharing the service instance", func() {
   119  					BeforeEach(func() {
   120  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   121  							v2v3action.Warnings{"share-service-warning"},
   122  							nil)
   123  					})
   124  
   125  					It("shares the service instance with the provided space and displays all warnings", func() {
   126  						Expect(executeErr).ToNot(HaveOccurred())
   127  
   128  						Expect(testUI.Out).To(Say("Sharing service instance some-service-instance into org some-org / space some-space as some-user\\.\\.\\."))
   129  						Expect(testUI.Out).To(Say("OK"))
   130  
   131  						Expect(testUI.Err).To(Say("share-service-warning"))
   132  
   133  						Expect(fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationCallCount()).To(Equal(1))
   134  						spaceNameArg, serviceInstanceNameArg, sourceSpaceGUIDArg, orgGUIDArg := fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationArgsForCall(0)
   135  						Expect(spaceNameArg).To(Equal("some-space"))
   136  						Expect(serviceInstanceNameArg).To(Equal("some-service-instance"))
   137  						Expect(sourceSpaceGUIDArg).To(Equal("some-space-guid"))
   138  						Expect(orgGUIDArg).To(Equal("some-org-guid"))
   139  					})
   140  				})
   141  
   142  				Context("when an error occurs sharing the service instance", func() {
   143  					var expectedErr error
   144  
   145  					BeforeEach(func() {
   146  						expectedErr = errors.New("sharing failed")
   147  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   148  							v2v3action.Warnings{"share-service-warning"},
   149  							expectedErr)
   150  					})
   151  
   152  					It("returns the error, and displays all warnings", func() {
   153  						Expect(executeErr).To(MatchError(expectedErr))
   154  
   155  						Expect(testUI.Out).ToNot(Say("OK"))
   156  						Expect(testUI.Err).To(Say("share-service-warning"))
   157  					})
   158  				})
   159  
   160  				Context("when the service instance is not shareable", func() {
   161  					var expectedErr error
   162  
   163  					BeforeEach(func() {
   164  						expectedErr = actionerror.ServiceInstanceNotShareableError{
   165  							FeatureFlagEnabled:          true,
   166  							ServiceBrokerSharingEnabled: false}
   167  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   168  							v2v3action.Warnings{"share-service-instance-warning"},
   169  							expectedErr)
   170  					})
   171  
   172  					It("returns ServiceInstanceNotShareableError and displays all warnings", func() {
   173  						Expect(executeErr).To(MatchError(expectedErr))
   174  
   175  						Expect(testUI.Out).ToNot(Say("OK"))
   176  						Expect(testUI.Err).To(Say("share-service-instance-warning"))
   177  					})
   178  				})
   179  
   180  				Context("when the service instance is already shared to the space", func() {
   181  					BeforeEach(func() {
   182  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   183  							v2v3action.Warnings{"share-service-warning"},
   184  							actionerror.ServiceInstanceAlreadySharedError{})
   185  					})
   186  
   187  					It("does not return an error and displays all warnings", func() {
   188  						Expect(executeErr).ToNot(HaveOccurred())
   189  
   190  						Expect(testUI.Out).To(Say("Service instance some-service-instance is already shared with that space."))
   191  						Expect(testUI.Out).To(Say("OK"))
   192  
   193  						Expect(testUI.Err).To(Say("share-service-warning"))
   194  					})
   195  				})
   196  			})
   197  
   198  			Context("when -o (org name) is provided", func() {
   199  				BeforeEach(func() {
   200  					cmd.OrgName = "some-other-org"
   201  				})
   202  
   203  				Context("when no errors occur sharing the service instance", func() {
   204  					BeforeEach(func() {
   205  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   206  							v2v3action.Warnings{"share-service-warning"},
   207  							nil)
   208  					})
   209  
   210  					It("shares the service instance with the provided space and org and displays all warnings", func() {
   211  						Expect(executeErr).ToNot(HaveOccurred())
   212  
   213  						Expect(testUI.Out).To(Say("Sharing service instance some-service-instance into org some-other-org / space some-space as some-user\\.\\.\\."))
   214  						Expect(testUI.Out).To(Say("OK"))
   215  
   216  						Expect(testUI.Err).To(Say("share-service-warning"))
   217  
   218  						Expect(fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameCallCount()).To(Equal(1))
   219  						spaceNameArg, serviceInstanceNameArg, sourceSpaceGUID, orgName := fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameArgsForCall(0)
   220  						Expect(spaceNameArg).To(Equal("some-space"))
   221  						Expect(serviceInstanceNameArg).To(Equal("some-service-instance"))
   222  						Expect(sourceSpaceGUID).To(Equal("some-space-guid"))
   223  						Expect(orgName).To(Equal("some-other-org"))
   224  					})
   225  				})
   226  
   227  				Context("when an error occurs sharing the service instance", func() {
   228  					var expectedErr error
   229  
   230  					BeforeEach(func() {
   231  						expectedErr = errors.New("sharing failed")
   232  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   233  							v2v3action.Warnings{"share-service-warning"},
   234  							expectedErr)
   235  					})
   236  
   237  					It("returns the error and displays all warnings", func() {
   238  						Expect(executeErr).To(MatchError(expectedErr))
   239  
   240  						Expect(testUI.Out).ToNot(Say("OK"))
   241  						Expect(testUI.Err).To(Say("share-service-warning"))
   242  					})
   243  				})
   244  
   245  				Context("when the service instance is not shareable", func() {
   246  					var expectedErr error
   247  
   248  					BeforeEach(func() {
   249  						expectedErr = actionerror.ServiceInstanceNotShareableError{
   250  							FeatureFlagEnabled:          false,
   251  							ServiceBrokerSharingEnabled: true}
   252  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   253  							v2v3action.Warnings{"share-service-instance-warning"},
   254  							expectedErr)
   255  					})
   256  
   257  					It("returns ServiceInstanceNotShareableError and displays all warnings", func() {
   258  						Expect(executeErr).To(MatchError(expectedErr))
   259  
   260  						Expect(testUI.Out).ToNot(Say("OK"))
   261  						Expect(testUI.Err).To(Say("share-service-instance-warning"))
   262  					})
   263  				})
   264  
   265  				Context("when the service instance is already shared to the space", func() {
   266  					BeforeEach(func() {
   267  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   268  							v2v3action.Warnings{"share-service-warning"},
   269  							actionerror.ServiceInstanceAlreadySharedError{})
   270  					})
   271  
   272  					It("does not return an error and displays all warnings", func() {
   273  						Expect(executeErr).ToNot(HaveOccurred())
   274  
   275  						Expect(testUI.Out).To(Say("Service instance some-service-instance is already shared with that space."))
   276  						Expect(testUI.Out).To(Say("OK"))
   277  
   278  						Expect(testUI.Err).To(Say("share-service-warning"))
   279  					})
   280  				})
   281  			})
   282  		})
   283  	})
   284  })