github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/v3_restart_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	"code.cloudfoundry.org/cli/command/translatableerror"
    13  	. "code.cloudfoundry.org/cli/command/v6"
    14  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    15  	"code.cloudfoundry.org/cli/util/configv3"
    16  	"code.cloudfoundry.org/cli/util/ui"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  var _ = Describe("v3-restart Command", func() {
    23  	var (
    24  		cmd             V3RestartCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v6fakes.FakeV3RestartActor
    29  		binaryName      string
    30  		executeErr      error
    31  		app             string
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v6fakes.FakeV3RestartActor)
    39  
    40  		binaryName = "faceman"
    41  		fakeConfig.BinaryNameReturns(binaryName)
    42  		app = "some-app"
    43  
    44  		cmd = V3RestartCommand{
    45  			RequiredArgs: flag.AppName{AppName: app},
    46  
    47  			UI:          testUI,
    48  			Config:      fakeConfig,
    49  			SharedActor: fakeSharedActor,
    50  			Actor:       fakeActor,
    51  		}
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("the API version is below the minimum", func() {
    59  		BeforeEach(func() {
    60  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion)
    61  		})
    62  
    63  		It("returns a MinimumAPIVersionNotMetError", func() {
    64  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    65  				CurrentVersion: ccversion.MinV3ClientVersion,
    66  				MinimumVersion: ccversion.MinVersionApplicationFlowV3,
    67  			}))
    68  		})
    69  
    70  		It("displays the experimental warning", func() {
    71  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    72  		})
    73  	})
    74  
    75  	When("checking target fails", func() {
    76  		BeforeEach(func() {
    77  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3)
    78  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    79  		})
    80  
    81  		It("returns an error", func() {
    82  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    83  
    84  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    85  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    86  			Expect(checkTargetedOrg).To(BeTrue())
    87  			Expect(checkTargetedSpace).To(BeTrue())
    88  		})
    89  	})
    90  
    91  	When("the user is not logged in", func() {
    92  		var expectedErr error
    93  
    94  		BeforeEach(func() {
    95  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3)
    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  	When("the user is logged in", func() {
   106  		BeforeEach(func() {
   107  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3)
   108  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
   109  				Name: "some-org",
   110  			})
   111  			fakeConfig.TargetedSpaceReturns(configv3.Space{
   112  				Name: "some-space",
   113  				GUID: "some-space-guid",
   114  			})
   115  			fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
   116  		})
   117  
   118  		When("stop app does not return an error", func() {
   119  			BeforeEach(func() {
   120  				fakeActor.StopApplicationReturns(v3action.Warnings{"stop-warning-1", "stop-warning-2"}, nil)
   121  			})
   122  
   123  			When("start app does not return an error", func() {
   124  				BeforeEach(func() {
   125  					fakeActor.StartApplicationReturns(v3action.Application{}, v3action.Warnings{"start-warning-1", "start-warning-2"}, nil)
   126  				})
   127  
   128  				When("get app does not return an error", func() {
   129  					Context("if the app was already started", func() {
   130  						BeforeEach(func() {
   131  							fakeActor.GetApplicationByNameAndSpaceReturns(v3action.Application{GUID: "some-app-guid", State: constant.ApplicationStarted}, v3action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   132  						})
   133  
   134  						It("says that the app was stopped, then started, and outputs warnings", func() {
   135  							Expect(executeErr).ToNot(HaveOccurred())
   136  
   137  							Expect(testUI.Err).To(Say("get-warning-1"))
   138  							Expect(testUI.Err).To(Say("get-warning-2"))
   139  
   140  							Expect(testUI.Out).To(Say(`Stopping app some-app in org some-org / space some-space as steve\.\.\.`))
   141  							Expect(testUI.Err).To(Say("stop-warning-1"))
   142  							Expect(testUI.Err).To(Say("stop-warning-2"))
   143  							Expect(testUI.Out).To(Say("OK"))
   144  
   145  							Expect(testUI.Out).To(Say(`Starting app some-app in org some-org / space some-space as steve\.\.\.`))
   146  							Expect(testUI.Err).To(Say("start-warning-1"))
   147  							Expect(testUI.Err).To(Say("start-warning-2"))
   148  							Expect(testUI.Out).To(Say("OK"))
   149  
   150  							Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   151  							appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   152  							Expect(appName).To(Equal("some-app"))
   153  							Expect(spaceGUID).To(Equal("some-space-guid"))
   154  
   155  							Expect(fakeActor.StopApplicationCallCount()).To(Equal(1))
   156  							appGUID := fakeActor.StopApplicationArgsForCall(0)
   157  							Expect(appGUID).To(Equal("some-app-guid"))
   158  
   159  							Expect(fakeActor.StartApplicationCallCount()).To(Equal(1))
   160  							appGUID = fakeActor.StartApplicationArgsForCall(0)
   161  							Expect(appGUID).To(Equal("some-app-guid"))
   162  						})
   163  					})
   164  
   165  					Context("if the app was not already started", func() {
   166  						BeforeEach(func() {
   167  							fakeActor.GetApplicationByNameAndSpaceReturns(v3action.Application{GUID: "some-app-guid", State: constant.ApplicationStopped}, v3action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   168  						})
   169  
   170  						It("says that the app was stopped, then started, and outputs warnings", func() {
   171  							Expect(executeErr).ToNot(HaveOccurred())
   172  
   173  							Expect(testUI.Err).To(Say("get-warning-1"))
   174  							Expect(testUI.Err).To(Say("get-warning-2"))
   175  
   176  							Expect(testUI.Out).ToNot(Say("Stopping"))
   177  							Expect(testUI.Err).ToNot(Say("stop-warning"))
   178  
   179  							Expect(testUI.Out).To(Say(`Starting app some-app in org some-org / space some-space as steve\.\.\.`))
   180  							Expect(testUI.Err).To(Say("start-warning-1"))
   181  							Expect(testUI.Err).To(Say("start-warning-2"))
   182  							Expect(testUI.Out).To(Say("OK"))
   183  
   184  							Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   185  							appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   186  							Expect(appName).To(Equal("some-app"))
   187  							Expect(spaceGUID).To(Equal("some-space-guid"))
   188  
   189  							Expect(fakeActor.StopApplicationCallCount()).To(BeZero(), "Expected StopApplication to not be called")
   190  
   191  							Expect(fakeActor.StartApplicationCallCount()).To(Equal(1))
   192  							appGUID := fakeActor.StartApplicationArgsForCall(0)
   193  							Expect(appGUID).To(Equal("some-app-guid"))
   194  						})
   195  					})
   196  				})
   197  
   198  				When("the get app call returns an error", func() {
   199  					Context("which is an ApplicationNotFoundError", func() {
   200  						BeforeEach(func() {
   201  							fakeActor.GetApplicationByNameAndSpaceReturns(v3action.Application{}, v3action.Warnings{"get-warning-1", "get-warning-2"}, actionerror.ApplicationNotFoundError{Name: app})
   202  						})
   203  
   204  						It("says that the app wasn't found", func() {
   205  							Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   206  							Expect(testUI.Out).ToNot(Say("Stopping"))
   207  							Expect(testUI.Out).ToNot(Say("Starting"))
   208  
   209  							Expect(testUI.Err).To(Say("get-warning-1"))
   210  							Expect(testUI.Err).To(Say("get-warning-2"))
   211  
   212  							Expect(fakeActor.StopApplicationCallCount()).To(BeZero(), "Expected StopApplication to not be called")
   213  							Expect(fakeActor.StartApplicationCallCount()).To(BeZero(), "Expected StartApplication to not be called")
   214  						})
   215  
   216  						When("it is an unknown error", func() {
   217  							var expectedErr error
   218  
   219  							BeforeEach(func() {
   220  								expectedErr = errors.New("some get app error")
   221  								fakeActor.GetApplicationByNameAndSpaceReturns(v3action.Application{State: constant.ApplicationStopped}, v3action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr)
   222  							})
   223  
   224  							It("says that the app failed to start", func() {
   225  								Expect(executeErr).To(Equal(expectedErr))
   226  								Expect(testUI.Out).ToNot(Say("Stopping"))
   227  								Expect(testUI.Out).ToNot(Say("Starting"))
   228  
   229  								Expect(testUI.Err).To(Say("get-warning-1"))
   230  								Expect(testUI.Err).To(Say("get-warning-2"))
   231  
   232  								Expect(fakeActor.StopApplicationCallCount()).To(BeZero(), "Expected StopApplication to not be called")
   233  								Expect(fakeActor.StartApplicationCallCount()).To(BeZero(), "Expected StartApplication to not be called")
   234  							})
   235  						})
   236  					})
   237  				})
   238  			})
   239  
   240  			When("the start app call returns an error", func() {
   241  				BeforeEach(func() {
   242  					fakeActor.GetApplicationByNameAndSpaceReturns(v3action.Application{GUID: "some-app-guid", State: constant.ApplicationStarted}, v3action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   243  				})
   244  
   245  				Context("and the error is some random error", func() {
   246  					var expectedErr error
   247  
   248  					BeforeEach(func() {
   249  						expectedErr = errors.New("some start error")
   250  						fakeActor.StartApplicationReturns(v3action.Application{}, v3action.Warnings{"start-warning-1", "start-warning-2"}, expectedErr)
   251  					})
   252  
   253  					It("says that the app failed to start", func() {
   254  						Expect(executeErr).To(Equal(expectedErr))
   255  						Expect(testUI.Out).To(Say(`Starting app some-app in org some-org / space some-space as steve\.\.\.`))
   256  
   257  						Expect(testUI.Err).To(Say("get-warning-1"))
   258  						Expect(testUI.Err).To(Say("get-warning-2"))
   259  						Expect(testUI.Err).To(Say("start-warning-1"))
   260  						Expect(testUI.Err).To(Say("start-warning-2"))
   261  					})
   262  				})
   263  
   264  				When("the start app call returns an ApplicationNotFoundError (someone else deleted app after we fetched app)", func() {
   265  					BeforeEach(func() {
   266  						fakeActor.StartApplicationReturns(v3action.Application{}, v3action.Warnings{"start-warning-1", "start-warning-2"}, actionerror.ApplicationNotFoundError{Name: app})
   267  					})
   268  
   269  					It("says that the app failed to start", func() {
   270  						Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   271  						Expect(testUI.Out).To(Say(`Starting app some-app in org some-org / space some-space as steve\.\.\.`))
   272  
   273  						Expect(testUI.Err).To(Say("get-warning-1"))
   274  						Expect(testUI.Err).To(Say("get-warning-2"))
   275  						Expect(testUI.Err).To(Say("start-warning-1"))
   276  						Expect(testUI.Err).To(Say("start-warning-2"))
   277  					})
   278  				})
   279  			})
   280  		})
   281  
   282  		When("the stop app call returns an error", func() {
   283  			BeforeEach(func() {
   284  				fakeActor.GetApplicationByNameAndSpaceReturns(v3action.Application{GUID: "some-app-guid", State: constant.ApplicationStarted}, v3action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   285  			})
   286  
   287  			Context("and the error is some random error", func() {
   288  				var expectedErr error
   289  
   290  				BeforeEach(func() {
   291  					expectedErr = errors.New("some stop error")
   292  					fakeActor.StopApplicationReturns(v3action.Warnings{"stop-warning-1", "stop-warning-2"}, expectedErr)
   293  				})
   294  
   295  				It("says that the app failed to start", func() {
   296  					Expect(executeErr).To(Equal(expectedErr))
   297  					Expect(testUI.Out).To(Say(`Stopping app some-app in org some-org / space some-space as steve\.\.\.`))
   298  
   299  					Expect(testUI.Err).To(Say("get-warning-1"))
   300  					Expect(testUI.Err).To(Say("get-warning-2"))
   301  					Expect(testUI.Err).To(Say("stop-warning-1"))
   302  					Expect(testUI.Err).To(Say("stop-warning-2"))
   303  
   304  					Expect(fakeActor.StartApplicationCallCount()).To(BeZero(), "Expected StartApplication to not be called")
   305  				})
   306  			})
   307  
   308  			When("the stop app call returns a ApplicationNotFoundError (someone else deleted app after we fetched summary)", func() {
   309  				BeforeEach(func() {
   310  					fakeActor.StopApplicationReturns(v3action.Warnings{"stop-warning-1", "stop-warning-2"}, actionerror.ApplicationNotFoundError{Name: app})
   311  				})
   312  
   313  				It("says that the app failed to start", func() {
   314  					Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   315  					Expect(testUI.Out).To(Say(`Stopping app some-app in org some-org / space some-space as steve\.\.\.`))
   316  
   317  					Expect(testUI.Err).To(Say("get-warning-1"))
   318  					Expect(testUI.Err).To(Say("get-warning-2"))
   319  					Expect(testUI.Err).To(Say("stop-warning-1"))
   320  					Expect(testUI.Err).To(Say("stop-warning-2"))
   321  
   322  					Expect(fakeActor.StartApplicationCallCount()).To(BeZero(), "Expected StartApplication to not be called")
   323  				})
   324  			})
   325  		})
   326  	})
   327  })