github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/quota/create_quota_test.go (about)

     1  package quota_test
     2  
     3  import (
     4  	. "github.com/cloudfoundry/cli/cf/commands/quota"
     5  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"github.com/cloudfoundry/cli/cf/api/quotas/fakes"
    10  	"github.com/cloudfoundry/cli/cf/errors"
    11  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    12  	"github.com/cloudfoundry/cli/testhelpers/configuration"
    13  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  )
    16  
    17  var _ = Describe("create-quota command", func() {
    18  	var (
    19  		ui                  *testterm.FakeUI
    20  		quotaRepo           *fakes.FakeQuotaRepository
    21  		requirementsFactory *testreq.FakeReqFactory
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		ui = &testterm.FakeUI{}
    26  		quotaRepo = &fakes.FakeQuotaRepository{}
    27  		requirementsFactory = &testreq.FakeReqFactory{}
    28  	})
    29  
    30  	runCommand := func(args ...string) bool {
    31  		cmd := NewCreateQuota(ui, configuration.NewRepositoryWithDefaults(), quotaRepo)
    32  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    33  	}
    34  
    35  	Context("when the user is not logged in", func() {
    36  		BeforeEach(func() {
    37  			requirementsFactory.LoginSuccess = false
    38  		})
    39  
    40  		It("fails requirements", func() {
    41  			Expect(runCommand("my-quota", "-m", "50G")).To(BeFalse())
    42  		})
    43  	})
    44  
    45  	Context("when the user is logged in", func() {
    46  		BeforeEach(func() {
    47  			requirementsFactory.LoginSuccess = true
    48  		})
    49  
    50  		It("fails requirements when called without a quota name", func() {
    51  			runCommand()
    52  			Expect(ui.FailedWithUsage).To(BeTrue())
    53  		})
    54  
    55  		It("creates a quota with a given name", func() {
    56  			runCommand("my-quota")
    57  			Expect(quotaRepo.CreateArgsForCall(0).Name).To(Equal("my-quota"))
    58  			Expect(ui.Outputs).To(ContainSubstrings(
    59  				[]string{"Creating quota", "my-quota", "my-user", "..."},
    60  				[]string{"OK"},
    61  			))
    62  		})
    63  
    64  		Context("when the -i flag is not provided", func() {
    65  			It("defaults the memory limit to unlimited", func() {
    66  				runCommand("my-quota")
    67  
    68  				Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
    69  			})
    70  		})
    71  
    72  		Context("when the -m flag is provided", func() {
    73  			It("sets the memory limit", func() {
    74  				runCommand("-m", "50G", "erryday makin fitty jeez")
    75  				Expect(quotaRepo.CreateArgsForCall(0).MemoryLimit).To(Equal(int64(51200)))
    76  			})
    77  
    78  			It("alerts the user when parsing the memory limit fails", func() {
    79  				runCommand("whoops", "12")
    80  
    81  				Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
    82  			})
    83  		})
    84  
    85  		Context("when the -i flag is provided", func() {
    86  			It("sets the memory limit", func() {
    87  				runCommand("-i", "50G", "erryday makin fitty jeez")
    88  				Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(51200)))
    89  			})
    90  
    91  			It("alerts the user when parsing the memory limit fails", func() {
    92  				runCommand("-i", "whoops", "wit mah hussle", "12")
    93  
    94  				Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
    95  			})
    96  
    97  			Context("and the provided value is -1", func() {
    98  				It("sets the memory limit", func() {
    99  					runCommand("-i", "-1", "yo")
   100  					Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
   101  				})
   102  			})
   103  		})
   104  		It("sets the route limit", func() {
   105  			runCommand("-r", "12", "ecstatic")
   106  
   107  			Expect(quotaRepo.CreateArgsForCall(0).RoutesLimit).To(Equal(12))
   108  		})
   109  
   110  		It("sets the service instance limit", func() {
   111  			runCommand("-s", "42", "black star")
   112  			Expect(quotaRepo.CreateArgsForCall(0).ServicesLimit).To(Equal(42))
   113  		})
   114  
   115  		It("defaults to not allowing paid service plans", func() {
   116  			runCommand("my-pro-bono-quota")
   117  			Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeFalse())
   118  		})
   119  
   120  		Context("when requesting to allow paid service plans", func() {
   121  			It("creates the quota with paid service plans allowed", func() {
   122  				runCommand("--allow-paid-service-plans", "my-for-profit-quota")
   123  				Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeTrue())
   124  			})
   125  		})
   126  
   127  		Context("when creating a quota returns an error", func() {
   128  			It("alerts the user when creating the quota fails", func() {
   129  				quotaRepo.CreateReturns(errors.New("WHOOP THERE IT IS"))
   130  				runCommand("my-quota")
   131  
   132  				Expect(ui.Outputs).To(ContainSubstrings(
   133  					[]string{"Creating quota", "my-quota"},
   134  					[]string{"FAILED"},
   135  				))
   136  			})
   137  
   138  			It("warns the user when quota already exists", func() {
   139  				quotaRepo.CreateReturns(errors.NewHttpError(400, "240002", "Quota Definition is taken: quota-sct"))
   140  				runCommand("Banana")
   141  
   142  				Expect(ui.Outputs).ToNot(ContainSubstrings(
   143  					[]string{"FAILED"},
   144  				))
   145  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"already exists"}))
   146  			})
   147  
   148  		})
   149  	})
   150  })