mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
refactor runner
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestAppCreate(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -52,7 +52,7 @@ func TestAppCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppDelete(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -83,7 +83,7 @@ func TestAppDelete(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppList(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -113,7 +113,7 @@ func TestAppList(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppGet(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -143,7 +143,7 @@ func TestAppGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppUpdate(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/runner"
|
||||
titancommon "github.com/iron-io/titan/common"
|
||||
)
|
||||
|
||||
@@ -47,6 +48,14 @@ func testRouter() *gin.Engine {
|
||||
return r
|
||||
}
|
||||
|
||||
func testRunner(t *testing.T) *runner.Runner {
|
||||
r, err := runner.New()
|
||||
if err != nil {
|
||||
t.Fatal("Test: failed to create new runner")
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func routerRequest(t *testing.T, router *gin.Engine, method, path string, body io.Reader) (*http.Request, *httptest.ResponseRecorder) {
|
||||
req, err := http.NewRequest(method, "http://localhost:8080"+path, body)
|
||||
if err != nil {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestRouteCreate(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -52,7 +52,7 @@ func TestRouteCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteDelete(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -83,7 +83,7 @@ func TestRouteDelete(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteList(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -113,7 +113,7 @@ func TestRouteList(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteGet(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -143,7 +143,7 @@ func TestRouteGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteUpdate(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@@ -81,7 +82,6 @@ func handleRunner(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsNotFound))
|
||||
return
|
||||
}
|
||||
|
||||
route := c.Param("route")
|
||||
if route == "" {
|
||||
route = c.Request.URL.Path
|
||||
@@ -109,17 +109,19 @@ func handleRunner(c *gin.Context) {
|
||||
log.WithField("routes", routes).Debug("Got routes from datastore")
|
||||
for _, el := range routes {
|
||||
if el.Path == route {
|
||||
run := runner.New(&runner.Config{
|
||||
Ctx: c,
|
||||
var stdout, stderr bytes.Buffer
|
||||
cfg := &runner.Config{
|
||||
Route: el,
|
||||
Payload: string(payload),
|
||||
Timeout: 30 * time.Second,
|
||||
ID: reqID,
|
||||
RequestURL: c.Request.URL.String(),
|
||||
AppName: appName,
|
||||
})
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
}
|
||||
|
||||
if err := run.Run(); err != nil {
|
||||
if result, err := Api.Runner.Run(c, cfg); err != nil {
|
||||
log.WithError(err).Error(models.ErrRunnerRunRoute)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRunnerRunRoute))
|
||||
} else {
|
||||
@@ -127,10 +129,11 @@ func handleRunner(c *gin.Context) {
|
||||
c.Header(k, v[0])
|
||||
}
|
||||
|
||||
if run.Status() == "success" {
|
||||
c.Data(http.StatusOK, "", run.ReadOut())
|
||||
if result.Status() == "success" {
|
||||
c.Data(http.StatusOK, "", stdout.Bytes())
|
||||
} else {
|
||||
c.Data(http.StatusInternalServerError, "", run.ReadErr())
|
||||
log.WithFields(logrus.Fields{"app": appName, "route": el, "req_id": reqID}).Debug(stderr.String())
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func TestRouteRunnerGet(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -45,7 +45,7 @@ func TestRouteRunnerGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteRunnerPost(t *testing.T) {
|
||||
New(&datastore.Mock{}, &models.Config{})
|
||||
New(&models.Config{}, &datastore.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -81,12 +81,12 @@ func TestRouteRunnerPost(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteRunnerExecution(t *testing.T) {
|
||||
New(&datastore.Mock{
|
||||
New(&models.Config{}, &datastore.Mock{
|
||||
FakeRoutes: []*models.Route{
|
||||
{Path: "/myroute", Image: "iron/hello", Headers: map[string][]string{"X-Function": []string{"Test"}}},
|
||||
{Path: "/myerror", Image: "iron/error", Headers: map[string][]string{"X-Function": []string{"Test"}}},
|
||||
},
|
||||
}, &models.Config{})
|
||||
}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
@@ -97,6 +97,10 @@ func TestRouteRunnerExecution(t *testing.T) {
|
||||
}{
|
||||
{"/r/myapp/myroute", ``, http.StatusOK, map[string][]string{"X-Function": []string{"Test"}}},
|
||||
{"/r/myapp/myerror", ``, http.StatusInternalServerError, map[string][]string{"X-Function": []string{"Test"}}},
|
||||
|
||||
// Added same tests again to check if time is reduced by the auth cache
|
||||
{"/r/myapp/myroute", ``, http.StatusOK, map[string][]string{"X-Function": []string{"Test"}}},
|
||||
{"/r/myapp/myerror", ``, http.StatusInternalServerError, map[string][]string{"X-Function": []string{"Test"}}},
|
||||
} {
|
||||
body := bytes.NewBuffer([]byte(test.body))
|
||||
_, rec := routerRequest(t, router, "GET", test.path, body)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api/ifaces"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/runner"
|
||||
titancommon "github.com/iron-io/titan/common"
|
||||
)
|
||||
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
var Api *Server
|
||||
|
||||
type Server struct {
|
||||
Runner *runner.Runner
|
||||
Router *gin.Engine
|
||||
Config *models.Config
|
||||
Datastore models.Datastore
|
||||
@@ -24,11 +26,12 @@ type Server struct {
|
||||
SpecialHandlers []ifaces.SpecialHandler
|
||||
}
|
||||
|
||||
func New(ds models.Datastore, config *models.Config) *Server {
|
||||
func New(c *models.Config, ds models.Datastore, r *runner.Runner) *Server {
|
||||
Api = &Server{
|
||||
Router: gin.Default(),
|
||||
Config: config,
|
||||
Config: c,
|
||||
Datastore: ds,
|
||||
Runner: r,
|
||||
}
|
||||
return Api
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestFullStack(t *testing.T) {
|
||||
ds, close := prepareBolt(t)
|
||||
defer close()
|
||||
|
||||
New(ds, &models.Config{})
|
||||
New(&models.Config{}, ds, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
for i, test := range []struct {
|
||||
|
||||
Reference in New Issue
Block a user