github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/application/scale_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/commands/application/applicationfakes"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  
    17  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    18  )
    19  
    20  var _ = Describe("scale command", func() {
    21  	var (
    22  		requirementsFactory *requirementsfakes.FakeFactory
    23  		restarter           *applicationfakes.FakeRestarter
    24  		appRepo             *applicationsfakes.FakeRepository
    25  		ui                  *testterm.FakeUI
    26  		config              coreconfig.Repository
    27  		app                 models.Application
    28  		OriginalCommand     commandregistry.Command
    29  		deps                commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    35  		deps.Config = config
    36  
    37  		//inject fake 'command dependency' into registry
    38  		commandregistry.Register(restarter)
    39  
    40  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("scale").SetDependency(deps, pluginCall))
    41  	}
    42  
    43  	BeforeEach(func() {
    44  		requirementsFactory = new(requirementsfakes.FakeFactory)
    45  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    46  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    47  
    48  		//save original command and restore later
    49  		OriginalCommand = commandregistry.Commands.FindCommand("restart")
    50  
    51  		restarter = new(applicationfakes.FakeRestarter)
    52  		//setup fakes to correctly interact with commandregistry
    53  		restarter.SetDependencyStub = func(_ commandregistry.Dependency, _ bool) commandregistry.Command {
    54  			return restarter
    55  		}
    56  		restarter.MetaDataReturns(commandregistry.CommandMetadata{Name: "restart"})
    57  
    58  		appRepo = new(applicationsfakes.FakeRepository)
    59  		ui = new(testterm.FakeUI)
    60  		config = testconfig.NewRepositoryWithDefaults()
    61  
    62  		app = models.Application{ApplicationFields: models.ApplicationFields{
    63  			Name:          "my-app",
    64  			GUID:          "my-app-guid",
    65  			InstanceCount: 42,
    66  			DiskQuota:     1024,
    67  			Memory:        256,
    68  		}}
    69  		applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    70  		applicationReq.GetApplicationReturns(app)
    71  		requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    72  		appRepo.UpdateReturns(app, nil)
    73  	})
    74  
    75  	AfterEach(func() {
    76  		commandregistry.Register(OriginalCommand)
    77  	})
    78  
    79  	Describe("requirements", func() {
    80  		It("requires the user to be logged in with a targed space", func() {
    81  			args := []string{"-m", "1G", "my-app"}
    82  
    83  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    84  
    85  			Expect(testcmd.RunCLICommand("scale", args, requirementsFactory, updateCommandDependency, false, ui)).To(BeFalse())
    86  
    87  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    88  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    89  
    90  			Expect(testcmd.RunCLICommand("scale", args, requirementsFactory, updateCommandDependency, false, ui)).To(BeFalse())
    91  		})
    92  
    93  		It("requires an app to be specified", func() {
    94  			passed := testcmd.RunCLICommand("scale", []string{"-m", "1G"}, requirementsFactory, updateCommandDependency, false, ui)
    95  
    96  			Expect(ui.Outputs()).To(ContainSubstrings(
    97  				[]string{"Incorrect Usage", "Requires", "argument"},
    98  			))
    99  			Expect(passed).To(BeFalse())
   100  		})
   101  
   102  		It("does not require any flags", func() {
   103  			Expect(testcmd.RunCLICommand("scale", []string{"my-app"}, requirementsFactory, updateCommandDependency, false, ui)).To(BeTrue())
   104  		})
   105  	})
   106  
   107  	Describe("scaling an app", func() {
   108  		Context("when no flags are specified", func() {
   109  			It("prints a description of the app's limits", func() {
   110  				testcmd.RunCLICommand("scale", []string{"my-app"}, requirementsFactory, updateCommandDependency, false, ui)
   111  
   112  				Expect(ui.Outputs()).To(ContainSubstrings(
   113  					[]string{"Showing", "my-app", "my-org", "my-space", "my-user"},
   114  					[]string{"OK"},
   115  					[]string{"memory", "256M"},
   116  					[]string{"disk", "1G"},
   117  					[]string{"instances", "42"},
   118  				))
   119  
   120  				Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"Scaling", "my-app", "my-org", "my-space", "my-user"}))
   121  			})
   122  		})
   123  
   124  		Context("when the user does not confirm 'yes'", func() {
   125  			It("does not restart the app", func() {
   126  				ui.Inputs = []string{"whatever"}
   127  				testcmd.RunCLICommand("scale", []string{"-i", "5", "-m", "512M", "-k", "2G", "my-app"}, requirementsFactory, updateCommandDependency, false, ui)
   128  
   129  				Expect(restarter.ApplicationRestartCallCount()).To(Equal(0))
   130  			})
   131  		})
   132  
   133  		Context("when the user provides the -f flag", func() {
   134  			It("does not prompt the user", func() {
   135  				testcmd.RunCLICommand("scale", []string{"-f", "-i", "5", "-m", "512M", "-k", "2G", "my-app"}, requirementsFactory, updateCommandDependency, false, ui)
   136  
   137  				application, orgName, spaceName := restarter.ApplicationRestartArgsForCall(0)
   138  				Expect(application).To(Equal(app))
   139  				Expect(orgName).To(Equal(config.OrganizationFields().Name))
   140  				Expect(spaceName).To(Equal(config.SpaceFields().Name))
   141  			})
   142  		})
   143  
   144  		Context("when the user confirms they want to restart", func() {
   145  			BeforeEach(func() {
   146  				ui.Inputs = []string{"yes"}
   147  			})
   148  
   149  			It("can set an app's instance count, memory limit and disk limit", func() {
   150  				testcmd.RunCLICommand("scale", []string{"-i", "5", "-m", "512M", "-k", "2G", "my-app"}, requirementsFactory, updateCommandDependency, false, ui)
   151  
   152  				Expect(ui.Outputs()).To(ContainSubstrings(
   153  					[]string{"Scaling", "my-app", "my-org", "my-space", "my-user"},
   154  					[]string{"OK"},
   155  				))
   156  
   157  				Expect(ui.Prompts).To(ContainSubstrings([]string{"This will cause the app to restart", "Are you sure", "my-app"}))
   158  
   159  				application, orgName, spaceName := restarter.ApplicationRestartArgsForCall(0)
   160  				Expect(application).To(Equal(app))
   161  				Expect(orgName).To(Equal(config.OrganizationFields().Name))
   162  				Expect(spaceName).To(Equal(config.SpaceFields().Name))
   163  
   164  				appGUID, params := appRepo.UpdateArgsForCall(0)
   165  				Expect(appGUID).To(Equal("my-app-guid"))
   166  				Expect(*params.Memory).To(Equal(int64(512)))
   167  				Expect(*params.InstanceCount).To(Equal(5))
   168  				Expect(*params.DiskQuota).To(Equal(int64(2048)))
   169  			})
   170  
   171  			It("does not scale the memory and disk limits if they are not specified", func() {
   172  				testcmd.RunCLICommand("scale", []string{"-i", "5", "my-app"}, requirementsFactory, updateCommandDependency, false, ui)
   173  
   174  				Expect(restarter.ApplicationRestartCallCount()).To(Equal(0))
   175  
   176  				appGUID, params := appRepo.UpdateArgsForCall(0)
   177  				Expect(appGUID).To(Equal("my-app-guid"))
   178  				Expect(*params.InstanceCount).To(Equal(5))
   179  				Expect(params.DiskQuota).To(BeNil())
   180  				Expect(params.Memory).To(BeNil())
   181  			})
   182  
   183  			It("does not scale the app's instance count if it is not specified", func() {
   184  				testcmd.RunCLICommand("scale", []string{"-m", "512M", "my-app"}, requirementsFactory, updateCommandDependency, false, ui)
   185  
   186  				application, orgName, spaceName := restarter.ApplicationRestartArgsForCall(0)
   187  				Expect(application).To(Equal(app))
   188  				Expect(orgName).To(Equal(config.OrganizationFields().Name))
   189  				Expect(spaceName).To(Equal(config.SpaceFields().Name))
   190  
   191  				appGUID, params := appRepo.UpdateArgsForCall(0)
   192  				Expect(appGUID).To(Equal("my-app-guid"))
   193  				Expect(*params.Memory).To(Equal(int64(512)))
   194  				Expect(params.DiskQuota).To(BeNil())
   195  				Expect(params.InstanceCount).To(BeNil())
   196  			})
   197  		})
   198  	})
   199  })