github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/quota/quotas_test.go (about)

     1  package quota_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/quotas/quotasfakes"
     5  	"code.cloudfoundry.org/cli/cf/commands/quota"
     6  	"code.cloudfoundry.org/cli/cf/errors"
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	"code.cloudfoundry.org/cli/cf/terminal"
    12  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  
    16  	"code.cloudfoundry.org/cli/cf/commandregistry"
    17  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    18  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("quotas command", func() {
    24  	var (
    25  		ui                  *testterm.FakeUI
    26  		config              coreconfig.Repository
    27  		quotaRepo           *quotasfakes.FakeQuotaRepository
    28  		requirementsFactory *requirementsfakes.FakeFactory
    29  		deps                commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.Config = config
    35  		deps.RepoLocator = deps.RepoLocator.SetQuotaRepository(quotaRepo)
    36  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("quotas").SetDependency(deps, pluginCall))
    37  	}
    38  
    39  	BeforeEach(func() {
    40  		ui = &testterm.FakeUI{}
    41  		quotaRepo = new(quotasfakes.FakeQuotaRepository)
    42  		requirementsFactory = new(requirementsfakes.FakeFactory)
    43  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    44  		config = testconfig.NewRepositoryWithDefaults()
    45  	})
    46  
    47  	runCommand := func(args ...string) bool {
    48  		return testcmd.RunCLICommand("quotas", args, requirementsFactory, updateCommandDependency, false, ui)
    49  	}
    50  
    51  	Describe("requirements", func() {
    52  		It("requires the user to be logged in", func() {
    53  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    54  			Expect(runCommand()).ToNot(HavePassedRequirements())
    55  		})
    56  
    57  		Context("when arguments are provided", func() {
    58  			var cmd commandregistry.Command
    59  			var flagContext flags.FlagContext
    60  
    61  			BeforeEach(func() {
    62  				cmd = &quota.ListQuotas{}
    63  				cmd.SetDependency(deps, false)
    64  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    65  			})
    66  
    67  			It("should fail with usage", func() {
    68  				flagContext.Parse("blahblah")
    69  
    70  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    71  				Expect(err).NotTo(HaveOccurred())
    72  
    73  				err = testcmd.RunRequirements(reqs)
    74  				Expect(err).To(HaveOccurred())
    75  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    76  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    77  			})
    78  		})
    79  	})
    80  
    81  	Context("when quotas exist", func() {
    82  		BeforeEach(func() {
    83  			quotaRepo.FindAllReturns([]models.QuotaFields{
    84  				{
    85  					Name:                    "quota-name",
    86  					MemoryLimit:             1024,
    87  					InstanceMemoryLimit:     512,
    88  					RoutesLimit:             111,
    89  					ServicesLimit:           222,
    90  					NonBasicServicesAllowed: true,
    91  					AppInstanceLimit:        -1,
    92  					ReservedRoutePorts:      "4",
    93  				},
    94  				{
    95  					Name:                    "quota-non-basic-not-allowed",
    96  					MemoryLimit:             434,
    97  					InstanceMemoryLimit:     -1,
    98  					RoutesLimit:             1,
    99  					ServicesLimit:           2,
   100  					NonBasicServicesAllowed: false,
   101  					AppInstanceLimit:        10,
   102  					ReservedRoutePorts:      "4",
   103  				},
   104  				{
   105  					Name:                    "quota-unlimited-routes",
   106  					MemoryLimit:             434,
   107  					InstanceMemoryLimit:     1,
   108  					RoutesLimit:             -1,
   109  					ServicesLimit:           2,
   110  					NonBasicServicesAllowed: false,
   111  					AppInstanceLimit:        10,
   112  					ReservedRoutePorts:      "4",
   113  				},
   114  			}, nil)
   115  		})
   116  
   117  		It("lists quotas", func() {
   118  			Expect(Expect(runCommand()).To(HavePassedRequirements())).To(HavePassedRequirements())
   119  			Expect(terminal.Decolorize(ui.Outputs()[0])).To(Equal("Getting quotas as my-user..."))
   120  			Expect(terminal.Decolorize(ui.Outputs()[1])).To(Equal("OK"))
   121  			Expect(terminal.Decolorize(ui.Outputs()[3])).To(MatchRegexp("name\\s*total memory\\s*instance memory\\s*routes\\s*service instances\\s*paid plans\\s*app instances\\s*route ports\\s*"))
   122  			Expect(terminal.Decolorize(ui.Outputs()[4])).To(MatchRegexp("quota-name\\s*1G\\s*512M\\s*111\\s*222\\s*allowed\\s*unlimited\\s*4"))
   123  			Expect(terminal.Decolorize(ui.Outputs()[5])).To(MatchRegexp("quota-non-basic-not-allowed\\s*434M\\s*unlimited\\s*1\\s*2\\s*disallowed\\s*10\\s*4"))
   124  			Expect(terminal.Decolorize(ui.Outputs()[6])).To(MatchRegexp("quota-unlimited-routes\\s*434M\\s*1M\\s*unlimited\\s*2\\s*disallowed\\s*10\\s*4"))
   125  		})
   126  
   127  		It("displays unlimited services properly", func() {
   128  			quotaRepo.FindAllReturns([]models.QuotaFields{
   129  				{
   130  					Name:                    "quota-with-no-limit-to-services",
   131  					MemoryLimit:             434,
   132  					InstanceMemoryLimit:     1,
   133  					RoutesLimit:             2,
   134  					ServicesLimit:           -1,
   135  					NonBasicServicesAllowed: false,
   136  					AppInstanceLimit:        7,
   137  				},
   138  			}, nil)
   139  			Expect(Expect(runCommand()).To(HavePassedRequirements())).To(HavePassedRequirements())
   140  			Expect(ui.Outputs()).To(ContainSubstrings(
   141  				[]string{"quota-with-no-limit-to-services", "434M", "1M", "2", "unlimited", "disallowed", "7"},
   142  			))
   143  
   144  			quotaRepo.FindAllReturns([]models.QuotaFields{
   145  				{
   146  					Name:                    "quota-with-no-limit-to-app-instance",
   147  					MemoryLimit:             434,
   148  					InstanceMemoryLimit:     1,
   149  					RoutesLimit:             2,
   150  					ServicesLimit:           7,
   151  					NonBasicServicesAllowed: false,
   152  					AppInstanceLimit:        -1,
   153  				},
   154  			}, nil)
   155  			Expect(Expect(runCommand()).To(HavePassedRequirements())).To(HavePassedRequirements())
   156  			Expect(ui.Outputs()).To(ContainSubstrings(
   157  				[]string{"quota-with-no-limit-to-app-instance", "434M", "1M", "2", "7", "disallowed", "unlimited"},
   158  			))
   159  
   160  			quotaRepo.FindAllReturns([]models.QuotaFields{
   161  				{
   162  					Name:                    "quota-with-no-limit-to-reserved-route-ports",
   163  					MemoryLimit:             434,
   164  					InstanceMemoryLimit:     1,
   165  					RoutesLimit:             2,
   166  					ServicesLimit:           7,
   167  					NonBasicServicesAllowed: false,
   168  					AppInstanceLimit:        7,
   169  					ReservedRoutePorts:      "-1",
   170  				},
   171  			}, nil)
   172  			Expect(Expect(runCommand()).To(HavePassedRequirements())).To(HavePassedRequirements())
   173  			Expect(ui.Outputs()).To(ContainSubstrings(
   174  				[]string{"quota-with-no-limit-to-app-instance", "434M", "1M", "2", "7", "disallowed", "7", "unlimited"},
   175  			))
   176  		})
   177  	})
   178  
   179  	Context("when an error occurs fetching quotas", func() {
   180  		BeforeEach(func() {
   181  			quotaRepo.FindAllReturns([]models.QuotaFields{}, errors.New("I haz a borken!"))
   182  		})
   183  
   184  		It("prints an error", func() {
   185  			runCommand()
   186  			Expect(ui.Outputs()).To(ContainSubstrings(
   187  				[]string{"Getting quotas as", "my-user"},
   188  				[]string{"FAILED"},
   189  			))
   190  		})
   191  	})
   192  
   193  })