github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+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/commands/application"
     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.ReadWriter
    26  		cmd                 *Scale
    27  		app                 models.Application
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
    32  		restarter = &testcmd.FakeApplicationRestarter{}
    33  		appRepo = &testApplication.FakeApplicationRepository{}
    34  		ui = new(testterm.FakeUI)
    35  		config = testconfig.NewRepositoryWithDefaults()
    36  		cmd = NewScale(ui, config, restarter, appRepo)
    37  	})
    38  
    39  	Describe("requirements", func() {
    40  		It("requires the user to be logged in with a targed space", func() {
    41  			args := []string{"-m", "1G", "my-app"}
    42  
    43  			requirementsFactory.LoginSuccess = false
    44  			requirementsFactory.TargetedSpaceSuccess = true
    45  
    46  			Expect(testcmd.RunCommand(cmd, args, requirementsFactory)).To(BeFalse())
    47  
    48  			requirementsFactory.LoginSuccess = true
    49  			requirementsFactory.TargetedSpaceSuccess = false
    50  
    51  			Expect(testcmd.RunCommand(cmd, args, requirementsFactory)).To(BeFalse())
    52  		})
    53  
    54  		It("requires an app to be specified", func() {
    55  			passed := testcmd.RunCommand(cmd, []string{"-m", "1G"}, requirementsFactory)
    56  
    57  			Expect(ui.FailedWithUsage).To(BeTrue())
    58  			Expect(passed).To(BeFalse())
    59  		})
    60  
    61  		It("does not require any flags", func() {
    62  			requirementsFactory.LoginSuccess = true
    63  			requirementsFactory.TargetedSpaceSuccess = true
    64  
    65  			Expect(testcmd.RunCommand(cmd, []string{"my-app"}, requirementsFactory)).To(BeTrue())
    66  		})
    67  	})
    68  
    69  	Describe("scaling an app", func() {
    70  		BeforeEach(func() {
    71  			app = maker.NewApp(maker.Overrides{"name": "my-app", "guid": "my-app-guid"})
    72  			app.InstanceCount = 42
    73  			app.DiskQuota = 1024
    74  			app.Memory = 256
    75  
    76  			requirementsFactory.Application = app
    77  			appRepo.UpdateAppResult = app
    78  		})
    79  
    80  		Context("when no flags are specified", func() {
    81  			It("prints a description of the app's limits", func() {
    82  				testcmd.RunCommand(cmd, []string{"my-app"}, requirementsFactory)
    83  
    84  				Expect(ui.Outputs).To(ContainSubstrings(
    85  					[]string{"Showing", "my-app", "my-org", "my-space", "my-user"},
    86  					[]string{"OK"},
    87  					[]string{"memory", "256M"},
    88  					[]string{"disk", "1G"},
    89  					[]string{"instances", "42"},
    90  				))
    91  
    92  				Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"Scaling", "my-app", "my-org", "my-space", "my-user"}))
    93  			})
    94  		})
    95  
    96  		Context("when the user does not confirm 'yes'", func() {
    97  			It("does not restart the app", func() {
    98  				ui.Inputs = []string{"whatever"}
    99  				testcmd.RunCommand(cmd, []string{"-i", "5", "-m", "512M", "-k", "2G", "my-app"}, requirementsFactory)
   100  
   101  				Expect(restarter.ApplicationRestartCallCount()).To(Equal(0))
   102  			})
   103  		})
   104  
   105  		Context("when the user provides the -f flag", func() {
   106  			It("does not prompt the user", func() {
   107  				testcmd.RunCommand(cmd, []string{"-f", "-i", "5", "-m", "512M", "-k", "2G", "my-app"}, requirementsFactory)
   108  
   109  				application, orgName, spaceName := restarter.ApplicationRestartArgsForCall(0)
   110  				Expect(application).To(Equal(app))
   111  				Expect(orgName).To(Equal(config.OrganizationFields().Name))
   112  				Expect(spaceName).To(Equal(config.SpaceFields().Name))
   113  			})
   114  		})
   115  
   116  		Context("when the user confirms they want to restart", func() {
   117  			BeforeEach(func() {
   118  				ui.Inputs = []string{"yes"}
   119  			})
   120  
   121  			It("can set an app's instance count, memory limit and disk limit", func() {
   122  				testcmd.RunCommand(cmd, []string{"-i", "5", "-m", "512M", "-k", "2G", "my-app"}, requirementsFactory)
   123  
   124  				Expect(ui.Outputs).To(ContainSubstrings(
   125  					[]string{"Scaling", "my-app", "my-org", "my-space", "my-user"},
   126  					[]string{"OK"},
   127  				))
   128  
   129  				Expect(ui.Prompts).To(ContainSubstrings([]string{"This will cause the app to restart", "Are you sure", "my-app"}))
   130  
   131  				application, orgName, spaceName := restarter.ApplicationRestartArgsForCall(0)
   132  				Expect(application).To(Equal(app))
   133  				Expect(orgName).To(Equal(config.OrganizationFields().Name))
   134  				Expect(spaceName).To(Equal(config.SpaceFields().Name))
   135  
   136  				Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid"))
   137  				Expect(*appRepo.UpdateParams.Memory).To(Equal(int64(512)))
   138  				Expect(*appRepo.UpdateParams.InstanceCount).To(Equal(5))
   139  				Expect(*appRepo.UpdateParams.DiskQuota).To(Equal(int64(2048)))
   140  			})
   141  
   142  			It("does not scale the memory and disk limits if they are not specified", func() {
   143  				testcmd.RunCommand(cmd, []string{"-i", "5", "my-app"}, requirementsFactory)
   144  
   145  				Expect(restarter.ApplicationRestartCallCount()).To(Equal(0))
   146  
   147  				Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid"))
   148  				Expect(*appRepo.UpdateParams.InstanceCount).To(Equal(5))
   149  				Expect(appRepo.UpdateParams.DiskQuota).To(BeNil())
   150  				Expect(appRepo.UpdateParams.Memory).To(BeNil())
   151  			})
   152  
   153  			It("does not scale the app's instance count if it is not specified", func() {
   154  				testcmd.RunCommand(cmd, []string{"-m", "512M", "my-app"}, requirementsFactory)
   155  
   156  				application, orgName, spaceName := restarter.ApplicationRestartArgsForCall(0)
   157  				Expect(application).To(Equal(app))
   158  				Expect(orgName).To(Equal(config.OrganizationFields().Name))
   159  				Expect(spaceName).To(Equal(config.SpaceFields().Name))
   160  
   161  				Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid"))
   162  				Expect(*appRepo.UpdateParams.Memory).To(Equal(int64(512)))
   163  				Expect(appRepo.UpdateParams.DiskQuota).To(BeNil())
   164  				Expect(appRepo.UpdateParams.InstanceCount).To(BeNil())
   165  			})
   166  		})
   167  	})
   168  })