github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/task_examiner/task_examiner_test.go (about) 1 package task_examiner_test 2 3 import ( 4 "errors" 5 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 9 "github.com/cloudfoundry-incubator/ltc/task_examiner" 10 "github.com/cloudfoundry-incubator/receptor" 11 "github.com/cloudfoundry-incubator/receptor/fake_receptor" 12 ) 13 14 var _ = Describe("TaskExaminer", func() { 15 var ( 16 fakeReceptorClient *fake_receptor.FakeClient 17 taskExaminer task_examiner.TaskExaminer 18 ) 19 20 BeforeEach(func() { 21 fakeReceptorClient = &fake_receptor.FakeClient{} 22 taskExaminer = task_examiner.New(fakeReceptorClient) 23 }) 24 25 Describe("TaskStatus", func() { 26 BeforeEach(func() { 27 getTaskResponse := receptor.TaskResponse{ 28 TaskGuid: "boop", 29 State: receptor.TaskStateCompleted, 30 CellID: "cell-01", 31 Failed: false, 32 FailureReason: "", 33 Result: "some-result", 34 } 35 fakeReceptorClient.GetTaskReturns(getTaskResponse, nil) 36 }) 37 38 It("returns a task status", func() { 39 taskInfo, err := taskExaminer.TaskStatus("boop") 40 Expect(err).NotTo(HaveOccurred()) 41 Expect(taskInfo.TaskGuid).To(Equal("boop")) 42 Expect(taskInfo.State).To(Equal(receptor.TaskStateCompleted)) 43 Expect(taskInfo.CellID).To(Equal("cell-01")) 44 Expect(taskInfo.Failed).To(BeFalse()) 45 Expect(taskInfo.FailureReason).To(BeEmpty()) 46 Expect(taskInfo.Result).To(Equal("some-result")) 47 48 Expect(fakeReceptorClient.GetTaskCallCount()).To(Equal(1)) 49 Expect(fakeReceptorClient.GetTaskArgsForCall(0)).To(Equal("boop")) 50 }) 51 52 Context("when the receptor returns errors", func() { 53 It("returns exists false for TaskNotFound", func() { 54 receptorError := receptor.Error{Type: receptor.TaskNotFound, Message: "could not locate this"} 55 fakeReceptorClient.GetTaskReturns(receptor.TaskResponse{}, receptorError) 56 57 _, err := taskExaminer.TaskStatus("boop1") 58 Expect(err).To(MatchError(task_examiner.TaskNotFoundErrorMessage)) 59 }) 60 61 It("bubbles up error for receptor Error anything but TaskNotFound", func() { 62 receptorError := receptor.Error{Type: receptor.TaskGuidAlreadyExists, Message: "could not locate this"} 63 fakeReceptorClient.GetTaskReturns(receptor.TaskResponse{}, receptorError) 64 65 _, err := taskExaminer.TaskStatus("boop1") 66 Expect(err).To(MatchError(receptorError)) 67 }) 68 69 It("bubbles up error for non-receptor error", func() { 70 fakeReceptorClient.GetTaskReturns(receptor.TaskResponse{}, errors.New("you done goofed")) 71 72 _, err := taskExaminer.TaskStatus("boop") 73 Expect(err).To(MatchError("you done goofed")) 74 }) 75 }) 76 }) 77 78 Describe("ListTasks", func() { 79 It("returns the list of task", func() { 80 taskListReturns := []receptor.TaskResponse{ 81 { 82 TaskGuid: "task-guid-1", 83 CellID: "cell-01", 84 Failed: false, 85 FailureReason: "", 86 Result: "Finished", 87 State: "COMPLETED", 88 }, 89 { 90 TaskGuid: "task-guid-2", 91 CellID: "cell-02", 92 Failed: true, 93 FailureReason: "failed", 94 Result: "Failed", 95 State: "COMPLETED", 96 }, 97 } 98 fakeReceptorClient.TasksReturns(taskListReturns, nil) 99 100 taskList, err := taskExaminer.ListTasks() 101 Expect(err).NotTo(HaveOccurred()) 102 Expect(taskList).To(HaveLen(2)) 103 104 task1 := taskList[0] 105 Expect(task1.TaskGuid).To(Equal("task-guid-1")) 106 Expect(task1.CellID).To(Equal("cell-01")) 107 Expect(task1.FailureReason).To(Equal("")) 108 Expect(task1.Result).To(Equal("Finished")) 109 Expect(task1.State).To(Equal("COMPLETED")) 110 111 task2 := taskList[1] 112 Expect(task2.TaskGuid).To(Equal("task-guid-2")) 113 Expect(task2.CellID).To(Equal("cell-02")) 114 Expect(task2.Result).To(Equal("Failed")) 115 }) 116 117 It("when receptor returns error", func() { 118 fakeReceptorClient.TasksReturns(nil, errors.New("Client not reachable.")) 119 120 _, err := taskExaminer.ListTasks() 121 Expect(err).To(MatchError("Client not reachable.")) 122 }) 123 124 It("when receptor returns empty list", func() { 125 fakeReceptorClient.TasksReturns([]receptor.TaskResponse{}, nil) 126 127 taskList, err := taskExaminer.ListTasks() 128 Expect(err).NotTo(HaveOccurred()) 129 Expect(taskList).To(HaveLen(0)) 130 }) 131 }) 132 })