mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fix up the tests
This commit is contained in:
@@ -242,7 +242,7 @@ func (ds *MySQLDatastore) InsertRoute(ctx context.Context, route *models.Route)
|
||||
headers,
|
||||
config
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`,
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`,
|
||||
route.AppName,
|
||||
route.Path,
|
||||
route.Image,
|
||||
|
||||
@@ -15,11 +15,11 @@ import (
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/datastore"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/mqs"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/datastore"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/drivers"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
||||
)
|
||||
|
||||
func setLogBuffer() *bytes.Buffer {
|
||||
@@ -208,7 +208,6 @@ func (r RunResult) Status() string {
|
||||
return "success"
|
||||
}
|
||||
|
||||
|
||||
func TestAsyncRunnersGracefulShutdown(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
mockTask := getMockTask()
|
||||
@@ -230,7 +229,7 @@ func TestAsyncRunnersGracefulShutdown(t *testing.T) {
|
||||
}()
|
||||
rnr, cancel := testRunner(t)
|
||||
defer cancel()
|
||||
startAsyncRunners(ctx, ts.URL+"/tasks", tasks, rnr, datastore.NewMock())
|
||||
startAsyncRunners(ctx, ts.URL+"/tasks", rnr, datastore.NewMock())
|
||||
|
||||
if err := ctx.Err(); err != context.DeadlineExceeded {
|
||||
t.Log(buf.String())
|
||||
|
||||
@@ -42,7 +42,7 @@ func TestRunnerHello(t *testing.T) {
|
||||
Stderr: &stderr,
|
||||
}
|
||||
|
||||
result, err := runner.Run(ctx, cfg)
|
||||
result, err := runner.run(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
||||
@@ -95,7 +95,7 @@ func TestRunnerError(t *testing.T) {
|
||||
Stderr: &stderr,
|
||||
}
|
||||
|
||||
result, err := runner.Run(ctx, cfg)
|
||||
result, err := runner.run(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
||||
|
||||
@@ -65,7 +65,10 @@ func (rnr *Runner) RunTrackedTask(newTask *models.Task, ctx context.Context, cfg
|
||||
result, err := rnr.RunTask(ctx, cfg)
|
||||
|
||||
completedAt := strfmt.DateTime(time.Now())
|
||||
status := result.Status()
|
||||
status := "error"
|
||||
if result != nil {
|
||||
status = result.Status()
|
||||
}
|
||||
newTask.CompletedAt = completedAt
|
||||
newTask.Status = status
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"gitlab-odx.oracle.com/odx/functions/api/datastore"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/mqs"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
||||
)
|
||||
|
||||
func setLogBuffer() *bytes.Buffer {
|
||||
@@ -25,19 +24,8 @@ func setLogBuffer() *bytes.Buffer {
|
||||
return &buf
|
||||
}
|
||||
|
||||
func mockTasksConduit() chan task.Request {
|
||||
tasks := make(chan task.Request)
|
||||
go func() {
|
||||
for range tasks {
|
||||
}
|
||||
}()
|
||||
return tasks
|
||||
}
|
||||
|
||||
func TestAppCreate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
for i, test := range []struct {
|
||||
mock models.Datastore
|
||||
path string
|
||||
@@ -58,7 +46,7 @@ func TestAppCreate(t *testing.T) {
|
||||
{datastore.NewMock(), "/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusOK, nil},
|
||||
} {
|
||||
rnr, cancel := testRunner(t)
|
||||
srv := testServer(test.mock, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(test.mock, &mqs.Mock{}, rnr)
|
||||
router := srv.Router
|
||||
|
||||
body := bytes.NewBuffer([]byte(test.body))
|
||||
@@ -85,8 +73,6 @@ func TestAppCreate(t *testing.T) {
|
||||
|
||||
func TestAppDelete(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
for i, test := range []struct {
|
||||
ds models.Datastore
|
||||
@@ -103,7 +89,7 @@ func TestAppDelete(t *testing.T) {
|
||||
), "/v1/apps/myapp", "", http.StatusOK, nil},
|
||||
} {
|
||||
rnr, cancel := testRunner(t)
|
||||
srv := testServer(test.ds, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(test.ds, &mqs.Mock{}, rnr)
|
||||
|
||||
_, rec := routerRequest(t, srv.Router, "DELETE", test.path, nil)
|
||||
|
||||
@@ -128,12 +114,10 @@ func TestAppDelete(t *testing.T) {
|
||||
|
||||
func TestAppList(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
rnr, cancel := testRunner(t)
|
||||
defer cancel()
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -165,12 +149,10 @@ func TestAppList(t *testing.T) {
|
||||
|
||||
func TestAppGet(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
rnr, cancel := testRunner(t)
|
||||
defer cancel()
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -202,8 +184,6 @@ func TestAppGet(t *testing.T) {
|
||||
|
||||
func TestAppUpdate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
for i, test := range []struct {
|
||||
mock models.Datastore
|
||||
@@ -230,7 +210,7 @@ func TestAppUpdate(t *testing.T) {
|
||||
), "/v1/apps/myapp", `{ "app": { "name": "othername" } }`, http.StatusBadRequest, nil},
|
||||
} {
|
||||
rnr, cancel := testRunner(t)
|
||||
srv := testServer(test.mock, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(test.mock, &mqs.Mock{}, rnr)
|
||||
|
||||
body := bytes.NewBuffer([]byte(test.body))
|
||||
_, rec := routerRequest(t, srv.Router, "PATCH", test.path, body)
|
||||
|
||||
@@ -13,8 +13,6 @@ import (
|
||||
|
||||
func TestRouteCreate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
for i, test := range []struct {
|
||||
mock models.Datastore
|
||||
@@ -37,7 +35,7 @@ func TestRouteCreate(t *testing.T) {
|
||||
{datastore.NewMock(), "/v1/apps/a/routes", `{ "route": { "image": "funcy/hello", "path": "/myroute" } }`, http.StatusOK, nil},
|
||||
} {
|
||||
rnr, cancel := testRunner(t)
|
||||
srv := testServer(test.mock, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(test.mock, &mqs.Mock{}, rnr)
|
||||
|
||||
body := bytes.NewBuffer([]byte(test.body))
|
||||
_, rec := routerRequest(t, srv.Router, "POST", test.path, body)
|
||||
@@ -66,8 +64,6 @@ func TestRouteCreate(t *testing.T) {
|
||||
|
||||
func TestRouteDelete(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
for i, test := range []struct {
|
||||
ds models.Datastore
|
||||
@@ -84,7 +80,7 @@ func TestRouteDelete(t *testing.T) {
|
||||
), "/v1/apps/a/routes/myroute", "", http.StatusOK, nil},
|
||||
} {
|
||||
rnr, cancel := testRunner(t)
|
||||
srv := testServer(test.ds, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(test.ds, &mqs.Mock{}, rnr)
|
||||
_, rec := routerRequest(t, srv.Router, "DELETE", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
@@ -108,12 +104,10 @@ func TestRouteDelete(t *testing.T) {
|
||||
|
||||
func TestRouteList(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
rnr, cancel := testRunner(t)
|
||||
defer cancel()
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -145,13 +139,11 @@ func TestRouteList(t *testing.T) {
|
||||
|
||||
func TestRouteGet(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
rnr, cancel := testRunner(t)
|
||||
defer cancel()
|
||||
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -183,8 +175,6 @@ func TestRouteGet(t *testing.T) {
|
||||
|
||||
func TestRouteUpdate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
defer close(tasks)
|
||||
|
||||
for i, test := range []struct {
|
||||
ds models.Datastore
|
||||
@@ -220,7 +210,7 @@ func TestRouteUpdate(t *testing.T) {
|
||||
), "/v1/apps/a/routes/myroute/do", `{ "route": { "path": "/otherpath" } }`, http.StatusBadRequest, nil},
|
||||
} {
|
||||
rnr, cancel := testRunner(t)
|
||||
srv := testServer(test.ds, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(test.ds, &mqs.Mock{}, rnr)
|
||||
|
||||
body := bytes.NewBuffer([]byte(test.body))
|
||||
|
||||
|
||||
@@ -13,11 +13,10 @@ import (
|
||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/mqs"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/server/internal/routecache"
|
||||
)
|
||||
|
||||
func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner, tasks chan task.Request, enqueue models.Enqueue) *gin.Engine {
|
||||
func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner, enqueue models.Enqueue) *gin.Engine {
|
||||
ctx := context.Background()
|
||||
|
||||
s := &Server{
|
||||
@@ -25,7 +24,6 @@ func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Ru
|
||||
Router: gin.New(),
|
||||
Datastore: ds,
|
||||
MQ: mq,
|
||||
tasks: tasks,
|
||||
Enqueue: enqueue,
|
||||
hotroutes: routecache.New(10),
|
||||
}
|
||||
@@ -39,7 +37,6 @@ func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Ru
|
||||
}
|
||||
|
||||
func TestRouteRunnerAsyncExecution(t *testing.T) {
|
||||
tasks := mockTasksConduit()
|
||||
ds := datastore.NewMockInit(
|
||||
[]*models.App{
|
||||
{Name: "myapp", Config: map[string]string{"app": "true"}},
|
||||
@@ -82,7 +79,7 @@ func TestRouteRunnerAsyncExecution(t *testing.T) {
|
||||
wg.Add(1)
|
||||
fmt.Println("About to start router")
|
||||
rnr, cancel := testRunner(t)
|
||||
router := testRouterAsync(ds, mq, rnr, tasks, func(_ context.Context, _ models.MessageQueue, task *models.Task) (*models.Task, error) {
|
||||
router := testRouterAsync(ds, mq, rnr, func(_ context.Context, _ models.MessageQueue, task *models.Task) (*models.Task, error) {
|
||||
if test.body != task.Payload {
|
||||
t.Errorf("Test %d: Expected task Payload to be the same as the test body", i)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/mqs"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
||||
)
|
||||
|
||||
func testRunner(t *testing.T) (*runner.Runner, context.CancelFunc) {
|
||||
@@ -25,7 +24,6 @@ func testRunner(t *testing.T) (*runner.Runner, context.CancelFunc) {
|
||||
|
||||
func TestRouteRunnerGet(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
|
||||
rnr, cancel := testRunner(t)
|
||||
defer cancel()
|
||||
@@ -34,7 +32,7 @@ func TestRouteRunnerGet(t *testing.T) {
|
||||
[]*models.App{
|
||||
{Name: "myapp", Config: models.Config{}},
|
||||
}, nil, nil,
|
||||
), &mqs.Mock{}, rnr, tasks)
|
||||
), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -68,7 +66,6 @@ func TestRouteRunnerGet(t *testing.T) {
|
||||
|
||||
func TestRouteRunnerPost(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
tasks := mockTasksConduit()
|
||||
|
||||
rnr, cancel := testRunner(t)
|
||||
defer cancel()
|
||||
@@ -77,7 +74,7 @@ func TestRouteRunnerPost(t *testing.T) {
|
||||
[]*models.App{
|
||||
{Name: "myapp", Config: models.Config{}},
|
||||
}, nil, nil,
|
||||
), &mqs.Mock{}, rnr, tasks)
|
||||
), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -114,10 +111,6 @@ func TestRouteRunnerPost(t *testing.T) {
|
||||
func TestRouteRunnerExecution(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
|
||||
tasks := make(chan task.Request)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
rnr, cancelrnr := testRunner(t)
|
||||
defer cancelrnr()
|
||||
|
||||
@@ -129,7 +122,7 @@ func TestRouteRunnerExecution(t *testing.T) {
|
||||
{Path: "/myroute", AppName: "myapp", Image: "funcy/hello", Headers: map[string][]string{"X-Function": {"Test"}}},
|
||||
{Path: "/myerror", AppName: "myapp", Image: "funcy/error", Headers: map[string][]string{"X-Function": {"Test"}}},
|
||||
}, nil,
|
||||
), &mqs.Mock{}, rnr, tasks)
|
||||
), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -171,10 +164,6 @@ func TestRouteRunnerTimeout(t *testing.T) {
|
||||
t.Skip("doesn't work on old Ubuntu")
|
||||
buf := setLogBuffer()
|
||||
|
||||
tasks := make(chan task.Request)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
rnr, cancelrnr := testRunner(t)
|
||||
defer cancelrnr()
|
||||
|
||||
@@ -185,7 +174,7 @@ func TestRouteRunnerTimeout(t *testing.T) {
|
||||
[]*models.Route{
|
||||
{Path: "/sleeper", AppName: "myapp", Image: "funcy/sleeper", Timeout: 1},
|
||||
}, nil,
|
||||
), &mqs.Mock{}, rnr, tasks)
|
||||
), &mqs.Mock{}, rnr)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
|
||||
@@ -16,13 +16,12 @@ import (
|
||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/mqs"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/server/internal/routecache"
|
||||
)
|
||||
|
||||
var tmpBolt = "/tmp/func_test_bolt.db"
|
||||
|
||||
func testServer(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner, tasks chan task.Request) *Server {
|
||||
func testServer(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner) *Server {
|
||||
ctx := context.Background()
|
||||
|
||||
s := &Server{
|
||||
@@ -30,7 +29,6 @@ func testServer(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner,
|
||||
Router: gin.New(),
|
||||
Datastore: ds,
|
||||
MQ: mq,
|
||||
tasks: tasks,
|
||||
Enqueue: DefaultEnqueue,
|
||||
hotroutes: routecache.New(2),
|
||||
}
|
||||
@@ -97,14 +95,10 @@ func TestFullStack(t *testing.T) {
|
||||
ds, closeBolt := prepareBolt(t)
|
||||
defer closeBolt()
|
||||
|
||||
tasks := make(chan task.Request)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
rnr, rnrcancel := testRunner(t)
|
||||
defer rnrcancel()
|
||||
|
||||
srv := testServer(ds, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(ds, &mqs.Mock{}, rnr)
|
||||
srv.hotroutes = routecache.New(2)
|
||||
|
||||
for _, test := range []struct {
|
||||
|
||||
Reference in New Issue
Block a user