github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/commands/application/restart_app_instance_test.go (about) 1 package application_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/api/appinstances/appinstancesfakes" 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 15 "code.cloudfoundry.org/cli/cf/commandregistry" 16 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("restart-app-instance", func() { 22 var ( 23 ui *testterm.FakeUI 24 config coreconfig.Repository 25 appInstancesRepo *appinstancesfakes.FakeAppInstancesRepository 26 requirementsFactory *requirementsfakes.FakeFactory 27 application models.Application 28 deps commandregistry.Dependency 29 ) 30 31 BeforeEach(func() { 32 33 ui = &testterm.FakeUI{} 34 appInstancesRepo = new(appinstancesfakes.FakeAppInstancesRepository) 35 config = testconfig.NewRepositoryWithDefaults() 36 requirementsFactory = new(requirementsfakes.FakeFactory) 37 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 38 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 39 40 application = models.Application{} 41 application.Name = "my-app" 42 application.GUID = "my-app-guid" 43 application.InstanceCount = 1 44 applicationReq := new(requirementsfakes.FakeApplicationRequirement) 45 applicationReq.GetApplicationReturns(application) 46 requirementsFactory.NewApplicationRequirementReturns(applicationReq) 47 }) 48 49 updateCommandDependency := func(pluginCall bool) { 50 deps.UI = ui 51 deps.Config = config 52 deps.RepoLocator = deps.RepoLocator.SetAppInstancesRepository(appInstancesRepo) 53 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("restart-app-instance").SetDependency(deps, pluginCall)) 54 } 55 56 runCommand := func(args ...string) bool { 57 return testcmd.RunCLICommand("restart-app-instance", args, requirementsFactory, updateCommandDependency, false, ui) 58 } 59 60 Describe("requirements", func() { 61 It("fails if not logged in", func() { 62 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 63 Expect(runCommand("my-app", "0")).To(BeFalse()) 64 }) 65 66 It("fails if a space is not targeted", func() { 67 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"}) 68 Expect(runCommand("my-app", "0")).To(BeFalse()) 69 }) 70 71 It("fails when there is not exactly two arguments", func() { 72 Expect(runCommand("my-app")).To(BeFalse()) 73 Expect(runCommand("my-app", "0", "0")).To(BeFalse()) 74 Expect(runCommand()).To(BeFalse()) 75 }) 76 }) 77 78 Describe("restarting an instance of an application", func() { 79 It("correctly 'restarts' the desired instance", func() { 80 runCommand("my-app", "0") 81 82 app_guid, instance := appInstancesRepo.DeleteInstanceArgsForCall(0) 83 Expect(app_guid).To(Equal(application.GUID)) 84 Expect(instance).To(Equal(0)) 85 Expect(ui.Outputs()).To(ContainSubstrings( 86 []string{"Restarting instance 0 of application my-app as my-user"}, 87 []string{"OK"}, 88 )) 89 }) 90 91 Context("when deleting the app instance fails", func() { 92 BeforeEach(func() { 93 appInstancesRepo.DeleteInstanceReturns(errors.New("deletion failed")) 94 }) 95 It("fails", func() { 96 runCommand("my-app", "0") 97 98 app_guid, instance := appInstancesRepo.DeleteInstanceArgsForCall(0) 99 Expect(app_guid).To(Equal(application.GUID)) 100 Expect(instance).To(Equal(0)) 101 102 Expect(ui.Outputs()).To(ContainSubstrings( 103 []string{"FAILED"}, 104 []string{"deletion failed"}, 105 )) 106 }) 107 }) 108 109 Context("when the instance passed is not an non-negative integer", func() { 110 It("fails when it is a string", func() { 111 runCommand("my-app", "some-silly-thing") 112 113 Expect(ui.Outputs()).To(ContainSubstrings( 114 []string{"Instance must be a non-negative integer"}, 115 )) 116 }) 117 }) 118 }) 119 })