mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Rewrite HTTP handler for routes HTTP POST/PUT/PATCH methods
- adding tests for HTTP PUT - more tests for HTTP PATCH
This commit is contained in:
@@ -84,6 +84,7 @@ func CreateUpdateApp(t *testing.T, ctx context.Context, fnclient *client.Functio
|
||||
},
|
||||
Context: ctx,
|
||||
}
|
||||
cfg.WithTimeout(time.Second * 60)
|
||||
appPayload, err := fnclient.Apps.PatchAppsApp(cfg)
|
||||
CheckAppResponseError(t, err)
|
||||
return appPayload
|
||||
@@ -98,3 +99,14 @@ func DeleteApp(t *testing.T, ctx context.Context, fnclient *client.Functions, ap
|
||||
_, err := fnclient.Apps.DeleteAppsApp(cfg)
|
||||
CheckAppResponseError(t, err)
|
||||
}
|
||||
|
||||
func GetApp(t *testing.T, ctx context.Context, fnclient *client.Functions, appName string) *models.App {
|
||||
cfg := &apps.GetAppsAppParams{
|
||||
App: appName,
|
||||
Context: ctx,
|
||||
}
|
||||
cfg.WithTimeout(time.Second * 60)
|
||||
app, err := fnclient.Apps.GetAppsApp(cfg)
|
||||
CheckAppResponseError(t, err)
|
||||
return app.Payload.App
|
||||
}
|
||||
|
||||
@@ -41,14 +41,8 @@ func TestApps(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{"A": "a"})
|
||||
cfg := &apps.GetAppsAppParams{
|
||||
Context: s.Context,
|
||||
App: s.AppName,
|
||||
}
|
||||
appPayload, err := s.Client.Apps.GetAppsApp(cfg)
|
||||
CheckAppResponseError(t, err)
|
||||
appBody := appPayload.Payload.App
|
||||
val, ok := appBody.Config["A"]
|
||||
app := GetApp(t, s.Context, s.Client, s.AppName)
|
||||
val, ok := app.Config["A"]
|
||||
if !ok {
|
||||
t.Error("Error during app config inspect: config map misses required entity `A` with value `a`.")
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestCalls(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
cfg := &call.GetAppsAppCallsCallParams{
|
||||
Call: "dummy",
|
||||
@@ -71,7 +71,7 @@ func TestCalls(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -106,7 +106,7 @@ func TestCalls(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -84,7 +84,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -115,7 +115,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -141,7 +141,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -202,7 +202,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -253,7 +253,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -298,7 +298,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
image := "denismakogon/os.environ"
|
||||
routeType := "sync"
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -330,7 +330,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -372,7 +372,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
size := 1 * 1024 * 1024 * 1024
|
||||
u := url.URL{
|
||||
|
||||
@@ -55,19 +55,28 @@ func CheckRouteResponseError(t *testing.T, err error) {
|
||||
msg := err.(*routes.PatchAppsAppRoutesRouteDefault).Payload.Error.Message
|
||||
code := err.(*routes.PatchAppsAppRoutesRouteDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
case *routes.PutAppsAppRoutesRouteBadRequest:
|
||||
msg := err.(*routes.PutAppsAppRoutesRouteBadRequest).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
case *routes.PutAppsAppRoutesRouteDefault:
|
||||
msg := err.(*routes.PutAppsAppRoutesRouteDefault).Payload.Error.Message
|
||||
code := err.(*routes.PutAppsAppRoutesRouteDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
default:
|
||||
t.Errorf("Unable to determine type of error: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertRouteFields(t *testing.T, routeObject *models.Route, path, image, routeType string) {
|
||||
func assertRouteFields(t *testing.T, routeObject *models.Route, path, image, routeType, routeFormat string) {
|
||||
|
||||
rPath := routeObject.Path
|
||||
rImage := routeObject.Image
|
||||
rType := routeObject.Type
|
||||
rTimeout := *routeObject.Timeout
|
||||
rIdleTimeout := *routeObject.IDLETimeout
|
||||
rFormat := routeObject.Format
|
||||
|
||||
if rPath != path {
|
||||
t.Errorf("Route path mismatch. Expected: %v. Actual: %v", path, rPath)
|
||||
}
|
||||
@@ -83,6 +92,9 @@ func assertRouteFields(t *testing.T, routeObject *models.Route, path, image, rou
|
||||
if rIdleTimeout == 0 {
|
||||
t.Error("Route idle timeout should have default value of 30 seconds, but got 0 seconds")
|
||||
}
|
||||
if rFormat != routeFormat {
|
||||
t.Errorf("Route format mismatch. Expected: %v. Actual: %v", routeFormat, rFormat)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -105,11 +117,11 @@ func createRoute(ctx context.Context, fnclient *client.Functions, appName, image
|
||||
|
||||
}
|
||||
|
||||
func CreateRoute(t *testing.T, ctx context.Context, fnclient *client.Functions, appName, routePath, image, routeType string, routeConfig map[string]string, headers map[string][]string) {
|
||||
func CreateRoute(t *testing.T, ctx context.Context, fnclient *client.Functions, appName, routePath, image, routeType, routeFormat string, routeConfig map[string]string, headers map[string][]string) {
|
||||
routeResponse, err := createRoute(ctx, fnclient, appName, image, routePath, routeType, routeConfig, headers)
|
||||
CheckRouteResponseError(t, err)
|
||||
|
||||
assertRouteFields(t, routeResponse.Payload.Route, routePath, image, routeType)
|
||||
assertRouteFields(t, routeResponse.Payload.Route, routePath, image, routeType, routeFormat)
|
||||
}
|
||||
|
||||
func deleteRoute(ctx context.Context, fnclient *client.Functions, appName, routePath string) (*routes.DeleteAppsAppRoutesRouteOK, error) {
|
||||
@@ -218,3 +230,25 @@ func assertContainsRoute(routeModels []*models.Route, expectedRoute string) bool
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func DeployRoute(t *testing.T, ctx context.Context, fnclient *client.Functions, appName, routePath, image, routeType, routeFormat string, routeConfig map[string]string, headers map[string][]string) *models.Route {
|
||||
cfg := &routes.PutAppsAppRoutesRouteParams{
|
||||
App: appName,
|
||||
Context: ctx,
|
||||
Route: routePath,
|
||||
Body: &models.RouteWrapper{
|
||||
Route: &models.Route{
|
||||
Config: routeConfig,
|
||||
Headers: headers,
|
||||
Image: image,
|
||||
Path: routePath,
|
||||
Type: routeType,
|
||||
Format: routeFormat,
|
||||
},
|
||||
},
|
||||
}
|
||||
cfg.WithTimeout(time.Second * 60)
|
||||
route, err := fnclient.Routes.PutAppsAppRoutesRoute(cfg)
|
||||
CheckRouteResponseError(t, err)
|
||||
return route.Payload.Route
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
func TestRoutes(t *testing.T) {
|
||||
newRouteType := "sync"
|
||||
newRoutePath := id.New().String()
|
||||
|
||||
t.Run("create-route-with-empty-type", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
@@ -29,7 +28,7 @@ func TestRoutes(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
@@ -39,7 +38,7 @@ func TestRoutes(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
if !assertContainsRoute(ListRoutes(t, s.Context, s.Client, s.AppName), s.RoutePath) {
|
||||
t.Errorf("Unable to find corresponding route `%v` in list", s.RoutePath)
|
||||
}
|
||||
@@ -52,7 +51,7 @@ func TestRoutes(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
rObjects := []*models.Route{GetRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)}
|
||||
if !assertContainsRoute(rObjects, s.RoutePath) {
|
||||
@@ -68,7 +67,7 @@ func TestRoutes(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
routeResp, err := UpdateRoute(
|
||||
t, s.Context, s.Client,
|
||||
@@ -77,7 +76,31 @@ func TestRoutes(t *testing.T) {
|
||||
s.Memory, s.RouteConfig, s.RouteHeaders, "")
|
||||
|
||||
CheckRouteResponseError(t, err)
|
||||
assertRouteFields(t, routeResp.Payload.Route, s.RoutePath, s.Image, newRouteType)
|
||||
assertRouteFields(t, routeResp.Payload.Route, s.RoutePath, s.Image, newRouteType, s.Format)
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
t.Run("patch-route-with-config", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
newRouteConf := map[string]string{
|
||||
"A": "a",
|
||||
}
|
||||
|
||||
routeResp, err := UpdateRoute(
|
||||
t, s.Context, s.Client,
|
||||
s.AppName, s.RoutePath,
|
||||
s.Image, s.RouteType, s.Format,
|
||||
s.Memory, newRouteConf, s.RouteHeaders, "")
|
||||
|
||||
CheckRouteResponseError(t, err)
|
||||
assertRouteFields(t, routeResp.Payload.Route, s.RoutePath, s.Image, s.RouteType, s.Format)
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
@@ -88,7 +111,7 @@ func TestRoutes(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
_, err := UpdateRoute(
|
||||
t, s.Context, s.Client,
|
||||
@@ -108,7 +131,7 @@ func TestRoutes(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
_, err := createRoute(s.Context, s.Client, s.AppName, s.Image, s.RoutePath, newRouteType, s.RouteConfig, s.RouteHeaders)
|
||||
if err == nil {
|
||||
@@ -124,7 +147,7 @@ func TestRoutes(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
@@ -141,4 +164,43 @@ func TestRoutes(t *testing.T) {
|
||||
}
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
t.Run("deploy-route-without-existing-app", func(T *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
DeployRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType, s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
GetApp(t, s.Context, s.Client, s.AppName)
|
||||
GetRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
t.Run("deploy-route-with-existing-app", func(T *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
DeployRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType, s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
GetApp(t, s.Context, s.Client, s.AppName)
|
||||
GetRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
t.Run("deploy-update-with-existing-route-and-app", func(T *testing.T) {
|
||||
newRouteType := "sync"
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, s.RoutePath, s.Image, s.RouteType,
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
updatedRoute := DeployRoute(
|
||||
t, s.Context, s.Client,
|
||||
s.AppName, s.RoutePath,
|
||||
s.Image, newRouteType,
|
||||
s.Format, s.RouteConfig, s.RouteHeaders)
|
||||
assertRouteFields(t, updatedRoute, s.RoutePath, s.Image, newRouteType, s.Format)
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user