github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/task_runner/command_factory/task_runner_command_factory_test.go (about) 1 package command_factory_test 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/onsi/gomega/gbytes" 12 13 "github.com/cloudfoundry-incubator/ltc/exit_handler/exit_codes" 14 "github.com/cloudfoundry-incubator/ltc/exit_handler/fake_exit_handler" 15 "github.com/cloudfoundry-incubator/ltc/task_examiner" 16 "github.com/cloudfoundry-incubator/ltc/task_examiner/fake_task_examiner" 17 "github.com/cloudfoundry-incubator/ltc/task_runner/command_factory" 18 "github.com/cloudfoundry-incubator/ltc/task_runner/fake_task_runner" 19 "github.com/cloudfoundry-incubator/ltc/terminal" 20 "github.com/cloudfoundry-incubator/ltc/terminal/colors" 21 "github.com/cloudfoundry-incubator/ltc/test_helpers" 22 "github.com/codegangsta/cli" 23 ) 24 25 var _ = Describe("TaskRunner CommandFactory", func() { 26 var ( 27 outputBuffer *gbytes.Buffer 28 terminalUI terminal.UI 29 fakeTaskRunner *fake_task_runner.FakeTaskRunner 30 fakeTaskExaminer *fake_task_examiner.FakeTaskExaminer 31 fakeExitHandler *fake_exit_handler.FakeExitHandler 32 ) 33 34 BeforeEach(func() { 35 outputBuffer = gbytes.NewBuffer() 36 terminalUI = terminal.NewUI(nil, outputBuffer, nil) 37 fakeTaskRunner = &fake_task_runner.FakeTaskRunner{} 38 fakeTaskExaminer = &fake_task_examiner.FakeTaskExaminer{} 39 fakeExitHandler = &fake_exit_handler.FakeExitHandler{} 40 }) 41 42 Describe("SubmitTaskCommand", func() { 43 var ( 44 submitTaskCommand cli.Command 45 tmpFile *os.File 46 jsonContents []byte 47 ) 48 49 BeforeEach(func() { 50 commandFactory := command_factory.NewTaskRunnerCommandFactory(fakeTaskRunner, terminalUI, fakeExitHandler) 51 submitTaskCommand = commandFactory.MakeSubmitTaskCommand() 52 }) 53 54 Context("when the json file exists", func() { 55 BeforeEach(func() { 56 var err error 57 tmpFile, err = ioutil.TempFile("", "tmp_json") 58 Expect(err).ToNot(HaveOccurred()) 59 60 jsonContents = []byte(`{"Value":"test value"}`) 61 Expect(ioutil.WriteFile(tmpFile.Name(), jsonContents, 0700)).To(Succeed()) 62 }) 63 64 It("submits a task from json", func() { 65 fakeTaskRunner.SubmitTaskReturns("some-task", nil) 66 67 args := []string{tmpFile.Name()} 68 test_helpers.ExecuteCommandWithArgs(submitTaskCommand, args) 69 70 Expect(outputBuffer).To(test_helpers.SayLine(colors.Green("Successfully submitted some-task"))) 71 Expect(fakeTaskRunner.SubmitTaskCallCount()).To(Equal(1)) 72 Expect(fakeTaskRunner.SubmitTaskArgsForCall(0)).To(Equal(jsonContents)) 73 }) 74 75 It("prints an error returned by the task_runner", func() { 76 fakeTaskRunner.SubmitTaskReturns("some-task", errors.New("taskypoo")) 77 78 args := []string{tmpFile.Name()} 79 test_helpers.ExecuteCommandWithArgs(submitTaskCommand, args) 80 81 Expect(fakeTaskRunner.SubmitTaskCallCount()).To(Equal(1)) 82 Expect(fakeTaskRunner.SubmitTaskArgsForCall(0)).To(Equal(jsonContents)) 83 84 Expect(outputBuffer).To(test_helpers.SayLine("Error submitting some-task: taskypoo")) 85 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 86 }) 87 88 }) 89 90 It("is an error when no path is passed in", func() { 91 test_helpers.ExecuteCommandWithArgs(submitTaskCommand, []string{}) 92 93 Expect(outputBuffer).To(test_helpers.SayLine("Path to JSON is required")) 94 Expect(fakeTaskRunner.SubmitTaskCallCount()).To(BeZero()) 95 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.InvalidSyntax})) 96 }) 97 98 Context("when the file cannot be read", func() { 99 It("prints an error", func() { 100 args := []string{filepath.Join(os.TempDir(), "file-no-existy")} 101 102 test_helpers.ExecuteCommandWithArgs(submitTaskCommand, args) 103 104 Expect(outputBuffer).To(test_helpers.Say("Error reading file")) 105 Expect(fakeTaskRunner.SubmitTaskCallCount()).To(Equal(0)) 106 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.FileSystemError})) 107 }) 108 }) 109 110 }) 111 112 Describe("DeleteTaskCommand", func() { 113 var deleteTaskCommand cli.Command 114 115 BeforeEach(func() { 116 commandFactory := command_factory.NewTaskRunnerCommandFactory(fakeTaskRunner, terminalUI, fakeExitHandler) 117 deleteTaskCommand = commandFactory.MakeDeleteTaskCommand() 118 }) 119 120 It("Deletes the given task", func() { 121 taskInfo := task_examiner.TaskInfo{ 122 TaskGuid: "task-guid-1", 123 State: "COMPLETED", 124 } 125 fakeTaskExaminer.TaskStatusReturns(taskInfo, nil) 126 fakeTaskRunner.DeleteTaskReturns(nil) 127 test_helpers.ExecuteCommandWithArgs(deleteTaskCommand, []string{"task-guid-1"}) 128 129 Expect(outputBuffer).To(test_helpers.SayLine(colors.Green("OK"))) 130 }) 131 132 It("returns error when fail to delete the task", func() { 133 taskInfo := task_examiner.TaskInfo{ 134 TaskGuid: "task-guid-1", 135 State: "COMPLETED", 136 } 137 fakeTaskExaminer.TaskStatusReturns(taskInfo, nil) 138 fakeTaskRunner.DeleteTaskReturns(errors.New("task in unknown state")) 139 140 test_helpers.ExecuteCommandWithArgs(deleteTaskCommand, []string{"task-guid-1"}) 141 142 Expect(outputBuffer).To(test_helpers.SayLine(colors.Red("Error deleting task-guid-1: " + "task in unknown state"))) 143 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 144 }) 145 146 It("fails with usage", func() { 147 test_helpers.ExecuteCommandWithArgs(deleteTaskCommand, []string{}) 148 149 Expect(outputBuffer).To(test_helpers.SayLine("Please input a valid <task-guid>")) 150 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.InvalidSyntax})) 151 }) 152 }) 153 154 Describe("CancelTaskCommand", func() { 155 var cancelTaskCommand cli.Command 156 157 BeforeEach(func() { 158 commandFactory := command_factory.NewTaskRunnerCommandFactory(fakeTaskRunner, terminalUI, fakeExitHandler) 159 cancelTaskCommand = commandFactory.MakeCancelTaskCommand() 160 }) 161 162 It("Cancels the given task", func() { 163 taskInfo := task_examiner.TaskInfo{ 164 TaskGuid: "task-guid-1", 165 State: "COMPLETED", 166 } 167 fakeTaskExaminer.TaskStatusReturns(taskInfo, nil) 168 169 test_helpers.ExecuteCommandWithArgs(cancelTaskCommand, []string{"task-guid-1"}) 170 171 Expect(outputBuffer).To(test_helpers.SayLine(colors.Green("OK"))) 172 }) 173 174 It("returns error when fail to cancel the task", func() { 175 taskInfo := task_examiner.TaskInfo{ 176 TaskGuid: "task-guid-1", 177 State: "COMPLETED", 178 } 179 fakeTaskExaminer.TaskStatusReturns(taskInfo, nil) 180 fakeTaskRunner.CancelTaskReturns(errors.New("task in unknown state")) 181 182 test_helpers.ExecuteCommandWithArgs(cancelTaskCommand, []string{"task-guid-1"}) 183 184 Expect(outputBuffer).To(test_helpers.SayLine(colors.Red("Error cancelling task-guid-1: " + "task in unknown state"))) 185 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 186 }) 187 188 It("fails with usage", func() { 189 test_helpers.ExecuteCommandWithArgs(cancelTaskCommand, []string{}) 190 191 Expect(outputBuffer).To(test_helpers.SayLine("Please input a valid <task-guid>")) 192 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.InvalidSyntax})) 193 }) 194 }) 195 })