github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/stop_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/configuration/core_config" 6 "github.com/cloudfoundry/cli/cf/models" 7 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 8 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 9 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 10 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 11 12 . "github.com/cloudfoundry/cli/cf/commands/application" 13 . "github.com/cloudfoundry/cli/testhelpers/matchers" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("stop command", func() { 19 var ( 20 ui *testterm.FakeUI 21 app models.Application 22 appRepo *testApplication.FakeApplicationRepository 23 requirementsFactory *testreq.FakeReqFactory 24 config core_config.ReadWriter 25 ) 26 27 BeforeEach(func() { 28 ui = &testterm.FakeUI{} 29 config = testconfig.NewRepositoryWithDefaults() 30 appRepo = &testApplication.FakeApplicationRepository{} 31 requirementsFactory = &testreq.FakeReqFactory{} 32 }) 33 34 runCommand := func(args ...string) bool { 35 return testcmd.RunCommand(NewStop(ui, config, appRepo), args, requirementsFactory) 36 } 37 38 It("fails requirements when not logged in", func() { 39 Expect(runCommand("some-app-name")).To(BeFalse()) 40 }) 41 42 Context("when logged in and an app exists", func() { 43 BeforeEach(func() { 44 requirementsFactory.LoginSuccess = true 45 46 app = models.Application{} 47 app.Name = "my-app" 48 app.Guid = "my-app-guid" 49 app.State = "started" 50 }) 51 52 JustBeforeEach(func() { 53 appRepo.ReadReturns.App = app 54 requirementsFactory.Application = app 55 }) 56 57 It("fails with usage when the app name is not given", func() { 58 runCommand() 59 Expect(ui.FailedWithUsage).To(BeTrue()) 60 }) 61 62 It("stops the app with the given name", func() { 63 runCommand("my-app") 64 65 Expect(ui.Outputs).To(ContainSubstrings( 66 []string{"Stopping app", "my-app", "my-org", "my-space", "my-user"}, 67 []string{"OK"}, 68 )) 69 70 Expect(requirementsFactory.ApplicationName).To(Equal("my-app")) 71 Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid")) 72 }) 73 74 It("warns the user when stopping the app fails", func() { 75 appRepo.UpdateErr = true 76 runCommand("my-app") 77 78 Expect(ui.Outputs).To(ContainSubstrings( 79 []string{"Stopping", "my-app"}, 80 []string{"FAILED"}, 81 []string{"Error updating app."}, 82 )) 83 Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid")) 84 }) 85 86 Context("when the app is stopped", func() { 87 BeforeEach(func() { 88 app.State = "stopped" 89 }) 90 91 It("warns the user when the app is already stopped", func() { 92 runCommand("my-app") 93 94 Expect(ui.Outputs).To(ContainSubstrings([]string{"my-app", "is already stopped"})) 95 Expect(appRepo.UpdateAppGuid).To(Equal("")) 96 }) 97 }) 98 99 Describe(".ApplicationStop()", func() { 100 It("returns the updated app model from ApplicationStop()", func() { 101 expectedStoppedApp := app 102 expectedStoppedApp.State = "stopped" 103 104 appRepo.UpdateAppResult = expectedStoppedApp 105 stopper := NewStop(ui, config, appRepo) 106 actualStoppedApp, err := stopper.ApplicationStop(app, config.OrganizationFields().Name, config.SpaceFields().Name) 107 108 Expect(err).NotTo(HaveOccurred()) 109 Expect(expectedStoppedApp).To(Equal(actualStoppedApp)) 110 }) 111 112 Context("When the app is already stopped", func() { 113 BeforeEach(func() { 114 app.State = "stopped" 115 }) 116 117 It("returns the app without updating it", func() { 118 stopper := NewStop(ui, config, appRepo) 119 updatedApp, err := stopper.ApplicationStop(app, config.OrganizationFields().Name, config.SpaceFields().Name) 120 121 Expect(err).NotTo(HaveOccurred()) 122 Expect(app).To(Equal(updatedApp)) 123 }) 124 }) 125 }) 126 }) 127 })