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

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