github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/v3_zdt_restart_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     7  	"code.cloudfoundry.org/cli/command/translatableerror"
     8  
     9  	"code.cloudfoundry.org/cli/actor/actionerror"
    10  	"code.cloudfoundry.org/cli/actor/v3action"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    12  	"code.cloudfoundry.org/cli/command/commandfakes"
    13  	"code.cloudfoundry.org/cli/command/flag"
    14  	. "code.cloudfoundry.org/cli/command/v6"
    15  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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("v3-zdt-restart Command", func() {
    24  	var (
    25  		cmd             V3ZeroDowntimeRestartCommand
    26  		testUI          *ui.UI
    27  		fakeConfig      *commandfakes.FakeConfig
    28  		fakeSharedActor *commandfakes.FakeSharedActor
    29  		fakeActor       *v6fakes.FakeV3ZeroDowntimeRestartActor
    30  		binaryName      string
    31  		executeErr      error
    32  		app             string
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    37  		fakeConfig = new(commandfakes.FakeConfig)
    38  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    39  		fakeActor = new(v6fakes.FakeV3ZeroDowntimeRestartActor)
    40  
    41  		binaryName = "faceman"
    42  		fakeConfig.BinaryNameReturns(binaryName)
    43  		app = "some-app"
    44  
    45  		cmd = V3ZeroDowntimeRestartCommand{
    46  			RequiredArgs: flag.AppName{AppName: app},
    47  
    48  			UI:          testUI,
    49  			Config:      fakeConfig,
    50  			SharedActor: fakeSharedActor,
    51  			Actor:       fakeActor,
    52  		}
    53  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinSupportedV3ClientVersion)
    54  	})
    55  
    56  	JustBeforeEach(func() {
    57  		executeErr = cmd.Execute(nil)
    58  	})
    59  
    60  	When("the API version is below the minimum", func() {
    61  		olderCurrentVersion := "3.0.1"
    62  
    63  		BeforeEach(func() {
    64  			fakeActor.CloudControllerAPIVersionReturns(olderCurrentVersion)
    65  		})
    66  
    67  		It("returns a MinimumAPIVersionNotMetError", func() {
    68  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    69  				CurrentVersion: olderCurrentVersion,
    70  				MinimumVersion: ccversion.MinSupportedV3ClientVersion,
    71  			}))
    72  		})
    73  	})
    74  
    75  	It("displays the experimental warning", func() {
    76  		Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    77  	})
    78  
    79  	When("checking target fails", func() {
    80  		BeforeEach(func() {
    81  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    82  		})
    83  
    84  		It("returns an error", func() {
    85  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    86  
    87  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    88  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    89  			Expect(checkTargetedOrg).To(BeTrue())
    90  			Expect(checkTargetedSpace).To(BeTrue())
    91  		})
    92  	})
    93  
    94  	When("the user is not logged in", func() {
    95  		var expectedErr error
    96  
    97  		BeforeEach(func() {
    98  			expectedErr = errors.New("some current user error")
    99  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   100  		})
   101  
   102  		It("return an error", func() {
   103  			Expect(executeErr).To(Equal(expectedErr))
   104  		})
   105  	})
   106  
   107  	When("the user is logged in", func() {
   108  		BeforeEach(func() {
   109  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
   110  				Name: "some-org",
   111  			})
   112  			fakeConfig.TargetedSpaceReturns(configv3.Space{
   113  				Name: "some-space",
   114  				GUID: "some-space-guid",
   115  			})
   116  			fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
   117  		})
   118  
   119  		When("the app exists", func() {
   120  			BeforeEach(func() {
   121  				fakeActor.GetApplicationByNameAndSpaceReturns(
   122  					v3action.Application{
   123  						GUID:  "some-app-guid",
   124  						State: constant.ApplicationStarted,
   125  					}, v3action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   126  				fakeActor.CreateDeploymentReturns("", v3action.Warnings{"deploy-warning-1", "deploy-warning-2"}, nil)
   127  			})
   128  
   129  			It("makes a deployment for the app", func() {
   130  				Expect(executeErr).ToNot(HaveOccurred())
   131  				Expect(fakeActor.CreateDeploymentCallCount()).To(Equal(1))
   132  
   133  				Expect(testUI.Err).To(Say("deploy-warning-1"))
   134  				Expect(testUI.Err).To(Say("deploy-warning-2"))
   135  				Expect(testUI.Out).To(Say("Starting deployment for app some-app in org some-org / space some-space as steve..."))
   136  				Expect(testUI.Out).To(Say("Waiting for app to start..."))
   137  				Expect(testUI.Out).To(Say("OK"))
   138  
   139  				Expect(fakeActor.ZeroDowntimePollStartCallCount()).To(Equal(1))
   140  			})
   141  
   142  			When("the app is stopped", func() {
   143  				BeforeEach(func() {
   144  					fakeActor.GetApplicationByNameAndSpaceReturns(
   145  						v3action.Application{
   146  							GUID:  "some-app-guid",
   147  							State: constant.ApplicationStopped,
   148  						}, v3action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   149  
   150  					fakeActor.StartApplicationReturns(v3action.Warnings{"start-warning-1", "start-warning-2"}, nil)
   151  				})
   152  
   153  				It("starts the app", func() {
   154  					Expect(testUI.Out).To(Say(`Starting app some-app in org some-org / space some-space as steve\.\.\.`))
   155  					Expect(testUI.Err).To(Say("start-warning-1"))
   156  					Expect(testUI.Err).To(Say("start-warning-2"))
   157  					Expect(testUI.Out).To(Say("OK"))
   158  
   159  					Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   160  					appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   161  					Expect(appName).To(Equal("some-app"))
   162  					Expect(spaceGUID).To(Equal("some-space-guid"))
   163  
   164  					Expect(fakeActor.StartApplicationCallCount()).To(Equal(1))
   165  					appGUID := fakeActor.StartApplicationArgsForCall(0)
   166  					Expect(appGUID).To(Equal("some-app-guid"))
   167  				})
   168  
   169  				When("it fails to start the app", func() {
   170  					BeforeEach(func() {
   171  						fakeActor.StartApplicationReturns(nil, errors.New("lol error"))
   172  					})
   173  
   174  					It("returns the error", func() {
   175  						Expect(executeErr).To(MatchError("lol error"))
   176  					})
   177  				})
   178  			})
   179  
   180  			When("the app fails to start", func() {
   181  				BeforeEach(func() {
   182  					fakeActor.ZeroDowntimePollStartReturns(errors.New("lol error"))
   183  				})
   184  
   185  				It("returns the error", func() {
   186  					Expect(executeErr).To(MatchError("lol error"))
   187  				})
   188  			})
   189  		})
   190  
   191  		When("it fails to get the app", func() {
   192  			When("the app isn't found", func() {
   193  				BeforeEach(func() {
   194  					fakeActor.GetApplicationByNameAndSpaceReturns(v3action.Application{}, v3action.Warnings{"get-warning-1", "get-warning-2"}, actionerror.ApplicationNotFoundError{Name: app})
   195  				})
   196  
   197  				It("says that the app wasn't found", func() {
   198  					Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   199  					Expect(testUI.Out).ToNot(Say("Stopping"))
   200  					Expect(testUI.Out).ToNot(Say("Starting"))
   201  
   202  					Expect(testUI.Err).To(Say("get-warning-1"))
   203  					Expect(testUI.Err).To(Say("get-warning-2"))
   204  
   205  					Expect(fakeActor.StartApplicationCallCount()).To(BeZero(), "Expected StartApplication to not be called")
   206  					Expect(fakeActor.CreateDeploymentCallCount()).To(BeZero(), "Expected CreateDeployment to not be called")
   207  				})
   208  			})
   209  
   210  			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: constant.ApplicationStopped}, 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.StartApplicationCallCount()).To(BeZero(), "Expected StartApplication to not be called")
   227  					Expect(fakeActor.CreateDeploymentCallCount()).To(BeZero(), "Expected CreateDeployment to not be called")
   228  				})
   229  			})
   230  		})
   231  	})
   232  })