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