github.com/sleungcy-sap/cli@v7.1.0+incompatible/actor/v7action/task_test.go (about)

     1  package v7action_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/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Task Actions", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    24  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    25  	})
    26  
    27  	Describe("RunTask", func() {
    28  		When("the application exists", func() {
    29  			BeforeEach(func() {
    30  				fakeCloudControllerClient.CreateApplicationTaskReturns(
    31  					ccv3.Task{
    32  						SequenceID: 3,
    33  					},
    34  					ccv3.Warnings{
    35  						"warning-1",
    36  						"warning-2",
    37  					},
    38  					nil,
    39  				)
    40  			})
    41  
    42  			It("creates and returns the task and all warnings", func() {
    43  				expectedTask := Task{
    44  					Command:    "some command",
    45  					Name:       "some-task-name",
    46  					MemoryInMB: 123,
    47  					DiskInMB:   321,
    48  				}
    49  				task, warnings, err := actor.RunTask("some-app-guid", expectedTask)
    50  				Expect(err).ToNot(HaveOccurred())
    51  
    52  				Expect(task).To(Equal(Task{
    53  					SequenceID: 3,
    54  				}))
    55  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    56  
    57  				Expect(fakeCloudControllerClient.CreateApplicationTaskCallCount()).To(Equal(1))
    58  				appGUIDArg, taskArg := fakeCloudControllerClient.CreateApplicationTaskArgsForCall(0)
    59  				Expect(appGUIDArg).To(Equal("some-app-guid"))
    60  				Expect(taskArg).To(Equal(ccv3.Task(expectedTask)))
    61  			})
    62  		})
    63  
    64  		When("the cloud controller client returns an error", func() {
    65  			var warnings Warnings
    66  			var err error
    67  			var expectedErr error
    68  
    69  			JustBeforeEach(func() {
    70  				_, warnings, err = actor.RunTask("some-app-guid", Task{Command: "some command"})
    71  			})
    72  
    73  			When("the cloud controller error is generic", func() {
    74  				BeforeEach(func() {
    75  					expectedErr = errors.New("I am a CloudControllerClient Error")
    76  					fakeCloudControllerClient.CreateApplicationTaskReturns(
    77  						ccv3.Task{},
    78  						ccv3.Warnings{"warning-1", "warning-2"},
    79  						expectedErr,
    80  					)
    81  				})
    82  
    83  				It("returns the same error and all warnings", func() {
    84  					Expect(err).To(MatchError(expectedErr))
    85  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    86  				})
    87  			})
    88  
    89  			When("the error is a TaskWorkersUnavailableError", func() {
    90  				BeforeEach(func() {
    91  					fakeCloudControllerClient.CreateApplicationTaskReturns(
    92  						ccv3.Task{},
    93  						ccv3.Warnings{"warning-1", "warning-2"},
    94  						ccerror.TaskWorkersUnavailableError{Message: "banana babans"},
    95  					)
    96  				})
    97  
    98  				It("returns a TaskWorkersUnavailableError and all warnings", func() {
    99  					Expect(err).To(MatchError(actionerror.TaskWorkersUnavailableError{Message: "banana babans"}))
   100  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   101  				})
   102  			})
   103  		})
   104  	})
   105  
   106  	Describe("GetApplicationTasks", func() {
   107  		When("the application exists", func() {
   108  			When("there are associated tasks", func() {
   109  				var (
   110  					task1 ccv3.Task
   111  					task2 ccv3.Task
   112  					task3 ccv3.Task
   113  				)
   114  
   115  				BeforeEach(func() {
   116  					task1 = ccv3.Task{
   117  						GUID:       "task-1-guid",
   118  						SequenceID: 1,
   119  						Name:       "task-1",
   120  						State:      constant.TaskSucceeded,
   121  						CreatedAt:  "some-time",
   122  						Command:    "some-command",
   123  					}
   124  					task2 = ccv3.Task{
   125  						GUID:       "task-2-guid",
   126  						SequenceID: 2,
   127  						Name:       "task-2",
   128  						State:      constant.TaskFailed,
   129  						CreatedAt:  "some-time",
   130  						Command:    "some-command",
   131  					}
   132  					task3 = ccv3.Task{
   133  						GUID:       "task-3-guid",
   134  						SequenceID: 3,
   135  						Name:       "task-3",
   136  						State:      constant.TaskRunning,
   137  						CreatedAt:  "some-time",
   138  						Command:    "some-command",
   139  					}
   140  					fakeCloudControllerClient.GetApplicationTasksReturns(
   141  						[]ccv3.Task{task3, task1, task2},
   142  						ccv3.Warnings{"warning-1", "warning-2"},
   143  						nil,
   144  					)
   145  				})
   146  
   147  				It("returns all tasks associated with the application and all warnings", func() {
   148  					tasks, warnings, err := actor.GetApplicationTasks("some-app-guid", Descending)
   149  					Expect(err).ToNot(HaveOccurred())
   150  
   151  					Expect(tasks).To(Equal([]Task{Task(task3), Task(task2), Task(task1)}))
   152  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   153  
   154  					tasks, warnings, err = actor.GetApplicationTasks("some-app-guid", Ascending)
   155  					Expect(err).ToNot(HaveOccurred())
   156  
   157  					Expect(tasks).To(Equal([]Task{Task(task1), Task(task2), Task(task3)}))
   158  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   159  
   160  					Expect(fakeCloudControllerClient.GetApplicationTasksCallCount()).To(Equal(2))
   161  					appGUID, query := fakeCloudControllerClient.GetApplicationTasksArgsForCall(0)
   162  					Expect(appGUID).To(Equal("some-app-guid"))
   163  					Expect(query).To(BeNil())
   164  				})
   165  			})
   166  
   167  			When("there are no associated tasks", func() {
   168  				BeforeEach(func() {
   169  					fakeCloudControllerClient.GetApplicationTasksReturns(
   170  						[]ccv3.Task{},
   171  						nil,
   172  						nil,
   173  					)
   174  				})
   175  
   176  				It("returns an empty list of tasks", func() {
   177  					tasks, _, err := actor.GetApplicationTasks("some-app-guid", Descending)
   178  					Expect(err).ToNot(HaveOccurred())
   179  					Expect(tasks).To(BeEmpty())
   180  				})
   181  			})
   182  		})
   183  
   184  		When("the cloud controller client returns an error", func() {
   185  			var expectedErr error
   186  
   187  			BeforeEach(func() {
   188  				expectedErr = errors.New("I am a CloudControllerClient Error")
   189  				fakeCloudControllerClient.GetApplicationTasksReturns(
   190  					[]ccv3.Task{},
   191  					ccv3.Warnings{"warning-1", "warning-2"},
   192  					expectedErr,
   193  				)
   194  			})
   195  
   196  			It("returns the same error and all warnings", func() {
   197  				_, warnings, err := actor.GetApplicationTasks("some-app-guid", Descending)
   198  				Expect(err).To(MatchError(expectedErr))
   199  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   200  			})
   201  		})
   202  	})
   203  
   204  	Describe("GetTaskBySequenceIDAndApplication", func() {
   205  		When("the cloud controller client does not return an error", func() {
   206  			When("the task is found", func() {
   207  				var task1 ccv3.Task
   208  
   209  				BeforeEach(func() {
   210  					task1 = ccv3.Task{
   211  						GUID:       "task-1-guid",
   212  						SequenceID: 1,
   213  					}
   214  					fakeCloudControllerClient.GetApplicationTasksReturns(
   215  						[]ccv3.Task{task1},
   216  						ccv3.Warnings{"get-task-warning-1"},
   217  						nil,
   218  					)
   219  				})
   220  
   221  				It("returns the task and warnings", func() {
   222  					task, warnings, err := actor.GetTaskBySequenceIDAndApplication(1, "some-app-guid")
   223  					Expect(err).ToNot(HaveOccurred())
   224  					Expect(task).To(Equal(Task(task1)))
   225  					Expect(warnings).To(ConsistOf("get-task-warning-1"))
   226  				})
   227  			})
   228  
   229  			When("the task is not found", func() {
   230  				BeforeEach(func() {
   231  					fakeCloudControllerClient.GetApplicationTasksReturns(
   232  						[]ccv3.Task{},
   233  						ccv3.Warnings{"get-task-warning-1"},
   234  						nil,
   235  					)
   236  				})
   237  
   238  				It("returns a TaskNotFoundError and warnings", func() {
   239  					_, warnings, err := actor.GetTaskBySequenceIDAndApplication(1, "some-app-guid")
   240  					Expect(err).To(MatchError(actionerror.TaskNotFoundError{SequenceID: 1}))
   241  					Expect(warnings).To(ConsistOf("get-task-warning-1"))
   242  				})
   243  			})
   244  		})
   245  
   246  		When("the cloud controller client returns an error", func() {
   247  			var expectedErr error
   248  
   249  			BeforeEach(func() {
   250  				expectedErr = errors.New("generic-error")
   251  				fakeCloudControllerClient.GetApplicationTasksReturns(
   252  					[]ccv3.Task{},
   253  					ccv3.Warnings{"get-task-warning-1"},
   254  					expectedErr,
   255  				)
   256  			})
   257  
   258  			It("returns the same error and warnings", func() {
   259  				_, warnings, err := actor.GetTaskBySequenceIDAndApplication(1, "some-app-guid")
   260  				Expect(err).To(Equal(expectedErr))
   261  				Expect(warnings).To(ConsistOf("get-task-warning-1"))
   262  			})
   263  		})
   264  	})
   265  
   266  	Describe("TerminateTask", func() {
   267  		When("the task exists", func() {
   268  			var returnedTask ccv3.Task
   269  
   270  			BeforeEach(func() {
   271  				returnedTask = ccv3.Task{
   272  					GUID:       "some-task-guid",
   273  					SequenceID: 1,
   274  				}
   275  				fakeCloudControllerClient.UpdateTaskCancelReturns(
   276  					returnedTask,
   277  					ccv3.Warnings{"update-task-warning"},
   278  					nil)
   279  			})
   280  
   281  			It("returns the task and warnings", func() {
   282  				task, warnings, err := actor.TerminateTask("some-task-guid")
   283  				Expect(err).NotTo(HaveOccurred())
   284  				Expect(warnings).To(ConsistOf("update-task-warning"))
   285  				Expect(task).To(Equal(Task(returnedTask)))
   286  			})
   287  		})
   288  
   289  		When("the cloud controller returns an error", func() {
   290  			var expectedErr error
   291  
   292  			BeforeEach(func() {
   293  				expectedErr = errors.New("cc-error")
   294  				fakeCloudControllerClient.UpdateTaskCancelReturns(
   295  					ccv3.Task{},
   296  					ccv3.Warnings{"update-task-warning"},
   297  					expectedErr)
   298  			})
   299  
   300  			It("returns the same error and warnings", func() {
   301  				_, warnings, err := actor.TerminateTask("some-task-guid")
   302  				Expect(err).To(MatchError(expectedErr))
   303  				Expect(warnings).To(ConsistOf("update-task-warning"))
   304  			})
   305  		})
   306  	})
   307  })