github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+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/commands/application"
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  
    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  		configRepo          core_config.ReadWriter
    24  		appInstancesRepo    *testApplication.FakeAppInstancesRepository
    25  		requirementsFactory *testreq.FakeReqFactory
    26  		application         models.Application
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		application = models.Application{}
    31  		application.Name = "my-app"
    32  		application.Guid = "my-app-guid"
    33  		application.InstanceCount = 1
    34  
    35  		ui = &testterm.FakeUI{}
    36  		appInstancesRepo = &testApplication.FakeAppInstancesRepository{}
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		requirementsFactory = &testreq.FakeReqFactory{
    39  			LoginSuccess:         true,
    40  			TargetedSpaceSuccess: true,
    41  			Application:          application,
    42  		}
    43  
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		cmd := NewRestartAppInstance(ui, configRepo, appInstancesRepo)
    48  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    49  	}
    50  
    51  	Describe("requirements", func() {
    52  		It("fails if not logged in", func() {
    53  			requirementsFactory.LoginSuccess = false
    54  			Expect(runCommand("my-app", "0")).To(BeFalse())
    55  		})
    56  
    57  		It("fails if a space is not targeted", func() {
    58  			requirementsFactory.TargetedSpaceSuccess = false
    59  			Expect(runCommand("my-app", "0")).To(BeFalse())
    60  		})
    61  
    62  		It("fails when there is not exactly two arguments", func() {
    63  			Expect(runCommand("my-app")).To(BeFalse())
    64  			Expect(runCommand("my-app", "0", "0")).To(BeFalse())
    65  			Expect(runCommand()).To(BeFalse())
    66  		})
    67  	})
    68  
    69  	Describe("restarting an instance of an application", func() {
    70  		It("correctly 'restarts' the desired instance", func() {
    71  			runCommand("my-app", "0")
    72  
    73  			app_guid, instance := appInstancesRepo.DeleteInstanceArgsForCall(0)
    74  			Expect(app_guid).To(Equal(application.Guid))
    75  			Expect(instance).To(Equal(0))
    76  			Expect(ui.Outputs).To(ContainSubstrings(
    77  				[]string{"Restarting instance 0 of application my-app as my-user"},
    78  				[]string{"OK"},
    79  			))
    80  		})
    81  
    82  		Context("when deleting the app instance fails", func() {
    83  			BeforeEach(func() {
    84  				appInstancesRepo.DeleteInstanceReturns(errors.New("deletion failed"))
    85  			})
    86  			It("fails", func() {
    87  				runCommand("my-app", "0")
    88  
    89  				app_guid, instance := appInstancesRepo.DeleteInstanceArgsForCall(0)
    90  				Expect(app_guid).To(Equal(application.Guid))
    91  				Expect(instance).To(Equal(0))
    92  
    93  				Expect(ui.Outputs).To(ContainSubstrings(
    94  					[]string{"FAILED"},
    95  					[]string{"deletion failed"},
    96  				))
    97  			})
    98  		})
    99  
   100  		Context("when the instance passed is not an non-negative integer", func() {
   101  			It("fails when it is a string", func() {
   102  				runCommand("my-app", "some-silly-thing")
   103  
   104  				Expect(ui.Outputs).To(ContainSubstrings(
   105  					[]string{"Instance must be a non-negative integer"},
   106  				))
   107  			})
   108  		})
   109  	})
   110  })