github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/cf/commands/application/restart_app_instance_test.go (about)

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