github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/application/restart_app_instance_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	testApplication "github.com/cloudfoundry/cli/cf/api/app_instances/fakes"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	"github.com/cloudfoundry/cli/cf/models"
     9  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    10  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  
    14  	"github.com/cloudfoundry/cli/cf/command_registry"
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("restart-app-instance", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		config              core_config.Repository
    24  		appInstancesRepo    *testApplication.FakeAppInstancesRepository
    25  		requirementsFactory *testreq.FakeReqFactory
    26  		application         models.Application
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		application = models.Application{}
    32  		application.Name = "my-app"
    33  		application.Guid = "my-app-guid"
    34  		application.InstanceCount = 1
    35  
    36  		ui = &testterm.FakeUI{}
    37  		appInstancesRepo = &testApplication.FakeAppInstancesRepository{}
    38  		config = testconfig.NewRepositoryWithDefaults()
    39  		requirementsFactory = &testreq.FakeReqFactory{
    40  			LoginSuccess:         true,
    41  			TargetedSpaceSuccess: true,
    42  			Application:          application,
    43  		}
    44  	})
    45  
    46  	updateCommandDependency := func(pluginCall bool) {
    47  		deps.Ui = ui
    48  		deps.Config = config
    49  		deps.RepoLocator = deps.RepoLocator.SetAppInstancesRepository(appInstancesRepo)
    50  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("restart-app-instance").SetDependency(deps, pluginCall))
    51  	}
    52  
    53  	runCommand := func(args ...string) bool {
    54  		return testcmd.RunCliCommand("restart-app-instance", args, requirementsFactory, updateCommandDependency, false)
    55  	}
    56  
    57  	Describe("requirements", func() {
    58  		It("fails if not logged in", func() {
    59  			requirementsFactory.LoginSuccess = false
    60  			Expect(runCommand("my-app", "0")).To(BeFalse())
    61  		})
    62  
    63  		It("fails if a space is not targeted", func() {
    64  			requirementsFactory.TargetedSpaceSuccess = false
    65  			Expect(runCommand("my-app", "0")).To(BeFalse())
    66  		})
    67  
    68  		It("fails when there is not exactly two arguments", func() {
    69  			Expect(runCommand("my-app")).To(BeFalse())
    70  			Expect(runCommand("my-app", "0", "0")).To(BeFalse())
    71  			Expect(runCommand()).To(BeFalse())
    72  		})
    73  	})
    74  
    75  	Describe("restarting an instance of an application", func() {
    76  		It("correctly 'restarts' the desired instance", func() {
    77  			runCommand("my-app", "0")
    78  
    79  			app_guid, instance := appInstancesRepo.DeleteInstanceArgsForCall(0)
    80  			Expect(app_guid).To(Equal(application.Guid))
    81  			Expect(instance).To(Equal(0))
    82  			Expect(ui.Outputs).To(ContainSubstrings(
    83  				[]string{"Restarting instance 0 of application my-app as my-user"},
    84  				[]string{"OK"},
    85  			))
    86  		})
    87  
    88  		Context("when deleting the app instance fails", func() {
    89  			BeforeEach(func() {
    90  				appInstancesRepo.DeleteInstanceReturns(errors.New("deletion failed"))
    91  			})
    92  			It("fails", func() {
    93  				runCommand("my-app", "0")
    94  
    95  				app_guid, instance := appInstancesRepo.DeleteInstanceArgsForCall(0)
    96  				Expect(app_guid).To(Equal(application.Guid))
    97  				Expect(instance).To(Equal(0))
    98  
    99  				Expect(ui.Outputs).To(ContainSubstrings(
   100  					[]string{"FAILED"},
   101  					[]string{"deletion failed"},
   102  				))
   103  			})
   104  		})
   105  
   106  		Context("when the instance passed is not an non-negative integer", func() {
   107  			It("fails when it is a string", func() {
   108  				runCommand("my-app", "some-silly-thing")
   109  
   110  				Expect(ui.Outputs).To(ContainSubstrings(
   111  					[]string{"Instance must be a non-negative integer"},
   112  				))
   113  			})
   114  		})
   115  	})
   116  })