github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/spacequota/update_space_quota_test.go (about)

     1  package spacequota_test
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    14  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    15  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    16  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/ginkgo/extensions/table"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("update-space-quota command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		quotaRepo           *spacequotasfakes.FakeSpaceQuotaRepository
    26  		requirementsFactory *requirementsfakes.FakeFactory
    27  
    28  		quota            models.SpaceQuota
    29  		quotaPaidService models.SpaceQuota
    30  		configRepo       coreconfig.Repository
    31  		deps             commandregistry.Dependency
    32  	)
    33  
    34  	updateCommandDependency := func(pluginCall bool) {
    35  		deps.UI = ui
    36  		deps.Config = configRepo
    37  		deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo)
    38  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("update-space-quota").SetDependency(deps, pluginCall))
    39  	}
    40  
    41  	runCommand := func(args ...string) bool {
    42  		return testcmd.RunCLICommand("update-space-quota", args, requirementsFactory, updateCommandDependency, false, ui)
    43  	}
    44  
    45  	BeforeEach(func() {
    46  		ui = &testterm.FakeUI{}
    47  		configRepo = testconfig.NewRepositoryWithDefaults()
    48  		quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository)
    49  		requirementsFactory = new(requirementsfakes.FakeFactory)
    50  	})
    51  
    52  	Describe("requirements", func() {
    53  		It("fails when the user is not logged in", func() {
    54  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    55  			Expect(runCommand("my-quota", "-m", "50G")).NotTo(HavePassedRequirements())
    56  		})
    57  
    58  		It("fails when the user does not have an org targeted", func() {
    59  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    60  			orgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    61  			orgReq.ExecuteReturns(errors.New("not targeting org"))
    62  			requirementsFactory.NewTargetedOrgRequirementReturns(orgReq)
    63  			Expect(runCommand()).NotTo(HavePassedRequirements())
    64  			Expect(runCommand("my-quota", "-m", "50G")).NotTo(HavePassedRequirements())
    65  		})
    66  
    67  		It("fails with usage if space quota name is not provided", func() {
    68  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    69  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    70  			runCommand()
    71  
    72  			Expect(ui.Outputs()).To(ContainSubstrings(
    73  				[]string{"Incorrect Usage", "Requires an argument"},
    74  			))
    75  		})
    76  
    77  	})
    78  
    79  	Context("when the user is logged in", func() {
    80  		BeforeEach(func() {
    81  			quota = models.SpaceQuota{
    82  				GUID:                    "my-quota-guid",
    83  				Name:                    "my-quota",
    84  				MemoryLimit:             1024,
    85  				InstanceMemoryLimit:     512,
    86  				RoutesLimit:             111,
    87  				ServicesLimit:           222,
    88  				AppInstanceLimit:        333,
    89  				NonBasicServicesAllowed: false,
    90  				OrgGUID:                 "my-org-guid",
    91  			}
    92  
    93  			quotaPaidService = models.SpaceQuota{NonBasicServicesAllowed: true}
    94  
    95  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    96  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    97  			requirementsFactory.NewMinAPIVersionRequirementReturns(requirements.Passing{})
    98  		})
    99  
   100  		JustBeforeEach(func() {
   101  			quotaRepo.FindByNameReturns(quota, nil)
   102  		})
   103  
   104  		Context("when the -m flag is provided", func() {
   105  			It("updates the memory limit", func() {
   106  				runCommand("-m", "15G", "my-quota")
   107  				Expect(quotaRepo.UpdateArgsForCall(0).Name).To(Equal("my-quota"))
   108  				Expect(quotaRepo.UpdateArgsForCall(0).MemoryLimit).To(Equal(int64(15360)))
   109  			})
   110  
   111  			It("alerts the user when parsing the memory limit fails", func() {
   112  				runCommand("-m", "whoops", "wit mah hussle", "my-org")
   113  
   114  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"}))
   115  			})
   116  		})
   117  
   118  		Context("when the -i flag is provided", func() {
   119  			It("sets the memory limit", func() {
   120  				runCommand("-i", "50G", "my-quota")
   121  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   122  				Expect(quotaRepo.UpdateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(51200)))
   123  			})
   124  
   125  			It("sets the memory limit to -1", func() {
   126  				runCommand("-i", "-1", "my-quota")
   127  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   128  				Expect(quotaRepo.UpdateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
   129  			})
   130  
   131  			It("alerts the user when parsing the memory limit fails", func() {
   132  				runCommand("-i", "whoops", "my-quota")
   133  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"}))
   134  			})
   135  		})
   136  
   137  		Context("when the -a flag is provided", func() {
   138  			It("sets the instance limit", func() {
   139  				runCommand("-a", "50", "my-quota")
   140  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   141  				Expect(quotaRepo.UpdateArgsForCall(0).AppInstanceLimit).To(Equal(50))
   142  			})
   143  
   144  			It("does not override the value if it's not provided", func() {
   145  				runCommand("-s", "5", "my-quota")
   146  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   147  				Expect(quotaRepo.UpdateArgsForCall(0).AppInstanceLimit).To(Equal(333))
   148  			})
   149  		})
   150  
   151  		Context("when the -r flag is provided", func() {
   152  			It("sets the route limit", func() {
   153  				runCommand("-r", "12", "ecstatic")
   154  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   155  				Expect(quotaRepo.UpdateArgsForCall(0).RoutesLimit).To(Equal(12))
   156  			})
   157  		})
   158  
   159  		Context("when the -s flag is provided", func() {
   160  			It("sets the service instance limit", func() {
   161  				runCommand("-s", "42", "my-quota")
   162  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   163  				Expect(quotaRepo.UpdateArgsForCall(0).ServicesLimit).To(Equal(42))
   164  			})
   165  		})
   166  
   167  		Context("when the -n flag is provided", func() {
   168  			It("sets the service instance name", func() {
   169  				runCommand("-n", "foo", "my-quota")
   170  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   171  				Expect(quotaRepo.UpdateArgsForCall(0).Name).To(Equal("foo"))
   172  			})
   173  		})
   174  
   175  		Context("when --allow-non-basic-services is provided", func() {
   176  			It("updates the quota to allow paid service plans", func() {
   177  				runCommand("--allow-paid-service-plans", "my-for-profit-quota")
   178  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   179  				Expect(quotaRepo.UpdateArgsForCall(0).NonBasicServicesAllowed).To(BeTrue())
   180  			})
   181  		})
   182  
   183  		Context("when --disallow-non-basic-services is provided", func() {
   184  			It("updates the quota to disallow paid service plans", func() {
   185  				quotaRepo.FindByNameReturns(quotaPaidService, nil)
   186  
   187  				runCommand("--disallow-paid-service-plans", "my-for-profit-quota")
   188  				Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   189  				Expect(quotaRepo.UpdateArgsForCall(0).NonBasicServicesAllowed).To(BeFalse())
   190  			})
   191  		})
   192  
   193  		Context("when --reserved-route-ports is provided", func() {
   194  			DescribeTable("updates the quota to the given number of reserved route ports",
   195  				func(numberOfReservedRoutes string) {
   196  					quotaRepo.FindByNameReturns(quotaPaidService, nil)
   197  
   198  					runCommand("--reserved-route-ports", numberOfReservedRoutes, "my-for-profit-quota")
   199  					Expect(quotaRepo.UpdateCallCount()).To(Equal(1))
   200  					Expect(quotaRepo.UpdateArgsForCall(0).ReservedRoutePortsLimit).To(Equal(json.Number(numberOfReservedRoutes)))
   201  				},
   202  				Entry("for positive values", "42"),
   203  				Entry("for 0", "0"),
   204  				Entry("for -1", "-1"),
   205  			)
   206  		})
   207  
   208  		Context("when updating a quota returns an error", func() {
   209  			It("alerts the user when creating the quota fails", func() {
   210  				quotaRepo.UpdateReturns(errors.New("WHOOP THERE IT IS"))
   211  				runCommand("my-quota")
   212  
   213  				Expect(ui.Outputs()).To(ContainSubstrings(
   214  					[]string{"Updating space quota", "my-quota", "my-user"},
   215  					[]string{"FAILED"},
   216  				))
   217  			})
   218  
   219  			It("fails if the allow and disallow flag are both passed", func() {
   220  				runCommand("--disallow-paid-service-plans", "--allow-paid-service-plans", "my-for-profit-quota")
   221  				Expect(ui.Outputs()).To(ContainSubstrings(
   222  					[]string{"FAILED"},
   223  				))
   224  			})
   225  		})
   226  	})
   227  })