github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+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/command_registry"
    13  	"github.com/cloudfoundry/cli/cf/commands/application"
    14  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("stop command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		app                 models.Application
    23  		appRepo             *testApplication.FakeApplicationRepository
    24  		requirementsFactory *testreq.FakeReqFactory
    25  		config              core_config.Repository
    26  		deps                command_registry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.Ui = ui
    31  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    32  		deps.Config = config
    33  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("stop").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		ui = &testterm.FakeUI{}
    38  		config = testconfig.NewRepositoryWithDefaults()
    39  		appRepo = &testApplication.FakeApplicationRepository{}
    40  		requirementsFactory = &testreq.FakeReqFactory{}
    41  	})
    42  
    43  	runCommand := func(args ...string) bool {
    44  		return testcmd.RunCliCommand("stop", args, requirementsFactory, updateCommandDependency, false)
    45  	}
    46  
    47  	It("fails requirements when not logged in", func() {
    48  		Expect(runCommand("some-app-name")).To(BeFalse())
    49  	})
    50  	It("fails requirements when a space is not targeted", func() {
    51  		requirementsFactory.LoginSuccess = true
    52  		requirementsFactory.TargetedSpaceSuccess = false
    53  		Expect(runCommand("some-app-name")).To(BeFalse())
    54  	})
    55  
    56  	Context("when logged in and an app exists", func() {
    57  		BeforeEach(func() {
    58  			requirementsFactory.LoginSuccess = true
    59  			requirementsFactory.TargetedSpaceSuccess = true
    60  
    61  			app = models.Application{}
    62  			app.Name = "my-app"
    63  			app.Guid = "my-app-guid"
    64  			app.State = "started"
    65  		})
    66  
    67  		JustBeforeEach(func() {
    68  			appRepo.ReadReturns.App = app
    69  			requirementsFactory.Application = app
    70  		})
    71  
    72  		It("fails with usage when the app name is not given", func() {
    73  			runCommand()
    74  			Expect(ui.Outputs).To(ContainSubstrings(
    75  				[]string{"Incorrect Usage", "Requires an argument"},
    76  			))
    77  		})
    78  
    79  		It("stops the app with the given name", func() {
    80  			runCommand("my-app")
    81  
    82  			Expect(ui.Outputs).To(ContainSubstrings(
    83  				[]string{"Stopping app", "my-app", "my-org", "my-space", "my-user"},
    84  				[]string{"OK"},
    85  			))
    86  
    87  			Expect(requirementsFactory.ApplicationName).To(Equal("my-app"))
    88  			Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid"))
    89  		})
    90  
    91  		It("warns the user when stopping the app fails", func() {
    92  			appRepo.UpdateErr = true
    93  			runCommand("my-app")
    94  
    95  			Expect(ui.Outputs).To(ContainSubstrings(
    96  				[]string{"Stopping", "my-app"},
    97  				[]string{"FAILED"},
    98  				[]string{"Error updating app."},
    99  			))
   100  			Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid"))
   101  		})
   102  
   103  		Context("when the app is stopped", func() {
   104  			BeforeEach(func() {
   105  				app.State = "stopped"
   106  			})
   107  
   108  			It("warns the user when the app is already stopped", func() {
   109  				runCommand("my-app")
   110  
   111  				Expect(ui.Outputs).To(ContainSubstrings([]string{"my-app", "is already stopped"}))
   112  				Expect(appRepo.UpdateAppGuid).To(Equal(""))
   113  			})
   114  		})
   115  
   116  		Describe(".ApplicationStop()", func() {
   117  			It("returns the updated app model from ApplicationStop()", func() {
   118  				expectedStoppedApp := app
   119  				expectedStoppedApp.State = "stopped"
   120  
   121  				appRepo.UpdateAppResult = expectedStoppedApp
   122  				updateCommandDependency(false)
   123  				stopper := command_registry.Commands.FindCommand("stop").(*application.Stop)
   124  				actualStoppedApp, err := stopper.ApplicationStop(app, config.OrganizationFields().Name, config.SpaceFields().Name)
   125  
   126  				Expect(err).NotTo(HaveOccurred())
   127  				Expect(expectedStoppedApp).To(Equal(actualStoppedApp))
   128  			})
   129  
   130  			Context("When the app is already stopped", func() {
   131  				BeforeEach(func() {
   132  					app.State = "stopped"
   133  				})
   134  
   135  				It("returns the app without updating it", func() {
   136  					stopper := command_registry.Commands.FindCommand("stop").(*application.Stop)
   137  					updatedApp, err := stopper.ApplicationStop(app, config.OrganizationFields().Name, config.SpaceFields().Name)
   138  
   139  					Expect(err).NotTo(HaveOccurred())
   140  					Expect(app).To(Equal(updatedApp))
   141  				})
   142  			})
   143  		})
   144  	})
   145  })