github.com/loafoe/cli@v7.1.0+incompatible/command/v7/set_space_quota_command_test.go (about) 1 package v7_test 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/actor/v7action" 6 "code.cloudfoundry.org/cli/cf/errors" 7 "code.cloudfoundry.org/cli/command/commandfakes" 8 "code.cloudfoundry.org/cli/command/flag" 9 "code.cloudfoundry.org/cli/command/v7/v7fakes" 10 "code.cloudfoundry.org/cli/resources" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 14 . "code.cloudfoundry.org/cli/command/v7" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("set-space-quota Command", func() { 21 var ( 22 cmd SetSpaceQuotaCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v7fakes.FakeActor 27 binaryName string 28 executeErr error 29 input *Buffer 30 31 org configv3.Organization 32 orgGUID string 33 34 spaceName string 35 spaceQuotaName string 36 37 getSpaceWarning string 38 applyQuotaWarning string 39 currentUser string 40 ) 41 42 BeforeEach(func() { 43 input = NewBuffer() 44 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 45 fakeConfig = new(commandfakes.FakeConfig) 46 fakeSharedActor = new(commandfakes.FakeSharedActor) 47 fakeActor = new(v7fakes.FakeActor) 48 getSpaceWarning = RandomString("get-space-warning") 49 applyQuotaWarning = RandomString("apply-quota-warning") 50 51 orgGUID = RandomString("org-guid") 52 spaceName = RandomString("space-name") 53 spaceQuotaName = RandomString("space-quota-name") 54 55 cmd = SetSpaceQuotaCommand{ 56 BaseCommand: BaseCommand{ 57 UI: testUI, 58 Config: fakeConfig, 59 SharedActor: fakeSharedActor, 60 Actor: fakeActor, 61 }, 62 RequiredArgs: flag.SetSpaceQuotaArgs{ 63 Space: spaceName, 64 SpaceQuota: spaceQuotaName, 65 }, 66 } 67 68 org = configv3.Organization{ 69 GUID: orgGUID, 70 } 71 72 binaryName = "faceman" 73 fakeConfig.BinaryNameReturns(binaryName) 74 75 currentUser = "current-user" 76 fakeConfig.CurrentUserNameReturns(currentUser, nil) 77 78 fakeConfig.TargetedOrganizationReturns(org) 79 80 fakeActor.GetSpaceByNameAndOrganizationReturns( 81 resources.Space{GUID: "some-space-guid"}, 82 v7action.Warnings{getSpaceWarning}, 83 nil, 84 ) 85 86 fakeActor.ApplySpaceQuotaByNameReturns( 87 v7action.Warnings{applyQuotaWarning}, 88 nil, 89 ) 90 }) 91 92 JustBeforeEach(func() { 93 executeErr = cmd.Execute(nil) 94 }) 95 96 When("the environment is not set up correctly", func() { 97 BeforeEach(func() { 98 fakeSharedActor.CheckTargetReturns(errors.New("check-target-failure")) 99 }) 100 101 It("requires a targeted org (but not space)", func() { 102 targetedOrgRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0) 103 Expect(targetedOrgRequired).To(BeTrue()) 104 Expect(targetedSpaceRequired).To(BeFalse()) 105 }) 106 107 It("should return the error", func() { 108 Expect(executeErr).To(MatchError("check-target-failure")) 109 }) 110 }) 111 112 It("signals what it's going to do", func() { 113 Expect(testUI.Out).To(Say("Setting space quota %s to space %s as %s...", 114 spaceQuotaName, spaceName, currentUser)) 115 }) 116 117 When("it can't get the space information", func() { 118 BeforeEach(func() { 119 fakeActor.GetSpaceByNameAndOrganizationReturns( 120 resources.Space{}, 121 v7action.Warnings{getSpaceWarning}, 122 actionerror.SpaceNotFoundError{Name: spaceName}, 123 ) 124 }) 125 126 It("returns an error", func() { 127 Expect(testUI.Err).To(Say(getSpaceWarning)) 128 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: spaceName})) 129 }) 130 }) 131 132 It("passes the expected arguments when getting the space", func() { 133 spaceNameArg, orgGUIDArg := fakeActor.GetSpaceByNameAndOrganizationArgsForCall(0) 134 Expect(spaceNameArg).To(Equal(spaceName)) 135 Expect(orgGUIDArg).To(Equal(orgGUID)) 136 }) 137 138 When("it can't apply the space quota", func() { 139 BeforeEach(func() { 140 fakeActor.ApplySpaceQuotaByNameReturns( 141 v7action.Warnings{getSpaceWarning}, 142 errors.New("some-apply-error"), 143 ) 144 }) 145 146 It("returns an error", func() { 147 Expect(testUI.Err).To(Say(getSpaceWarning)) 148 Expect(executeErr).To(MatchError("some-apply-error")) 149 }) 150 }) 151 152 It("prints the warnings", func() { 153 Expect(fakeActor.ApplySpaceQuotaByNameCallCount()).To(Equal(1)) 154 Expect(testUI.Err).To(Say(applyQuotaWarning)) 155 }) 156 157 It("prints 'OK'", func() { 158 Expect(testUI.Out).To(Say("OK")) 159 }) 160 161 It("does not error", func() { 162 Expect(executeErr).To(Not(HaveOccurred())) 163 }) 164 })