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