Files
fn-serverless/api/agent/drivers/docker/docker_test.go
Reed Allman e13a6fd029 death to format (#1281)
* get rid of old format stuff, utils usage, fix up for fdk2.0 interface

* pure agent format removal, TODO remove format field, fix up all tests

* shitter's clogged

* fix agent tests

* start rolling through server tests

* tests compile, some failures

* remove json / content type detection on invoke/httptrigger, fix up tests

* remove hello, fixup system tests

the fucking status checker test just hangs and it's testing that it doesn't
work so the test passes but the test doesn't pass fuck life it's not worth it

* fix migration

* meh

* make dbhelper shut up about dbhelpers not being used

* move fail status at least into main thread, jfc

* fix status call to have FN_LISTENER

also turns off the stdout/stderr blocking between calls, because it's
impossible to debug without that (without syslog), now that stdout and stderr
go to the same place (either to host stderr or nowhere) and isn't used for
function output this shouldn't be a big fuss really

* remove stdin

* cleanup/remind: fixed bug where watcher would leak if container dies first

* silence system-test logs until fail, fix datastore tests

postgres does weird things with constraints when renaming tables, took the
easy way out

system-tests were loud as fuck and made you download a circleci text file of
the logs, made them only yell when they goof

* fix fdk-go dep for test image. fun

* fix swagger and remove test about format

* update all the gopkg files

* add back FN_FORMAT for fdks that assert things. pfft

* add useful error for functions that exit

this error is really confounding because containers can exit for all manner of
reason, we're just guessing that this is the most likely cause for now, and
this error message should very likely change or be removed from the client
path anyway (context.Canceled wasn't all that useful either, but anyway, I'd
been hunting for this... so found it). added a test to avoid being publicly
shamed for 1 line commits (beware...).
2018-10-26 10:43:04 -07:00

216 lines
5.7 KiB
Go

package docker
import (
"bytes"
"context"
"io"
"strings"
"testing"
"time"
"github.com/fnproject/fn/api/agent/drivers"
)
type taskDockerTest struct {
id string
input io.Reader
output io.Writer
errors io.Writer
}
func (f *taskDockerTest) Command() string { return "" }
func (f *taskDockerTest) EnvVars() map[string]string { return map[string]string{} }
func (f *taskDockerTest) Id() string { return f.id }
func (f *taskDockerTest) Group() string { return "" }
func (f *taskDockerTest) Image() string { return "hello-world" }
func (f *taskDockerTest) Timeout() time.Duration { return 30 * time.Second }
func (f *taskDockerTest) Logger() (stdout, stderr io.Writer) { return f.output, f.errors }
func (f *taskDockerTest) WriteStat(context.Context, drivers.Stat) { /* TODO */ }
func (f *taskDockerTest) Volumes() [][2]string { return [][2]string{} }
func (f *taskDockerTest) Memory() uint64 { return 256 * 1024 * 1024 }
func (f *taskDockerTest) CPUs() uint64 { return 0 }
func (f *taskDockerTest) FsSize() uint64 { return 0 }
func (f *taskDockerTest) TmpFsSize() uint64 { return 0 }
func (f *taskDockerTest) WorkDir() string { return "" }
func (f *taskDockerTest) Close() {}
func (f *taskDockerTest) Input() io.Reader { return f.input }
func (f *taskDockerTest) Extensions() map[string]string { return nil }
func (f *taskDockerTest) LoggerConfig() drivers.LoggerConfig { return drivers.LoggerConfig{} }
func (f *taskDockerTest) UDSAgentPath() string { return "" }
func (f *taskDockerTest) UDSDockerPath() string { return "" }
func (f *taskDockerTest) UDSDockerDest() string { return "" }
func TestRunnerDocker(t *testing.T) {
dkr := NewDocker(drivers.Config{})
ctx := context.Background()
var output bytes.Buffer
var errors bytes.Buffer
task := &taskDockerTest{"test-docker", bytes.NewBufferString(`{"isDebug": true}`), &output, &errors}
cookie, err := dkr.CreateCookie(ctx, task)
if err != nil {
t.Fatal("Couldn't create task cookie")
}
defer cookie.Close(ctx)
err = dkr.PrepareCookie(ctx, cookie)
if err != nil {
t.Fatal("Couldn't prepare task test")
}
waiter, err := cookie.Run(ctx)
if err != nil {
t.Fatal(err)
}
result := waiter.Wait(ctx)
if result.Error() != nil {
t.Fatal(result.Error())
}
if result.Status() != "success" {
t.Fatalf("Test should successfully run the image: %s output: %s errors: %s",
result.Error(), output.String(), errors.String())
}
}
func TestRunnerDockerNetworks(t *testing.T) {
dkr := NewDocker(drivers.Config{
DockerNetworks: "test1 test2",
})
ctx := context.Background()
var output bytes.Buffer
var errors bytes.Buffer
task1 := &taskDockerTest{"test-docker1", bytes.NewBufferString(`{"isDebug": true}`), &output, &errors}
task2 := &taskDockerTest{"test-docker2", bytes.NewBufferString(`{"isDebug": true}`), &output, &errors}
cookie1, err := dkr.CreateCookie(ctx, task1)
if err != nil {
t.Fatal("Couldn't create task1 cookie")
}
defer cookie1.Close(ctx)
err = dkr.PrepareCookie(ctx, cookie1)
if err != nil {
t.Fatal("Couldn't prepare task1 test")
}
cookie2, err := dkr.CreateCookie(ctx, task2)
if err != nil {
t.Fatal("Couldn't create task2 cookie")
}
defer cookie2.Close(ctx)
err = dkr.PrepareCookie(ctx, cookie2)
if err != nil {
t.Fatal("Couldn't prepare task2 test")
}
defer cookie2.Close(ctx)
c1 := cookie1.(*cookie)
c2 := cookie2.(*cookie)
var tally = map[string]uint64{
"test1": 0,
"test2": 0,
}
tally[c1.netId]++
tally[c2.netId]++
for key, val := range tally {
if val != 1 {
t.Fatalf("netId unbalanced network usage for %s expected 1 got %d", key, val)
}
}
}
func TestRunnerDockerVersion(t *testing.T) {
dkr := NewDocker(drivers.Config{
ServerVersion: "0.0.0",
})
if dkr == nil {
t.Fatal("should not be nil")
}
err := checkDockerVersion(dkr, "1.0.0")
if err != nil {
t.Fatal(err)
}
err = checkDockerVersion(dkr, "9999.0.0")
if err == nil {
t.Fatal("should have failed")
}
}
func TestRunnerDockerStdout(t *testing.T) {
dkr := NewDocker(drivers.Config{})
ctx := context.Background()
var output bytes.Buffer
var errors bytes.Buffer
task := &taskDockerTest{"test-docker-stdin", bytes.NewBufferString(""), &output, &errors}
cookie, err := dkr.CreateCookie(ctx, task)
if err != nil {
t.Fatal("Couldn't create task cookie")
}
defer cookie.Close(ctx)
err = dkr.PrepareCookie(ctx, cookie)
if err != nil {
t.Fatal("Couldn't prepare task test")
}
waiter, err := cookie.Run(ctx)
if err != nil {
t.Fatal(err)
}
result := waiter.Wait(ctx)
if result.Error() != nil {
t.Fatal(result.Error())
}
if result.Status() != "success" {
t.Fatalf("Test should successfully run the image: %s output: %s errors: %s",
result.Error(), output.String(), errors.String())
}
// if hello world image changes, change dis
expect := "Hello from Docker!"
got := output.String()
if !strings.Contains(got, expect) {
t.Errorf("Test expected output to contain '%s', got '%s'", expect, got)
}
}
//
//func TestRegistry(t *testing.T) {
// image := "fnproject/fn-test-utils"
//
// sizer, err := CheckRegistry(context.Background(), image, docker.AuthConfiguration{})
// if err != nil {
// t.Fatal("expected registry check not to fail, got:", err)
// }
//
// size, err := sizer.Size()
// if err != nil {
// t.Fatal("expected sizer not to fail, got:", err)
// }
//
// if size <= 0 {
// t.Fatal("expected positive size for image that exists, got size:", size)
// }
//}