github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/create_org_quota_command_test.go (about) 1 package v7_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/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/flag" 11 v7 "code.cloudfoundry.org/cli/command/v7" 12 "code.cloudfoundry.org/cli/command/v7/v7fakes" 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("create-org-quota Command", func() { 21 var ( 22 cmd v7.CreateOrgQuotaCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v7fakes.FakeActor 27 orgQuotaName string 28 executeErr error 29 30 currentUserName string 31 ) 32 33 BeforeEach(func() { 34 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 35 fakeConfig = new(commandfakes.FakeConfig) 36 fakeSharedActor = new(commandfakes.FakeSharedActor) 37 fakeActor = new(v7fakes.FakeActor) 38 orgQuotaName = "new-org-quota-name" 39 40 cmd = v7.CreateOrgQuotaCommand{ 41 BaseCommand: v7.BaseCommand{ 42 UI: testUI, 43 Config: fakeConfig, 44 SharedActor: fakeSharedActor, 45 Actor: fakeActor, 46 }, 47 RequiredArgs: flag.OrganizationQuota{OrganizationQuotaName: orgQuotaName}, 48 } 49 50 currentUserName = "bob" 51 fakeActor.GetCurrentUserReturns(configv3.User{Name: currentUserName}, nil) 52 }) 53 54 JustBeforeEach(func() { 55 executeErr = cmd.Execute(nil) 56 }) 57 58 When("the environment is not set up correctly", func() { 59 BeforeEach(func() { 60 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{}) 61 }) 62 63 It("returns an error", func() { 64 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{})) 65 66 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 67 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 68 Expect(checkTargetedOrg).To(BeFalse()) 69 Expect(checkTargetedSpace).To(BeFalse()) 70 }) 71 }) 72 73 When("the organization quota already exists", func() { 74 BeforeEach(func() { 75 fakeActor.CreateOrganizationQuotaReturns(v7action.Warnings{"warn-abc"}, ccerror.QuotaAlreadyExists{Message: "quota-already-exists"}) 76 }) 77 78 It("displays that it already exists, but does not return an error", func() { 79 Expect(executeErr).ToNot(HaveOccurred()) 80 81 Expect(fakeActor.CreateOrganizationQuotaCallCount()).To(Equal(1)) 82 Expect(testUI.Err).To(Say("warn-abc")) 83 Expect(testUI.Err).To(Say("quota-already-exists")) 84 }) 85 }) 86 87 When("creating the organization quota fails", func() { 88 BeforeEach(func() { 89 fakeActor.CreateOrganizationQuotaReturns(v7action.Warnings{"warn-456", "warn-789"}, errors.New("create-org-quota-err")) 90 }) 91 92 It("returns an error", func() { 93 Expect(executeErr).To(MatchError("create-org-quota-err")) 94 95 Expect(fakeActor.CreateOrganizationQuotaCallCount()).To(Equal(1)) 96 Expect(testUI.Err).To(Say("warn-456")) 97 Expect(testUI.Err).To(Say("warn-789")) 98 }) 99 }) 100 101 When("the org quota is created successfully", func() { 102 When("the org quota is provided a value for each flag", func() { 103 // before each to make sure the env is set up 104 BeforeEach(func() { 105 cmd.PaidServicePlans = true 106 cmd.NumAppInstances = flag.IntegerLimit{IsSet: true, Value: 10} 107 cmd.PerProcessMemory = flag.MegabytesWithUnlimited{IsSet: true, Value: 9} 108 cmd.TotalMemory = flag.MegabytesWithUnlimited{IsSet: true, Value: 2048} 109 cmd.TotalRoutes = flag.IntegerLimit{IsSet: true, Value: 7} 110 cmd.TotalReservedPorts = flag.IntegerLimit{IsSet: true, Value: 1} 111 cmd.TotalServiceInstances = flag.IntegerLimit{IsSet: true, Value: 2} 112 cmd.TotalLogVolume = flag.BytesWithUnlimited{IsSet: true, Value: 8} 113 fakeActor.CreateOrganizationQuotaReturns( 114 v7action.Warnings{"warning"}, 115 nil) 116 }) 117 118 It("creates a quota with the values given from the flags", func() { 119 Expect(executeErr).ToNot(HaveOccurred()) 120 Expect(fakeActor.CreateOrganizationQuotaCallCount()).To(Equal(1)) 121 122 quotaName, quotaLimits := fakeActor.CreateOrganizationQuotaArgsForCall(0) 123 Expect(quotaName).To(Equal(orgQuotaName)) 124 125 Expect(quotaLimits.TotalInstances.IsSet).To(Equal(true)) 126 Expect(quotaLimits.TotalInstances.Value).To(Equal(10)) 127 128 Expect(quotaLimits.PerProcessMemoryInMB.IsSet).To(Equal(true)) 129 Expect(quotaLimits.PerProcessMemoryInMB.Value).To(Equal(9)) 130 131 Expect(*quotaLimits.PaidServicesAllowed).To(Equal(true)) 132 133 Expect(quotaLimits.TotalMemoryInMB.IsSet).To(Equal(true)) 134 Expect(quotaLimits.TotalMemoryInMB.Value).To(Equal(2048)) 135 136 Expect(quotaLimits.TotalRoutes.IsSet).To(Equal(true)) 137 Expect(quotaLimits.TotalRoutes.Value).To(Equal(7)) 138 139 Expect(quotaLimits.TotalReservedPorts.IsSet).To(Equal(true)) 140 Expect(quotaLimits.TotalReservedPorts.Value).To(Equal(1)) 141 142 Expect(quotaLimits.TotalServiceInstances.IsSet).To(Equal(true)) 143 Expect(quotaLimits.TotalServiceInstances.Value).To(Equal(2)) 144 145 Expect(quotaLimits.TotalLogVolume.IsSet).To(Equal(true)) 146 Expect(quotaLimits.TotalLogVolume.Value).To(Equal(8)) 147 148 Expect(testUI.Out).To(Say("Creating org quota %s as bob...", orgQuotaName)) 149 Expect(testUI.Out).To(Say("OK")) 150 }) 151 }) 152 }) 153 })