github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/cf/commands/spacequota/create_space_quota_test.go (about)

     1  package spacequota_test
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/errors"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  
    11  	"code.cloudfoundry.org/cli/cf/api/resources"
    12  	"code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes"
    13  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig/coreconfigfakes"
    14  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    15  	testterm "code.cloudfoundry.org/cli/testhelpers/terminal"
    16  
    17  	"code.cloudfoundry.org/cli/cf/api"
    18  	"code.cloudfoundry.org/cli/cf/commands/spacequota"
    19  	"code.cloudfoundry.org/cli/cf/flags"
    20  	. "code.cloudfoundry.org/cli/testhelpers/matchers"
    21  	"github.com/blang/semver"
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  var _ = Describe("create-space-quota", func() {
    27  	var (
    28  		ui                  *testterm.FakeUI
    29  		quotaRepo           *spacequotasfakes.FakeSpaceQuotaRepository
    30  		requirementsFactory *requirementsfakes.FakeFactory
    31  		config              *coreconfigfakes.FakeRepository
    32  
    33  		loginReq         *requirementsfakes.FakeRequirement
    34  		targetedOrgReq   *requirementsfakes.FakeTargetedOrgRequirement
    35  		minApiVersionReq *requirementsfakes.FakeRequirement
    36  		reqFactory       *requirementsfakes.FakeFactory
    37  
    38  		deps        commandregistry.Dependency
    39  		cmd         spacequota.CreateSpaceQuota
    40  		flagContext flags.FlagContext
    41  	)
    42  
    43  	BeforeEach(func() {
    44  		ui = &testterm.FakeUI{}
    45  		quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository)
    46  		requirementsFactory = new(requirementsfakes.FakeFactory)
    47  		config = new(coreconfigfakes.FakeRepository)
    48  
    49  		repoLocator := api.RepositoryLocator{}
    50  		repoLocator = repoLocator.SetSpaceQuotaRepository(quotaRepo)
    51  
    52  		deps = commandregistry.Dependency{
    53  			UI:          ui,
    54  			Config:      config,
    55  			RepoLocator: repoLocator,
    56  		}
    57  
    58  		reqFactory = new(requirementsfakes.FakeFactory)
    59  
    60  		loginReq = new(requirementsfakes.FakeRequirement)
    61  		loginReq.ExecuteReturns(nil)
    62  		reqFactory.NewLoginRequirementReturns(loginReq)
    63  
    64  		targetedOrgReq = new(requirementsfakes.FakeTargetedOrgRequirement)
    65  		targetedOrgReq.ExecuteReturns(nil)
    66  		reqFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    67  
    68  		minApiVersionReq = new(requirementsfakes.FakeRequirement)
    69  		minApiVersionReq.ExecuteReturns(nil)
    70  		reqFactory.NewMinAPIVersionRequirementReturns(minApiVersionReq)
    71  
    72  		cmd = spacequota.CreateSpaceQuota{}
    73  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    74  	})
    75  
    76  	Describe("Requirements", func() {
    77  		BeforeEach(func() {
    78  			cmd.SetDependency(deps, false)
    79  		})
    80  		Context("when not exactly one arg is provided", func() {
    81  			It("fails", func() {
    82  				flagContext.Parse()
    83  				_, err := cmd.Requirements(reqFactory, flagContext)
    84  				Expect(err).To(HaveOccurred())
    85  				Expect(ui.Outputs()).To(ContainSubstrings(
    86  					[]string{"FAILED"},
    87  					[]string{"Incorrect Usage. Requires an argument"},
    88  				))
    89  			})
    90  		})
    91  
    92  		Context("when provided exactly one arg", func() {
    93  			var actualRequirements []requirements.Requirement
    94  			var err error
    95  
    96  			Context("when no flags are provided", func() {
    97  				BeforeEach(func() {
    98  					flagContext.Parse("myquota")
    99  					actualRequirements, err = cmd.Requirements(reqFactory, flagContext)
   100  					Expect(err).NotTo(HaveOccurred())
   101  				})
   102  
   103  				It("returns a login requirement", func() {
   104  					Expect(reqFactory.NewLoginRequirementCallCount()).To(Equal(1))
   105  					Expect(actualRequirements).To(ContainElement(loginReq))
   106  				})
   107  
   108  				It("returns a targeted org requirement", func() {
   109  					Expect(reqFactory.NewTargetedOrgRequirementCallCount()).To(Equal(1))
   110  					Expect(actualRequirements).To(ContainElement(targetedOrgReq))
   111  				})
   112  
   113  				It("does not return a min api requirement", func() {
   114  					Expect(reqFactory.NewMinAPIVersionRequirementCallCount()).To(Equal(0))
   115  				})
   116  			})
   117  
   118  			Context("when the -a flag is provided", func() {
   119  				BeforeEach(func() {
   120  					flagContext.Parse("myquota", "-a", "2")
   121  					actualRequirements, err = cmd.Requirements(reqFactory, flagContext)
   122  					Expect(err).NotTo(HaveOccurred())
   123  				})
   124  
   125  				It("returns a min api version requirement", func() {
   126  					Expect(reqFactory.NewMinAPIVersionRequirementCallCount()).To(Equal(1))
   127  					commandName, requiredVersion := reqFactory.NewMinAPIVersionRequirementArgsForCall(0)
   128  					Expect(commandName).To(Equal("Option '-a'"))
   129  					expectVersion, _ := semver.Make("2.40.0")
   130  					Expect(requiredVersion).To(Equal(expectVersion))
   131  					Expect(actualRequirements).To(ContainElement(minApiVersionReq))
   132  				})
   133  			})
   134  
   135  			Context("when the --reserved-route-ports is provided", func() {
   136  				BeforeEach(func() {
   137  					flagContext.Parse("myquota", "--reserved-route-ports", "2")
   138  					actualRequirements, err = cmd.Requirements(reqFactory, flagContext)
   139  					Expect(err).NotTo(HaveOccurred())
   140  				})
   141  
   142  				It("return a minimum api version requirement", func() {
   143  					Expect(reqFactory.NewMinAPIVersionRequirementCallCount()).To(Equal(1))
   144  					commandName, requiredVersion := reqFactory.NewMinAPIVersionRequirementArgsForCall(0)
   145  					Expect(commandName).To(Equal("Option '--reserved-route-ports'"))
   146  					expectVersion, _ := semver.Make("2.55.0")
   147  					Expect(requiredVersion).To(Equal(expectVersion))
   148  					Expect(actualRequirements).To(ContainElement(minApiVersionReq))
   149  				})
   150  			})
   151  		})
   152  	})
   153  
   154  	Describe("Execute", func() {
   155  		var runCLIErr error
   156  
   157  		BeforeEach(func() {
   158  			orgFields := models.OrganizationFields{
   159  				Name: "my-org",
   160  				GUID: "my-org-guid",
   161  			}
   162  
   163  			config.OrganizationFieldsReturns(orgFields)
   164  			config.UsernameReturns("my-user")
   165  		})
   166  
   167  		JustBeforeEach(func() {
   168  			cmd.SetDependency(deps, false)
   169  			runCLIErr = cmd.Execute(flagContext)
   170  		})
   171  
   172  		Context("when creating a quota succeeds", func() {
   173  			Context("without any flags", func() {
   174  				BeforeEach(func() {
   175  					flagContext.Parse("my-quota")
   176  				})
   177  
   178  				It("creates a quota with a given name", func() {
   179  					Expect(quotaRepo.CreateArgsForCall(0).Name).To(Equal("my-quota"))
   180  					Expect(quotaRepo.CreateArgsForCall(0).OrgGUID).To(Equal("my-org-guid"))
   181  					Expect(ui.Outputs()).To(ContainSubstrings(
   182  						[]string{"Creating space quota", "my-quota", "my-org", "my-user", "..."},
   183  						[]string{"OK"},
   184  					))
   185  				})
   186  
   187  				It("sets the instance memory limit to unlimiited", func() {
   188  					Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
   189  				})
   190  
   191  				It("sets the instance limit to unlimited", func() {
   192  					Expect(quotaRepo.CreateArgsForCall(0).AppInstanceLimit).To(Equal(resources.UnlimitedAppInstances))
   193  				})
   194  
   195  				It("defaults to not allowing paid service plans", func() {
   196  					Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeFalse())
   197  				})
   198  			})
   199  
   200  			Context("when the -m flag is provided with valid value", func() {
   201  				BeforeEach(func() {
   202  					flagContext.Parse("-m", "50G", "erryday makin fitty jeez")
   203  				})
   204  
   205  				It("sets the memory limit", func() {
   206  					Expect(quotaRepo.CreateArgsForCall(0).MemoryLimit).To(Equal(int64(51200)))
   207  				})
   208  			})
   209  
   210  			Context("when the -i flag is provided with positive value", func() {
   211  				BeforeEach(func() {
   212  					flagContext.Parse("-i", "50G", "erryday makin fitty jeez")
   213  				})
   214  
   215  				It("sets the memory limit", func() {
   216  					Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(51200)))
   217  				})
   218  			})
   219  
   220  			Context("when the -i flag is provided with -1", func() {
   221  				BeforeEach(func() {
   222  					flagContext.Parse("-i", "-1", "wit mah hussle")
   223  				})
   224  
   225  				It("accepts it as an appropriate value", func() {
   226  					Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1)))
   227  				})
   228  			})
   229  
   230  			Context("when the -a flag is provided", func() {
   231  				BeforeEach(func() {
   232  					flagContext.Parse("-a", "50", "my special quota")
   233  				})
   234  
   235  				It("sets the instance limit", func() {
   236  					Expect(quotaRepo.CreateArgsForCall(0).AppInstanceLimit).To(Equal(50))
   237  				})
   238  			})
   239  
   240  			Context("when the -r flag is provided", func() {
   241  				BeforeEach(func() {
   242  					flagContext.Parse("-r", "12", "ecstatic")
   243  				})
   244  
   245  				It("sets the route limit", func() {
   246  					Expect(quotaRepo.CreateArgsForCall(0).RoutesLimit).To(Equal(12))
   247  				})
   248  			})
   249  
   250  			Context("when the -s flag is provided", func() {
   251  				BeforeEach(func() {
   252  					flagContext.Parse("-s", "42", "black star")
   253  				})
   254  
   255  				It("sets the service instance limit", func() {
   256  					Expect(quotaRepo.CreateArgsForCall(0).ServicesLimit).To(Equal(42))
   257  				})
   258  			})
   259  
   260  			Context("when the --reserved-route-ports flag is provided", func() {
   261  				BeforeEach(func() {
   262  					flagContext.Parse("--reserved-route-ports", "5", "square quota")
   263  				})
   264  
   265  				It("sets the quotas TCP route limit", func() {
   266  					Expect(quotaRepo.CreateArgsForCall(0).ReservedRoutePortsLimit).To(Equal(json.Number("5")))
   267  				})
   268  			})
   269  
   270  			Context("when requesting to allow paid service plans", func() {
   271  				BeforeEach(func() {
   272  					flagContext.Parse("--allow-paid-service-plans", "my-for-profit-quota")
   273  				})
   274  
   275  				It("creates the quota with paid service plans allowed", func() {
   276  					Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeTrue())
   277  				})
   278  			})
   279  		})
   280  
   281  		Context("when the -i flag is provided with invalid value", func() {
   282  			BeforeEach(func() {
   283  				flagContext.Parse("-i", "whoops", "yo", "12")
   284  				cmd.SetDependency(deps, false)
   285  			})
   286  
   287  			It("alerts the user when parsing the memory limit fails", func() {
   288  				Expect(runCLIErr).To(HaveOccurred())
   289  				runCLIErrStr := runCLIErr.Error()
   290  				Expect(runCLIErrStr).To(ContainSubstring("Invalid instance memory limit: whoops"))
   291  				Expect(runCLIErrStr).To(ContainSubstring("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   292  			})
   293  		})
   294  
   295  		Context("when the -m flag is provided with invalid value", func() {
   296  			BeforeEach(func() {
   297  				flagContext.Parse("-m", "whoops", "wit mah hussle")
   298  				cmd.SetDependency(deps, false)
   299  			})
   300  
   301  			It("alerts the user when parsing the memory limit fails", func() {
   302  				Expect(runCLIErr).To(HaveOccurred())
   303  				runCLIErrStr := runCLIErr.Error()
   304  				Expect(runCLIErrStr).To(ContainSubstring("Invalid memory limit: whoops"))
   305  				Expect(runCLIErrStr).To(ContainSubstring("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   306  			})
   307  		})
   308  
   309  		Context("when the request fails", func() {
   310  			BeforeEach(func() {
   311  				flagContext.Parse("my-quota")
   312  				quotaRepo.CreateReturns(errors.New("WHOOP THERE IT IS"))
   313  				cmd.SetDependency(deps, false)
   314  			})
   315  
   316  			It("alets the user when creating the quota fails", func() {
   317  				Expect(runCLIErr).To(HaveOccurred())
   318  				Expect(ui.Outputs()).To(ContainSubstrings(
   319  					[]string{"Creating space quota", "my-quota", "my-org"},
   320  				))
   321  				Expect(runCLIErr.Error()).To(Equal("WHOOP THERE IT IS"))
   322  			})
   323  		})
   324  
   325  		Context("when the quota already exists", func() {
   326  			BeforeEach(func() {
   327  				flagContext.Parse("my-quota")
   328  				quotaRepo.CreateReturns(errors.NewHTTPError(400, errors.QuotaDefinitionNameTaken, "Quota Definition is taken: quota-sct"))
   329  			})
   330  
   331  			It("warns the user", func() {
   332  				Expect(runCLIErr).NotTo(HaveOccurred())
   333  				Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"}))
   334  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"already exists"}))
   335  			})
   336  		})
   337  	})
   338  })