2013-11-20 02:38:59 -05:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2014-05-09 14:09:59 -04:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2013-11-20 02:38:59 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJobStatusOK(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := New()
|
2013-11-20 02:38:59 -05:00
|
|
|
eng.Register("return_ok", func(job *Job) Status { return StatusOK })
|
|
|
|
err := eng.Job("return_ok").Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected: err=%v\nReceived: err=%v", nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobStatusErr(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := New()
|
2013-11-20 02:38:59 -05:00
|
|
|
eng.Register("return_err", func(job *Job) Status { return StatusErr })
|
|
|
|
err := eng.Job("return_err").Run()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("When a job returns StatusErr, Run() should return an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobStatusNotFound(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := New()
|
2013-11-20 02:38:59 -05:00
|
|
|
eng.Register("return_not_found", func(job *Job) Status { return StatusNotFound })
|
|
|
|
err := eng.Job("return_not_found").Run()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("When a job returns StatusNotFound, Run() should return an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
eng.Register("say_something_in_stdout", func(job *Job) Status {
|
|
|
|
job.Printf("Hello world\n")
|
|
|
|
return StatusOK
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobStderrString(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
|
|
|
|
eng.Register("say_something_in_stderr", func(job *Job) Status {
|
|
|
|
job.Errorf("Warning, something might happen\nHere it comes!\nOh no...\nSomething happened\n")
|
|
|
|
return StatusOK
|
|
|
|
})
|
|
|
|
|
|
|
|
job := eng.Job("say_something_in_stderr")
|
2014-05-09 14:09:59 -04:00
|
|
|
var outputBuffer = bytes.NewBuffer(nil)
|
|
|
|
job.Stderr.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
|
|
|
var output = Tail(outputBuffer, 1)
|
2013-11-20 02:38:59 -05:00
|
|
|
if expectedOutput := "Something happened"; output != expectedOutput {
|
|
|
|
t.Fatalf("Stderr last line:\nExpected: %v\nReceived: %v", expectedOutput, output)
|
|
|
|
}
|
|
|
|
}
|