github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/service_plan_test.go (about) 1 package v7action_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 . "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 "code.cloudfoundry.org/cli/resources" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("Service Plan Actions", func() { 16 var ( 17 actor *Actor 18 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 19 ) 20 21 BeforeEach(func() { 22 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 23 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 24 }) 25 26 Describe("GetServicePlanByNameOfferingAndBroker", func() { 27 const servicePlanName = "myServicePlan" 28 29 var ( 30 servicePlan resources.ServicePlan 31 warnings Warnings 32 executionError error 33 serviceBrokerName string 34 serviceOfferingName string 35 ) 36 37 BeforeEach(func() { 38 serviceBrokerName = "" 39 serviceOfferingName = "" 40 }) 41 42 JustBeforeEach(func() { 43 servicePlan, warnings, executionError = actor.GetServicePlanByNameOfferingAndBroker( 44 servicePlanName, 45 serviceOfferingName, 46 serviceBrokerName, 47 ) 48 }) 49 50 When("the cloud controller request is successful", func() { 51 When("the cloud controller returns one service plan", func() { 52 BeforeEach(func() { 53 fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{ 54 { 55 Name: "some-service-plan", 56 GUID: "some-service-plan-guid", 57 }, 58 }, ccv3.Warnings{"some-service-plan-warning"}, nil) 59 }) 60 61 It("returns a service plan and warnings", func() { 62 Expect(executionError).NotTo(HaveOccurred()) 63 64 Expect(servicePlan).To(Equal(resources.ServicePlan{Name: "some-service-plan", GUID: "some-service-plan-guid"})) 65 Expect(warnings).To(ConsistOf("some-service-plan-warning")) 66 Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1)) 67 Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf( 68 ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}}, 69 )) 70 }) 71 }) 72 73 When("the cloud controller returns no service plans", func() { 74 BeforeEach(func() { 75 fakeCloudControllerClient.GetServicePlansReturns( 76 nil, 77 ccv3.Warnings{"some-service-plan-warning"}, 78 nil) 79 }) 80 81 It("returns an error and warnings", func() { 82 Expect(executionError).To(MatchError(actionerror.ServicePlanNotFoundError{ 83 PlanName: servicePlanName, 84 OfferingName: serviceOfferingName, 85 })) 86 Expect(warnings).To(ConsistOf("some-service-plan-warning")) 87 }) 88 }) 89 90 When("the cloud controller returns more than one service plan", func() { 91 BeforeEach(func() { 92 fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{ 93 { 94 Name: "some-service-plan-1", 95 GUID: "some-service-plan-guid-1", 96 }, 97 { 98 Name: "some-service-plan-2", 99 GUID: "some-service-plan-guid-2", 100 }, 101 }, ccv3.Warnings{"some-service-plan-warning"}, nil) 102 }) 103 104 It("returns an error and warnings", func() { 105 Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{Name: servicePlanName})) 106 Expect(warnings).To(ConsistOf("some-service-plan-warning")) 107 }) 108 }) 109 }) 110 111 When("the cloud controller returns an error", func() { 112 BeforeEach(func() { 113 fakeCloudControllerClient.GetServicePlansReturns( 114 nil, 115 ccv3.Warnings{"some-service-plan-warning"}, 116 errors.New("random error"), 117 ) 118 }) 119 120 It("returns an error and warnings", func() { 121 Expect(executionError).To(MatchError("random error")) 122 Expect(warnings).To(ConsistOf("some-service-plan-warning")) 123 }) 124 }) 125 126 When("the offering name is provided", func() { 127 BeforeEach(func() { 128 serviceOfferingName = "some-offering-name" 129 fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{ 130 { 131 Name: "some-service-plan", 132 GUID: "some-service-plan-guid", 133 }, 134 { 135 Name: "some-service-plan", 136 GUID: "some-other-service-plan-guid", 137 }, 138 }, ccv3.Warnings{"some-service-plan-warning"}, nil) 139 }) 140 141 It("queries on the service plan and offering name", func() { 142 Expect(warnings).To(ConsistOf("some-service-plan-warning")) 143 Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1)) 144 Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf( 145 ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}}, 146 ccv3.Query{Key: ccv3.ServiceOfferingNamesFilter, Values: []string{serviceOfferingName}}, 147 )) 148 }) 149 150 When("it returns multiple results", func() { 151 It("returns an error with the right hint", func() { 152 Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{ 153 Name: servicePlanName, 154 ServiceOfferingName: serviceOfferingName, 155 })) 156 }) 157 }) 158 159 When("the plan is not found", func() { 160 BeforeEach(func() { 161 fakeCloudControllerClient.GetServicePlansReturns( 162 []resources.ServicePlan{}, 163 ccv3.Warnings{"some-service-plan-warning"}, 164 nil, 165 ) 166 }) 167 It("returns an error with the right hint", func() { 168 Expect(executionError).To(MatchError(actionerror.ServicePlanNotFoundError{ 169 PlanName: servicePlanName, 170 OfferingName: serviceOfferingName, 171 })) 172 }) 173 }) 174 }) 175 176 When("the broker name is provided", func() { 177 BeforeEach(func() { 178 serviceBrokerName = "some-broker-name" 179 fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{ 180 { 181 Name: "some-service-plan", 182 GUID: "some-service-plan-guid", 183 }, 184 { 185 Name: "some-service-plan", 186 GUID: "other-some-service-plan-guid", 187 }, 188 }, ccv3.Warnings{"some-service-plan-warning"}, nil) 189 }) 190 191 It("queries on the service plan name and the broker name", func() { 192 Expect(warnings).To(ConsistOf("some-service-plan-warning")) 193 Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1)) 194 Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf( 195 ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}}, 196 ccv3.Query{Key: ccv3.ServiceBrokerNamesFilter, Values: []string{serviceBrokerName}}, 197 )) 198 }) 199 200 When("it returns multiple results", func() { 201 It("returns an error with the right hint", func() { 202 Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{ 203 Name: servicePlanName, 204 ServiceBrokerName: serviceBrokerName, 205 })) 206 }) 207 }) 208 209 When("the plan is not found", func() { 210 BeforeEach(func() { 211 fakeCloudControllerClient.GetServicePlansReturns( 212 []resources.ServicePlan{}, 213 ccv3.Warnings{"some-service-plan-warning"}, 214 nil, 215 ) 216 }) 217 218 It("returns an error with the right hint", func() { 219 Expect(executionError).To(MatchError(actionerror.ServicePlanNotFoundError{ 220 PlanName: servicePlanName, 221 ServiceBrokerName: serviceBrokerName, 222 })) 223 }) 224 }) 225 }) 226 227 When("the broker and offering name are provided", func() { 228 BeforeEach(func() { 229 serviceOfferingName = "some-offering-name" 230 serviceBrokerName = "some-broker-name" 231 fakeCloudControllerClient.GetServicePlansReturns([]resources.ServicePlan{ 232 { 233 Name: "some-service-plan", 234 GUID: "some-service-plan-guid", 235 }, 236 { 237 Name: "some-service-plan", 238 GUID: "other-some-service-plan-guid", 239 }, 240 }, ccv3.Warnings{"some-service-plan-warning"}, nil) 241 }) 242 243 It("queries on the service plan, offering and broker names", func() { 244 Expect(warnings).To(ConsistOf("some-service-plan-warning")) 245 Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1)) 246 Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf( 247 ccv3.Query{Key: ccv3.NameFilter, Values: []string{servicePlanName}}, 248 ccv3.Query{Key: ccv3.ServiceOfferingNamesFilter, Values: []string{serviceOfferingName}}, 249 ccv3.Query{Key: ccv3.ServiceBrokerNamesFilter, Values: []string{serviceBrokerName}}, 250 )) 251 }) 252 253 When("it returns multiple results", func() { 254 It("returns an error with the right hint", func() { 255 Expect(executionError).To(MatchError(actionerror.DuplicateServicePlanError{ 256 Name: servicePlanName, 257 ServiceOfferingName: serviceOfferingName, 258 ServiceBrokerName: serviceBrokerName, 259 })) 260 }) 261 }) 262 }) 263 }) 264 })