mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
prior to this patch we were allowing 256MB for every function run, just because that was the default for the docker driver and we were not using the memory field on any given route configuration. this fixes that, now docker containers will get the correct memory limit passed into the container from the route. the default is still 128. there is also an env var now, `MEMORY_MB` that is set on each function call, see the linked issue below for rationale. closes #186 ran the given function code from #186, and now i only see allocations up to 32MB before the function is killed. yay. notes: there is no max for memory. for open source fn i'm not sure we want to cap it, really. in the services repo we probably should add a cap before prod. since we don't know any given fn server's ram, we can't try to make sure the setting on any given route is something that can even be run. remove envconfig & bytefmt this updates the glide.yaml file to remove the unused deps, but trying to install fresh is broken atm so i couldn't remove from vendor/, going to fix separately (next update we just won't get these). also changed the skip dir to be the cli dir now that its name has changed (related to brokenness). fix how ram slots were being allocated. integer division is significantly slower than subtraction.
152 lines
4.1 KiB
Go
152 lines
4.1 KiB
Go
package runner
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fnproject/fn/api/datastore"
|
|
"github.com/fnproject/fn/api/id"
|
|
"github.com/fnproject/fn/api/logs"
|
|
"github.com/fnproject/fn/api/models"
|
|
"github.com/fnproject/fn/api/runner/task"
|
|
)
|
|
|
|
func TestRunnerHello(t *testing.T) {
|
|
buf := setLogBuffer()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
ds := datastore.NewMock()
|
|
fnl := logs.NewMock()
|
|
fLogger := NewFuncLogger(fnl)
|
|
runner, err := New(ctx, fLogger, ds)
|
|
if err != nil {
|
|
t.Fatalf("Test error during New() - %s", err)
|
|
}
|
|
|
|
for i, test := range []struct {
|
|
route *models.Route
|
|
payload string
|
|
expectedStatus string
|
|
expectedOut string
|
|
expectedErr string
|
|
taskID string
|
|
}{
|
|
{&models.Route{Image: "funcy/hello"}, ``, "success", "Hello World!", "", id.New().String()},
|
|
{&models.Route{Image: "funcy/hello"}, `{"name": "test"}`, "success", "Hello test!", "", id.New().String()},
|
|
} {
|
|
var stdout, stderr bytes.Buffer
|
|
cfg := &task.Config{
|
|
ID: test.taskID,
|
|
Image: test.route.Image,
|
|
Timeout: 10 * time.Second,
|
|
Memory: 128,
|
|
Ready: make(chan struct{}),
|
|
Stdin: strings.NewReader(test.payload),
|
|
AppName: test.route.AppName,
|
|
Stdout: &stdout,
|
|
Stderr: nopCloser{&stderr},
|
|
}
|
|
|
|
result, err := runner.run(ctx, cfg)
|
|
if err != nil {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
|
}
|
|
|
|
if test.expectedStatus != result.Status() {
|
|
t.Log(buf.String())
|
|
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.Log(buf.String())
|
|
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.Log(buf.String())
|
|
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunnerError(t *testing.T) {
|
|
buf := setLogBuffer()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
ds := datastore.NewMock()
|
|
fnl := logs.NewMock()
|
|
fLogger := NewFuncLogger(fnl)
|
|
runner, err := New(ctx, fLogger, ds)
|
|
if err != nil {
|
|
t.Fatalf("Test error during New() - %s", err)
|
|
}
|
|
|
|
for i, test := range []struct {
|
|
route *models.Route
|
|
payload string
|
|
expectedStatus string
|
|
expectedOut string
|
|
expectedErr string
|
|
taskID string
|
|
}{
|
|
{&models.Route{Image: "funcy/error"}, ``, "error", "", "", id.New().String()},
|
|
{&models.Route{Image: "funcy/error"}, `{"name": "test"}`, "error", "", "", id.New().String()},
|
|
} {
|
|
var stdout, stderr bytes.Buffer
|
|
cfg := &task.Config{
|
|
ID: fmt.Sprintf("err-%d-%d", i, time.Now().Unix()),
|
|
Image: test.route.Image,
|
|
Timeout: 10 * time.Second,
|
|
Memory: 128,
|
|
Ready: make(chan struct{}),
|
|
Stdin: strings.NewReader(test.payload),
|
|
Stdout: &stdout,
|
|
Stderr: nopCloser{&stderr},
|
|
}
|
|
|
|
result, err := runner.run(ctx, cfg)
|
|
if err != nil {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
|
}
|
|
|
|
if test.expectedStatus != result.Status() {
|
|
t.Log(buf.String())
|
|
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.Log(buf.String())
|
|
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.Log(buf.String())
|
|
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunnerMemory(t *testing.T) {
|
|
// make sure we get MB out of a task.Config when turned into a containerTask
|
|
// (so if Config.Memory changes to not be MB we hear about it)
|
|
|
|
cfg := &task.Config{
|
|
Memory: 128,
|
|
}
|
|
|
|
task := &containerTask{cfg: cfg}
|
|
|
|
const exp = 128 * 1024 * 1024
|
|
if task.Memory() != exp {
|
|
t.Fatalf("Expected Memory to return %v but got %v", exp, task.Memory())
|
|
}
|
|
}
|