github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/application/stop_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes"
     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/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    14  
    15  	"code.cloudfoundry.org/cli/cf/commandregistry"
    16  	"code.cloudfoundry.org/cli/cf/commands/application"
    17  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("stop command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		app                 models.Application
    26  		appRepo             *applicationsfakes.FakeRepository
    27  		requirementsFactory *requirementsfakes.FakeFactory
    28  		config              coreconfig.Repository
    29  		deps                commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    35  		deps.Config = config
    36  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("stop").SetDependency(deps, pluginCall))
    37  	}
    38  
    39  	BeforeEach(func() {
    40  		ui = &testterm.FakeUI{}
    41  		config = testconfig.NewRepositoryWithDefaults()
    42  		appRepo = new(applicationsfakes.FakeRepository)
    43  		requirementsFactory = new(requirementsfakes.FakeFactory)
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		return testcmd.RunCLICommand("stop", args, requirementsFactory, updateCommandDependency, false, ui)
    48  	}
    49  
    50  	It("fails requirements when not logged in", func() {
    51  		requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    52  		Expect(runCommand("some-app-name")).To(BeFalse())
    53  	})
    54  
    55  	It("fails requirements when a space is not targeted", func() {
    56  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    57  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    58  		Expect(runCommand("some-app-name")).To(BeFalse())
    59  	})
    60  
    61  	Context("when logged in and an app exists", func() {
    62  		BeforeEach(func() {
    63  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    64  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    65  
    66  			app = models.Application{}
    67  			app.Name = "my-app"
    68  			app.GUID = "my-app-guid"
    69  			app.State = "started"
    70  		})
    71  
    72  		JustBeforeEach(func() {
    73  			appRepo.ReadReturns(app, nil)
    74  			applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    75  			applicationReq.GetApplicationReturns(app)
    76  			requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    77  		})
    78  
    79  		It("fails with usage when the app name is not given", func() {
    80  			runCommand()
    81  			Expect(ui.Outputs()).To(ContainSubstrings(
    82  				[]string{"Incorrect Usage", "Requires an argument"},
    83  			))
    84  		})
    85  
    86  		It("stops the app with the given name", func() {
    87  			runCommand("my-app")
    88  
    89  			Expect(ui.Outputs()).To(ContainSubstrings(
    90  				[]string{"Stopping app", "my-app", "my-org", "my-space", "my-user"},
    91  				[]string{"OK"},
    92  			))
    93  
    94  			appGUID, _ := appRepo.UpdateArgsForCall(0)
    95  			Expect(appGUID).To(Equal("my-app-guid"))
    96  		})
    97  
    98  		It("warns the user when stopping the app fails", func() {
    99  			appRepo.UpdateReturns(models.Application{}, errors.New("Error updating app."))
   100  			runCommand("my-app")
   101  
   102  			Expect(ui.Outputs()).To(ContainSubstrings(
   103  				[]string{"Stopping", "my-app"},
   104  				[]string{"FAILED"},
   105  				[]string{"Error updating app."},
   106  			))
   107  			appGUID, _ := appRepo.UpdateArgsForCall(0)
   108  			Expect(appGUID).To(Equal("my-app-guid"))
   109  		})
   110  
   111  		Context("when the app is stopped", func() {
   112  			BeforeEach(func() {
   113  				app.State = "stopped"
   114  			})
   115  
   116  			It("warns the user when the app is already stopped", func() {
   117  				runCommand("my-app")
   118  
   119  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"my-app", "is already stopped"}))
   120  				Expect(appRepo.UpdateCallCount()).To(BeZero())
   121  			})
   122  		})
   123  
   124  		Describe(".ApplicationStop()", func() {
   125  			It("returns the updated app model from ApplicationStop()", func() {
   126  				expectedStoppedApp := app
   127  				expectedStoppedApp.State = "stopped"
   128  
   129  				appRepo.UpdateReturns(expectedStoppedApp, nil)
   130  				updateCommandDependency(false)
   131  				stopper := commandregistry.Commands.FindCommand("stop").(*application.Stop)
   132  				actualStoppedApp, err := stopper.ApplicationStop(app, config.OrganizationFields().Name, config.SpaceFields().Name)
   133  
   134  				Expect(err).NotTo(HaveOccurred())
   135  				Expect(expectedStoppedApp).To(Equal(actualStoppedApp))
   136  			})
   137  
   138  			Context("When the app is already stopped", func() {
   139  				BeforeEach(func() {
   140  					app.State = "stopped"
   141  				})
   142  
   143  				It("returns the app without updating it", func() {
   144  					stopper := commandregistry.Commands.FindCommand("stop").(*application.Stop)
   145  					updatedApp, err := stopper.ApplicationStop(app, config.OrganizationFields().Name, config.SpaceFields().Name)
   146  
   147  					Expect(err).NotTo(HaveOccurred())
   148  					Expect(app).To(Equal(updatedApp))
   149  				})
   150  			})
   151  		})
   152  	})
   153  })