fix up the tests

This commit is contained in:
Reed Allman
2017-06-06 05:04:22 -07:00
parent 478245de4c
commit 636af2f7ea
9 changed files with 30 additions and 78 deletions

View File

@@ -242,7 +242,7 @@ func (ds *MySQLDatastore) InsertRoute(ctx context.Context, route *models.Route)
headers, headers,
config config
) )
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`, VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`,
route.AppName, route.AppName,
route.Path, route.Path,
route.Image, route.Image,

View File

@@ -15,11 +15,11 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin" "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/models"
"gitlab-odx.oracle.com/odx/functions/api/mqs" "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/drivers"
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
) )
func setLogBuffer() *bytes.Buffer { func setLogBuffer() *bytes.Buffer {
@@ -208,7 +208,6 @@ func (r RunResult) Status() string {
return "success" return "success"
} }
func TestAsyncRunnersGracefulShutdown(t *testing.T) { func TestAsyncRunnersGracefulShutdown(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
mockTask := getMockTask() mockTask := getMockTask()
@@ -230,7 +229,7 @@ func TestAsyncRunnersGracefulShutdown(t *testing.T) {
}() }()
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
defer cancel() 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 { if err := ctx.Err(); err != context.DeadlineExceeded {
t.Log(buf.String()) t.Log(buf.String())

View File

@@ -42,7 +42,7 @@ func TestRunnerHello(t *testing.T) {
Stderr: &stderr, Stderr: &stderr,
} }
result, err := runner.Run(ctx, cfg) result, err := runner.run(ctx, cfg)
if err != nil { if err != nil {
t.Log(buf.String()) t.Log(buf.String())
t.Fatalf("Test %d: error during Run() - %s", i, err) t.Fatalf("Test %d: error during Run() - %s", i, err)
@@ -95,7 +95,7 @@ func TestRunnerError(t *testing.T) {
Stderr: &stderr, Stderr: &stderr,
} }
result, err := runner.Run(ctx, cfg) result, err := runner.run(ctx, cfg)
if err != nil { if err != nil {
t.Log(buf.String()) t.Log(buf.String())
t.Fatalf("Test %d: error during Run() - %s", i, err) t.Fatalf("Test %d: error during Run() - %s", i, err)

View File

@@ -65,7 +65,10 @@ func (rnr *Runner) RunTrackedTask(newTask *models.Task, ctx context.Context, cfg
result, err := rnr.RunTask(ctx, cfg) result, err := rnr.RunTask(ctx, cfg)
completedAt := strfmt.DateTime(time.Now()) completedAt := strfmt.DateTime(time.Now())
status := result.Status() status := "error"
if result != nil {
status = result.Status()
}
newTask.CompletedAt = completedAt newTask.CompletedAt = completedAt
newTask.Status = status newTask.Status = status

View File

@@ -12,7 +12,6 @@ import (
"gitlab-odx.oracle.com/odx/functions/api/datastore" "gitlab-odx.oracle.com/odx/functions/api/datastore"
"gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/mqs" "gitlab-odx.oracle.com/odx/functions/api/mqs"
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
) )
func setLogBuffer() *bytes.Buffer { func setLogBuffer() *bytes.Buffer {
@@ -25,19 +24,8 @@ func setLogBuffer() *bytes.Buffer {
return &buf return &buf
} }
func mockTasksConduit() chan task.Request {
tasks := make(chan task.Request)
go func() {
for range tasks {
}
}()
return tasks
}
func TestAppCreate(t *testing.T) { func TestAppCreate(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
for i, test := range []struct { for i, test := range []struct {
mock models.Datastore mock models.Datastore
path string path string
@@ -58,7 +46,7 @@ func TestAppCreate(t *testing.T) {
{datastore.NewMock(), "/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusOK, nil}, {datastore.NewMock(), "/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusOK, nil},
} { } {
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
srv := testServer(test.mock, &mqs.Mock{}, rnr, tasks) srv := testServer(test.mock, &mqs.Mock{}, rnr)
router := srv.Router router := srv.Router
body := bytes.NewBuffer([]byte(test.body)) body := bytes.NewBuffer([]byte(test.body))
@@ -85,8 +73,6 @@ func TestAppCreate(t *testing.T) {
func TestAppDelete(t *testing.T) { func TestAppDelete(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
for i, test := range []struct { for i, test := range []struct {
ds models.Datastore ds models.Datastore
@@ -99,11 +85,11 @@ func TestAppDelete(t *testing.T) {
{datastore.NewMockInit( {datastore.NewMockInit(
[]*models.App{{ []*models.App{{
Name: "myapp", Name: "myapp",
}},nil, nil, }}, nil, nil,
), "/v1/apps/myapp", "", http.StatusOK, nil}, ), "/v1/apps/myapp", "", http.StatusOK, nil},
} { } {
rnr, cancel := testRunner(t) 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) _, rec := routerRequest(t, srv.Router, "DELETE", test.path, nil)
@@ -128,12 +114,10 @@ func TestAppDelete(t *testing.T) {
func TestAppList(t *testing.T) { func TestAppList(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
defer cancel() defer cancel()
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks) srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -165,12 +149,10 @@ func TestAppList(t *testing.T) {
func TestAppGet(t *testing.T) { func TestAppGet(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
defer cancel() defer cancel()
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks) srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -202,8 +184,6 @@ func TestAppGet(t *testing.T) {
func TestAppUpdate(t *testing.T) { func TestAppUpdate(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
for i, test := range []struct { for i, test := range []struct {
mock models.Datastore mock models.Datastore
@@ -226,11 +206,11 @@ func TestAppUpdate(t *testing.T) {
{datastore.NewMockInit( {datastore.NewMockInit(
[]*models.App{{ []*models.App{{
Name: "myapp", Name: "myapp",
}}, nil,nil, }}, nil, nil,
), "/v1/apps/myapp", `{ "app": { "name": "othername" } }`, http.StatusBadRequest, nil}, ), "/v1/apps/myapp", `{ "app": { "name": "othername" } }`, http.StatusBadRequest, nil},
} { } {
rnr, cancel := testRunner(t) 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)) body := bytes.NewBuffer([]byte(test.body))
_, rec := routerRequest(t, srv.Router, "PATCH", test.path, body) _, rec := routerRequest(t, srv.Router, "PATCH", test.path, body)

View File

@@ -13,8 +13,6 @@ import (
func TestRouteCreate(t *testing.T) { func TestRouteCreate(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
for i, test := range []struct { for i, test := range []struct {
mock models.Datastore 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}, {datastore.NewMock(), "/v1/apps/a/routes", `{ "route": { "image": "funcy/hello", "path": "/myroute" } }`, http.StatusOK, nil},
} { } {
rnr, cancel := testRunner(t) 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)) body := bytes.NewBuffer([]byte(test.body))
_, rec := routerRequest(t, srv.Router, "POST", test.path, body) _, rec := routerRequest(t, srv.Router, "POST", test.path, body)
@@ -66,8 +64,6 @@ func TestRouteCreate(t *testing.T) {
func TestRouteDelete(t *testing.T) { func TestRouteDelete(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
for i, test := range []struct { for i, test := range []struct {
ds models.Datastore ds models.Datastore
@@ -84,7 +80,7 @@ func TestRouteDelete(t *testing.T) {
), "/v1/apps/a/routes/myroute", "", http.StatusOK, nil}, ), "/v1/apps/a/routes/myroute", "", http.StatusOK, nil},
} { } {
rnr, cancel := testRunner(t) 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) _, rec := routerRequest(t, srv.Router, "DELETE", test.path, nil)
if rec.Code != test.expectedCode { if rec.Code != test.expectedCode {
@@ -108,12 +104,10 @@ func TestRouteDelete(t *testing.T) {
func TestRouteList(t *testing.T) { func TestRouteList(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
defer cancel() defer cancel()
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks) srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -145,13 +139,11 @@ func TestRouteList(t *testing.T) {
func TestRouteGet(t *testing.T) { func TestRouteGet(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
defer cancel() defer cancel()
srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks) srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -183,8 +175,6 @@ func TestRouteGet(t *testing.T) {
func TestRouteUpdate(t *testing.T) { func TestRouteUpdate(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
defer close(tasks)
for i, test := range []struct { for i, test := range []struct {
ds models.Datastore ds models.Datastore
@@ -220,7 +210,7 @@ func TestRouteUpdate(t *testing.T) {
), "/v1/apps/a/routes/myroute/do", `{ "route": { "path": "/otherpath" } }`, http.StatusBadRequest, nil}, ), "/v1/apps/a/routes/myroute/do", `{ "route": { "path": "/otherpath" } }`, http.StatusBadRequest, nil},
} { } {
rnr, cancel := testRunner(t) 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)) body := bytes.NewBuffer([]byte(test.body))

View File

@@ -13,11 +13,10 @@ import (
"gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/mqs" "gitlab-odx.oracle.com/odx/functions/api/mqs"
"gitlab-odx.oracle.com/odx/functions/api/runner" "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" "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() ctx := context.Background()
s := &Server{ s := &Server{
@@ -25,7 +24,6 @@ func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Ru
Router: gin.New(), Router: gin.New(),
Datastore: ds, Datastore: ds,
MQ: mq, MQ: mq,
tasks: tasks,
Enqueue: enqueue, Enqueue: enqueue,
hotroutes: routecache.New(10), hotroutes: routecache.New(10),
} }
@@ -39,7 +37,6 @@ func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Ru
} }
func TestRouteRunnerAsyncExecution(t *testing.T) { func TestRouteRunnerAsyncExecution(t *testing.T) {
tasks := mockTasksConduit()
ds := datastore.NewMockInit( ds := datastore.NewMockInit(
[]*models.App{ []*models.App{
{Name: "myapp", Config: map[string]string{"app": "true"}}, {Name: "myapp", Config: map[string]string{"app": "true"}},
@@ -82,7 +79,7 @@ func TestRouteRunnerAsyncExecution(t *testing.T) {
wg.Add(1) wg.Add(1)
fmt.Println("About to start router") fmt.Println("About to start router")
rnr, cancel := testRunner(t) 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 { if test.body != task.Payload {
t.Errorf("Test %d: Expected task Payload to be the same as the test body", i) t.Errorf("Test %d: Expected task Payload to be the same as the test body", i)
} }

View File

@@ -11,7 +11,6 @@ import (
"gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/mqs" "gitlab-odx.oracle.com/odx/functions/api/mqs"
"gitlab-odx.oracle.com/odx/functions/api/runner" "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) { 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) { func TestRouteRunnerGet(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
defer cancel() defer cancel()
@@ -34,7 +32,7 @@ func TestRouteRunnerGet(t *testing.T) {
[]*models.App{ []*models.App{
{Name: "myapp", Config: models.Config{}}, {Name: "myapp", Config: models.Config{}},
}, nil, nil, }, nil, nil,
), &mqs.Mock{}, rnr, tasks) ), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -68,7 +66,6 @@ func TestRouteRunnerGet(t *testing.T) {
func TestRouteRunnerPost(t *testing.T) { func TestRouteRunnerPost(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := mockTasksConduit()
rnr, cancel := testRunner(t) rnr, cancel := testRunner(t)
defer cancel() defer cancel()
@@ -77,7 +74,7 @@ func TestRouteRunnerPost(t *testing.T) {
[]*models.App{ []*models.App{
{Name: "myapp", Config: models.Config{}}, {Name: "myapp", Config: models.Config{}},
}, nil, nil, }, nil, nil,
), &mqs.Mock{}, rnr, tasks) ), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -114,10 +111,6 @@ func TestRouteRunnerPost(t *testing.T) {
func TestRouteRunnerExecution(t *testing.T) { func TestRouteRunnerExecution(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
tasks := make(chan task.Request)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rnr, cancelrnr := testRunner(t) rnr, cancelrnr := testRunner(t)
defer cancelrnr() 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: "/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"}}}, {Path: "/myerror", AppName: "myapp", Image: "funcy/error", Headers: map[string][]string{"X-Function": {"Test"}}},
}, nil, }, nil,
), &mqs.Mock{}, rnr, tasks) ), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -171,10 +164,6 @@ func TestRouteRunnerTimeout(t *testing.T) {
t.Skip("doesn't work on old Ubuntu") t.Skip("doesn't work on old Ubuntu")
buf := setLogBuffer() buf := setLogBuffer()
tasks := make(chan task.Request)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rnr, cancelrnr := testRunner(t) rnr, cancelrnr := testRunner(t)
defer cancelrnr() defer cancelrnr()
@@ -185,7 +174,7 @@ func TestRouteRunnerTimeout(t *testing.T) {
[]*models.Route{ []*models.Route{
{Path: "/sleeper", AppName: "myapp", Image: "funcy/sleeper", Timeout: 1}, {Path: "/sleeper", AppName: "myapp", Image: "funcy/sleeper", Timeout: 1},
}, nil, }, nil,
), &mqs.Mock{}, rnr, tasks) ), &mqs.Mock{}, rnr)
for i, test := range []struct { for i, test := range []struct {
path string path string

View File

@@ -16,13 +16,12 @@ import (
"gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/mqs" "gitlab-odx.oracle.com/odx/functions/api/mqs"
"gitlab-odx.oracle.com/odx/functions/api/runner" "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" "gitlab-odx.oracle.com/odx/functions/api/server/internal/routecache"
) )
var tmpBolt = "/tmp/func_test_bolt.db" 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() ctx := context.Background()
s := &Server{ s := &Server{
@@ -30,7 +29,6 @@ func testServer(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner,
Router: gin.New(), Router: gin.New(),
Datastore: ds, Datastore: ds,
MQ: mq, MQ: mq,
tasks: tasks,
Enqueue: DefaultEnqueue, Enqueue: DefaultEnqueue,
hotroutes: routecache.New(2), hotroutes: routecache.New(2),
} }
@@ -97,14 +95,10 @@ func TestFullStack(t *testing.T) {
ds, closeBolt := prepareBolt(t) ds, closeBolt := prepareBolt(t)
defer closeBolt() defer closeBolt()
tasks := make(chan task.Request)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rnr, rnrcancel := testRunner(t) rnr, rnrcancel := testRunner(t)
defer rnrcancel() defer rnrcancel()
srv := testServer(ds, &mqs.Mock{}, rnr, tasks) srv := testServer(ds, &mqs.Mock{}, rnr)
srv.hotroutes = routecache.New(2) srv.hotroutes = routecache.New(2)
for _, test := range []struct { for _, test := range []struct {