2013-11-20 02:38:59 -05:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2014-05-09 14:09:59 -04:00
|
|
|
"bytes"
|
2015-03-25 03:44:12 -04:00
|
|
|
"errors"
|
2014-05-09 14:09:59 -04:00
|
|
|
"fmt"
|
2013-11-20 02:38:59 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
func TestJobOK(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := New()
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("return_ok", func(job *Job) error { return nil })
|
2013-11-20 02:38:59 -05:00
|
|
|
err := eng.Job("return_ok").Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected: err=%v\nReceived: err=%v", nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
func TestJobErr(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := New()
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("return_err", func(job *Job) error { return errors.New("return_err") })
|
2013-11-20 02:38:59 -05:00
|
|
|
err := eng.Job("return_err").Run()
|
|
|
|
if err == nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
t.Fatalf("When a job returns error, Run() should return an error")
|
2013-11-20 02:38:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobStdoutString(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := New()
|
2013-11-20 02:38:59 -05:00
|
|
|
// FIXME: test multiple combinations of output and status
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("say_something_in_stdout", func(job *Job) error {
|
2013-11-20 02:38:59 -05:00
|
|
|
job.Printf("Hello world\n")
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2013-11-20 02:38:59 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
job := eng.Job("say_something_in_stdout")
|
2014-05-09 14:09:59 -04:00
|
|
|
var outputBuffer = bytes.NewBuffer(nil)
|
|
|
|
job.Stdout.Add(outputBuffer)
|
2013-11-20 02:38:59 -05:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-09 14:09:59 -04:00
|
|
|
fmt.Println(outputBuffer)
|
|
|
|
var output = Tail(outputBuffer, 1)
|
2013-11-20 02:38:59 -05:00
|
|
|
if expectedOutput := "Hello world"; output != expectedOutput {
|
|
|
|
t.Fatalf("Stdout last line:\nExpected: %v\nReceived: %v", expectedOutput, output)
|
|
|
|
}
|
|
|
|
}
|