github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/composite/update_service_instance_test.go (about) 1 package composite_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/actor/v2action/composite" 9 "code.cloudfoundry.org/cli/actor/v2action/composite/compositefakes" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("UpdateServiceInstanceCompositeActor", func() { 16 var ( 17 composite *UpdateServiceInstanceCompositeActor 18 fakeGetServiceInstanceActor *compositefakes.FakeGetServiceInstanceActor 19 fakeGetServicePlanActor *compositefakes.FakeGetServicePlanActor 20 fakeGetAPIVersionActor *compositefakes.FakeGetAPIVersionActor 21 fakeUpdateServiceInstanceMaintenanceInfoActor *compositefakes.FakeUpdateServiceInstanceMaintenanceInfoActor 22 err error 23 warnings v2action.Warnings 24 ) 25 26 BeforeEach(func() { 27 fakeGetServiceInstanceActor = new(compositefakes.FakeGetServiceInstanceActor) 28 fakeGetServicePlanActor = new(compositefakes.FakeGetServicePlanActor) 29 fakeGetAPIVersionActor = new(compositefakes.FakeGetAPIVersionActor) 30 fakeUpdateServiceInstanceMaintenanceInfoActor = new(compositefakes.FakeUpdateServiceInstanceMaintenanceInfoActor) 31 composite = &UpdateServiceInstanceCompositeActor{ 32 GetServiceInstanceActor: fakeGetServiceInstanceActor, 33 GetServicePlanActor: fakeGetServicePlanActor, 34 GetAPIVersionActor: fakeGetAPIVersionActor, 35 UpdateServiceInstanceMaintenanceInfoActor: fakeUpdateServiceInstanceMaintenanceInfoActor, 36 } 37 }) 38 39 Describe("UpgradeServiceInstance", func() { 40 var ( 41 serviceInstanceGUID = "service-instance-guid" 42 servicePlanGUID = "service-plan-guid" 43 currentMaintenanceInfo = ccv2.MaintenanceInfo{ 44 Version: "1.1.2-def", 45 } 46 serviceInstance = v2action.ServiceInstance{ 47 GUID: serviceInstanceGUID, 48 ServicePlanGUID: servicePlanGUID, 49 MaintenanceInfo: currentMaintenanceInfo, 50 } 51 ) 52 53 JustBeforeEach(func() { 54 warnings, err = composite.UpgradeServiceInstance(serviceInstance) 55 }) 56 57 When("the plan exists", func() { 58 var maintenanceInfo v2action.MaintenanceInfo 59 60 When("the plan has a newer maintenance info", func() { 61 BeforeEach(func() { 62 maintenanceInfo = v2action.MaintenanceInfo{ 63 Version: "1.2.3-abc", 64 } 65 servicePlan := v2action.ServicePlan{ 66 MaintenanceInfo: ccv2.MaintenanceInfo(maintenanceInfo), 67 } 68 fakeGetServicePlanActor.GetServicePlanReturns(servicePlan, v2action.Warnings{"plan-lookup-warning"}, nil) 69 fakeUpdateServiceInstanceMaintenanceInfoActor.UpdateServiceInstanceMaintenanceInfoReturns(v2action.Warnings{"update-service-instance-warning"}, nil) 70 }) 71 72 It("updates the service instance with the latest maintenanceInfo on the plan", func() { 73 Expect(err).To(BeNil()) 74 Expect(fakeUpdateServiceInstanceMaintenanceInfoActor.UpdateServiceInstanceMaintenanceInfoCallCount()).To(Equal(1)) 75 guid, minfo := fakeUpdateServiceInstanceMaintenanceInfoActor.UpdateServiceInstanceMaintenanceInfoArgsForCall(0) 76 Expect(guid).To(Equal(serviceInstanceGUID)) 77 Expect(minfo).To(Equal(maintenanceInfo)) 78 79 Expect(fakeGetServicePlanActor.GetServicePlanCallCount()).To(Equal(1)) 80 planGUID := fakeGetServicePlanActor.GetServicePlanArgsForCall(0) 81 Expect(planGUID).To(Equal(servicePlanGUID)) 82 }) 83 84 It("returns all warnings", func() { 85 Expect(warnings).To(ConsistOf("plan-lookup-warning", "update-service-instance-warning")) 86 }) 87 88 When("updating the service instance fails", func() { 89 BeforeEach(func() { 90 fakeUpdateServiceInstanceMaintenanceInfoActor.UpdateServiceInstanceMaintenanceInfoReturns( 91 v2action.Warnings{"update-service-instance-warning"}, 92 errors.New("something really bad happened"), 93 ) 94 }) 95 96 It("returns the error and warnings", func() { 97 Expect(err).To(MatchError("something really bad happened")) 98 Expect(warnings).To(ConsistOf("plan-lookup-warning", "update-service-instance-warning")) 99 }) 100 }) 101 }) 102 103 When("the plan has the same maintenance info", func() { 104 BeforeEach(func() { 105 maintenanceInfo = v2action.MaintenanceInfo{ 106 Version: currentMaintenanceInfo.Version, 107 } 108 servicePlan := v2action.ServicePlan{ 109 MaintenanceInfo: ccv2.MaintenanceInfo(maintenanceInfo), 110 } 111 fakeGetServicePlanActor.GetServicePlanReturns(servicePlan, v2action.Warnings{"plan-lookup-warning"}, nil) 112 }) 113 114 It("returns an error informing that no upgrade is available", func() { 115 expectedErr := actionerror.ServiceUpgradeNotAvailableError{} 116 Expect(err).To(MatchError(expectedErr)) 117 }) 118 119 It("does not attempt to call UpdateServiceInstanceMaintenanceInfo", func() { 120 Expect(fakeUpdateServiceInstanceMaintenanceInfoActor.UpdateServiceInstanceMaintenanceInfoCallCount()).To(Equal(0)) 121 }) 122 123 It("returns all warnings", func() { 124 Expect(warnings).To(ConsistOf("plan-lookup-warning")) 125 }) 126 }) 127 }) 128 129 When("fetching the plan fails", func() { 130 BeforeEach(func() { 131 fakeGetServicePlanActor.GetServicePlanReturns( 132 v2action.ServicePlan{}, 133 v2action.Warnings{"plan-lookup-warning"}, 134 errors.New("something really bad happened"), 135 ) 136 }) 137 138 It("returns an error and warnings", func() { 139 Expect(err).To(MatchError("something really bad happened")) 140 Expect(warnings).To(ConsistOf("plan-lookup-warning")) 141 }) 142 }) 143 }) 144 145 Describe("GetServiceInstanceByNameAndSpace", func() { 146 var serviceInstance v2action.ServiceInstance 147 148 JustBeforeEach(func() { 149 serviceInstance, warnings, err = composite.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 150 }) 151 152 When("the service instance exists", func() { 153 BeforeEach(func() { 154 fakeGetServiceInstanceActor.GetServiceInstanceByNameAndSpaceReturns( 155 v2action.ServiceInstance{ 156 GUID: "some-service-instance-guid", 157 Name: "some-service-instance", 158 }, 159 v2action.Warnings{"foo"}, 160 nil, 161 ) 162 }) 163 164 It("returns the service instance and warnings", func() { 165 Expect(err).ToNot(HaveOccurred()) 166 Expect(serviceInstance).To(Equal(v2action.ServiceInstance{ 167 GUID: "some-service-instance-guid", 168 Name: "some-service-instance", 169 })) 170 Expect(warnings).To(ConsistOf("foo")) 171 172 Expect(fakeGetServiceInstanceActor.GetServiceInstanceByNameAndSpaceCallCount()).To(Equal(1)) 173 174 serviceInstanceGUID, spaceGUID := fakeGetServiceInstanceActor.GetServiceInstanceByNameAndSpaceArgsForCall(0) 175 Expect(serviceInstanceGUID).To(Equal("some-service-instance")) 176 Expect(spaceGUID).To(Equal("some-space-guid")) 177 }) 178 }) 179 180 When("there is an error getting the service instance", func() { 181 BeforeEach(func() { 182 fakeGetServiceInstanceActor.GetServiceInstanceByNameAndSpaceReturns( 183 v2action.ServiceInstance{}, 184 v2action.Warnings{"foo"}, 185 errors.New("something really bad happened"), 186 ) 187 }) 188 189 It("returns an error and warnings", func() { 190 Expect(err).To(MatchError("something really bad happened")) 191 Expect(warnings).To(ConsistOf("foo")) 192 }) 193 }) 194 }) 195 196 Describe("CloudControllerAPIVersion", func() { 197 When("CloudControllerAPIVersion returns an API version", func() { 198 BeforeEach(func() { 199 fakeGetAPIVersionActor.CloudControllerAPIVersionReturns("2.42") 200 }) 201 202 It("calls the get API version function and returns the result", func() { 203 Expect(composite.CloudControllerAPIVersion()).To(Equal("2.42")) 204 Expect(fakeGetAPIVersionActor.CloudControllerAPIVersionCallCount()).To(Equal(1)) 205 }) 206 }) 207 }) 208 })