github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/quota/quota_test.go (about) 1 package quota_test 2 3 import ( 4 . "github.com/onsi/ginkgo" 5 . "github.com/onsi/gomega" 6 7 "code.cloudfoundry.org/cli/cf/api/quotas/quotasfakes" 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 10 "code.cloudfoundry.org/cli/cf/errors" 11 "code.cloudfoundry.org/cli/cf/models" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 14 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 15 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 16 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 17 18 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 19 ) 20 21 var _ = Describe("quota", func() { 22 var ( 23 ui *testterm.FakeUI 24 requirementsFactory *requirementsfakes.FakeFactory 25 config coreconfig.Repository 26 quotaRepo *quotasfakes.FakeQuotaRepository 27 deps commandregistry.Dependency 28 ) 29 30 updateCommandDependency := func(pluginCall bool) { 31 deps.UI = ui 32 deps.Config = config 33 deps.RepoLocator = deps.RepoLocator.SetQuotaRepository(quotaRepo) 34 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("quota").SetDependency(deps, pluginCall)) 35 } 36 37 BeforeEach(func() { 38 ui = &testterm.FakeUI{} 39 requirementsFactory = new(requirementsfakes.FakeFactory) 40 quotaRepo = new(quotasfakes.FakeQuotaRepository) 41 config = testconfig.NewRepositoryWithDefaults() 42 }) 43 44 runCommand := func(args ...string) bool { 45 return testcmd.RunCLICommand("quota", args, requirementsFactory, updateCommandDependency, false, ui) 46 } 47 48 Context("When not logged in", func() { 49 BeforeEach(func() { 50 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 51 }) 52 It("fails requirements", func() { 53 Expect(runCommand("quota-name")).To(BeFalse()) 54 }) 55 }) 56 57 Context("When logged in", func() { 58 BeforeEach(func() { 59 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 60 }) 61 62 Context("When not providing a quota name", func() { 63 It("fails with usage", func() { 64 runCommand() 65 Expect(ui.Outputs()).To(ContainSubstrings( 66 []string{"Incorrect Usage", "Requires", "argument"}, 67 )) 68 }) 69 }) 70 71 Context("When providing a quota name", func() { 72 Context("that exists", func() { 73 BeforeEach(func() { 74 quotaRepo.FindByNameReturns(models.QuotaFields{ 75 GUID: "my-quota-guid", 76 Name: "muh-muh-muh-my-qua-quota", 77 MemoryLimit: 512, 78 InstanceMemoryLimit: 5, 79 RoutesLimit: 2000, 80 ServicesLimit: 47, 81 NonBasicServicesAllowed: true, 82 AppInstanceLimit: 7, 83 ReservedRoutePorts: "5", 84 }, nil) 85 }) 86 87 It("shows you that quota", func() { 88 runCommand("muh-muh-muh-my-qua-quota") 89 90 Expect(ui.Outputs()).To(ContainSubstrings( 91 []string{"Getting quota", "muh-muh-muh-my-qua-quota", "my-user"}, 92 []string{"OK"}, 93 []string{"Total Memory", "512M"}, 94 []string{"Instance Memory", "5M"}, 95 []string{"Routes", "2000"}, 96 []string{"Services", "47"}, 97 []string{"Paid service plans", "allowed"}, 98 []string{"App instance limit", "7"}, 99 []string{"Reserved Route Ports", "5"}, 100 )) 101 }) 102 }) 103 104 Context("when the app instance limit is -1", func() { 105 BeforeEach(func() { 106 quotaRepo.FindByNameReturns(models.QuotaFields{ 107 GUID: "my-quota-guid", 108 Name: "muh-muh-muh-my-qua-quota", 109 MemoryLimit: 512, 110 InstanceMemoryLimit: 5, 111 RoutesLimit: 2000, 112 ServicesLimit: 47, 113 NonBasicServicesAllowed: true, 114 AppInstanceLimit: -1, 115 }, nil) 116 }) 117 118 It("shows you that quota", func() { 119 runCommand("muh-muh-muh-my-qua-quota") 120 121 Expect(ui.Outputs()).To(ContainSubstrings( 122 []string{"Getting quota", "muh-muh-muh-my-qua-quota", "my-user"}, 123 []string{"OK"}, 124 []string{"Total Memory", "512M"}, 125 []string{"Instance Memory", "5M"}, 126 []string{"Routes", "2000"}, 127 []string{"Services", "47"}, 128 []string{"Paid service plans", "allowed"}, 129 []string{"App instance limit", "unlimited"}, 130 )) 131 }) 132 }) 133 134 Context("when the reserved route ports is -1", func() { 135 BeforeEach(func() { 136 quotaRepo.FindByNameReturns(models.QuotaFields{ 137 GUID: "my-quota-guid", 138 Name: "muh-muh-muh-my-qua-quota", 139 MemoryLimit: 512, 140 InstanceMemoryLimit: 5, 141 RoutesLimit: 2000, 142 ServicesLimit: 47, 143 NonBasicServicesAllowed: true, 144 ReservedRoutePorts: "-1", 145 }, nil) 146 }) 147 148 It("shows you that quota", func() { 149 runCommand("muh-muh-muh-my-qua-quota") 150 151 Expect(ui.Outputs()).To(ContainSubstrings( 152 []string{"Getting quota", "muh-muh-muh-my-qua-quota", "my-user"}, 153 []string{"OK"}, 154 []string{"Total Memory", "512M"}, 155 []string{"Instance Memory", "5M"}, 156 []string{"Routes", "2000"}, 157 []string{"Services", "47"}, 158 []string{"Paid service plans", "allowed"}, 159 []string{"Reserved Route Ports", "unlimited"}, 160 )) 161 }) 162 }) 163 164 Context("when the reserved route ports is not provided by the API", func() { 165 BeforeEach(func() { 166 quotaRepo.FindByNameReturns(models.QuotaFields{ 167 GUID: "my-quota-guid", 168 Name: "muh-muh-muh-my-qua-quota", 169 MemoryLimit: 512, 170 InstanceMemoryLimit: 5, 171 RoutesLimit: 2000, 172 ServicesLimit: 47, 173 NonBasicServicesAllowed: true, 174 }, nil) 175 }) 176 177 It("does not show reserved route ports", func() { 178 runCommand("muh-muh-muh-my-qua-quota") 179 180 Expect(ui.Outputs()).ToNot(ContainSubstrings( 181 []string{"Reserved Route Ports"}, 182 )) 183 }) 184 }) 185 186 Context("when instance memory limit is -1", func() { 187 BeforeEach(func() { 188 quotaRepo.FindByNameReturns(models.QuotaFields{ 189 GUID: "my-quota-guid", 190 Name: "muh-muh-muh-my-qua-quota", 191 MemoryLimit: 512, 192 InstanceMemoryLimit: -1, 193 RoutesLimit: 2000, 194 ServicesLimit: 47, 195 NonBasicServicesAllowed: true, 196 }, nil) 197 }) 198 199 It("shows you that quota", func() { 200 runCommand("muh-muh-muh-my-qua-quota") 201 202 Expect(ui.Outputs()).To(ContainSubstrings( 203 []string{"Getting quota", "muh-muh-muh-my-qua-quota", "my-user"}, 204 []string{"OK"}, 205 []string{"Total Memory", "512M"}, 206 []string{"Instance Memory", "unlimited"}, 207 []string{"Routes", "2000"}, 208 []string{"Services", "47"}, 209 []string{"Paid service plans", "allowed"}, 210 )) 211 }) 212 }) 213 214 Context("when the services limit is -1", func() { 215 BeforeEach(func() { 216 quotaRepo.FindByNameReturns(models.QuotaFields{ 217 GUID: "my-quota-guid", 218 Name: "muh-muh-muh-my-qua-quota", 219 MemoryLimit: 512, 220 InstanceMemoryLimit: 14, 221 RoutesLimit: 2000, 222 ServicesLimit: -1, 223 NonBasicServicesAllowed: true, 224 }, nil) 225 }) 226 227 It("shows you that quota", func() { 228 runCommand("muh-muh-muh-my-qua-quota") 229 230 Expect(ui.Outputs()).To(ContainSubstrings( 231 []string{"Getting quota", "muh-muh-muh-my-qua-quota", "my-user"}, 232 []string{"OK"}, 233 []string{"Total Memory", "512M"}, 234 []string{"Instance Memory", "14M"}, 235 []string{"Routes", "2000"}, 236 []string{"Services", "unlimited"}, 237 []string{"Paid service plans", "allowed"}, 238 )) 239 }) 240 }) 241 242 Context("that doesn't exist", func() { 243 BeforeEach(func() { 244 quotaRepo.FindByNameReturns(models.QuotaFields{}, errors.New("oops i accidentally a quota")) 245 }) 246 247 It("gives an error", func() { 248 runCommand("an-quota") 249 250 Expect(ui.Outputs()).To(ContainSubstrings( 251 []string{"FAILED"}, 252 []string{"oops"}, 253 )) 254 }) 255 }) 256 }) 257 }) 258 })