Unskip tests (#516)

* Unskip tests

* fix fn output for errors

* Change Error model and add fn routes call return on error
This commit is contained in:
Seif Lotfy سيف لطفي
2017-02-10 01:31:39 +01:00
committed by Pedro Nasser
parent 6214f1711a
commit ab9428a937
5 changed files with 38 additions and 21 deletions

View File

@@ -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

View File

@@ -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{

View File

@@ -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)
}
}

View File

@@ -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])
}
}
}

View File

@@ -260,6 +260,7 @@ func callfn(u string, content io.Reader, output io.Writer, method string, env []
}
io.Copy(output, resp.Body)
return nil
}