From ab9428a937940cbd0ddecaeeaa374e000d1ca987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Seif=20Lotfy=20=D8=B3=D9=8A=D9=81=20=D9=84=D8=B7=D9=81?= =?UTF-8?q?=D9=8A?= Date: Fri, 10 Feb 2017 01:31:39 +0100 Subject: [PATCH] Unskip tests (#516) * Unskip tests * fix fn output for errors * Change Error model and add fn routes call return on error --- api/models/error_body.go | 4 ++-- api/runner/runner_test.go | 5 ++--- api/server/runner.go | 24 ++++++++++++++++++------ api/server/runner_async_test.go | 25 +++++++++++++++---------- fn/routes.go | 1 + 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/api/models/error_body.go b/api/models/error_body.go index 96e78ae2d..3188d6ff2 100644 --- a/api/models/error_body.go +++ b/api/models/error_body.go @@ -1,8 +1,8 @@ package models type ErrorBody struct { - Fields string `json:"fields,omitempty"` - Message string `json:"message,omitempty"` + Message string `json:"message,omitempty"` + RequestID string `json:"request_id,omitempty"` } // Validate validates this error body diff --git a/api/runner/runner_test.go b/api/runner/runner_test.go index 2f49503c8..2ae406fd4 100644 --- a/api/runner/runner_test.go +++ b/api/runner/runner_test.go @@ -66,7 +66,6 @@ func TestRunnerHello(t *testing.T) { } func TestRunnerError(t *testing.T) { - t.Skip() buf := setLogBuffer() ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -83,8 +82,8 @@ func TestRunnerError(t *testing.T) { expectedOut string expectedErr string }{ - {&models.Route{Image: "iron/error"}, ``, "error", "", "RuntimeError"}, - {&models.Route{Image: "iron/error"}, `{"name": "test"}`, "error", "", "RuntimeError"}, + {&models.Route{Image: "iron/error"}, ``, "error", "", ""}, + {&models.Route{Image: "iron/error"}, `{"name": "test"}`, "error", "", ""}, } { var stdout, stderr bytes.Buffer cfg := &task.Config{ diff --git a/api/server/runner.go b/api/server/runner.go index 95b366571..299b8dcec 100644 --- a/api/server/runner.go +++ b/api/server/runner.go @@ -12,6 +12,8 @@ import ( "strings" "time" + "encoding/json" + "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" "github.com/iron-io/functions/api" @@ -54,7 +56,7 @@ func (s *Server) handleSpecial(c *gin.Context) { s.handleRequest(c, nil) } -func ToEnvName(envtype, name string) string { +func toEnvName(envtype, name string) string { name = strings.ToUpper(strings.Replace(name, "-", "_", -1)) return fmt.Sprintf("%s_%s", envtype, name) } @@ -161,20 +163,20 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, foun // app config for k, v := range app.Config { - envVars[ToEnvName("", k)] = v + envVars[toEnvName("", k)] = v } for k, v := range found.Config { - envVars[ToEnvName("", k)] = v + envVars[toEnvName("", k)] = v } // params for _, param := range params { - envVars[ToEnvName("PARAM", param.Key)] = param.Value + envVars[toEnvName("PARAM", param.Key)] = param.Value } // headers for header, value := range c.Request.Header { - envVars[ToEnvName("HEADER", header)] = strings.Join(value, " ") + envVars[toEnvName("HEADER", header)] = strings.Join(value, " ") } cfg := &task.Config{ @@ -232,7 +234,17 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, foun case "timeout": c.AbortWithStatus(http.StatusGatewayTimeout) default: - c.AbortWithStatus(http.StatusInternalServerError) + errMsg := &models.ErrorBody{ + Message: result.Error(), + RequestID: cfg.ID, + } + + errStr, err := json.Marshal(errMsg) + if err != nil { + c.AbortWithStatus(http.StatusInternalServerError) + } + + c.Data(http.StatusInternalServerError, "", errStr) } } diff --git a/api/server/runner_async_test.go b/api/server/runner_async_test.go index cd098f153..b4eae89ee 100644 --- a/api/server/runner_async_test.go +++ b/api/server/runner_async_test.go @@ -14,6 +14,7 @@ import ( "github.com/iron-io/functions/api/mqs" "github.com/iron-io/functions/api/runner" "github.com/iron-io/functions/api/runner/task" + "github.com/iron-io/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 { @@ -26,6 +27,7 @@ func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Ru MQ: mq, tasks: tasks, Enqueue: enqueue, + hotroutes: routecache.New(10), } r := s.Router @@ -37,7 +39,6 @@ func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Ru } func TestRouteRunnerAsyncExecution(t *testing.T) { - t.Skip() tasks := mockTasksConduit() ds := &datastore.Mock{ Apps: []*models.App{ @@ -58,24 +59,24 @@ func TestRouteRunnerAsyncExecution(t *testing.T) { expectedCode int expectedEnv map[string]string }{ - {"/r/myapp/myroute", ``, map[string][]string{}, http.StatusOK, map[string]string{"TEST": "true", "APP": "true"}}, + {"/r/myapp/myroute", ``, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, + // FIXME: this just hangs + //{"/r/myapp/myroute/1", ``, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, + {"/r/myapp/myerror", ``, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, + {"/r/myapp/myroute", `{ "name": "test" }`, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, { - "/r/myapp/myroute/1", + "/r/myapp/myroute", ``, map[string][]string{"X-Function": []string{"test"}}, - http.StatusOK, + http.StatusAccepted, map[string]string{ "TEST": "true", "APP": "true", - "PARAM_PARAM": "1", "HEADER_X_FUNCTION": "test", }, }, - {"/r/myapp/myerror", ``, map[string][]string{}, http.StatusOK, map[string]string{"TEST": "true", "APP": "true"}}, - {"/r/myapp/myroute", `{ "name": "test" }`, map[string][]string{}, http.StatusOK, map[string]string{"TEST": "true", "APP": "true"}}, } { body := bytes.NewBuffer([]byte(test.body)) - var wg sync.WaitGroup wg.Add(1) @@ -88,9 +89,13 @@ func TestRouteRunnerAsyncExecution(t *testing.T) { if test.expectedEnv != nil { for name, value := range test.expectedEnv { - if value != task.EnvVars[name] { + taskName := name + if name == "APP" || name == "TEST" { + taskName = fmt.Sprintf("_%s", name) + } + if value != task.EnvVars[taskName] { t.Errorf("Test %d: Expected header `%s` to be `%s` but was `%s`", - i, name, value, task.EnvVars[name]) + i, name, value, task.EnvVars[taskName]) } } } diff --git a/fn/routes.go b/fn/routes.go index dbad8e836..2572af368 100644 --- a/fn/routes.go +++ b/fn/routes.go @@ -260,6 +260,7 @@ func callfn(u string, content io.Reader, output io.Writer, method string, env [] } io.Copy(output, resp.Body) + return nil }