github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/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 fakeConfig.CurrentUserReturns(configv3.User{Name: currentUserName}, nil) 52 }) 53 54 JustBeforeEach(func() { 55 56 executeErr = cmd.Execute(nil) 57 }) 58 59 When("the environment is not set up correctly", func() { 60 BeforeEach(func() { 61 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{}) 62 }) 63 64 It("returns an error", func() { 65 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{})) 66 67 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 68 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 69 Expect(checkTargetedOrg).To(BeFalse()) 70 Expect(checkTargetedSpace).To(BeFalse()) 71 }) 72 }) 73 74 When("the organization quota already exists", func() { 75 BeforeEach(func() { 76 fakeActor.CreateOrganizationQuotaReturns(v7action.Warnings{"warn-abc"}, ccerror.QuotaAlreadyExists{Message: "quota-already-exists"}) 77 }) 78 79 It("displays that it already exists, but does not return an error", func() { 80 Expect(executeErr).ToNot(HaveOccurred()) 81 82 Expect(fakeActor.CreateOrganizationQuotaCallCount()).To(Equal(1)) 83 Expect(testUI.Err).To(Say("warn-abc")) 84 Expect(testUI.Err).To(Say("quota-already-exists")) 85 }) 86 }) 87 88 When("creating the organization quota fails", func() { 89 BeforeEach(func() { 90 fakeActor.CreateOrganizationQuotaReturns(v7action.Warnings{"warn-456", "warn-789"}, errors.New("create-org-quota-err")) 91 }) 92 93 It("returns an error", func() { 94 Expect(executeErr).To(MatchError("create-org-quota-err")) 95 96 Expect(fakeActor.CreateOrganizationQuotaCallCount()).To(Equal(1)) 97 Expect(testUI.Err).To(Say("warn-456")) 98 Expect(testUI.Err).To(Say("warn-789")) 99 }) 100 }) 101 102 When("the org quota is created successfully", func() { 103 When("the org quota is provided a value for each flag", func() { 104 // before each to make sure the env is set up 105 BeforeEach(func() { 106 cmd.PaidServicePlans = true 107 cmd.NumAppInstances = flag.IntegerLimit{IsSet: true, Value: 10} 108 cmd.PerProcessMemory = flag.MemoryWithUnlimited{IsSet: true, Value: 9} 109 cmd.TotalMemory = flag.MemoryWithUnlimited{IsSet: true, Value: 2048} 110 cmd.TotalRoutes = flag.IntegerLimit{IsSet: true, Value: 7} 111 cmd.TotalReservedPorts = flag.IntegerLimit{IsSet: true, Value: 1} 112 cmd.TotalServiceInstances = flag.IntegerLimit{IsSet: true, Value: 2} 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(testUI.Out).To(Say("Creating org quota %s as bob...", orgQuotaName)) 146 Expect(testUI.Out).To(Say("OK")) 147 }) 148 }) 149 }) 150 })