github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+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/command/commandfakes"
    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("services Command", func() {
    21  	var (
    22  		cmd             ServicesCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeServiceInstancesActor
    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.FakeServiceInstancesActor)
    36  
    37  		cmd = ServicesCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeActor,
    42  		}
    43  
    44  		binaryName = "faceman"
    45  		fakeConfig.BinaryNameReturns(binaryName)
    46  	})
    47  
    48  	JustBeforeEach(func() {
    49  		executeErr = cmd.Execute(nil)
    50  	})
    51  
    52  	When("an error is encountered checking if the environment is setup correctly", func() {
    53  		BeforeEach(func() {
    54  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    55  		})
    56  
    57  		It("returns an error", func() {
    58  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    59  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    60  			checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0)
    61  			Expect(checkTargetedOrgArg).To(BeTrue())
    62  			Expect(checkTargetedSpaceArg).To(BeTrue())
    63  		})
    64  	})
    65  
    66  	When("the user is logged in and an org and space are targeted", func() {
    67  		BeforeEach(func() {
    68  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    69  				Name: "some-org",
    70  			})
    71  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    72  				GUID: "some-space-guid",
    73  				Name: "some-space",
    74  			})
    75  		})
    76  
    77  		When("getting the current user fails", func() {
    78  			var expectedErr error
    79  
    80  			BeforeEach(func() {
    81  				expectedErr = errors.New("some-error that happened")
    82  				fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    83  			})
    84  
    85  			It("returns the error", func() {
    86  				Expect(executeErr).To(MatchError(expectedErr))
    87  				Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
    88  			})
    89  		})
    90  
    91  		When("getting the current user succeeds", func() {
    92  			var (
    93  				fakeUser configv3.User
    94  			)
    95  
    96  			BeforeEach(func() {
    97  				fakeUser = configv3.User{Name: "some-user"}
    98  				fakeConfig.CurrentUserReturns(fakeUser, nil)
    99  				fakeConfig.TargetedSpaceReturns(configv3.Space{
   100  					GUID: "some-space-guid",
   101  					Name: "some-space",
   102  				})
   103  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{
   104  					Name: "some-org",
   105  				})
   106  			})
   107  
   108  			When("there are no services", func() {
   109  				BeforeEach(func() {
   110  					fakeActor.GetServiceInstancesSummaryBySpaceReturns(
   111  						nil,
   112  						v2action.Warnings{"get-summary-warnings"},
   113  						nil,
   114  					)
   115  				})
   116  
   117  				It("displays that there are no services", func() {
   118  					Expect(executeErr).ToNot(HaveOccurred())
   119  					Expect(testUI.Out).To(Say("Getting services in org %s / space %s as %s...", "some-org",
   120  						"some-space", fakeUser.Name))
   121  
   122  					out := testUI.Out.(*Buffer).Contents()
   123  					Expect(out).To(MatchRegexp("No services found"))
   124  					Expect(out).ToNot(MatchRegexp(`name\s+service\s+plan\s+bound apps\s+last operation`))
   125  					Expect(testUI.Err).To(Say("get-summary-warnings"))
   126  				})
   127  			})
   128  
   129  			When("there are services", func() {
   130  				BeforeEach(func() {
   131  					fakeActor.GetServiceInstancesSummaryBySpaceReturns(
   132  						[]v2action.ServiceInstanceSummary{
   133  							{
   134  								ServiceInstance: v2action.ServiceInstance{
   135  									Name: "instance-1",
   136  									LastOperation: ccv2.LastOperation{
   137  										Type:  "some-type",
   138  										State: "some-state",
   139  									},
   140  									Type: constant.ServiceInstanceTypeManagedService,
   141  								},
   142  								ServicePlan: v2action.ServicePlan{Name: "some-plan"},
   143  								Service: v2action.Service{
   144  									Label:             "some-service-1",
   145  									ServiceBrokerName: "broker-1",
   146  								},
   147  								BoundApplications: []v2action.BoundApplication{
   148  									{AppName: "app-2"},
   149  									{AppName: "app-1"},
   150  								},
   151  							},
   152  							{
   153  								ServiceInstance: v2action.ServiceInstance{
   154  									Name: "instance-3",
   155  									Type: constant.ServiceInstanceTypeUserProvidedService,
   156  								},
   157  							},
   158  							{
   159  								ServiceInstance: v2action.ServiceInstance{
   160  									Name: "instance-2",
   161  									Type: constant.ServiceInstanceTypeManagedService,
   162  								},
   163  								Service: v2action.Service{
   164  									Label:             "some-service-2",
   165  									ServiceBrokerName: "broker-2",
   166  								},
   167  							},
   168  						},
   169  						v2action.Warnings{"get-summary-warnings"},
   170  						nil,
   171  					)
   172  				})
   173  
   174  				It("displays all the services and apps in alphanumeric sorted order together with the org & space & warnings", func() {
   175  					Expect(executeErr).ToNot(HaveOccurred())
   176  					Expect(testUI.Out).To(Say("Getting services in org %s / space %s as %s...", "some-org",
   177  						"some-space", fakeUser.Name))
   178  					Expect(testUI.Out).To(Say(`name\s+service\s+plan\s+bound apps\s+last operation\s+broker`))
   179  					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`))
   180  					Expect(testUI.Out).To(Say(`instance-2\s+some-service-2\s+broker-2`))
   181  					Expect(testUI.Out).To(Say(`instance-3\s+user-provided\s+`))
   182  					Expect(testUI.Err).To(Say("get-summary-warnings"))
   183  				})
   184  			})
   185  		})
   186  	})
   187  })