github.com/arunkumar7540/cli@v6.45.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/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.ManagedService, 141 MaintenanceInfo: ccv2.MaintenanceInfo{ 142 Version: "2.0.0", 143 }, 144 }, 145 ServicePlan: v2action.ServicePlan{ 146 Name: "some-plan", 147 MaintenanceInfo: ccv2.MaintenanceInfo{ 148 Version: "3.0.0", 149 }, 150 }, 151 Service: v2action.Service{ 152 Label: "some-service-1", 153 ServiceBrokerName: "broker-1", 154 }, 155 BoundApplications: []v2action.BoundApplication{ 156 {AppName: "app-2"}, 157 {AppName: "app-1"}, 158 }, 159 }, 160 { 161 ServiceInstance: v2action.ServiceInstance{ 162 Name: "instance-3", 163 Type: constant.UserProvidedService, 164 }, 165 }, 166 { 167 ServiceInstance: v2action.ServiceInstance{ 168 Name: "instance-2", 169 Type: constant.ManagedService, 170 MaintenanceInfo: ccv2.MaintenanceInfo{ 171 Version: "2.0.0", 172 }, 173 }, 174 ServicePlan: v2action.ServicePlan{ 175 Name: "some-plan", 176 MaintenanceInfo: ccv2.MaintenanceInfo{ 177 Version: "2.0.0", 178 }, 179 }, 180 Service: v2action.Service{ 181 Label: "some-service-2", 182 ServiceBrokerName: "broker-2", 183 }, 184 }, 185 }, 186 v2action.Warnings{"get-summary-warnings"}, 187 nil, 188 ) 189 }) 190 191 It("displays all the services and apps in alphanumeric sorted order together with the org & space & warnings & upgrades", func() { 192 Expect(executeErr).ToNot(HaveOccurred()) 193 Expect(testUI.Out).To(Say("Getting services in org %s / space %s as %s...", "some-org", "some-space", fakeUser.Name)) 194 Expect(testUI.Out).To(Say(`name\s+service\s+plan\s+bound apps\s+last operation\s+broker\s+upgrade available`)) 195 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`)) 196 Expect(testUI.Out).To(Say(`instance-2\s+some-service-2\s+some-plan\s+broker-2\s+no`)) 197 Expect(testUI.Out).To(Say(`instance-3\s+user-provided\s+$`)) 198 Expect(testUI.Err).To(Say("get-summary-warnings")) 199 }) 200 }) 201 }) 202 }) 203 })