mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
By default, BoltDB will hang while waiting to acquire lock to the datafile, thus the users might find themselves waiting for something but not what. The added timeout aims inform use about what's happening. Also this renames MQADR to TASKSRV, refactor configuration to read environment variables. RunAsyncRunner now fills the gaps when parsing TASKSRV. Fixes #119
112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package runner
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/iron-io/functions/api/models"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func TestRunnerHello(t *testing.T) {
|
|
runner, err := New(NewMetricLogger())
|
|
if err != nil {
|
|
t.Fatalf("Test error during New() - %s", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
for i, test := range []struct {
|
|
route *models.Route
|
|
payload string
|
|
expectedStatus string
|
|
expectedOut string
|
|
expectedErr string
|
|
}{
|
|
{&models.Route{Image: "iron/hello"}, ``, "success", "Hello World!", ""},
|
|
{&models.Route{Image: "iron/hello"}, `{"name": "test"}`, "success", "Hello test!", ""},
|
|
} {
|
|
var stdout, stderr bytes.Buffer
|
|
cfg := &Config{
|
|
ID: fmt.Sprintf("hello-%d-%d", i, time.Now().Unix()),
|
|
Image: test.route.Image,
|
|
Timeout: 5 * time.Second,
|
|
Env: map[string]string{
|
|
"PAYLOAD": test.payload,
|
|
},
|
|
Stdout: &stdout,
|
|
Stderr: &stderr,
|
|
}
|
|
|
|
result, err := runner.Run(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
|
}
|
|
|
|
if test.expectedStatus != result.Status() {
|
|
t.Fatalf("Test %d: expected result status to be `%s` but it was `%s`", i, test.expectedStatus, result.Status())
|
|
}
|
|
|
|
if !bytes.Contains(stdout.Bytes(), []byte(test.expectedOut)) {
|
|
t.Fatalf("Test %d: expected output log to contain `%s` in `%s`", i, test.expectedOut, stdout.String())
|
|
}
|
|
|
|
if !bytes.Contains(stderr.Bytes(), []byte(test.expectedErr)) {
|
|
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunnerError(t *testing.T) {
|
|
runner, err := New(NewMetricLogger())
|
|
if err != nil {
|
|
t.Fatalf("Test error during New() - %s", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
for i, test := range []struct {
|
|
route *models.Route
|
|
payload string
|
|
expectedStatus string
|
|
expectedOut string
|
|
expectedErr string
|
|
}{
|
|
{&models.Route{Image: "iron/error"}, ``, "error", "", "RuntimeError"},
|
|
{&models.Route{Image: "iron/error"}, `{"name": "test"}`, "error", "", "RuntimeError"},
|
|
} {
|
|
var stdout, stderr bytes.Buffer
|
|
cfg := &Config{
|
|
ID: fmt.Sprintf("err-%d-%d", i, time.Now().Unix()),
|
|
Image: test.route.Image,
|
|
Timeout: 5 * time.Second,
|
|
Env: map[string]string{
|
|
"PAYLOAD": test.payload,
|
|
},
|
|
Stdout: &stdout,
|
|
Stderr: &stderr,
|
|
}
|
|
|
|
result, err := runner.Run(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
|
}
|
|
|
|
if test.expectedStatus != result.Status() {
|
|
t.Fatalf("Test %d: expected result status to be `%s` but it was `%s`", i, test.expectedStatus, result.Status())
|
|
}
|
|
|
|
if !bytes.Contains(stdout.Bytes(), []byte(test.expectedOut)) {
|
|
t.Fatalf("Test %d: expected output log to contain `%s` in `%s`", i, test.expectedOut, stdout.String())
|
|
}
|
|
|
|
if !bytes.Contains(stderr.Bytes(), []byte(test.expectedErr)) {
|
|
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
|