github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/start_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes"
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	v7 "code.cloudfoundry.org/cli/command/v7"
    13  	"code.cloudfoundry.org/cli/command/v7/shared/sharedfakes"
    14  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    15  	"code.cloudfoundry.org/cli/resources"
    16  	"code.cloudfoundry.org/cli/util/configv3"
    17  	"code.cloudfoundry.org/cli/util/ui"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("start Command", func() {
    24  	var (
    25  		cmd                v7.StartCommand
    26  		testUI             *ui.UI
    27  		fakeConfig         *commandfakes.FakeConfig
    28  		fakeSharedActor    *commandfakes.FakeSharedActor
    29  		fakeActor          *v7fakes.FakeActor
    30  		fakeLogCacheClient *sharedactionfakes.FakeLogCacheClient
    31  		fakeAppStager      *sharedfakes.FakeAppStager
    32  
    33  		binaryName string
    34  		executeErr error
    35  		app        resources.Application
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    40  		fakeConfig = new(commandfakes.FakeConfig)
    41  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    42  		fakeActor = new(v7fakes.FakeActor)
    43  		fakeLogCacheClient = new(sharedactionfakes.FakeLogCacheClient)
    44  		fakeAppStager = new(sharedfakes.FakeAppStager)
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		app = resources.Application{Name: "app-name", GUID: "app-guid"}
    49  
    50  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    51  			Name: "some-org",
    52  		})
    53  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    54  			Name: "some-space",
    55  			GUID: "some-space-guid",
    56  		})
    57  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    58  		fakeActor.GetApplicationByNameAndSpaceReturns(app, v7action.Warnings{"get-app-warning"}, nil)
    59  
    60  		cmd = v7.StartCommand{
    61  			RequiredArgs: flag.AppName{AppName: app.Name},
    62  			BaseCommand: v7.BaseCommand{
    63  				UI:          testUI,
    64  				Config:      fakeConfig,
    65  				SharedActor: fakeSharedActor,
    66  				Actor:       fakeActor,
    67  			},
    68  			LogCacheClient: fakeLogCacheClient,
    69  			Stager:         fakeAppStager,
    70  		}
    71  	})
    72  
    73  	JustBeforeEach(func() {
    74  		executeErr = cmd.Execute(nil)
    75  	})
    76  
    77  	When("checking target fails", func() {
    78  		BeforeEach(func() {
    79  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    80  		})
    81  
    82  		It("returns an error", func() {
    83  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    84  
    85  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    86  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    87  			Expect(checkTargetedOrg).To(BeTrue())
    88  			Expect(checkTargetedSpace).To(BeTrue())
    89  		})
    90  	})
    91  
    92  	When("the user is not logged in", func() {
    93  		var expectedErr error
    94  
    95  		BeforeEach(func() {
    96  			expectedErr = errors.New("some current user error")
    97  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    98  		})
    99  
   100  		It("return an error", func() {
   101  			Expect(executeErr).To(Equal(expectedErr))
   102  		})
   103  	})
   104  
   105  	It("gets the application", func() {
   106  		Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   107  		inputAppName, inputSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   108  		Expect(inputAppName).To(Equal(app.Name))
   109  		Expect(inputSpaceGUID).To(Equal("some-space-guid"))
   110  	})
   111  
   112  	When("Getting the application fails", func() {
   113  		BeforeEach(func() {
   114  			fakeActor.GetApplicationByNameAndSpaceReturns(
   115  				resources.Application{},
   116  				v7action.Warnings{"get-app-warning"},
   117  				errors.New("get-app-error"),
   118  			)
   119  		})
   120  
   121  		It("displays all warnings and returns an error", func() {
   122  			Expect(executeErr).To(MatchError("get-app-error"))
   123  			Expect(testUI.Err).To(Say("get-app-warning"))
   124  		})
   125  	})
   126  
   127  	Context("a new package is available", func() {
   128  		BeforeEach(func() {
   129  			fakeActor.GetUnstagedNewestPackageGUIDReturns("package-guid", v7action.Warnings{}, nil)
   130  			fakeActor.GetApplicationByNameAndSpaceReturns(app, v7action.Warnings{"get-app-warning"}, nil)
   131  		})
   132  
   133  		When("the app is stopped", func() {
   134  			BeforeEach(func() {
   135  				app = resources.Application{Name: "app-name", GUID: "app-guid", State: constant.ApplicationStopped}
   136  				fakeActor.GetApplicationByNameAndSpaceReturns(app, v7action.Warnings{"get-app-warning"}, nil)
   137  			})
   138  
   139  			It("stages the new package and starts the app with the new droplet", func() {
   140  				Expect(executeErr).ToNot(HaveOccurred())
   141  				Expect(fakeAppStager.StageAndStartCallCount()).To(Equal(1))
   142  
   143  				inputApp, inputSpace, inputOrg, inputPkgGUID, inputStrategy, inputNoWait, inputAppAction := fakeAppStager.StageAndStartArgsForCall(0)
   144  				Expect(inputApp).To(Equal(app))
   145  				Expect(inputSpace).To(Equal(cmd.Config.TargetedSpace()))
   146  				Expect(inputOrg).To(Equal(cmd.Config.TargetedOrganization()))
   147  				Expect(inputPkgGUID).To(Equal("package-guid"))
   148  				Expect(inputStrategy).To(Equal(constant.DeploymentStrategyDefault))
   149  				Expect(inputNoWait).To(Equal(false))
   150  				Expect(inputAppAction).To(Equal(constant.ApplicationStarting))
   151  			})
   152  
   153  			When("staging and starting the app returns an error", func() {
   154  				BeforeEach(func() {
   155  					fakeAppStager.StageAndStartReturns(errors.New("stage-and-start-error"))
   156  				})
   157  
   158  				It("returns an error", func() {
   159  					Expect(executeErr).To(MatchError("stage-and-start-error"))
   160  				})
   161  			})
   162  		})
   163  
   164  		When("the app is started", func() {
   165  			BeforeEach(func() {
   166  				app = resources.Application{Name: "app-name", GUID: "app-guid", State: constant.ApplicationStarted}
   167  				fakeActor.GetApplicationByNameAndSpaceReturns(app, v7action.Warnings{"get-app-warning"}, nil)
   168  			})
   169  
   170  			It("starts the app with the current droplet", func() {
   171  				Expect(executeErr).ToNot(HaveOccurred())
   172  				Expect(fakeAppStager.StartAppCallCount()).To(Equal(1))
   173  
   174  				inputApp, inputDroplet, inputStrategy, inputNoWait, inputSpace, inputOrg, inputAppAction := fakeAppStager.StartAppArgsForCall(0)
   175  				Expect(inputApp).To(Equal(app))
   176  				Expect(inputDroplet).To(Equal(resources.Droplet{}))
   177  				Expect(inputStrategy).To(Equal(constant.DeploymentStrategyDefault))
   178  				Expect(inputNoWait).To(Equal(false))
   179  				Expect(inputSpace).To(Equal(cmd.Config.TargetedSpace()))
   180  				Expect(inputOrg).To(Equal(cmd.Config.TargetedOrganization()))
   181  				Expect(inputAppAction).To(Equal(constant.ApplicationStarting))
   182  			})
   183  
   184  			When("starting the app returns an error", func() {
   185  				BeforeEach(func() {
   186  					fakeAppStager.StartAppReturns(errors.New("start-error"))
   187  				})
   188  
   189  				It("returns an error", func() {
   190  					Expect(executeErr).To(MatchError("start-error"))
   191  				})
   192  			})
   193  		})
   194  	})
   195  
   196  	Context("no new package is available", func() {
   197  		BeforeEach(func() {
   198  			fakeActor.GetUnstagedNewestPackageGUIDReturns("", v7action.Warnings{}, nil)
   199  		})
   200  
   201  		It("starts the app with the current droplet", func() {
   202  			Expect(executeErr).ToNot(HaveOccurred())
   203  			Expect(fakeAppStager.StartAppCallCount()).To(Equal(1))
   204  
   205  			inputApp, inputDroplet, inputStrategy, inputNoWait, inputSpace, inputOrg, inputAppAction := fakeAppStager.StartAppArgsForCall(0)
   206  			Expect(inputApp).To(Equal(app))
   207  			Expect(inputDroplet).To(Equal(resources.Droplet{}))
   208  			Expect(inputStrategy).To(Equal(constant.DeploymentStrategyDefault))
   209  			Expect(inputNoWait).To(Equal(false))
   210  			Expect(inputSpace).To(Equal(cmd.Config.TargetedSpace()))
   211  			Expect(inputOrg).To(Equal(cmd.Config.TargetedOrganization()))
   212  			Expect(inputAppAction).To(Equal(constant.ApplicationStarting))
   213  		})
   214  
   215  		When("starting the app returns an error", func() {
   216  			BeforeEach(func() {
   217  				fakeAppStager.StartAppReturns(errors.New("start-error"))
   218  			})
   219  
   220  			It("returns an error", func() {
   221  				Expect(executeErr).To(MatchError("start-error"))
   222  			})
   223  		})
   224  	})
   225  
   226  	When("getting attempting to get the unstaged package returns an error", func() {
   227  		var expectedErr error
   228  		BeforeEach(func() {
   229  			expectedErr = errors.New("error getting package")
   230  			fakeActor.GetUnstagedNewestPackageGUIDReturns("", v7action.Warnings{"needs-stage-warnings"}, expectedErr)
   231  		})
   232  
   233  		It("errors", func() {
   234  			Expect(testUI.Err).To(Say("needs-stage-warnings"))
   235  			Expect(executeErr).To(MatchError(expectedErr))
   236  			Expect(fakeActor.GetUnstagedNewestPackageGUIDCallCount()).To(Equal(1))
   237  			appGUID := fakeActor.GetUnstagedNewestPackageGUIDArgsForCall(0)
   238  			Expect(appGUID).To(Equal("app-guid"))
   239  		})
   240  	})
   241  
   242  })