github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/application/restart_test.go (about)

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