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

     1  package application_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/models"
     5  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     6  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     7  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
     8  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
     9  
    10  	"github.com/cloudfoundry/cli/cf/command_registry"
    11  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    12  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("restart command", func() {
    18  	var (
    19  		ui                  *testterm.FakeUI
    20  		requirementsFactory *testreq.FakeReqFactory
    21  		starter             *testcmd.FakeApplicationStarter
    22  		stopper             *testcmd.FakeApplicationStopper
    23  		config              core_config.Repository
    24  		app                 models.Application
    25  		originalStop        command_registry.Command
    26  		originalStart       command_registry.Command
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.Config = config
    33  
    34  		//inject fake 'stopper and starter' into registry
    35  		command_registry.Register(starter)
    36  		command_registry.Register(stopper)
    37  
    38  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("restart").SetDependency(deps, pluginCall))
    39  	}
    40  
    41  	runCommand := func(args ...string) bool {
    42  		return testcmd.RunCliCommand("restart", args, requirementsFactory, updateCommandDependency, false)
    43  	}
    44  
    45  	BeforeEach(func() {
    46  		ui = &testterm.FakeUI{}
    47  		deps = command_registry.NewDependency()
    48  		requirementsFactory = &testreq.FakeReqFactory{}
    49  		starter = &testcmd.FakeApplicationStarter{}
    50  		stopper = &testcmd.FakeApplicationStopper{}
    51  		config = testconfig.NewRepositoryWithDefaults()
    52  
    53  		app = models.Application{}
    54  		app.Name = "my-app"
    55  		app.Guid = "my-app-guid"
    56  
    57  		//save original command and restore later
    58  		originalStart = command_registry.Commands.FindCommand("start")
    59  		originalStop = command_registry.Commands.FindCommand("stop")
    60  
    61  		//setup fakes to correctly interact with command_registry
    62  		starter.SetDependencyStub = func(_ command_registry.Dependency, _ bool) command_registry.Command {
    63  			return starter
    64  		}
    65  		starter.MetaDataReturns(command_registry.CommandMetadata{Name: "start"})
    66  
    67  		stopper.SetDependencyStub = func(_ command_registry.Dependency, _ bool) command_registry.Command {
    68  			return stopper
    69  		}
    70  		stopper.MetaDataReturns(command_registry.CommandMetadata{Name: "stop"})
    71  	})
    72  
    73  	AfterEach(func() {
    74  		command_registry.Register(originalStart)
    75  		command_registry.Register(originalStop)
    76  	})
    77  
    78  	Describe("requirements", func() {
    79  		It("fails with usage when not provided exactly one arg", func() {
    80  			requirementsFactory.LoginSuccess = true
    81  			runCommand()
    82  			Expect(ui.Outputs).To(ContainSubstrings(
    83  				[]string{"Incorrect Usage", "Requires an argument"},
    84  			))
    85  		})
    86  
    87  		It("fails when not logged in", func() {
    88  			requirementsFactory.Application = app
    89  			requirementsFactory.TargetedSpaceSuccess = true
    90  
    91  			Expect(runCommand()).To(BeFalse())
    92  		})
    93  
    94  		It("fails when a space is not targeted", func() {
    95  			requirementsFactory.Application = app
    96  			requirementsFactory.LoginSuccess = true
    97  
    98  			Expect(runCommand()).To(BeFalse())
    99  		})
   100  	})
   101  
   102  	Context("when logged in, targeting a space, and an app name is provided", func() {
   103  		BeforeEach(func() {
   104  			requirementsFactory.Application = app
   105  			requirementsFactory.LoginSuccess = true
   106  			requirementsFactory.TargetedSpaceSuccess = true
   107  
   108  			stopper.ApplicationStopReturns(app, nil)
   109  		})
   110  
   111  		It("restarts the given app", func() {
   112  			runCommand("my-app")
   113  
   114  			application, orgName, spaceName := stopper.ApplicationStopArgsForCall(0)
   115  			Expect(application).To(Equal(app))
   116  			Expect(orgName).To(Equal(config.OrganizationFields().Name))
   117  			Expect(spaceName).To(Equal(config.SpaceFields().Name))
   118  
   119  			application, orgName, spaceName = starter.ApplicationStartArgsForCall(0)
   120  			Expect(application).To(Equal(app))
   121  			Expect(orgName).To(Equal(config.OrganizationFields().Name))
   122  			Expect(spaceName).To(Equal(config.SpaceFields().Name))
   123  
   124  			Expect(requirementsFactory.ApplicationName).To(Equal("my-app"))
   125  		})
   126  	})
   127  })