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

     1  package spacequota_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/fakes"
     5  	quotafakes "github.com/cloudfoundry/cli/cf/api/space_quotas/fakes"
     6  	"github.com/cloudfoundry/cli/cf/errors"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  
    13  	"github.com/cloudfoundry/cli/cf/command_registry"
    14  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("set-space-quota command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		spaceRepo           *fakes.FakeSpaceRepository
    24  		quotaRepo           *quotafakes.FakeSpaceQuotaRepository
    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.SetSpaceRepository(spaceRepo)
    35  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("set-space-quota").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		configRepo = testconfig.NewRepositoryWithDefaults()
    41  		spaceRepo = &fakes.FakeSpaceRepository{}
    42  		quotaRepo = &quotafakes.FakeSpaceQuotaRepository{}
    43  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		return testcmd.RunCliCommand("set-space-quota", args, requirementsFactory, updateCommandDependency, false)
    48  	}
    49  
    50  	Describe("requirements", func() {
    51  		It("requires the user to be logged in", func() {
    52  			requirementsFactory.LoginSuccess = false
    53  			Expect(runCommand("space", "space-quota")).ToNot(HavePassedRequirements())
    54  		})
    55  
    56  		It("requires the user to target an org", func() {
    57  			requirementsFactory.TargetedOrgSuccess = false
    58  			Expect(runCommand("space", "space-quota")).ToNot(HavePassedRequirements())
    59  		})
    60  
    61  		It("fails with usage if the user does not provide a quota and space", func() {
    62  			requirementsFactory.TargetedOrgSuccess = true
    63  			requirementsFactory.LoginSuccess = true
    64  			runCommand()
    65  			Expect(ui.Outputs).To(ContainSubstrings(
    66  				[]string{"Incorrect Usage", "Requires", "arguments"},
    67  			))
    68  		})
    69  	})
    70  
    71  	Context("when logged in", func() {
    72  		JustBeforeEach(func() {
    73  			requirementsFactory.LoginSuccess = true
    74  			requirementsFactory.TargetedOrgSuccess = true
    75  			Expect(runCommand("my-space", "quota-name")).To(HavePassedRequirements())
    76  		})
    77  
    78  		Context("when the space and quota both exist", func() {
    79  			BeforeEach(func() {
    80  				quotaRepo.FindByNameReturns(
    81  					models.SpaceQuota{
    82  						Name:                    "quota-name",
    83  						Guid:                    "quota-guid",
    84  						MemoryLimit:             1024,
    85  						InstanceMemoryLimit:     512,
    86  						RoutesLimit:             111,
    87  						ServicesLimit:           222,
    88  						NonBasicServicesAllowed: true,
    89  						OrgGuid:                 "my-org-guid",
    90  					}, nil)
    91  
    92  				spaceRepo.Spaces = []models.Space{
    93  					models.Space{
    94  						SpaceFields: models.SpaceFields{
    95  							Name: "my-space",
    96  							Guid: "my-space-guid",
    97  						},
    98  						SpaceQuotaGuid: "",
    99  					},
   100  				}
   101  			})
   102  
   103  			Context("when the space quota was not previously assigned to a space", func() {
   104  				It("associates the provided space with the provided space quota", func() {
   105  					spaceGuid, quotaGuid := quotaRepo.AssociateSpaceWithQuotaArgsForCall(0)
   106  
   107  					Expect(spaceGuid).To(Equal("my-space-guid"))
   108  					Expect(quotaGuid).To(Equal("quota-guid"))
   109  					Expect(ui.Outputs).To(ContainSubstrings(
   110  						[]string{"Assigning space quota", "to space", "my-user"},
   111  						[]string{"OK"},
   112  					))
   113  				})
   114  			})
   115  
   116  			Context("when the space quota was previously assigned to a space", func() {
   117  				BeforeEach(func() {
   118  					spaceRepo.Spaces = []models.Space{
   119  						models.Space{
   120  							SpaceFields: models.SpaceFields{
   121  								Name: "my-space",
   122  								Guid: "my-space-guid",
   123  							},
   124  							SpaceQuotaGuid: "another-quota",
   125  						},
   126  					}
   127  				})
   128  
   129  				It("warns the user that the operation was not performed", func() {
   130  					Expect(quotaRepo.UpdateCallCount()).To(Equal(0))
   131  					Expect(ui.Outputs).To(ContainSubstrings(
   132  						[]string{"Assigning space quota", "to space", "my-user"},
   133  						[]string{"FAILED"},
   134  						[]string{"This space already has an assigned space quota."},
   135  					))
   136  				})
   137  			})
   138  		})
   139  
   140  		Context("when an error occurs fetching the space", func() {
   141  			BeforeEach(func() {
   142  				spaceRepo.FindByNameErr = true
   143  			})
   144  
   145  			It("prints an error", func() {
   146  				Expect(ui.Outputs).To(ContainSubstrings(
   147  					[]string{"Assigning space quota", "to space", "my-user"},
   148  					[]string{"FAILED"},
   149  					[]string{"Error finding space by name"},
   150  				))
   151  			})
   152  		})
   153  
   154  		Context("when an error occurs fetching the quota", func() {
   155  			BeforeEach(func() {
   156  				spaceRepo.Spaces = []models.Space{
   157  					models.Space{
   158  						SpaceFields: models.SpaceFields{
   159  							Name: "my-space",
   160  							Guid: "my-space-guid",
   161  						},
   162  						SpaceQuotaGuid: "",
   163  					},
   164  				}
   165  				spaceRepo.FindByNameErr = false
   166  				quotaRepo.FindByNameReturns(models.SpaceQuota{}, errors.New("I can't find my quota name!"))
   167  			})
   168  
   169  			It("prints an error", func() {
   170  				Expect(ui.Outputs).To(ContainSubstrings(
   171  					[]string{"Assigning space quota", "to space", "my-user"},
   172  					[]string{"FAILED"},
   173  					[]string{"I can't find my quota name!"},
   174  				))
   175  			})
   176  		})
   177  	})
   178  })