github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/share_service_command_test.go (about)

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