github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/create_space_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/types" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("create-space-quota Command", func() { 22 var ( 23 cmd v7.CreateSpaceQuotaCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v7fakes.FakeActor 28 binaryName string 29 executeErr error 30 31 userName string 32 spaceQuotaName string 33 ) 34 35 BeforeEach(func() { 36 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 37 fakeConfig = new(commandfakes.FakeConfig) 38 fakeSharedActor = new(commandfakes.FakeSharedActor) 39 fakeActor = new(v7fakes.FakeActor) 40 41 binaryName = "faceman" 42 fakeConfig.BinaryNameReturns(binaryName) 43 spaceQuotaName = "some-space-quota" 44 userName = "some-user-name" 45 46 cmd = v7.CreateSpaceQuotaCommand{ 47 BaseCommand: v7.BaseCommand{ 48 UI: testUI, 49 Config: fakeConfig, 50 SharedActor: fakeSharedActor, 51 Actor: fakeActor, 52 }, 53 RequiredArgs: flag.SpaceQuota{SpaceQuota: spaceQuotaName}, 54 } 55 }) 56 57 JustBeforeEach(func() { 58 executeErr = cmd.Execute(nil) 59 }) 60 61 When("the environment is not set up correctly", func() { 62 BeforeEach(func() { 63 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 64 }) 65 66 It("returns an error", func() { 67 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 68 69 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 70 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 71 Expect(checkTargetedOrg).To(BeTrue()) 72 Expect(checkTargetedSpace).To(BeFalse()) 73 }) 74 }) 75 76 When("the environment is setup correctly", func() { 77 BeforeEach(func() { 78 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 79 Name: "some-org-name", 80 GUID: "some-org-guid", 81 }) 82 fakeActor.GetCurrentUserReturns(configv3.User{ 83 Name: userName, 84 Origin: "some-user-origin", 85 }, nil) 86 }) 87 88 It("prints text indicating it is creating a space quota", func() { 89 Expect(executeErr).NotTo(HaveOccurred()) 90 Expect(testUI.Out).To(Say(`Creating space quota %s for org %s as %s\.\.\.`, spaceQuotaName, "some-org-name", userName)) 91 }) 92 93 When("creating the space quota errors", func() { 94 BeforeEach(func() { 95 fakeActor.CreateSpaceQuotaReturns( 96 v7action.Warnings{"warnings-1", "warnings-2"}, 97 errors.New("err-create-space-quota"), 98 ) 99 }) 100 101 It("returns an error and displays warnings", func() { 102 Expect(executeErr).To(MatchError("err-create-space-quota")) 103 Expect(testUI.Err).To(Say("warnings-1")) 104 Expect(testUI.Err).To(Say("warnings-2")) 105 }) 106 }) 107 108 When("no flag limits are given", func() { 109 var ( 110 falseValue = false 111 ) 112 BeforeEach(func() { 113 fakeActor.CreateSpaceQuotaReturns( 114 v7action.Warnings{"warnings-1", "warnings-2"}, 115 nil, 116 ) 117 }) 118 119 It("creates the space quota in the targeted organization", func() { 120 Expect(fakeActor.CreateSpaceQuotaCallCount()).To(Equal(1)) 121 expectedSpaceQuotaName, expectedOrgGUID, expectedLimits := fakeActor.CreateSpaceQuotaArgsForCall(0) 122 Expect(expectedSpaceQuotaName).To(Equal(spaceQuotaName)) 123 Expect(expectedOrgGUID).To(Equal("some-org-guid")) 124 Expect(expectedLimits).To(Equal(v7action.QuotaLimits{PaidServicesAllowed: &falseValue})) 125 }) 126 127 It("prints all warnings, text indicating creation completion, ok and then a tip", func() { 128 Expect(executeErr).ToNot(HaveOccurred()) 129 Expect(testUI.Err).To(Say("warnings-1")) 130 Expect(testUI.Err).To(Say("warnings-2")) 131 Expect(testUI.Out).To(Say("Creating space quota some-space-quota for org some-org-name as some-user-name...")) 132 Expect(testUI.Out).To(Say("OK")) 133 }) 134 }) 135 136 When("all flag limits are given", func() { 137 BeforeEach(func() { 138 cmd.TotalMemory = flag.MegabytesWithUnlimited{IsSet: true, Value: 47} 139 cmd.PerProcessMemory = flag.MegabytesWithUnlimited{IsSet: true, Value: 23} 140 cmd.NumAppInstances = flag.IntegerLimit{IsSet: true, Value: 4} 141 cmd.PaidServicePlans = true 142 cmd.TotalServiceInstances = flag.IntegerLimit{IsSet: true, Value: 9} 143 cmd.TotalRoutes = flag.IntegerLimit{IsSet: true, Value: 1} 144 cmd.TotalReservedPorts = flag.IntegerLimit{IsSet: true, Value: 7} 145 cmd.TotalLogVolume = flag.BytesWithUnlimited{IsSet: true, Value: 512} 146 147 fakeActor.CreateSpaceQuotaReturns( 148 v7action.Warnings{"warnings-1", "warnings-2"}, 149 nil, 150 ) 151 }) 152 153 It("creates the space quota with the specified limits in the targeted organization", func() { 154 Expect(fakeActor.CreateSpaceQuotaCallCount()).To(Equal(1)) 155 expectedSpaceQuotaName, expectedOrgGUID, expectedLimits := fakeActor.CreateSpaceQuotaArgsForCall(0) 156 Expect(expectedSpaceQuotaName).To(Equal(spaceQuotaName)) 157 Expect(expectedOrgGUID).To(Equal("some-org-guid")) 158 trueValue := true 159 Expect(expectedLimits).To(Equal(v7action.QuotaLimits{ 160 TotalMemoryInMB: &types.NullInt{IsSet: true, Value: 47}, 161 PerProcessMemoryInMB: &types.NullInt{IsSet: true, Value: 23}, 162 TotalInstances: &types.NullInt{IsSet: true, Value: 4}, 163 PaidServicesAllowed: &trueValue, 164 TotalServiceInstances: &types.NullInt{IsSet: true, Value: 9}, 165 TotalRoutes: &types.NullInt{IsSet: true, Value: 1}, 166 TotalReservedPorts: &types.NullInt{IsSet: true, Value: 7}, 167 TotalLogVolume: &types.NullInt{IsSet: true, Value: 512}, 168 })) 169 }) 170 171 It("prints all warnings, text indicating creation completion, ok and then a tip", func() { 172 Expect(executeErr).ToNot(HaveOccurred()) 173 Expect(testUI.Err).To(Say("warnings-1")) 174 Expect(testUI.Err).To(Say("warnings-2")) 175 Expect(testUI.Out).To(Say("Creating space quota some-space-quota for org some-org-name as some-user-name...")) 176 Expect(testUI.Out).To(Say("OK")) 177 }) 178 }) 179 180 When("the space quota already exists", func() { 181 BeforeEach(func() { 182 fakeActor.CreateSpaceQuotaReturns(v7action.Warnings{"some-warning"}, ccerror.QuotaAlreadyExists{Message: "yikes"}) 183 }) 184 185 It("displays all warnings, that the space quota already exists, and does not error", func() { 186 Expect(executeErr).ToNot(HaveOccurred()) 187 188 Expect(testUI.Err).To(Say("some-warning")) 189 Expect(testUI.Out).To(Say(`Creating space quota %s for org %s as %s\.\.\.`, spaceQuotaName, "some-org-name", userName)) 190 Expect(testUI.Err).To(Say(`yikes`)) 191 Expect(testUI.Out).To(Say("OK")) 192 }) 193 }) 194 }) 195 })