Fixing tests.

This commit is contained in:
Travis Reeder
2016-10-13 08:30:23 -07:00
parent 3e443e604c
commit 74402bdfea
6 changed files with 47 additions and 53 deletions

View File

@@ -108,7 +108,7 @@ func RunAsyncRunner(ctx context.Context, tasksrv string, n int) {
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
wg.Add(1) wg.Add(1)
go startAsyncRunners(ctx, &wg, i, u) go startAsyncRunners(ctx, &wg, i, u, runTask)
} }
wg.Wait() wg.Wait()
@@ -124,7 +124,8 @@ func isHostOpen(host string) bool {
return available return available
} }
func startAsyncRunners(ctx context.Context, wg *sync.WaitGroup, i int, url string) { // todo: not a big fan of this anonymous function for testing, should use an interface and make a Mock object for testing - TR
func startAsyncRunners(ctx context.Context, wg *sync.WaitGroup, i int, url string, runTask func(ctx context.Context, task *models.Task) (drivers.RunResult, error)) {
ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"async_runner": i}) ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"async_runner": i})
defer wg.Done() defer wg.Done()
for { for {

View File

@@ -98,7 +98,7 @@ func TestRunTask(t *testing.T) {
mockTask := getMockTask() mockTask := getMockTask()
mockTask.Image = &helloImage mockTask.Image = &helloImage
result, err := runTask(&mockTask) result, err := runTask(context.Background(), &mockTask)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -116,7 +116,7 @@ func TestGetTask(t *testing.T) {
defer ts.Close() defer ts.Close()
url := ts.URL + "/tasks" url := ts.URL + "/tasks"
task, err := getTask(url) task, err := getTask(context.Background(), url)
if err != nil { if err != nil {
t.Log(buf.String()) t.Log(buf.String())
t.Error("expected no error, got", err) t.Error("expected no error, got", err)
@@ -149,7 +149,7 @@ func TestGetTaskError(t *testing.T) {
for i, test := range tests { for i, test := range tests {
url := ts.URL + test["url"].(string) url := ts.URL + test["url"].(string)
_, err := getTask(url) _, err := getTask(context.Background(), url)
if err == nil { if err == nil {
t.Log(buf.String()) t.Log(buf.String())
t.Errorf("expected error '%s'", test["error"].(string)) t.Errorf("expected error '%s'", test["error"].(string))
@@ -175,7 +175,7 @@ func TestDeleteTask(t *testing.T) {
t.Error("expected error 'Not reserver', got", err) t.Error("expected error 'Not reserver', got", err)
} }
_, err = getTask(url) _, err = getTask(context.Background(), url)
if err != nil { if err != nil {
t.Log(buf.String()) t.Log(buf.String())
t.Error("expected no error, got", err) t.Error("expected no error, got", err)
@@ -190,22 +190,23 @@ func TestDeleteTask(t *testing.T) {
func TestTasksrvURL(t *testing.T) { func TestTasksrvURL(t *testing.T) {
tests := []struct { tests := []struct {
port, in, out string in, out string
}{ }{
{"8080", "//127.0.0.1", "http://127.0.0.1:8080/tasks"}, // we shouldn't be swapping out no port with 8080, those would be 80. Also, we shouldn't accept anything but a full URL (base URL) in API_URL
{"8080", "//127.0.0.1/", "http://127.0.0.1:8080/tasks"}, // {"//localhost", "http://localhost:8080/tasks"},
{"8080", "//127.0.0.1:8081", "http://127.0.0.1:8081/tasks"}, // {"//localhost/", "http://localhost:8080/tasks"},
{"8080", "//127.0.0.1:8081/", "http://127.0.0.1:8081/tasks"}, {"//localhost:8081", "http://localhost:8081/tasks"},
{"8080", "http://127.0.0.1", "http://127.0.0.1:8080/tasks"}, {"//localhost:8081/", "http://localhost:8081/tasks"},
{"8080", "http://127.0.0.1/", "http://127.0.0.1:8080/tasks"}, // {"http://localhost", "http://localhost:8080/tasks"},
{"8080", "http://127.0.0.1:8081", "http://127.0.0.1:8081/tasks"}, // {"http://localhost/", "http://localhost:8080/tasks"},
{"8080", "http://127.0.0.1:8081/", "http://127.0.0.1:8081/tasks"}, {"http://localhost:8081", "http://localhost:8081/tasks"},
{"8080", "http://127.0.0.1:8081/endpoint", "http://127.0.0.1:8081/endpoint"}, {"http://localhost:8081/", "http://localhost:8081/tasks"},
{"http://localhost:8081/endpoint", "http://localhost:8081/endpoint"},
} }
for _, tt := range tests { for _, tt := range tests {
if got, _ := tasksrvURL(tt.in, tt.port); got != tt.out { if got, _ := tasksrvURL(tt.in); got != tt.out {
t.Errorf("port: %s\ttasksrv: %s\texpected: %s\tgot: %s", tt.port, tt.in, tt.out, got) t.Errorf("tasksrv: %s\texpected: %s\tgot: %s\t", tt.in, tt.out, got)
} }
} }
} }
@@ -219,7 +220,7 @@ func TestAsyncRunnersGracefulShutdown(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
go startAsyncRunners(ctx, &wg, 0, ts.URL+"/tasks", func(task *models.Task) (drivers.RunResult, error) { go startAsyncRunners(ctx, &wg, 0, ts.URL+"/tasks", func(ctx context.Context, task *models.Task) (drivers.RunResult, error) {
return nil, nil return nil, nil
}) })
wg.Wait() wg.Wait()

View File

@@ -13,8 +13,8 @@ import (
func TestRouteCreate(t *testing.T) { func TestRouteCreate(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t)) s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -57,8 +57,8 @@ func TestRouteCreate(t *testing.T) {
func TestRouteDelete(t *testing.T) { func TestRouteDelete(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t)) s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -91,8 +91,8 @@ func TestRouteDelete(t *testing.T) {
func TestRouteList(t *testing.T) { func TestRouteList(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t)) s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -124,8 +124,8 @@ func TestRouteList(t *testing.T) {
func TestRouteGet(t *testing.T) { func TestRouteGet(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t)) s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -157,8 +157,8 @@ func TestRouteGet(t *testing.T) {
func TestRouteUpdate(t *testing.T) { func TestRouteUpdate(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t)) s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string

View File

@@ -14,8 +14,8 @@ import (
"github.com/iron-io/runner/common" "github.com/iron-io/runner/common"
) )
func testRouterAsync(enqueueFunc models.Enqueue) *gin.Engine { func testRouterAsync(s *Server, enqueueFunc models.Enqueue) *gin.Engine {
r := gin.New() r := s.Router
r.Use(gin.Logger()) r.Use(gin.Logger())
ctx := context.Background() ctx := context.Background()
r.Use(func(c *gin.Context) { r.Use(func(c *gin.Context) {
@@ -23,16 +23,12 @@ func testRouterAsync(enqueueFunc models.Enqueue) *gin.Engine {
c.Set("ctx", ctx) c.Set("ctx", ctx)
c.Next() c.Next()
}) })
bindHandlers(r, s.bindHandlers()
func(ctx *gin.Context) {
handleRequest(ctx, enqueueFunc)
},
func(ctx *gin.Context) {})
return r return r
} }
func TestRouteRunnerAsyncExecution(t *testing.T) { func TestRouteRunnerAsyncExecution(t *testing.T) {
New(&datastore.Mock{ s := New(&datastore.Mock{
FakeApps: []*models.App{ FakeApps: []*models.App{
{Name: "myapp", Config: map[string]string{"app": "true"}}, {Name: "myapp", Config: map[string]string{"app": "true"}},
}, },
@@ -71,7 +67,7 @@ func TestRouteRunnerAsyncExecution(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
router := testRouterAsync(func(task *models.Task) (*models.Task, error) { router := testRouterAsync(s, func(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

@@ -23,12 +23,12 @@ func testRunner(t *testing.T) *runner.Runner {
func TestRouteRunnerGet(t *testing.T) { func TestRouteRunnerGet(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{ s := New(&datastore.Mock{
FakeApps: []*models.App{ FakeApps: []*models.App{
{Name: "myapp", Config: models.Config{}}, {Name: "myapp", Config: models.Config{}},
}, },
}, &mqs.Mock{}, testRunner(t)) }, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -62,12 +62,12 @@ func TestRouteRunnerGet(t *testing.T) {
func TestRouteRunnerPost(t *testing.T) { func TestRouteRunnerPost(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{ s := New(&datastore.Mock{
FakeApps: []*models.App{ FakeApps: []*models.App{
{Name: "myapp", Config: models.Config{}}, {Name: "myapp", Config: models.Config{}},
}, },
}, &mqs.Mock{}, testRunner(t)) }, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string
@@ -104,7 +104,7 @@ func TestRouteRunnerPost(t *testing.T) {
func TestRouteRunnerExecution(t *testing.T) { func TestRouteRunnerExecution(t *testing.T) {
buf := setLogBuffer() buf := setLogBuffer()
New(&datastore.Mock{ s := New(&datastore.Mock{
FakeApps: []*models.App{ FakeApps: []*models.App{
{Name: "myapp", Config: models.Config{}}, {Name: "myapp", Config: models.Config{}},
}, },
@@ -113,7 +113,7 @@ func TestRouteRunnerExecution(t *testing.T) {
{Path: "/myerror", AppName: "myapp", Image: "iron/error", Headers: map[string][]string{"X-Function": {"Test"}}}, {Path: "/myerror", AppName: "myapp", Image: "iron/error", Headers: map[string][]string{"X-Function": {"Test"}}},
}, },
}, &mqs.Mock{}, testRunner(t)) }, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
path string path string

View File

@@ -20,8 +20,8 @@ import (
var tmpBolt = "/tmp/func_test_bolt.db" var tmpBolt = "/tmp/func_test_bolt.db"
func testRouter() *gin.Engine { func testRouter(s *Server) *gin.Engine {
r := gin.New() r := s.Router
r.Use(gin.Logger()) r.Use(gin.Logger())
ctx := context.Background() ctx := context.Background()
r.Use(func(c *gin.Context) { r.Use(func(c *gin.Context) {
@@ -29,11 +29,7 @@ func testRouter() *gin.Engine {
c.Set("ctx", ctx) c.Set("ctx", ctx)
c.Next() c.Next()
}) })
bindHandlers(r, s.bindHandlers()
func(ctx *gin.Context) {
handleRequest(ctx, nil)
},
func(ctx *gin.Context) {})
return r return r
} }
@@ -90,8 +86,8 @@ func TestFullStack(t *testing.T) {
ds, close := prepareBolt(t) ds, close := prepareBolt(t)
defer close() defer close()
New(ds, &mqs.Mock{}, testRunner(t)) s := New(ds, &mqs.Mock{}, testRunner(t))
router := testRouter() router := testRouter(s)
for i, test := range []struct { for i, test := range []struct {
method string method string