func TestErrorOnNonZeroExitCode(t *testing.T) { mockJob := mock.CreateMockJob("IgnorePayload") config := TaskConfig{FunctionName: "name", FunctionCmd: "testscripts/nonZeroExit.sh", WarningLines: 5} response, err := config.Process(mockJob) assert.Nil(t, response) assert.EqualError(t, err, "exit status 2") }
func getSuccessResponseWithConfig(payload string, config TaskConfig, t *testing.T) string { mockJob := mock.CreateMockJob(payload) mockJob.GearmanHandle = "H:lap:123" _, err := config.Process(mockJob) assert.Nil(t, err) return string(mockJob.OutData()) }
func TestStderrCapturedWarningsOnFailedJobs(t *testing.T) { mockJob := mock.CreateMockJob("IgnorePayload") config := TaskConfig{FunctionName: "name", FunctionCmd: "testscripts/logStderrFail.sh", WarningLines: 2, ParseArgs: true} _, err := config.Process(mockJob) assert.Error(t, err) warnings := mockJob.Warnings() assert.Equal(t, warnings, [][]byte{[]byte("stderr7\n"), []byte("stderr8\n")}) }
func TestStderrCapturedInWarnings(t *testing.T) { mockJob := mock.CreateMockJob("IgnorePayload") config := TaskConfig{FunctionName: "name", FunctionCmd: "testscripts/logStderr.sh", WarningLines: 2, ParseArgs: true} _, err := config.Process(mockJob) assert.NoError(t, err) warnings := mockJob.Warnings() assert.Equal(t, 2, len(warnings)) assert.Equal(t, string(warnings[0]), "stderr7\n") assert.Equal(t, string(warnings[1]), "stderr8\n") }
// TestJobFuncConversion tests that our JobFunc is called when 'worker.fn' is called with a job. func TestJobFuncConversion(t *testing.T) { payload := "I'm a payload!" jobFunc := func(job Job) ([]byte, error) { if string(job.Data()) != payload { t.Fatalf("expected payload %s, received %s", payload, string(job.Data())) } return []byte{}, nil } worker := NewWorker("test", jobFunc) worker.fn(mock.CreateMockJob(payload)) }
func TestHandleStderrAndStdoutTogether(t *testing.T) { mockJob := mock.CreateMockJob("IgnorePayload") config := TaskConfig{FunctionName: "name", FunctionCmd: "testscripts/logStdoutAndStderr.sh", WarningLines: 5, ParseArgs: true} _, err := config.Process(mockJob) assert.NoError(t, err) warnings := mockJob.Warnings() if len(warnings) == 0 { t.Fatal("Empty warnings") } lastWarning := warnings[len(warnings)-1] assert.Equal(t, "stderr2\n", string(lastWarning)) assert.Equal(t, "stdout1\nstdout2\n", string(mockJob.OutData())) }
func TestSendStderrWarnings(t *testing.T) { stdErrStr := "" for i := 0; i < 30; i++ { stdErrStr += fmt.Sprintf("line #%d\n", i) } expectedStr := "" for i := 20; i < 30; i++ { expectedStr += fmt.Sprintf("line #%d\n", i) } mockJob := mock.CreateMockJob("") assert.Nil(t, sendStderrWarnings(bytes.NewBufferString(stdErrStr), mockJob, 10)) assert.Equal(t, expectedStr, string(bytes.Join(mockJob.GearmanWarnings, []byte{}))) }
func TestStderrCapturedWhenHanging(t *testing.T) { mockJob := mock.CreateMockJob("IgnorePayload") config := TaskConfig{ FunctionName: "name", FunctionCmd: "testscripts/stderrAndHang.sh", WarningLines: 2, ParseArgs: true, CmdTimeout: time.Second, } _, err := config.Process(mockJob) assert.EqualError(t, err, "process timed out after 1s") warnings := mockJob.Warnings() assert.Equal(t, 2, len(warnings)) assert.Equal(t, string(warnings[0]), "stderr7\n") assert.Equal(t, string(warnings[1]), "stderr8\n") }
func testRetryOnFailure(retryCount int) ([]byte, error) { // Get a temp file for the script to hold its state. file, err := ioutil.TempFile("", "temp") if err != nil { return nil, fmt.Errorf("Could not create temporary file: %s", err.Error()) } filename := file.Name() defer os.Remove(filename) defer file.Close() mockJob := mock.CreateMockJob(filename) config := TaskConfig{ FunctionName: "name", FunctionCmd: "testscripts/succeedOnFifthRun.sh", RetryCount: retryCount, } return config.Process(mockJob) }
func TestHaltGraceful(t *testing.T) { mockJob := mock.CreateMockJob("IgnorePayload") haltChan := make(chan struct{}) go func() { time.Sleep(1 * time.Second) close(haltChan) }() config := TaskConfig{ FunctionName: "name", FunctionCmd: "testscripts/stderrAndHang.sh", WarningLines: 2, ParseArgs: true, CmdTimeout: 2 * time.Second, Halt: haltChan, } _, err := config.Process(mockJob) assert.NoError(t, err) }
// Simple program to run the worker Process method. We have this so that we can easily test forwarding // stderr from the worker process to this process. If we don't separate this into a another process then // we mix the stderr of the test process itself with the worker process which makes things a bit trickier. func main() { mockJob := mock.CreateMockJob("IgnorePayload") config := gearcmd.TaskConfig{FunctionName: "name", FunctionCmd: "testscripts/logStderr.sh", WarningLines: 5} config.Process(mockJob) }