github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/organization_quota_test.go (about) 1 package v2action_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/v2actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("OrganizationQuota Actions", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient 20 ) 21 22 BeforeEach(func() { 23 fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient) 24 actor = NewActor(fakeCloudControllerClient, nil, nil) 25 }) 26 27 Describe("GetOrganizationQuota", func() { 28 When("the org quota exists", func() { 29 BeforeEach(func() { 30 fakeCloudControllerClient.GetOrganizationQuotaReturns( 31 ccv2.OrganizationQuota{ 32 GUID: "some-org-quota-guid", 33 Name: "some-org-quota", 34 }, 35 ccv2.Warnings{"warning-1"}, 36 nil, 37 ) 38 }) 39 40 It("returns the org quota and warnings", func() { 41 orgQuota, warnings, err := actor.GetOrganizationQuota("some-org-quota-guid") 42 Expect(err).ToNot(HaveOccurred()) 43 Expect(orgQuota).To(Equal(OrganizationQuota{ 44 GUID: "some-org-quota-guid", 45 Name: "some-org-quota", 46 })) 47 Expect(warnings).To(ConsistOf("warning-1")) 48 49 Expect(fakeCloudControllerClient.GetOrganizationQuotaCallCount()).To(Equal(1)) 50 Expect(fakeCloudControllerClient.GetOrganizationQuotaArgsForCall(0)).To(Equal( 51 "some-org-quota-guid")) 52 }) 53 }) 54 55 When("the org quota does not exist", func() { 56 BeforeEach(func() { 57 fakeCloudControllerClient.GetOrganizationQuotaReturns(ccv2.OrganizationQuota{}, nil, ccerror.ResourceNotFoundError{}) 58 }) 59 60 It("returns an OrganizationQuotaNotFoundError", func() { 61 _, _, err := actor.GetOrganizationQuota("some-org-quota-guid") 62 Expect(err).To(MatchError(actionerror.OrganizationQuotaNotFoundError{GUID: "some-org-quota-guid"})) 63 }) 64 }) 65 66 When("the cloud controller client returns an error", func() { 67 var expectedErr error 68 69 BeforeEach(func() { 70 expectedErr = errors.New("some org quota error") 71 fakeCloudControllerClient.GetOrganizationQuotaReturns(ccv2.OrganizationQuota{}, nil, expectedErr) 72 }) 73 74 It("returns the error", func() { 75 _, _, err := actor.GetOrganizationQuota("some-org-quota-guid") 76 Expect(err).To(MatchError(expectedErr)) 77 }) 78 }) 79 }) 80 81 Describe("GetOrganizationQuotaByName", func() { 82 var ( 83 quotaName string 84 85 quota OrganizationQuota 86 warnings Warnings 87 executeErr error 88 ) 89 90 JustBeforeEach(func() { 91 quotaName = "some-quota" 92 quota, warnings, executeErr = actor.GetOrganizationQuotaByName(quotaName) 93 }) 94 95 When("fetching quotas succeeds", func() { 96 When("a single quota is returned", func() { 97 BeforeEach(func() { 98 fakeCloudControllerClient.GetOrganizationQuotasReturns( 99 []ccv2.OrganizationQuota{ 100 { 101 GUID: "some-quota-definition-guid", 102 Name: "some-quota", 103 }, 104 }, 105 ccv2.Warnings{"quota-warning-1", "quota-warning-2"}, 106 nil) 107 }) 108 109 It("returns the found quota and warnings", func() { 110 Expect(executeErr).ToNot(HaveOccurred()) 111 Expect(quota).To(Equal(OrganizationQuota{ 112 GUID: "some-quota-definition-guid", 113 Name: "some-quota", 114 }, 115 )) 116 Expect(warnings).To(ConsistOf("quota-warning-1", "quota-warning-2")) 117 118 Expect(fakeCloudControllerClient.GetOrganizationQuotasCallCount()).To(Equal(1)) 119 Expect(fakeCloudControllerClient.GetOrganizationQuotasArgsForCall(0)).To(Equal([]ccv2.Filter{ 120 { 121 Type: constant.NameFilter, 122 Operator: constant.EqualOperator, 123 Values: []string{"some-quota"}, 124 }, 125 })) 126 }) 127 }) 128 129 When("more than one quota is returned", func() { 130 BeforeEach(func() { 131 fakeCloudControllerClient.GetOrganizationQuotasReturns( 132 []ccv2.OrganizationQuota{ 133 { 134 GUID: "some-quota-definition-guid-1", 135 Name: "some-quota-1", 136 }, 137 { 138 GUID: "some-quota-definition-guid-2", 139 Name: "some-quota-2", 140 }, 141 }, 142 ccv2.Warnings{"quota-warning-1", "quota-warning-2"}, 143 nil) 144 }) 145 146 It("returns an error that multiple quotas were found and does not try to create the org", func() { 147 Expect(executeErr).To(MatchError(actionerror.MultipleOrganizationQuotasFoundForNameError{ 148 Name: quotaName, 149 GUIDs: []string{ 150 "some-quota-definition-guid-1", 151 "some-quota-definition-guid-2", 152 }, 153 })) 154 Expect(warnings).To(ConsistOf("quota-warning-1", "quota-warning-2")) 155 156 Expect(fakeCloudControllerClient.GetOrganizationQuotasCallCount()).To(Equal(1)) 157 Expect(fakeCloudControllerClient.CreateOrganizationCallCount()).To(Equal(0)) 158 }) 159 }) 160 161 When("no quotas are returned", func() { 162 BeforeEach(func() { 163 fakeCloudControllerClient.GetOrganizationQuotasReturns( 164 []ccv2.OrganizationQuota{}, 165 ccv2.Warnings{"quota-warning-1", "quota-warning-2"}, 166 nil) 167 }) 168 169 It("returns an error that no quotas were found and does not try to create the org", func() { 170 Expect(executeErr).To(MatchError(actionerror.QuotaNotFoundForNameError{Name: quotaName})) 171 Expect(warnings).To(ConsistOf("quota-warning-1", "quota-warning-2")) 172 173 Expect(fakeCloudControllerClient.GetOrganizationQuotasCallCount()).To(Equal(1)) 174 Expect(fakeCloudControllerClient.CreateOrganizationCallCount()).To(Equal(0)) 175 }) 176 }) 177 178 }) 179 180 When("fetching the quota fails", func() { 181 BeforeEach(func() { 182 fakeCloudControllerClient.GetOrganizationQuotasReturns( 183 nil, 184 ccv2.Warnings{"quota-warning-1", "quota-warning-2"}, 185 errors.New("no quota found")) 186 }) 187 188 It("returns warnings and the error, and does not try to create the org", func() { 189 Expect(executeErr).To(MatchError("no quota found")) 190 Expect(warnings).To(ConsistOf("quota-warning-1", "quota-warning-2")) 191 192 Expect(fakeCloudControllerClient.GetOrganizationQuotasCallCount()).To(Equal(1)) 193 Expect(fakeCloudControllerClient.CreateOrganizationCallCount()).To(Equal(0)) 194 }) 195 }) 196 }) 197 })