mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* license: assert license for Go code * license: add in shell scripts * license: assert license for Ruby code * license: assert license to individual cases * license: assert license to Dockerfile
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
// Copyright 2016 Iron.io
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/iron-io/functions/api/datastore"
|
|
"github.com/iron-io/functions/api/models"
|
|
"github.com/iron-io/functions/api/mqs"
|
|
"github.com/iron-io/runner/common"
|
|
)
|
|
|
|
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) {
|
|
ctx, _ := common.LoggerWithFields(ctx, extractFields(c))
|
|
c.Set("ctx", ctx)
|
|
c.Next()
|
|
})
|
|
s.bindHandlers()
|
|
return r
|
|
}
|
|
|
|
func TestRouteRunnerAsyncExecution(t *testing.T) {
|
|
t.Skip()
|
|
// todo: I broke how this test works trying to clean up the code a bit. Is there a better way to do this test rather than having to override the default route behavior?
|
|
s := New(&datastore.Mock{
|
|
FakeApps: []*models.App{
|
|
{Name: "myapp", Config: map[string]string{"app": "true"}},
|
|
},
|
|
FakeRoutes: []*models.Route{
|
|
{Type: "async", Path: "/myroute", AppName: "myapp", Image: "iron/hello", Config: map[string]string{"test": "true"}},
|
|
{Type: "async", Path: "/myerror", AppName: "myapp", Image: "iron/error", Config: map[string]string{"test": "true"}},
|
|
{Type: "async", Path: "/myroute/:param", AppName: "myapp", Image: "iron/hello", Config: map[string]string{"test": "true"}},
|
|
},
|
|
}, &mqs.Mock{}, testRunner(t))
|
|
|
|
for i, test := range []struct {
|
|
path string
|
|
body string
|
|
headers map[string][]string
|
|
expectedCode int
|
|
expectedEnv map[string]string
|
|
}{
|
|
{"/r/myapp/myroute", ``, map[string][]string{}, http.StatusOK, map[string]string{"CONFIG_TEST": "true", "CONFIG_APP": "true"}},
|
|
{
|
|
"/r/myapp/myroute/1",
|
|
``,
|
|
map[string][]string{"X-Function": []string{"test"}},
|
|
http.StatusOK,
|
|
map[string]string{
|
|
"CONFIG_TEST": "true",
|
|
"CONFIG_APP": "true",
|
|
"PARAM_PARAM": "1",
|
|
"HEADER_X_FUNCTION": "test",
|
|
},
|
|
},
|
|
{"/r/myapp/myerror", ``, map[string][]string{}, http.StatusOK, map[string]string{"CONFIG_TEST": "true", "CONFIG_APP": "true"}},
|
|
{"/r/myapp/myroute", `{ "name": "test" }`, map[string][]string{}, http.StatusOK, map[string]string{"CONFIG_TEST": "true", "CONFIG_APP": "true"}},
|
|
} {
|
|
body := bytes.NewBuffer([]byte(test.body))
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
fmt.Println("About to start router")
|
|
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)
|
|
}
|
|
|
|
if test.expectedEnv != nil {
|
|
for name, value := range test.expectedEnv {
|
|
if value != task.EnvVars[name] {
|
|
t.Errorf("Test %d: Expected header `%s` to be `%s` but was `%s`",
|
|
i, name, value, task.EnvVars[name])
|
|
}
|
|
}
|
|
}
|
|
|
|
wg.Done()
|
|
return task, nil
|
|
})
|
|
|
|
fmt.Println("makeing requests")
|
|
req, rec := newRouterRequest(t, "POST", test.path, body)
|
|
for name, value := range test.headers {
|
|
req.Header.Set(name, value[0])
|
|
}
|
|
fmt.Println("About to start router2")
|
|
router.ServeHTTP(rec, req)
|
|
fmt.Println("after servehttp")
|
|
|
|
if rec.Code != test.expectedCode {
|
|
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
|
i, test.expectedCode, rec.Code)
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
}
|