github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/spacequota/space_quota_test.go (about) 1 package spacequota_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 12 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 13 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 14 15 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("quotas command", func() { 21 var ( 22 ui *testterm.FakeUI 23 quotaRepo *spacequotasfakes.FakeSpaceQuotaRepository 24 requirementsFactory *requirementsfakes.FakeFactory 25 configRepo coreconfig.Repository 26 deps commandregistry.Dependency 27 ) 28 29 updateCommandDependency := func(pluginCall bool) { 30 deps.UI = ui 31 deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo) 32 deps.Config = configRepo 33 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("space-quota").SetDependency(deps, pluginCall)) 34 } 35 36 BeforeEach(func() { 37 ui = &testterm.FakeUI{} 38 quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository) 39 configRepo = testconfig.NewRepositoryWithDefaults() 40 requirementsFactory = new(requirementsfakes.FakeFactory) 41 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 42 }) 43 44 runCommand := func(args ...string) bool { 45 return testcmd.RunCLICommand("space-quota", args, requirementsFactory, updateCommandDependency, false, ui) 46 } 47 48 Describe("requirements", func() { 49 It("requires the user to be logged in", func() { 50 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 51 Expect(runCommand("foo")).ToNot(HavePassedRequirements()) 52 }) 53 54 It("requires the user to target an org", func() { 55 orgReq := new(requirementsfakes.FakeTargetedOrgRequirement) 56 orgReq.ExecuteReturns(errors.New("not targeting org")) 57 requirementsFactory.NewTargetedOrgRequirementReturns(orgReq) 58 Expect(runCommand("bar")).ToNot(HavePassedRequirements()) 59 }) 60 61 It("fails when a quota name is not provided", func() { 62 requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement)) 63 Expect(runCommand()).ToNot(HavePassedRequirements()) 64 }) 65 }) 66 67 Context("when logged in", func() { 68 JustBeforeEach(func() { 69 requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement)) 70 runCommand("quota-name") 71 }) 72 73 Context("when quotas exist", func() { 74 BeforeEach(func() { 75 quotaRepo.FindByNameReturns( 76 models.SpaceQuota{ 77 Name: "quota-name", 78 MemoryLimit: 1024, 79 InstanceMemoryLimit: -1, 80 RoutesLimit: 111, 81 ServicesLimit: 222, 82 NonBasicServicesAllowed: true, 83 OrgGUID: "my-org-guid", 84 AppInstanceLimit: 5, 85 ReservedRoutePortsLimit: "4", 86 }, nil) 87 }) 88 89 It("lists the specific quota info", func() { 90 Expect(quotaRepo.FindByNameArgsForCall(0)).To(Equal("quota-name")) 91 Expect(ui.Outputs()).To(ContainSubstrings( 92 []string{"Getting space quota quota-name info as", "my-user"}, 93 []string{"OK"}, 94 []string{"total memory limit", "1G"}, 95 []string{"instance memory limit", "unlimited"}, 96 []string{"routes", "111"}, 97 []string{"service", "222"}, 98 []string{"non basic services", "allowed"}, 99 []string{"app instance limit", "5"}, 100 []string{"reserved route ports", "4"}, 101 )) 102 }) 103 104 Context("when the services are unlimited", func() { 105 BeforeEach(func() { 106 quotaRepo.FindByNameReturns( 107 models.SpaceQuota{ 108 Name: "quota-name", 109 MemoryLimit: 1024, 110 InstanceMemoryLimit: 14, 111 RoutesLimit: 111, 112 ServicesLimit: -1, 113 NonBasicServicesAllowed: true, 114 OrgGUID: "my-org-guid", 115 }, nil) 116 117 }) 118 119 It("replaces -1 with unlimited", func() { 120 Expect(quotaRepo.FindByNameArgsForCall(0)).To(Equal("quota-name")) 121 Expect(ui.Outputs()).To(ContainSubstrings( 122 []string{"Getting space quota quota-name info as", "my-user"}, 123 []string{"OK"}, 124 []string{"total memory limit", "1G"}, 125 []string{"instance memory limit", "14M"}, 126 []string{"routes", "111"}, 127 []string{"service", "unlimited"}, 128 []string{"non basic services", "allowed"}, 129 )) 130 }) 131 }) 132 133 Context("when the app instances are unlimited", func() { 134 BeforeEach(func() { 135 quotaRepo.FindByNameReturns( 136 models.SpaceQuota{ 137 Name: "quota-name", 138 MemoryLimit: 1024, 139 InstanceMemoryLimit: -1, 140 RoutesLimit: 111, 141 ServicesLimit: 222, 142 NonBasicServicesAllowed: true, 143 OrgGUID: "my-org-guid", 144 AppInstanceLimit: -1, 145 }, nil) 146 }) 147 148 It("replaces -1 with unlimited", func() { 149 Expect(quotaRepo.FindByNameArgsForCall(0)).To(Equal("quota-name")) 150 Expect(ui.Outputs()).To(ContainSubstrings( 151 []string{"Getting space quota quota-name info as", "my-user"}, 152 []string{"OK"}, 153 []string{"total memory limit", "1G"}, 154 []string{"instance memory limit", "unlimited"}, 155 []string{"routes", "111"}, 156 []string{"service", "222"}, 157 []string{"non basic services", "allowed"}, 158 []string{"app instance limit", "unlimited"}, 159 )) 160 }) 161 }) 162 }) 163 Context("when an error occurs fetching quotas", func() { 164 BeforeEach(func() { 165 quotaRepo.FindByNameReturns(models.SpaceQuota{}, errors.New("I haz a borken!")) 166 }) 167 168 It("prints an error", func() { 169 Expect(ui.Outputs()).To(ContainSubstrings( 170 []string{"Getting space quota quota-name info as", "my-user"}, 171 []string{"FAILED"}, 172 )) 173 }) 174 }) 175 }) 176 177 })