github.com/loafoe/cli@v7.1.0+incompatible/command/v7/org_quotas_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/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v7" 10 "code.cloudfoundry.org/cli/command/v7/v7fakes" 11 "code.cloudfoundry.org/cli/resources" 12 "code.cloudfoundry.org/cli/types" 13 "code.cloudfoundry.org/cli/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("org-quotas command", func() { 22 var ( 23 cmd OrgQuotasCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v7fakes.FakeActor 28 executeErr error 29 args []string 30 binaryName string 31 trueValue = true 32 ) 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v7fakes.FakeActor) 39 args = nil 40 41 cmd = OrgQuotasCommand{ 42 BaseCommand: BaseCommand{ 43 UI: testUI, 44 Config: fakeConfig, 45 SharedActor: fakeSharedActor, 46 Actor: fakeActor, 47 }, 48 } 49 50 binaryName = "faceman" 51 fakeConfig.BinaryNameReturns(binaryName) 52 }) 53 54 JustBeforeEach(func() { 55 executeErr = cmd.Execute(args) 56 }) 57 58 When("running the command successfully", func() { 59 BeforeEach(func() { 60 fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil) 61 orgQuotas := []resources.OrganizationQuota{ 62 { 63 Quota: resources.Quota{ 64 Name: "org-quota-1", 65 Apps: resources.AppLimit{ 66 TotalMemory: &types.NullInt{Value: 1048576, IsSet: true}, 67 InstanceMemory: &types.NullInt{Value: 32, IsSet: true}, 68 TotalAppInstances: &types.NullInt{Value: 3, IsSet: true}, 69 }, 70 Services: resources.ServiceLimit{ 71 TotalServiceInstances: &types.NullInt{Value: 3, IsSet: true}, 72 PaidServicePlans: &trueValue, 73 }, 74 Routes: resources.RouteLimit{ 75 TotalRoutes: &types.NullInt{Value: 5, IsSet: true}, 76 TotalReservedPorts: &types.NullInt{Value: 2, IsSet: true}, 77 }, 78 }, 79 }, 80 } 81 fakeActor.GetOrganizationQuotasReturns(orgQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil) 82 }) 83 84 It("should print text indicating the command status", func() { 85 Expect(executeErr).NotTo(HaveOccurred()) 86 Expect(testUI.Out).To(Say(`Getting org quotas as apple\.\.\.`)) 87 Expect(testUI.Err).To(Say("some-warning-1")) 88 Expect(testUI.Err).To(Say("some-warning-2")) 89 }) 90 91 It("retrieves and displays all quotas", func() { 92 Expect(executeErr).NotTo(HaveOccurred()) 93 Expect(fakeActor.GetOrganizationQuotasCallCount()).To(Equal(1)) 94 Expect(testUI.Out).To(Say(`name\s+total memory\s+instance memory\s+routes\s+service instances\s+paid service plans\s+app instances\s+route ports`)) 95 Expect(testUI.Out).To(Say(`org-quota-1\s+1T\s+32M\s+5\s+3\s+allowed\s+3\s+2`)) 96 }) 97 98 When("there are limits that have not been configured", func() { 99 BeforeEach(func() { 100 orgQuotas := []resources.OrganizationQuota{ 101 { 102 Quota: resources.Quota{ 103 Name: "default", 104 Apps: resources.AppLimit{ 105 TotalMemory: &types.NullInt{Value: 0, IsSet: false}, 106 InstanceMemory: &types.NullInt{Value: 0, IsSet: false}, 107 TotalAppInstances: &types.NullInt{Value: 0, IsSet: false}, 108 }, 109 Services: resources.ServiceLimit{ 110 TotalServiceInstances: &types.NullInt{Value: 0, IsSet: false}, 111 PaidServicePlans: &trueValue, 112 }, 113 Routes: resources.RouteLimit{ 114 TotalRoutes: &types.NullInt{Value: 0, IsSet: false}, 115 TotalReservedPorts: &types.NullInt{Value: 0, IsSet: false}, 116 }, 117 }, 118 }, 119 } 120 fakeActor.GetOrganizationQuotasReturns(orgQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil) 121 122 }) 123 124 It("should convert default values from the API into readable outputs", func() { 125 Expect(executeErr).NotTo(HaveOccurred()) 126 Expect(testUI.Out).To(Say(`name\s+total memory\s+instance memory\s+routes\s+service instances\s+paid service plans\s+app instances\s+route ports`)) 127 Expect(testUI.Out).To(Say(`default\s+unlimited\s+unlimited\s+unlimited\s+unlimited\s+allowed\s+unlimited\s+unlimited`)) 128 129 }) 130 }) 131 }) 132 133 When("the environment is not set up correctly", func() { 134 BeforeEach(func() { 135 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 136 }) 137 138 It("returns an error", func() { 139 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 140 141 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 142 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 143 Expect(checkTargetedOrg).To(BeFalse()) 144 Expect(checkTargetedSpace).To(BeFalse()) 145 }) 146 }) 147 148 When("getting quotas fails", func() { 149 BeforeEach(func() { 150 fakeActor.GetOrganizationQuotasReturns(nil, v7action.Warnings{"some-warning-1", "some-warning-2"}, errors.New("some-error")) 151 }) 152 153 It("prints warnings and returns error", func() { 154 Expect(executeErr).To(MatchError("some-error")) 155 156 Expect(testUI.Err).To(Say("some-warning-1")) 157 Expect(testUI.Err).To(Say("some-warning-2")) 158 }) 159 }) 160 161 When("the quota list is empty", func() { 162 BeforeEach(func() { 163 fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil) 164 fakeActor.GetOrganizationQuotasReturns([]resources.OrganizationQuota{}, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil) 165 }) 166 167 It("prints warnings and returns error", func() { 168 Expect(executeErr).NotTo(HaveOccurred()) 169 170 Expect(testUI.Err).To(Say("some-warning-1")) 171 Expect(testUI.Err).To(Say("some-warning-2")) 172 Expect(testUI.Out).To(Say(`Getting org quotas as apple\.\.\.`)) 173 Expect(testUI.Out).To(Say("No organization quotas found.")) 174 }) 175 }) 176 })