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
for i := 0; i < n; i++ {
wg.Add(1)
go startAsyncRunners(ctx, &wg, i, u)
go startAsyncRunners(ctx, &wg, i, u, runTask)
}
wg.Wait()
@@ -124,7 +124,8 @@ func isHostOpen(host string) bool {
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})
defer wg.Done()
for {

View File

@@ -98,7 +98,7 @@ func TestRunTask(t *testing.T) {
mockTask := getMockTask()
mockTask.Image = &helloImage
result, err := runTask(&mockTask)
result, err := runTask(context.Background(), &mockTask)
if err != nil {
t.Error(err)
}
@@ -116,7 +116,7 @@ func TestGetTask(t *testing.T) {
defer ts.Close()
url := ts.URL + "/tasks"
task, err := getTask(url)
task, err := getTask(context.Background(), url)
if err != nil {
t.Log(buf.String())
t.Error("expected no error, got", err)
@@ -149,7 +149,7 @@ func TestGetTaskError(t *testing.T) {
for i, test := range tests {
url := ts.URL + test["url"].(string)
_, err := getTask(url)
_, err := getTask(context.Background(), url)
if err == nil {
t.Log(buf.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)
}
_, err = getTask(url)
_, err = getTask(context.Background(), url)
if err != nil {
t.Log(buf.String())
t.Error("expected no error, got", err)
@@ -190,22 +190,23 @@ func TestDeleteTask(t *testing.T) {
func TestTasksrvURL(t *testing.T) {
tests := []struct {
port, in, out string
in, out string
}{
{"8080", "//127.0.0.1", "http://127.0.0.1:8080/tasks"},
{"8080", "//127.0.0.1/", "http://127.0.0.1:8080/tasks"},
{"8080", "//127.0.0.1:8081", "http://127.0.0.1:8081/tasks"},
{"8080", "//127.0.0.1:8081/", "http://127.0.0.1:8081/tasks"},
{"8080", "http://127.0.0.1", "http://127.0.0.1:8080/tasks"},
{"8080", "http://127.0.0.1/", "http://127.0.0.1:8080/tasks"},
{"8080", "http://127.0.0.1:8081", "http://127.0.0.1:8081/tasks"},
{"8080", "http://127.0.0.1:8081/", "http://127.0.0.1:8081/tasks"},
{"8080", "http://127.0.0.1:8081/endpoint", "http://127.0.0.1:8081/endpoint"},
// 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
// {"//localhost", "http://localhost:8080/tasks"},
// {"//localhost/", "http://localhost:8080/tasks"},
{"//localhost:8081", "http://localhost:8081/tasks"},
{"//localhost:8081/", "http://localhost:8081/tasks"},
// {"http://localhost", "http://localhost:8080/tasks"},
// {"http://localhost/", "http://localhost:8080/tasks"},
{"http://localhost:8081", "http://localhost:8081/tasks"},
{"http://localhost:8081/", "http://localhost:8081/tasks"},
{"http://localhost:8081/endpoint", "http://localhost:8081/endpoint"},
}
for _, tt := range tests {
if got, _ := tasksrvURL(tt.in, tt.port); got != tt.out {
t.Errorf("port: %s\ttasksrv: %s\texpected: %s\tgot: %s", tt.port, tt.in, tt.out, got)
if got, _ := tasksrvURL(tt.in); got != tt.out {
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)
var wg sync.WaitGroup
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
})
wg.Wait()

View File

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

View File

@@ -14,8 +14,8 @@ import (
"github.com/iron-io/runner/common"
)
func testRouterAsync(enqueueFunc models.Enqueue) *gin.Engine {
r := gin.New()
func testRouterAsync(s *Server, enqueueFunc models.Enqueue) *gin.Engine {
r := s.Router
r.Use(gin.Logger())
ctx := context.Background()
r.Use(func(c *gin.Context) {
@@ -23,16 +23,12 @@ func testRouterAsync(enqueueFunc models.Enqueue) *gin.Engine {
c.Set("ctx", ctx)
c.Next()
})
bindHandlers(r,
func(ctx *gin.Context) {
handleRequest(ctx, enqueueFunc)
},
func(ctx *gin.Context) {})
s.bindHandlers()
return r
}
func TestRouteRunnerAsyncExecution(t *testing.T) {
New(&datastore.Mock{
s := New(&datastore.Mock{
FakeApps: []*models.App{
{Name: "myapp", Config: map[string]string{"app": "true"}},
},
@@ -71,7 +67,7 @@ func TestRouteRunnerAsyncExecution(t *testing.T) {
var wg sync.WaitGroup
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 {
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) {
buf := setLogBuffer()
New(&datastore.Mock{
s := New(&datastore.Mock{
FakeApps: []*models.App{
{Name: "myapp", Config: models.Config{}},
},
}, &mqs.Mock{}, testRunner(t))
router := testRouter()
router := testRouter(s)
for i, test := range []struct {
path string
@@ -62,12 +62,12 @@ func TestRouteRunnerGet(t *testing.T) {
func TestRouteRunnerPost(t *testing.T) {
buf := setLogBuffer()
New(&datastore.Mock{
s := New(&datastore.Mock{
FakeApps: []*models.App{
{Name: "myapp", Config: models.Config{}},
},
}, &mqs.Mock{}, testRunner(t))
router := testRouter()
router := testRouter(s)
for i, test := range []struct {
path string
@@ -104,7 +104,7 @@ func TestRouteRunnerPost(t *testing.T) {
func TestRouteRunnerExecution(t *testing.T) {
buf := setLogBuffer()
New(&datastore.Mock{
s := New(&datastore.Mock{
FakeApps: []*models.App{
{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"}}},
},
}, &mqs.Mock{}, testRunner(t))
router := testRouter()
router := testRouter(s)
for i, test := range []struct {
path string

View File

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