github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/spacequota/space_quotas_test.go (about) 1 package spacequota_test 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api/space_quotas/fakes" 5 "github.com/cloudfoundry/cli/cf/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/errors" 8 "github.com/cloudfoundry/cli/cf/models" 9 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 10 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 11 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 12 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 13 14 . "github.com/cloudfoundry/cli/testhelpers/matchers" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("quotas command", func() { 20 var ( 21 ui *testterm.FakeUI 22 quotaRepo *fakes.FakeSpaceQuotaRepository 23 configRepo core_config.Repository 24 requirementsFactory *testreq.FakeReqFactory 25 deps command_registry.Dependency 26 ) 27 28 updateCommandDependency := func(pluginCall bool) { 29 deps.Ui = ui 30 deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo) 31 deps.Config = configRepo 32 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("space-quotas").SetDependency(deps, pluginCall)) 33 } 34 35 BeforeEach(func() { 36 ui = &testterm.FakeUI{} 37 quotaRepo = &fakes.FakeSpaceQuotaRepository{} 38 requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true} 39 configRepo = testconfig.NewRepositoryWithDefaults() 40 }) 41 42 runCommand := func(args ...string) bool { 43 return testcmd.RunCliCommand("space-quotas", args, requirementsFactory, updateCommandDependency, false) 44 } 45 46 Describe("requirements", func() { 47 It("requires the user to be logged in", func() { 48 requirementsFactory.LoginSuccess = false 49 Expect(runCommand()).ToNot(HavePassedRequirements()) 50 }) 51 52 It("requires the user to target an org", func() { 53 requirementsFactory.TargetedOrgSuccess = false 54 Expect(runCommand()).ToNot(HavePassedRequirements()) 55 }) 56 It("should fail with usage when provided any arguments", func() { 57 requirementsFactory.LoginSuccess = true 58 requirementsFactory.TargetedOrgSuccess = true 59 Expect(runCommand("blahblah")).To(BeFalse()) 60 Expect(ui.Outputs).To(ContainSubstrings( 61 []string{"Incorrect Usage", "No argument"}, 62 )) 63 }) 64 }) 65 66 Context("when requirements have been met", func() { 67 JustBeforeEach(func() { 68 requirementsFactory.LoginSuccess = true 69 requirementsFactory.TargetedOrgSuccess = true 70 Expect(runCommand()).To(HavePassedRequirements()) 71 }) 72 73 Context("when quotas exist", func() { 74 BeforeEach(func() { 75 quotaRepo.FindByOrgReturns([]models.SpaceQuota{ 76 models.SpaceQuota{ 77 Name: "quota-name", 78 MemoryLimit: 1024, 79 InstanceMemoryLimit: 512, 80 RoutesLimit: 111, 81 ServicesLimit: 222, 82 NonBasicServicesAllowed: true, 83 OrgGuid: "my-org-guid", 84 }, 85 models.SpaceQuota{ 86 Name: "quota-non-basic-not-allowed", 87 MemoryLimit: 434, 88 InstanceMemoryLimit: -1, 89 RoutesLimit: 1, 90 ServicesLimit: 2, 91 NonBasicServicesAllowed: false, 92 OrgGuid: "my-org-guid", 93 }, 94 }, nil) 95 }) 96 97 It("lists quotas", func() { 98 Expect(quotaRepo.FindByOrgArgsForCall(0)).To(Equal("my-org-guid")) 99 Expect(ui.Outputs).To(ContainSubstrings( 100 []string{"Getting space quotas as", "my-user"}, 101 []string{"OK"}, 102 []string{"name", "total memory limit", "instance memory limit", "routes", "service instances", "paid service plans"}, 103 []string{"quota-name", "1G", "512M", "111", "222", "allowed"}, 104 []string{"quota-non-basic-not-allowed", "434M", "unlimited", "1", "2", "disallowed"}, 105 )) 106 }) 107 Context("when services are unlimited", func() { 108 BeforeEach(func() { 109 quotaRepo.FindByOrgReturns([]models.SpaceQuota{ 110 models.SpaceQuota{ 111 Name: "quota-non-basic-not-allowed", 112 MemoryLimit: 434, 113 InstanceMemoryLimit: 57, 114 RoutesLimit: 1, 115 ServicesLimit: -1, 116 NonBasicServicesAllowed: false, 117 OrgGuid: "my-org-guid", 118 }, 119 }, nil) 120 }) 121 It("replaces -1 with unlimited", func() { 122 Expect(quotaRepo.FindByOrgArgsForCall(0)).To(Equal("my-org-guid")) 123 Expect(ui.Outputs).To(ContainSubstrings( 124 125 []string{"quota-non-basic-not-allowed", "434M", "57M ", "1", "unlimited", "disallowed"}, 126 )) 127 }) 128 129 }) 130 }) 131 132 Context("when an error occurs fetching quotas", func() { 133 BeforeEach(func() { 134 quotaRepo.FindByOrgReturns([]models.SpaceQuota{}, errors.New("I haz a borken!")) 135 }) 136 137 It("prints an error", func() { 138 Expect(ui.Outputs).To(ContainSubstrings( 139 []string{"Getting space quotas as", "my-user"}, 140 []string{"FAILED"}, 141 )) 142 }) 143 }) 144 }) 145 146 })