mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Fix tests better errors and cleanup, remove panics
This commit is contained in:
@@ -4,48 +4,44 @@ import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/funcy/functions_go/client"
|
||||
"github.com/funcy/functions_go/client/apps"
|
||||
"github.com/funcy/functions_go/models"
|
||||
)
|
||||
|
||||
func CheckAppResponseError(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
|
||||
func CheckAppResponseError(t *testing.T, e error) {
|
||||
if e != nil {
|
||||
switch err := e.(type) {
|
||||
case *apps.DeleteAppsAppDefault:
|
||||
msg := err.(*apps.DeleteAppsAppDefault).Payload.Error.Message
|
||||
code := err.(*apps.DeleteAppsAppDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
|
||||
t.FailNow()
|
||||
case *apps.PostAppsDefault:
|
||||
msg := err.(*apps.PostAppsDefault).Payload.Error.Message
|
||||
code := err.(*apps.PostAppsDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
|
||||
t.FailNow()
|
||||
case *apps.GetAppsAppNotFound:
|
||||
msg := err.(*apps.GetAppsAppNotFound).Payload.Error.Message
|
||||
if !strings.Contains("App not found", msg) {
|
||||
t.Errorf("Unexpected error occurred: %v", msg)
|
||||
if !strings.Contains("App not found", err.Payload.Error.Message) {
|
||||
t.Errorf("Unexpected error occurred: %v Original Location: %s", err.Payload.Error.Message, MyCaller())
|
||||
t.FailNow()
|
||||
}
|
||||
case *apps.GetAppsAppDefault:
|
||||
msg := err.(*apps.GetAppsAppDefault).Payload.Error.Message
|
||||
code := err.(*apps.GetAppsAppDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
|
||||
t.FailNow()
|
||||
case *apps.PatchAppsAppDefault:
|
||||
msg := err.(*apps.PatchAppsAppDefault).Payload.Error.Message
|
||||
code := err.(*apps.PatchAppsAppDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
|
||||
t.FailNow()
|
||||
case *apps.PatchAppsAppNotFound:
|
||||
msg := err.(*apps.PatchAppsAppNotFound).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v. Original Location: %s", err.Payload.Error.Message, MyCaller())
|
||||
t.FailNow()
|
||||
case *apps.PatchAppsAppBadRequest:
|
||||
msg := err.(*apps.PatchAppsAppBadRequest).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v. Original Location: %s", err.Payload.Error.Message, MyCaller())
|
||||
t.FailNow()
|
||||
default:
|
||||
t.Errorf("Unable to determine type of error: %s", err)
|
||||
t.Errorf("Unable to determine type of error: %s Original Location: %s", err, MyCaller())
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func CreateAppNoAssert(ctx context.Context, fnclient *client.Functions, appName string, config map[string]string) (*apps.PostAppsOK, error) {
|
||||
@@ -58,8 +54,16 @@ func CreateAppNoAssert(ctx context.Context, fnclient *client.Functions, appName
|
||||
},
|
||||
Context: ctx,
|
||||
}
|
||||
|
||||
return fnclient.Apps.PostApps(cfg)
|
||||
ok, err := fnclient.Apps.PostApps(cfg)
|
||||
if err == nil {
|
||||
approutesLock.Lock()
|
||||
_, got := appsandroutes[appName]
|
||||
if !got {
|
||||
appsandroutes[appName] = []string{}
|
||||
}
|
||||
approutesLock.Unlock()
|
||||
}
|
||||
return ok, err
|
||||
}
|
||||
|
||||
func CreateApp(t *testing.T, ctx context.Context, fnclient *client.Functions, appName string, config map[string]string) {
|
||||
@@ -109,3 +113,12 @@ func GetApp(t *testing.T, ctx context.Context, fnclient *client.Functions, appNa
|
||||
CheckAppResponseError(t, err)
|
||||
return app.Payload.App
|
||||
}
|
||||
|
||||
func DeleteAppNoT(ctx context.Context, fnclient *client.Functions, appName string) {
|
||||
cfg := &apps.DeleteAppsAppParams{
|
||||
App: appName,
|
||||
Context: ctx,
|
||||
}
|
||||
cfg.WithTimeout(time.Second * 60)
|
||||
fnclient.Apps.DeleteAppsApp(cfg)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
func TestMain(m *testing.M) {
|
||||
// call flag.Parse() here if TestMain uses flags
|
||||
s := SetupDefaultSuite()
|
||||
defer Cleanup()
|
||||
defer s.Cancel()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
@@ -9,60 +9,52 @@ import (
|
||||
"github.com/funcy/functions_go/models"
|
||||
)
|
||||
|
||||
func CheckRouteResponseError(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
|
||||
func CheckRouteResponseError(t *testing.T, e error) {
|
||||
if e != nil {
|
||||
switch err := e.(type) {
|
||||
case *routes.PostAppsAppRoutesDefault:
|
||||
msg := err.(*routes.PostAppsAppRoutesDefault).Payload.Error.Message
|
||||
code := err.(*routes.PostAppsAppRoutesDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
|
||||
t.FailNow()
|
||||
case *routes.PostAppsAppRoutesBadRequest:
|
||||
msg := err.(*routes.PostAppsAppRoutesBadRequest).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
t.FailNow()
|
||||
case *routes.PostAppsAppRoutesConflict:
|
||||
msg := err.(*routes.PostAppsAppRoutesConflict).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
t.FailNow()
|
||||
case *routes.GetAppsAppRoutesRouteNotFound:
|
||||
msg := err.(*routes.GetAppsAppRoutesRouteNotFound).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
t.FailNow()
|
||||
case *routes.GetAppsAppRoutesRouteDefault:
|
||||
msg := err.(*routes.GetAppsAppRoutesRouteDefault).Payload.Error.Message
|
||||
code := err.(*routes.GetAppsAppRoutesRouteDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
|
||||
t.FailNow()
|
||||
case *routes.DeleteAppsAppRoutesRouteNotFound:
|
||||
msg := err.(*routes.DeleteAppsAppRoutesRouteNotFound).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
t.FailNow()
|
||||
case *routes.DeleteAppsAppRoutesRouteDefault:
|
||||
msg := err.(*routes.DeleteAppsAppRoutesRouteDefault).Payload.Error.Message
|
||||
code := err.(*routes.DeleteAppsAppRoutesRouteDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
|
||||
t.FailNow()
|
||||
case *routes.GetAppsAppRoutesNotFound:
|
||||
msg := err.(*routes.GetAppsAppRoutesNotFound).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
t.FailNow()
|
||||
case *routes.GetAppsAppRoutesDefault:
|
||||
msg := err.(*routes.GetAppsAppRoutesDefault).Payload.Error.Message
|
||||
code := err.(*routes.GetAppsAppRoutesDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
|
||||
t.FailNow()
|
||||
case *routes.PatchAppsAppRoutesRouteBadRequest:
|
||||
msg := err.(*routes.PatchAppsAppRoutesRouteBadRequest).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
t.FailNow()
|
||||
case *routes.PatchAppsAppRoutesRouteNotFound:
|
||||
msg := err.(*routes.PatchAppsAppRoutesRouteNotFound).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
t.FailNow()
|
||||
case *routes.PatchAppsAppRoutesRouteDefault:
|
||||
msg := err.(*routes.PatchAppsAppRoutesRouteDefault).Payload.Error.Message
|
||||
code := err.(*routes.PatchAppsAppRoutesRouteDefault).Code()
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
|
||||
case *routes.PutAppsAppRoutesRouteBadRequest:
|
||||
msg := err.(*routes.PutAppsAppRoutesRouteBadRequest).Payload.Error.Message
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
|
||||
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)
|
||||
t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
|
||||
t.FailNow()
|
||||
default:
|
||||
t.Errorf("Unable to determine type of error: %s", err)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,8 +103,18 @@ func createRoute(ctx context.Context, fnclient *client.Functions, appName, image
|
||||
},
|
||||
Context: ctx,
|
||||
}
|
||||
|
||||
return fnclient.Routes.PostAppsAppRoutes(cfg)
|
||||
ok, err := fnclient.Routes.PostAppsAppRoutes(cfg)
|
||||
if err == nil {
|
||||
approutesLock.Lock()
|
||||
r, got := appsandroutes[appName]
|
||||
if got {
|
||||
appsandroutes[appName] = append(r, routePath)
|
||||
} else {
|
||||
appsandroutes[appName] = []string{routePath}
|
||||
}
|
||||
approutesLock.Unlock()
|
||||
}
|
||||
return ok, err
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -48,9 +49,11 @@ func APIClient() *client.Functions {
|
||||
}
|
||||
|
||||
var (
|
||||
getServer sync.Once
|
||||
cancel2 context.CancelFunc
|
||||
s *server.Server
|
||||
getServer sync.Once
|
||||
cancel2 context.CancelFunc
|
||||
s *server.Server
|
||||
appsandroutes = make(map[string][]string)
|
||||
approutesLock sync.Mutex
|
||||
)
|
||||
|
||||
func getServerWithCancel() (*server.Server, context.CancelFunc) {
|
||||
@@ -122,8 +125,8 @@ func SetupDefaultSuite() *SuiteSetup {
|
||||
ss := &SuiteSetup{
|
||||
Context: ctx,
|
||||
Client: APIClient(),
|
||||
AppName: RandStringBytes(10),
|
||||
RoutePath: "/" + RandStringBytes(10),
|
||||
AppName: "fnintegrationtestapp" + RandStringBytes(10),
|
||||
RoutePath: "/fnintegrationtestroute" + RandStringBytes(10),
|
||||
Image: "funcy/hello",
|
||||
Format: "default",
|
||||
RouteType: "async",
|
||||
@@ -150,6 +153,20 @@ func SetupDefaultSuite() *SuiteSetup {
|
||||
return ss
|
||||
}
|
||||
|
||||
func Cleanup() {
|
||||
ctx := context.Background()
|
||||
c := APIClient()
|
||||
approutesLock.Lock()
|
||||
defer approutesLock.Unlock()
|
||||
for appName, rs := range appsandroutes {
|
||||
for _, routePath := range rs {
|
||||
deleteRoute(ctx, c, appName, routePath)
|
||||
}
|
||||
DeleteAppNoT(ctx, c, appName)
|
||||
}
|
||||
appsandroutes = make(map[string][]string)
|
||||
}
|
||||
|
||||
func EnvAsHeader(req *http.Request, selectedEnv []string) {
|
||||
detectedEnv := os.Environ()
|
||||
if len(selectedEnv) > 0 {
|
||||
@@ -192,3 +209,21 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env []
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func MyCaller() string {
|
||||
fpcs := make([]uintptr, 1)
|
||||
n := runtime.Callers(3, fpcs)
|
||||
if n == 0 {
|
||||
return "n/a"
|
||||
}
|
||||
fun := runtime.FuncForPC(fpcs[0] - 1)
|
||||
if fun == nil {
|
||||
return "n/a"
|
||||
}
|
||||
f, l := fun.FileLine(fpcs[0] - 1)
|
||||
return fmt.Sprintf("%s:%d", f, l)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user