github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/terminate_task_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/resources"
    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("terminate-task Command", func() {
    22  	var (
    23  		cmd             TerminateTaskCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		binaryName      string
    29  		executeErr      error
    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(v7fakes.FakeActor)
    37  
    38  		cmd = TerminateTaskCommand{
    39  			BaseCommand: BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeActor,
    44  			},
    45  		}
    46  
    47  		cmd.RequiredArgs.AppName = "some-app-name"
    48  		cmd.RequiredArgs.SequenceID = "1"
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("the task id argument is not an integer", func() {
    59  		BeforeEach(func() {
    60  			cmd.RequiredArgs.SequenceID = "not-an-integer"
    61  		})
    62  
    63  		It("returns an ParseArgumentError", func() {
    64  			Expect(executeErr).To(MatchError(translatableerror.ParseArgumentError{
    65  				ArgumentName: "TASK_ID",
    66  				ExpectedType: "integer",
    67  			}))
    68  		})
    69  	})
    70  
    71  	When("checking target fails", func() {
    72  		BeforeEach(func() {
    73  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    74  		})
    75  
    76  		It("returns an error", func() {
    77  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    78  
    79  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    80  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    81  			Expect(checkTargetedOrg).To(BeTrue())
    82  			Expect(checkTargetedSpace).To(BeTrue())
    83  		})
    84  	})
    85  
    86  	When("the user is logged in, and a space and org are targeted", func() {
    87  		BeforeEach(func() {
    88  			fakeConfig.HasTargetedOrganizationReturns(true)
    89  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    90  				GUID: "some-org-guid",
    91  				Name: "some-org",
    92  			})
    93  			fakeConfig.HasTargetedSpaceReturns(true)
    94  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    95  				GUID: "some-space-guid",
    96  				Name: "some-space",
    97  			})
    98  		})
    99  
   100  		When("getting the current user returns an error", func() {
   101  			var expectedErr error
   102  
   103  			BeforeEach(func() {
   104  				expectedErr = errors.New("get current user error")
   105  				fakeConfig.CurrentUserReturns(
   106  					configv3.User{},
   107  					expectedErr)
   108  			})
   109  
   110  			It("returns the error", func() {
   111  				Expect(executeErr).To(MatchError(expectedErr))
   112  			})
   113  		})
   114  
   115  		When("getting the current user does not return an error", func() {
   116  			BeforeEach(func() {
   117  				fakeConfig.CurrentUserReturns(
   118  					configv3.User{Name: "some-user"},
   119  					nil)
   120  			})
   121  
   122  			When("provided a valid application name and task sequence ID", func() {
   123  				BeforeEach(func() {
   124  					fakeActor.GetApplicationByNameAndSpaceReturns(
   125  						resources.Application{GUID: "some-app-guid"},
   126  						v7action.Warnings{"get-application-warning"},
   127  						nil)
   128  					fakeActor.GetTaskBySequenceIDAndApplicationReturns(
   129  						v7action.Task{GUID: "some-task-guid"},
   130  						v7action.Warnings{"get-task-warning"},
   131  						nil)
   132  					fakeActor.TerminateTaskReturns(
   133  						v7action.Task{},
   134  						v7action.Warnings{"terminate-task-warning"},
   135  						nil)
   136  				})
   137  
   138  				It("cancels the task and displays all warnings", func() {
   139  					Expect(executeErr).ToNot(HaveOccurred())
   140  
   141  					Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   142  					appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   143  					Expect(appName).To(Equal("some-app-name"))
   144  					Expect(spaceGUID).To(Equal("some-space-guid"))
   145  
   146  					Expect(fakeActor.GetTaskBySequenceIDAndApplicationCallCount()).To(Equal(1))
   147  					sequenceID, applicationGUID := fakeActor.GetTaskBySequenceIDAndApplicationArgsForCall(0)
   148  					Expect(sequenceID).To(Equal(1))
   149  					Expect(applicationGUID).To(Equal("some-app-guid"))
   150  
   151  					Expect(fakeActor.TerminateTaskCallCount()).To(Equal(1))
   152  					taskGUID := fakeActor.TerminateTaskArgsForCall(0)
   153  					Expect(taskGUID).To(Equal("some-task-guid"))
   154  
   155  					Expect(testUI.Err).To(Say("get-application-warning"))
   156  					Expect(testUI.Err).To(Say("get-task-warning"))
   157  					Expect(testUI.Out).To(Say("Terminating task 1 of app some-app-name in org some-org / space some-space as some-user..."))
   158  					Expect(testUI.Err).To(Say("terminate-task-warning"))
   159  					Expect(testUI.Out).To(Say("OK"))
   160  				})
   161  			})
   162  
   163  			When("there are errors", func() {
   164  				When("the error is translatable", func() {
   165  					var (
   166  						returnedErr error
   167  						expectedErr error
   168  					)
   169  
   170  					BeforeEach(func() {
   171  						expectedErr = errors.New("request-error")
   172  						returnedErr = ccerror.RequestError{Err: expectedErr}
   173  					})
   174  
   175  					When("getting the app returns the error", func() {
   176  						BeforeEach(func() {
   177  							fakeActor.GetApplicationByNameAndSpaceReturns(
   178  								resources.Application{GUID: "some-app-guid"},
   179  								nil,
   180  								returnedErr)
   181  						})
   182  
   183  						It("returns a translatable error", func() {
   184  							Expect(executeErr).To(MatchError(ccerror.RequestError{Err: expectedErr}))
   185  						})
   186  					})
   187  
   188  					When("getting the task returns the error", func() {
   189  						BeforeEach(func() {
   190  							fakeActor.GetApplicationByNameAndSpaceReturns(
   191  								resources.Application{GUID: "some-app-guid"},
   192  								nil,
   193  								nil)
   194  							fakeActor.GetTaskBySequenceIDAndApplicationReturns(
   195  								v7action.Task{},
   196  								nil,
   197  								returnedErr)
   198  						})
   199  
   200  						It("returns a translatable error", func() {
   201  							Expect(executeErr).To(MatchError(ccerror.RequestError{Err: expectedErr}))
   202  						})
   203  					})
   204  
   205  					When("terminating the task returns the error", func() {
   206  						BeforeEach(func() {
   207  							fakeActor.GetApplicationByNameAndSpaceReturns(
   208  								resources.Application{GUID: "some-app-guid"},
   209  								nil,
   210  								nil)
   211  							fakeActor.GetTaskBySequenceIDAndApplicationReturns(
   212  								v7action.Task{GUID: "some-task-guid"},
   213  								nil,
   214  								nil)
   215  							fakeActor.TerminateTaskReturns(
   216  								v7action.Task{GUID: "some-task-guid"},
   217  								nil,
   218  								returnedErr)
   219  						})
   220  
   221  						It("returns a translatable error", func() {
   222  							Expect(executeErr).To(MatchError(ccerror.RequestError{Err: expectedErr}))
   223  						})
   224  					})
   225  				})
   226  
   227  				When("the error is not translatable", func() {
   228  					var expectedErr error
   229  
   230  					BeforeEach(func() {
   231  						expectedErr = errors.New("bananapants")
   232  					})
   233  
   234  					When("getting the app returns the error", func() {
   235  						BeforeEach(func() {
   236  							fakeActor.GetApplicationByNameAndSpaceReturns(
   237  								resources.Application{GUID: "some-app-guid"},
   238  								v7action.Warnings{"get-application-warning-1", "get-application-warning-2"},
   239  								expectedErr)
   240  						})
   241  
   242  						It("return the error and outputs all warnings", func() {
   243  							Expect(executeErr).To(MatchError(expectedErr))
   244  
   245  							Expect(testUI.Err).To(Say("get-application-warning-1"))
   246  							Expect(testUI.Err).To(Say("get-application-warning-2"))
   247  						})
   248  					})
   249  
   250  					When("getting the task returns the error", func() {
   251  						BeforeEach(func() {
   252  							fakeActor.GetApplicationByNameAndSpaceReturns(
   253  								resources.Application{GUID: "some-app-guid"},
   254  								nil,
   255  								nil)
   256  							fakeActor.GetTaskBySequenceIDAndApplicationReturns(
   257  								v7action.Task{},
   258  								v7action.Warnings{"get-task-warning-1", "get-task-warning-2"},
   259  								expectedErr)
   260  						})
   261  
   262  						It("return the error and outputs all warnings", func() {
   263  							Expect(executeErr).To(MatchError(expectedErr))
   264  
   265  							Expect(testUI.Err).To(Say("get-task-warning-1"))
   266  							Expect(testUI.Err).To(Say("get-task-warning-2"))
   267  						})
   268  					})
   269  
   270  					When("terminating the task returns the error", func() {
   271  						BeforeEach(func() {
   272  							fakeActor.GetApplicationByNameAndSpaceReturns(
   273  								resources.Application{GUID: "some-app-guid"},
   274  								nil,
   275  								nil)
   276  							fakeActor.GetTaskBySequenceIDAndApplicationReturns(
   277  								v7action.Task{GUID: "some-task-guid"},
   278  								nil,
   279  								nil)
   280  							fakeActor.TerminateTaskReturns(
   281  								v7action.Task{},
   282  								v7action.Warnings{"terminate-task-warning-1", "terminate-task-warning-2"},
   283  								expectedErr)
   284  						})
   285  
   286  						It("returns the error and outputs all warnings", func() {
   287  							Expect(executeErr).To(MatchError(expectedErr))
   288  
   289  							Expect(testUI.Err).To(Say("terminate-task-warning-1"))
   290  							Expect(testUI.Err).To(Say("terminate-task-warning-2"))
   291  						})
   292  					})
   293  				})
   294  			})
   295  		})
   296  	})
   297  })