github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/spacequota/create_quota_test.go (about)

     1  package spacequota_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/command_registry"
     5  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     6  	"github.com/cloudfoundry/cli/cf/models"
     7  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  
    11  	test_org "github.com/cloudfoundry/cli/cf/api/organizations/fakes"
    12  	"github.com/cloudfoundry/cli/cf/api/space_quotas/fakes"
    13  	"github.com/cloudfoundry/cli/cf/errors"
    14  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    15  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    16  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    17  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    18  )
    19  
    20  var _ = Describe("create-quota command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		quotaRepo           *fakes.FakeSpaceQuotaRepository
    24  		orgRepo             *test_org.FakeOrganizationRepository
    25  		requirementsFactory *testreq.FakeReqFactory
    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  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    35  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("create-space-quota").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		configRepo = testconfig.NewRepositoryWithDefaults()
    41  		quotaRepo = &fakes.FakeSpaceQuotaRepository{}
    42  		orgRepo = &test_org.FakeOrganizationRepository{}
    43  		requirementsFactory = &testreq.FakeReqFactory{}
    44  
    45  		org := models.Organization{}
    46  		org.Name = "my-org"
    47  		org.Guid = "my-org-guid"
    48  		orgRepo.ListOrgsReturns([]models.Organization{org}, nil)
    49  		orgRepo.FindByNameReturns(org, nil)
    50  	})
    51  
    52  	runCommand := func(args ...string) bool {
    53  		return testcmd.RunCliCommand("create-space-quota", args, requirementsFactory, updateCommandDependency, false)
    54  	}
    55  
    56  	Context("requirements", func() {
    57  		It("requires the user to be logged in", func() {
    58  			requirementsFactory.LoginSuccess = false
    59  
    60  			Expect(runCommand("my-quota", "-m", "50G")).To(BeFalse())
    61  		})
    62  
    63  		It("requires the user to target an org", func() {
    64  			requirementsFactory.TargetedOrgSuccess = false
    65  
    66  			Expect(runCommand("my-quota", "-m", "50G")).To(BeFalse())
    67  		})
    68  	})
    69  
    70  	Context("when requirements have been met", func() {
    71  		BeforeEach(func() {
    72  			requirementsFactory.LoginSuccess = true
    73  			requirementsFactory.TargetedOrgSuccess = true
    74  		})
    75  
    76  		It("fails requirements when called without a quota name", func() {
    77  			runCommand()
    78  			Expect(ui.Outputs).To(ContainSubstrings(
    79  				[]string{"Incorrect Usage", "Requires an argument"},
    80  			))
    81  		})
    82  
    83  		It("creates a quota with a given name", func() {
    84  			runCommand("my-quota")
    85  			Expect(quotaRepo.CreateArgsForCall(0).Name).To(Equal("my-quota"))
    86  			Expect(quotaRepo.CreateArgsForCall(0).OrgGuid).To(Equal("my-org-guid"))
    87  
    88  			Expect(ui.Outputs).To(ContainSubstrings(
    89  				[]string{"Creating space quota", "my-org", "my-quota", "my-user", "..."},
    90  				[]string{"OK"},
    91  			))
    92  		})
    93  
    94  		Context("when the -i flag is not provided", func() {
    95  			It("sets the instance memory limit to unlimiited", func() {
    96  				runCommand("my-quota")
    97  
    98  				Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
    99  			})
   100  		})
   101  
   102  		Context("when the -m flag is provided", func() {
   103  			It("sets the memory limit", func() {
   104  				runCommand("-m", "50G", "erryday makin fitty jeez")
   105  				Expect(quotaRepo.CreateArgsForCall(0).MemoryLimit).To(Equal(int64(51200)))
   106  			})
   107  
   108  			It("alerts the user when parsing the memory limit fails", func() {
   109  				runCommand("-m", "whoops", "wit mah hussle")
   110  
   111  				Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
   112  			})
   113  		})
   114  
   115  		Context("when the -i flag is provided", func() {
   116  			It("sets the memory limit", func() {
   117  				runCommand("-i", "50G", "erryday makin fitty jeez")
   118  				Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(51200)))
   119  			})
   120  
   121  			It("accepts -1 without units as an appropriate value", func() {
   122  				runCommand("-i", "-1", "wit mah hussle")
   123  				Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
   124  			})
   125  
   126  			It("alerts the user when parsing the memory limit fails", func() {
   127  				runCommand("-i", "whoops", "yo", "12")
   128  
   129  				Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
   130  			})
   131  		})
   132  
   133  		It("sets the route limit", func() {
   134  			runCommand("-r", "12", "ecstatic")
   135  
   136  			Expect(quotaRepo.CreateArgsForCall(0).RoutesLimit).To(Equal(12))
   137  		})
   138  
   139  		It("sets the service instance limit", func() {
   140  			runCommand("-s", "42", "black star")
   141  			Expect(quotaRepo.CreateArgsForCall(0).ServicesLimit).To(Equal(42))
   142  		})
   143  
   144  		It("defaults to not allowing paid service plans", func() {
   145  			runCommand("my-pro-bono-quota")
   146  			Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeFalse())
   147  		})
   148  
   149  		Context("when requesting to allow paid service plans", func() {
   150  			It("creates the quota with paid service plans allowed", func() {
   151  				runCommand("--allow-paid-service-plans", "my-for-profit-quota")
   152  				Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeTrue())
   153  			})
   154  		})
   155  
   156  		Context("when creating a quota returns an error", func() {
   157  			It("alerts the user when creating the quota fails", func() {
   158  				quotaRepo.CreateReturns(errors.New("WHOOP THERE IT IS"))
   159  				runCommand("my-quota")
   160  
   161  				Expect(ui.Outputs).To(ContainSubstrings(
   162  					[]string{"Creating space quota", "my-quota", "my-org"},
   163  					[]string{"FAILED"},
   164  				))
   165  			})
   166  
   167  			It("warns the user when quota already exists", func() {
   168  				quotaRepo.CreateReturns(errors.NewHttpError(400, "240002", "Quota Definition is taken: quota-sct"))
   169  				runCommand("Banana")
   170  
   171  				Expect(ui.Outputs).ToNot(ContainSubstrings(
   172  					[]string{"FAILED"},
   173  				))
   174  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"already exists"}))
   175  			})
   176  
   177  		})
   178  	})
   179  })