github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/services_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/v2action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    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("services Command", func() {
    22  	var (
    23  		cmd             ServicesCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v6fakes.FakeServiceInstancesActor
    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.FakeServiceInstancesActor)
    37  
    38  		cmd = ServicesCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  
    48  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionMaintenanceInfoInSummaryV2)
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	When("an error is encountered checking if the environment is setup correctly", func() {
    56  		BeforeEach(func() {
    57  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    58  		})
    59  
    60  		It("returns an error", func() {
    61  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    62  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    63  			checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0)
    64  			Expect(checkTargetedOrgArg).To(BeTrue())
    65  			Expect(checkTargetedSpaceArg).To(BeTrue())
    66  		})
    67  	})
    68  
    69  	When("the user is logged in and an org and space are targeted", func() {
    70  		BeforeEach(func() {
    71  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    72  				Name: "some-org",
    73  			})
    74  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    75  				GUID: "some-space-guid",
    76  				Name: "some-space",
    77  			})
    78  		})
    79  
    80  		When("getting the current user fails", func() {
    81  			var expectedErr error
    82  
    83  			BeforeEach(func() {
    84  				expectedErr = errors.New("some-error that happened")
    85  				fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    86  			})
    87  
    88  			It("returns the error", func() {
    89  				Expect(executeErr).To(MatchError(expectedErr))
    90  				Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
    91  			})
    92  		})
    93  
    94  		When("getting the current user succeeds", func() {
    95  			var (
    96  				fakeUser configv3.User
    97  			)
    98  
    99  			BeforeEach(func() {
   100  				fakeUser = configv3.User{Name: "some-user"}
   101  				fakeConfig.CurrentUserReturns(fakeUser, nil)
   102  				fakeConfig.TargetedSpaceReturns(configv3.Space{
   103  					GUID: "some-space-guid",
   104  					Name: "some-space",
   105  				})
   106  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{
   107  					Name: "some-org",
   108  				})
   109  			})
   110  
   111  			When("there are no services", func() {
   112  				BeforeEach(func() {
   113  					fakeActor.GetServiceInstancesSummaryBySpaceReturns(
   114  						nil,
   115  						v2action.Warnings{"get-summary-warnings"},
   116  						nil,
   117  					)
   118  				})
   119  
   120  				It("displays that there are no services", func() {
   121  					Expect(executeErr).ToNot(HaveOccurred())
   122  					Expect(testUI.Out).To(Say("Getting services in org %s / space %s as %s...", "some-org",
   123  						"some-space", fakeUser.Name))
   124  
   125  					out := testUI.Out.(*Buffer).Contents()
   126  					Expect(out).To(MatchRegexp("No services found"))
   127  					Expect(out).ToNot(MatchRegexp(`name\s+service\s+plan\s+bound apps\s+last operation`))
   128  					Expect(testUI.Err).To(Say("get-summary-warnings"))
   129  				})
   130  			})
   131  
   132  			When("there are services", func() {
   133  				BeforeEach(func() {
   134  					fakeActor.GetServiceInstancesSummaryBySpaceReturns(
   135  						[]v2action.ServiceInstanceSummary{
   136  							{
   137  								ServiceInstance: v2action.ServiceInstance{
   138  									Name: "instance-1",
   139  									LastOperation: ccv2.LastOperation{
   140  										Type:  "some-type",
   141  										State: "some-state",
   142  									},
   143  									Type: constant.ManagedService,
   144  									MaintenanceInfo: ccv2.MaintenanceInfo{
   145  										Version: "2.0.0",
   146  									},
   147  								},
   148  								ServicePlan: v2action.ServicePlan{
   149  									Name: "some-plan",
   150  									MaintenanceInfo: ccv2.MaintenanceInfo{
   151  										Version: "3.0.0",
   152  									},
   153  								},
   154  								Service: v2action.Service{
   155  									Label:             "some-service-1",
   156  									ServiceBrokerName: "broker-1",
   157  								},
   158  								BoundApplications: []v2action.BoundApplication{
   159  									{AppName: "app-2"},
   160  									{AppName: "app-1"},
   161  								},
   162  							},
   163  							{
   164  								ServiceInstance: v2action.ServiceInstance{
   165  									Name: "instance-3",
   166  									Type: constant.UserProvidedService,
   167  								},
   168  							},
   169  							{
   170  								ServiceInstance: v2action.ServiceInstance{
   171  									Name: "instance-2",
   172  									Type: constant.ManagedService,
   173  									MaintenanceInfo: ccv2.MaintenanceInfo{
   174  										Version: "2.0.0",
   175  									},
   176  								},
   177  								ServicePlan: v2action.ServicePlan{
   178  									Name: "some-plan",
   179  									MaintenanceInfo: ccv2.MaintenanceInfo{
   180  										Version: "2.0.0",
   181  									},
   182  								},
   183  								Service: v2action.Service{
   184  									Label:             "some-service-2",
   185  									ServiceBrokerName: "broker-2",
   186  								},
   187  							},
   188  						},
   189  						v2action.Warnings{"get-summary-warnings"},
   190  						nil,
   191  					)
   192  				})
   193  
   194  				It("displays all the services and apps in alphanumeric sorted order together with the org & space & warnings & upgrades", func() {
   195  					Expect(executeErr).ToNot(HaveOccurred())
   196  					Expect(testUI.Out).To(Say("Getting services in org %s / space %s as %s...", "some-org", "some-space", fakeUser.Name))
   197  					Expect(testUI.Out).To(Say(`name\s+service\s+plan\s+bound apps\s+last operation\s+broker\s+upgrade available`))
   198  					Expect(testUI.Out).To(Say(`instance-1\s+some-service-1\s+some-plan\s+app-1, app-2\s+some-type some-state\s+broker-1\s+yes`))
   199  					Expect(testUI.Out).To(Say(`instance-2\s+some-service-2\s+some-plan\s+broker-2\s+no`))
   200  					Expect(testUI.Out).To(Say(`instance-3\s+user-provided\s+$`))
   201  					Expect(testUI.Err).To(Say("get-summary-warnings"))
   202  				})
   203  
   204  				When("CC version is too old to show upgrade information", func() {
   205  					BeforeEach(func() {
   206  						fakeActor.CloudControllerAPIVersionReturns(ccversion.MinSupportedV2ClientVersion)
   207  					})
   208  
   209  					It("shows a tip with a minimum service upgrade version required", func() {
   210  						Expect(testUI.Out).To(Say("TIP: Please upgrade to CC API v%s or higher for individual service upgrades", ccversion.MinVersionMaintenanceInfoInSummaryV2))
   211  					})
   212  				})
   213  			})
   214  		})
   215  	})
   216  })