github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_restart_command_test.go (about)

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