github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/services_command_test.go (about)

     1  package v2_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/v2"
    12  	"code.cloudfoundry.org/cli/command/v2/v2fakes"
    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       *v2fakes.FakeServicesActor
    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(v2fakes.FakeServicesActor)
    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  	Context("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  	Context("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  		Context("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  		Context("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  			Context("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  			Context("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{Label: "some-service-1"},
   144  								BoundApplications: []v2action.BoundApplication{
   145  									{AppName: "app-1"},
   146  									{AppName: "app-2"},
   147  								},
   148  							},
   149  							{
   150  								ServiceInstance: v2action.ServiceInstance{
   151  									Name: "instance-2",
   152  									Type: constant.ServiceInstanceTypeManagedService,
   153  								},
   154  								Service: v2action.Service{Label: "some-service-2"},
   155  							},
   156  							{
   157  								ServiceInstance: v2action.ServiceInstance{
   158  									Name: "instance-3",
   159  									Type: constant.ServiceInstanceTypeUserProvidedService,
   160  								},
   161  							},
   162  						},
   163  						v2action.Warnings{"get-summary-warnings"},
   164  						nil,
   165  					)
   166  				})
   167  
   168  				It("displays all the services in the org & space & warnings", func() {
   169  					Expect(executeErr).ToNot(HaveOccurred())
   170  					Expect(testUI.Out).To(Say("Getting services in org %s / space %s as %s...", "some-org",
   171  						"some-space", fakeUser.Name))
   172  					Expect(testUI.Out).To(Say("name\\s+service\\s+plan\\s+bound apps\\s+last operation"))
   173  					Expect(testUI.Out).To(Say("instance-1\\s+some-service-1\\s+some-plan\\s+app-1, app-2\\s+some-type some-state"))
   174  					Expect(testUI.Out).To(Say("instance-2\\s+some-service-2\\s+"))
   175  					Expect(testUI.Out).To(Say("instance-3\\s+user-provided\\s+"))
   176  					Expect(testUI.Err).To(Say("get-summary-warnings"))
   177  				})
   178  			})
   179  		})
   180  	})
   181  })