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