github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/spacequota/update_space_quota_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  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    12  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("update-space-quota command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		quotaRepo           *fakes.FakeSpaceQuotaRepository
    22  		requirementsFactory *testreq.FakeReqFactory
    23  
    24  		quota            models.SpaceQuota
    25  		quotaPaidService models.SpaceQuota
    26  		configRepo       core_config.Repository
    27  		deps             command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.Config = configRepo
    33  		deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo)
    34  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("update-space-quota").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	runCommand := func(args ...string) bool {
    38  		return testcmd.RunCliCommand("update-space-quota", args, requirementsFactory, updateCommandDependency, false)
    39  	}
    40  
    41  	BeforeEach(func() {
    42  		ui = &testterm.FakeUI{}
    43  		configRepo = testconfig.NewRepositoryWithDefaults()
    44  		quotaRepo = &fakes.FakeSpaceQuotaRepository{}
    45  		requirementsFactory = &testreq.FakeReqFactory{}
    46  	})
    47  
    48  	Describe("requirements", func() {
    49  		It("fails when the user is not logged in", func() {
    50  			requirementsFactory.LoginSuccess = false
    51  			requirementsFactory.TargetedOrgSuccess = true
    52  			Expect(runCommand("my-quota", "-m", "50G")).NotTo(HavePassedRequirements())
    53  		})
    54  
    55  		It("fails when the user does not have an org targeted", func() {
    56  			requirementsFactory.TargetedOrgSuccess = false
    57  			requirementsFactory.LoginSuccess = true
    58  			Expect(runCommand()).NotTo(HavePassedRequirements())
    59  			Expect(runCommand("my-quota", "-m", "50G")).NotTo(HavePassedRequirements())
    60  		})
    61  
    62  		It("fails with usage if space quota name is not provided", func() {
    63  			requirementsFactory.TargetedOrgSuccess = true
    64  			requirementsFactory.LoginSuccess = true
    65  			runCommand()
    66  
    67  			Expect(ui.Outputs).To(ContainSubstrings(
    68  				[]string{"Incorrect Usage", "Requires an argument"},
    69  			))
    70  		})
    71  	})
    72  
    73  	Context("when the user is logged in", func() {
    74  		BeforeEach(func() {
    75  			quota = models.SpaceQuota{
    76  				Guid:                    "my-quota-guid",
    77  				Name:                    "my-quota",
    78  				MemoryLimit:             1024,
    79  				InstanceMemoryLimit:     512,
    80  				RoutesLimit:             111,
    81  				ServicesLimit:           222,
    82  				NonBasicServicesAllowed: false,
    83  				OrgGuid:                 "my-org-guid",
    84  			}
    85  
    86  			quotaPaidService = models.SpaceQuota{NonBasicServicesAllowed: true}
    87  
    88  			requirementsFactory.LoginSuccess = true
    89  			requirementsFactory.TargetedOrgSuccess = true
    90  		})
    91  
    92  		JustBeforeEach(func() {
    93  			quotaRepo.FindByNameReturns(quota, nil)
    94  		})
    95  
    96  		Context("when the -m flag is provided", func() {
    97  			It("updates the memory limit", func() {
    98  				runCommand("-m", "15G", "my-quota")
    99  				Expect(quotaRepo.UpdateArgsForCall(0).Name).To(Equal("my-quota"))
   100  				Expect(quotaRepo.UpdateArgsForCall(0).MemoryLimit).To(Equal(int64(15360)))
   101  			})
   102  
   103  			It("alerts the user when parsing the memory limit fails", func() {
   104  				runCommand("-m", "whoops", "wit mah hussle", "my-org")
   105  
   106  				Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
   107  			})
   108  		})
   109  
   110  		Context("when the -i flag is provided", func() {
   111  			It("sets the memory limit", func() {
   112  				runCommand("-i", "50G", "my-quota")
   113  				Expect(quotaRepo.UpdateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(51200)))
   114  			})
   115  
   116  			It("sets the memory limit to -1", func() {
   117  				runCommand("-i", "-1", "my-quota")
   118  				Expect(quotaRepo.UpdateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
   119  			})
   120  
   121  			It("alerts the user when parsing the memory limit fails", func() {
   122  				runCommand("-i", "whoops", "my-quota")
   123  				Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
   124  			})
   125  		})
   126  
   127  		Context("when the -r flag is provided", func() {
   128  			It("sets the route limit", func() {
   129  				runCommand("-r", "12", "ecstatic")
   130  				Expect(quotaRepo.UpdateArgsForCall(0).RoutesLimit).To(Equal(12))
   131  			})
   132  		})
   133  
   134  		Context("when the -s flag is provided", func() {
   135  			It("sets the service instance limit", func() {
   136  				runCommand("-s", "42", "my-quota")
   137  				Expect(quotaRepo.UpdateArgsForCall(0).ServicesLimit).To(Equal(42))
   138  			})
   139  		})
   140  
   141  		Context("when the -n flag is provided", func() {
   142  			It("sets the service instance name", func() {
   143  				runCommand("-n", "foo", "my-quota")
   144  				Expect(quotaRepo.UpdateArgsForCall(0).Name).To(Equal("foo"))
   145  			})
   146  		})
   147  
   148  		Context("when --allow-non-basic-services is provided", func() {
   149  			It("updates the quota to allow paid service plans", func() {
   150  				runCommand("--allow-paid-service-plans", "my-for-profit-quota")
   151  				Expect(quotaRepo.UpdateArgsForCall(0).NonBasicServicesAllowed).To(BeTrue())
   152  			})
   153  		})
   154  
   155  		Context("when --disallow-non-basic-services is provided", func() {
   156  			It("updates the quota to disallow paid service plans", func() {
   157  				quotaRepo.FindByNameReturns(quotaPaidService, nil)
   158  
   159  				runCommand("--disallow-paid-service-plans", "my-for-profit-quota")
   160  				Expect(quotaRepo.UpdateArgsForCall(0).NonBasicServicesAllowed).To(BeFalse())
   161  			})
   162  		})
   163  
   164  		Context("when updating a quota returns an error", func() {
   165  			It("alerts the user when creating the quota fails", func() {
   166  				quotaRepo.UpdateReturns(errors.New("WHOOP THERE IT IS"))
   167  				runCommand("my-quota")
   168  
   169  				Expect(ui.Outputs).To(ContainSubstrings(
   170  					[]string{"Updating space quota", "my-quota", "my-user"},
   171  					[]string{"FAILED"},
   172  				))
   173  			})
   174  
   175  			It("fails if the allow and disallow flag are both passed", func() {
   176  				runCommand("--disallow-paid-service-plans", "--allow-paid-service-plans", "my-for-profit-quota")
   177  				Expect(ui.Outputs).To(ContainSubstrings(
   178  					[]string{"FAILED"},
   179  				))
   180  			})
   181  		})
   182  	})
   183  })