github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/share_service_command_test.go (about)

     1  package v6_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/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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             ShareServiceCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.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(v6fakes.FakeShareServiceActor)
    36  
    37  		cmd = 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  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	When("the API version is below the minimum", func() {
    56  		BeforeEach(func() {
    57  			fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinSupportedV3ClientVersion)
    58  		})
    59  
    60  		It("returns a MinimumAPIVersionNotMetError", func() {
    61  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    62  				CurrentVersion: ccversion.MinSupportedV3ClientVersion,
    63  				MinimumVersion: ccversion.MinVersionShareServiceV3,
    64  			}))
    65  		})
    66  	})
    67  
    68  	When("the environment is not correctly setup", func() {
    69  		BeforeEach(func() {
    70  			fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinVersionShareServiceV3)
    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  	When("the user is logged in, and a space and org are targeted", func() {
    85  		BeforeEach(func() {
    86  			fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinVersionShareServiceV3)
    87  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    88  				GUID: "some-org-guid",
    89  				Name: "some-org",
    90  			})
    91  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    92  				GUID: "some-space-guid",
    93  			})
    94  		})
    95  
    96  		When("an error occurs getting the current user", func() {
    97  			var expectedErr error
    98  
    99  			BeforeEach(func() {
   100  				expectedErr = errors.New("get current user error")
   101  				fakeConfig.CurrentUserReturns(
   102  					configv3.User{},
   103  					expectedErr)
   104  			})
   105  
   106  			It("returns the error", func() {
   107  				Expect(executeErr).To(MatchError(expectedErr))
   108  			})
   109  		})
   110  
   111  		When("no errors occur getting the current user", func() {
   112  			BeforeEach(func() {
   113  				fakeConfig.CurrentUserReturns(
   114  					configv3.User{Name: "some-user"},
   115  					nil)
   116  			})
   117  
   118  			When("'-o' (org name) is not provided", func() {
   119  				When("no errors occur sharing the service instance", func() {
   120  					BeforeEach(func() {
   121  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   122  							v2v3action.Warnings{"share-service-warning"},
   123  							nil)
   124  					})
   125  
   126  					It("shares the service instance with the provided space and displays all warnings", func() {
   127  						Expect(executeErr).ToNot(HaveOccurred())
   128  
   129  						Expect(testUI.Out).To(Say(`Sharing service instance some-service-instance into org some-org / space some-space as some-user\.\.\.`))
   130  						Expect(testUI.Out).To(Say("OK"))
   131  
   132  						Expect(testUI.Err).To(Say("share-service-warning"))
   133  
   134  						Expect(fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationCallCount()).To(Equal(1))
   135  						spaceNameArg, serviceInstanceNameArg, sourceSpaceGUIDArg, orgGUIDArg := fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationArgsForCall(0)
   136  						Expect(spaceNameArg).To(Equal("some-space"))
   137  						Expect(serviceInstanceNameArg).To(Equal("some-service-instance"))
   138  						Expect(sourceSpaceGUIDArg).To(Equal("some-space-guid"))
   139  						Expect(orgGUIDArg).To(Equal("some-org-guid"))
   140  					})
   141  				})
   142  
   143  				When("an error occurs sharing the service instance", func() {
   144  					var expectedErr error
   145  
   146  					BeforeEach(func() {
   147  						expectedErr = errors.New("sharing failed")
   148  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   149  							v2v3action.Warnings{"share-service-warning"},
   150  							expectedErr)
   151  					})
   152  
   153  					It("returns the error, and displays all warnings", func() {
   154  						Expect(executeErr).To(MatchError(expectedErr))
   155  
   156  						Expect(testUI.Out).ToNot(Say("OK"))
   157  						Expect(testUI.Err).To(Say("share-service-warning"))
   158  					})
   159  				})
   160  
   161  				When("the service instance is not shareable", func() {
   162  					var expectedErr error
   163  
   164  					BeforeEach(func() {
   165  						expectedErr = actionerror.ServiceInstanceNotShareableError{
   166  							FeatureFlagEnabled:          true,
   167  							ServiceBrokerSharingEnabled: false}
   168  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   169  							v2v3action.Warnings{"share-service-instance-warning"},
   170  							expectedErr)
   171  					})
   172  
   173  					It("returns ServiceInstanceNotShareableError and displays all warnings", func() {
   174  						Expect(executeErr).To(MatchError(expectedErr))
   175  
   176  						Expect(testUI.Out).ToNot(Say("OK"))
   177  						Expect(testUI.Err).To(Say("share-service-instance-warning"))
   178  					})
   179  				})
   180  
   181  				When("the service instance is already shared to the space", func() {
   182  					BeforeEach(func() {
   183  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationReturns(
   184  							v2v3action.Warnings{"share-service-warning"},
   185  							actionerror.ServiceInstanceAlreadySharedError{})
   186  					})
   187  
   188  					It("does not return an error and displays all warnings", func() {
   189  						Expect(executeErr).ToNot(HaveOccurred())
   190  
   191  						Expect(testUI.Out).To(Say("Service instance some-service-instance is already shared with that space."))
   192  						Expect(testUI.Out).To(Say("OK"))
   193  
   194  						Expect(testUI.Err).To(Say("share-service-warning"))
   195  					})
   196  				})
   197  			})
   198  
   199  			When("-o (org name) is provided", func() {
   200  				BeforeEach(func() {
   201  					cmd.OrgName = "some-other-org"
   202  				})
   203  
   204  				When("no errors occur sharing the service instance", func() {
   205  					BeforeEach(func() {
   206  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   207  							v2v3action.Warnings{"share-service-warning"},
   208  							nil)
   209  					})
   210  
   211  					It("shares the service instance with the provided space and org and displays all warnings", func() {
   212  						Expect(executeErr).ToNot(HaveOccurred())
   213  
   214  						Expect(testUI.Out).To(Say(`Sharing service instance some-service-instance into org some-other-org / space some-space as some-user\.\.\.`))
   215  						Expect(testUI.Out).To(Say("OK"))
   216  
   217  						Expect(testUI.Err).To(Say("share-service-warning"))
   218  
   219  						Expect(fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameCallCount()).To(Equal(1))
   220  						spaceNameArg, serviceInstanceNameArg, sourceSpaceGUID, orgName := fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameArgsForCall(0)
   221  						Expect(spaceNameArg).To(Equal("some-space"))
   222  						Expect(serviceInstanceNameArg).To(Equal("some-service-instance"))
   223  						Expect(sourceSpaceGUID).To(Equal("some-space-guid"))
   224  						Expect(orgName).To(Equal("some-other-org"))
   225  					})
   226  				})
   227  
   228  				When("an error occurs sharing the service instance", func() {
   229  					var expectedErr error
   230  
   231  					BeforeEach(func() {
   232  						expectedErr = errors.New("sharing failed")
   233  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   234  							v2v3action.Warnings{"share-service-warning"},
   235  							expectedErr)
   236  					})
   237  
   238  					It("returns the error and displays all warnings", func() {
   239  						Expect(executeErr).To(MatchError(expectedErr))
   240  
   241  						Expect(testUI.Out).ToNot(Say("OK"))
   242  						Expect(testUI.Err).To(Say("share-service-warning"))
   243  					})
   244  				})
   245  
   246  				When("the service instance is not shareable", func() {
   247  					var expectedErr error
   248  
   249  					BeforeEach(func() {
   250  						expectedErr = actionerror.ServiceInstanceNotShareableError{
   251  							FeatureFlagEnabled:          false,
   252  							ServiceBrokerSharingEnabled: true}
   253  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   254  							v2v3action.Warnings{"share-service-instance-warning"},
   255  							expectedErr)
   256  					})
   257  
   258  					It("returns ServiceInstanceNotShareableError and displays all warnings", func() {
   259  						Expect(executeErr).To(MatchError(expectedErr))
   260  
   261  						Expect(testUI.Out).ToNot(Say("OK"))
   262  						Expect(testUI.Err).To(Say("share-service-instance-warning"))
   263  					})
   264  				})
   265  
   266  				When("the service instance is already shared to the space", func() {
   267  					BeforeEach(func() {
   268  						fakeActor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationNameReturns(
   269  							v2v3action.Warnings{"share-service-warning"},
   270  							actionerror.ServiceInstanceAlreadySharedError{})
   271  					})
   272  
   273  					It("does not return an error and displays all warnings", func() {
   274  						Expect(executeErr).ToNot(HaveOccurred())
   275  
   276  						Expect(testUI.Out).To(Say("Service instance some-service-instance is already shared with that space."))
   277  						Expect(testUI.Out).To(Say("OK"))
   278  
   279  						Expect(testUI.Err).To(Say("share-service-warning"))
   280  					})
   281  				})
   282  			})
   283  		})
   284  	})
   285  })