github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/spacequota/space_quotas_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/flags"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  
    16  	"code.cloudfoundry.org/cli/cf/commands/spacequota"
    17  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("quotas command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		quotaRepo           *spacequotasfakes.FakeSpaceQuotaRepository
    26  		configRepo          coreconfig.Repository
    27  		requirementsFactory *requirementsfakes.FakeFactory
    28  		deps                commandregistry.Dependency
    29  	)
    30  
    31  	updateCommandDependency := func(pluginCall bool) {
    32  		deps.UI = ui
    33  		deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo)
    34  		deps.Config = configRepo
    35  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("space-quotas").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository)
    41  		requirementsFactory = new(requirementsfakes.FakeFactory)
    42  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    43  		configRepo = testconfig.NewRepositoryWithDefaults()
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		return testcmd.RunCLICommand("space-quotas", args, requirementsFactory, updateCommandDependency, false, ui)
    48  	}
    49  
    50  	Describe("requirements", func() {
    51  		It("requires the user to be logged in", func() {
    52  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    53  			Expect(runCommand()).ToNot(HavePassedRequirements())
    54  		})
    55  
    56  		It("requires the user to target an org", func() {
    57  			orgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    58  			orgReq.ExecuteReturns(errors.New("not targeting org"))
    59  			requirementsFactory.NewTargetedOrgRequirementReturns(orgReq)
    60  			Expect(runCommand()).ToNot(HavePassedRequirements())
    61  		})
    62  
    63  		Context("when arguments are provided", func() {
    64  			var cmd commandregistry.Command
    65  			var flagContext flags.FlagContext
    66  
    67  			BeforeEach(func() {
    68  				cmd = &spacequota.ListSpaceQuotas{}
    69  				cmd.SetDependency(deps, false)
    70  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    71  			})
    72  
    73  			It("should fail with usage", func() {
    74  				flagContext.Parse("blahblah")
    75  
    76  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    77  				Expect(err).NotTo(HaveOccurred())
    78  
    79  				err = testcmd.RunRequirements(reqs)
    80  				Expect(err).To(HaveOccurred())
    81  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    82  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    83  			})
    84  		})
    85  	})
    86  
    87  	Context("when requirements have been met", func() {
    88  		JustBeforeEach(func() {
    89  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    90  			runCommand()
    91  		})
    92  
    93  		Context("when quotas exist", func() {
    94  			BeforeEach(func() {
    95  				quotaRepo.FindByOrgReturns([]models.SpaceQuota{
    96  					{
    97  						Name:                    "quota-name",
    98  						MemoryLimit:             1024,
    99  						InstanceMemoryLimit:     512,
   100  						RoutesLimit:             111,
   101  						ServicesLimit:           222,
   102  						NonBasicServicesAllowed: true,
   103  						OrgGUID:                 "my-org-guid",
   104  						AppInstanceLimit:        7,
   105  						ReservedRoutePortsLimit: "6",
   106  					},
   107  					{
   108  						Name:                    "quota-non-basic-not-allowed",
   109  						MemoryLimit:             434,
   110  						InstanceMemoryLimit:     -1,
   111  						RoutesLimit:             1,
   112  						ServicesLimit:           2,
   113  						NonBasicServicesAllowed: false,
   114  						OrgGUID:                 "my-org-guid",
   115  						AppInstanceLimit:        1,
   116  						ReservedRoutePortsLimit: "3",
   117  					},
   118  					{
   119  						Name:                    "quota-app-instances",
   120  						MemoryLimit:             434,
   121  						InstanceMemoryLimit:     512,
   122  						RoutesLimit:             1,
   123  						ServicesLimit:           2,
   124  						NonBasicServicesAllowed: false,
   125  						OrgGUID:                 "my-org-guid",
   126  						AppInstanceLimit:        -1,
   127  						ReservedRoutePortsLimit: "0",
   128  					},
   129  				}, nil)
   130  			})
   131  
   132  			It("lists quotas", func() {
   133  				Expect(quotaRepo.FindByOrgArgsForCall(0)).To(Equal("my-org-guid"))
   134  				Expect(ui.Outputs()).To(ContainSubstrings(
   135  					[]string{"Getting space quotas as", "my-user"},
   136  					[]string{"OK"},
   137  					[]string{"name", "total memory", "instance memory", "routes", "service instances", "paid plans", "app instances"},
   138  					[]string{"quota-name", "1G", "512M", "111", "222", "allowed", "7", "6"},
   139  					[]string{"quota-non-basic-not-allowed", "434M", "unlimited", "1", "2", "disallowed", "1", "3"},
   140  					[]string{"quota-app-instances", "434M", "512M", "1", "2", "disallowed", "unlimited", "0"},
   141  				))
   142  			})
   143  
   144  			Context("when services are unlimited", func() {
   145  				BeforeEach(func() {
   146  					quotaRepo.FindByOrgReturns([]models.SpaceQuota{
   147  						{
   148  							Name:                    "quota-non-basic-not-allowed",
   149  							MemoryLimit:             434,
   150  							InstanceMemoryLimit:     57,
   151  							RoutesLimit:             1,
   152  							ServicesLimit:           -1,
   153  							NonBasicServicesAllowed: false,
   154  							OrgGUID:                 "my-org-guid",
   155  						},
   156  					}, nil)
   157  				})
   158  
   159  				It("replaces -1 with unlimited", func() {
   160  					Expect(quotaRepo.FindByOrgArgsForCall(0)).To(Equal("my-org-guid"))
   161  					Expect(ui.Outputs()).To(ContainSubstrings(
   162  
   163  						[]string{"quota-non-basic-not-allowed", "434M", "57M ", "1", "unlimited", "disallowed"},
   164  					))
   165  				})
   166  
   167  			})
   168  
   169  			Context("when reserved route ports are unlimited", func() {
   170  				BeforeEach(func() {
   171  					quotaRepo.FindByOrgReturns([]models.SpaceQuota{
   172  						{
   173  							Name:                    "quota-non-basic-not-allowed",
   174  							MemoryLimit:             434,
   175  							InstanceMemoryLimit:     57,
   176  							RoutesLimit:             1,
   177  							ServicesLimit:           6,
   178  							NonBasicServicesAllowed: false,
   179  							OrgGUID:                 "my-org-guid",
   180  							ReservedRoutePortsLimit: "-1",
   181  						},
   182  					}, nil)
   183  				})
   184  
   185  				It("replaces -1 with unlimited", func() {
   186  					Expect(quotaRepo.FindByOrgArgsForCall(0)).To(Equal("my-org-guid"))
   187  					Expect(ui.Outputs()).To(ContainSubstrings(
   188  
   189  						[]string{"quota-non-basic-not-allowed", "434M", "57M ", "1", "6", "disallowed", "unlimited"},
   190  					))
   191  				})
   192  			})
   193  
   194  			Context("when app instances are not provided", func() {
   195  				BeforeEach(func() {
   196  					quotaRepo.FindByOrgReturns([]models.SpaceQuota{
   197  						{
   198  							Name:                    "quota-non-basic-not-allowed",
   199  							MemoryLimit:             434,
   200  							InstanceMemoryLimit:     57,
   201  							RoutesLimit:             1,
   202  							ServicesLimit:           512,
   203  							NonBasicServicesAllowed: false,
   204  							OrgGUID:                 "my-org-guid",
   205  							AppInstanceLimit:        -1,
   206  						},
   207  					}, nil)
   208  				})
   209  
   210  				It("should not contain app instance limit column", func() {
   211  					Expect(quotaRepo.FindByOrgArgsForCall(0)).To(Equal("my-org-guid"))
   212  					Expect(ui.Outputs()).To(ContainSubstrings(
   213  						[]string{"app instances"},
   214  						[]string{"unlimited"},
   215  					))
   216  				})
   217  			})
   218  		})
   219  
   220  		Context("when an error occurs fetching quotas", func() {
   221  			BeforeEach(func() {
   222  				quotaRepo.FindByOrgReturns([]models.SpaceQuota{}, errors.New("I haz a borken!"))
   223  			})
   224  
   225  			It("prints an error", func() {
   226  				Expect(ui.Outputs()).To(ContainSubstrings(
   227  					[]string{"Getting space quotas as", "my-user"},
   228  					[]string{"FAILED"},
   229  				))
   230  			})
   231  		})
   232  	})
   233  
   234  })