Fix tests better errors and cleanup, remove panics

This commit is contained in:
James
2017-08-18 12:36:18 -07:00
parent 17dc103d84
commit 461959cc29
4 changed files with 123 additions and 72 deletions

View File

@@ -4,48 +4,44 @@ import (
"context" "context"
"strings" "strings"
"testing" "testing"
"time"
"github.com/funcy/functions_go/client" "github.com/funcy/functions_go/client"
"github.com/funcy/functions_go/client/apps" "github.com/funcy/functions_go/client/apps"
"github.com/funcy/functions_go/models" "github.com/funcy/functions_go/models"
) )
func CheckAppResponseError(t *testing.T, err error) { func CheckAppResponseError(t *testing.T, e error) {
if err != nil { if e != nil {
switch err.(type) { switch err := e.(type) {
case *apps.DeleteAppsAppDefault: case *apps.DeleteAppsAppDefault:
msg := err.(*apps.DeleteAppsAppDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
code := err.(*apps.DeleteAppsAppDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *apps.PostAppsDefault: case *apps.PostAppsDefault:
msg := err.(*apps.PostAppsDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
code := err.(*apps.PostAppsDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *apps.GetAppsAppNotFound: case *apps.GetAppsAppNotFound:
msg := err.(*apps.GetAppsAppNotFound).Payload.Error.Message if !strings.Contains("App not found", err.Payload.Error.Message) {
if !strings.Contains("App not found", msg) { t.Errorf("Unexpected error occurred: %v Original Location: %s", err.Payload.Error.Message, MyCaller())
t.Errorf("Unexpected error occurred: %v", msg) t.FailNow()
} }
case *apps.GetAppsAppDefault: case *apps.GetAppsAppDefault:
msg := err.(*apps.GetAppsAppDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
code := err.(*apps.GetAppsAppDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *apps.PatchAppsAppDefault: case *apps.PatchAppsAppDefault:
msg := err.(*apps.PatchAppsAppDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
code := err.(*apps.PatchAppsAppDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *apps.PatchAppsAppNotFound: case *apps.PatchAppsAppNotFound:
msg := err.(*apps.PatchAppsAppNotFound).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Original Location: %s", err.Payload.Error.Message, MyCaller())
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *apps.PatchAppsAppBadRequest: case *apps.PatchAppsAppBadRequest:
msg := err.(*apps.PatchAppsAppBadRequest).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Original Location: %s", err.Payload.Error.Message, MyCaller())
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
default: 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) { 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, Context: ctx,
} }
ok, err := fnclient.Apps.PostApps(cfg)
return 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) { 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) CheckAppResponseError(t, err)
return app.Payload.App 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)
}

View File

@@ -8,6 +8,7 @@ import (
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags // call flag.Parse() here if TestMain uses flags
s := SetupDefaultSuite() s := SetupDefaultSuite()
defer Cleanup()
defer s.Cancel() defer s.Cancel()
os.Exit(m.Run()) os.Exit(m.Run())
} }

View File

@@ -9,60 +9,52 @@ import (
"github.com/funcy/functions_go/models" "github.com/funcy/functions_go/models"
) )
func CheckRouteResponseError(t *testing.T, err error) { func CheckRouteResponseError(t *testing.T, e error) {
if err != nil { if e != nil {
switch err.(type) { switch err := e.(type) {
case *routes.PostAppsAppRoutesDefault: case *routes.PostAppsAppRoutesDefault:
msg := err.(*routes.PostAppsAppRoutesDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
code := err.(*routes.PostAppsAppRoutesDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *routes.PostAppsAppRoutesBadRequest: case *routes.PostAppsAppRoutesBadRequest:
msg := err.(*routes.PostAppsAppRoutesBadRequest).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *routes.PostAppsAppRoutesConflict: case *routes.PostAppsAppRoutesConflict:
msg := err.(*routes.PostAppsAppRoutesConflict).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *routes.GetAppsAppRoutesRouteNotFound: case *routes.GetAppsAppRoutesRouteNotFound:
msg := err.(*routes.GetAppsAppRoutesRouteNotFound).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *routes.GetAppsAppRoutesRouteDefault: case *routes.GetAppsAppRoutesRouteDefault:
msg := err.(*routes.GetAppsAppRoutesRouteDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
code := err.(*routes.GetAppsAppRoutesRouteDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *routes.DeleteAppsAppRoutesRouteNotFound: case *routes.DeleteAppsAppRoutesRouteNotFound:
msg := err.(*routes.DeleteAppsAppRoutesRouteNotFound).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *routes.DeleteAppsAppRoutesRouteDefault: case *routes.DeleteAppsAppRoutesRouteDefault:
msg := err.(*routes.DeleteAppsAppRoutesRouteDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
code := err.(*routes.DeleteAppsAppRoutesRouteDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *routes.GetAppsAppRoutesNotFound: case *routes.GetAppsAppRoutesNotFound:
msg := err.(*routes.GetAppsAppRoutesNotFound).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *routes.GetAppsAppRoutesDefault: case *routes.GetAppsAppRoutesDefault:
msg := err.(*routes.GetAppsAppRoutesDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
code := err.(*routes.GetAppsAppRoutesDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *routes.PatchAppsAppRoutesRouteBadRequest: case *routes.PatchAppsAppRoutesRouteBadRequest:
msg := err.(*routes.PatchAppsAppRoutesRouteBadRequest).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *routes.PatchAppsAppRoutesRouteNotFound: case *routes.PatchAppsAppRoutesRouteNotFound:
msg := err.(*routes.PatchAppsAppRoutesRouteNotFound).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg) t.FailNow()
case *routes.PatchAppsAppRoutesRouteDefault: case *routes.PatchAppsAppRoutesRouteDefault:
msg := err.(*routes.PatchAppsAppRoutesRouteDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
code := err.(*routes.PatchAppsAppRoutesRouteDefault).Code()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
case *routes.PutAppsAppRoutesRouteBadRequest: case *routes.PutAppsAppRoutesRouteBadRequest:
msg := err.(*routes.PutAppsAppRoutesRouteBadRequest).Payload.Error.Message t.Errorf("Unexpected error occurred: %v.", err.Payload.Error.Message)
t.Errorf("Unexpected error occurred: %v.", msg)
case *routes.PutAppsAppRoutesRouteDefault: case *routes.PutAppsAppRoutesRouteDefault:
msg := err.(*routes.PutAppsAppRoutesRouteDefault).Payload.Error.Message t.Errorf("Unexpected error occurred: %v. Status code: %v", err.Payload.Error.Message, err.Code())
code := err.(*routes.PutAppsAppRoutesRouteDefault).Code() t.FailNow()
t.Errorf("Unexpected error occurred: %v. Status code: %v", msg, code)
default: default:
t.Errorf("Unable to determine type of error: %s", err) 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, Context: ctx,
} }
ok, err := fnclient.Routes.PostAppsAppRoutes(cfg)
return 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
} }

View File

@@ -2,6 +2,7 @@ package tests
import ( import (
"context" "context"
"runtime"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -48,9 +49,11 @@ func APIClient() *client.Functions {
} }
var ( var (
getServer sync.Once getServer sync.Once
cancel2 context.CancelFunc cancel2 context.CancelFunc
s *server.Server s *server.Server
appsandroutes = make(map[string][]string)
approutesLock sync.Mutex
) )
func getServerWithCancel() (*server.Server, context.CancelFunc) { func getServerWithCancel() (*server.Server, context.CancelFunc) {
@@ -122,8 +125,8 @@ func SetupDefaultSuite() *SuiteSetup {
ss := &SuiteSetup{ ss := &SuiteSetup{
Context: ctx, Context: ctx,
Client: APIClient(), Client: APIClient(),
AppName: RandStringBytes(10), AppName: "fnintegrationtestapp" + RandStringBytes(10),
RoutePath: "/" + RandStringBytes(10), RoutePath: "/fnintegrationtestroute" + RandStringBytes(10),
Image: "funcy/hello", Image: "funcy/hello",
Format: "default", Format: "default",
RouteType: "async", RouteType: "async",
@@ -150,6 +153,20 @@ func SetupDefaultSuite() *SuiteSetup {
return ss 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) { func EnvAsHeader(req *http.Request, selectedEnv []string) {
detectedEnv := os.Environ() detectedEnv := os.Environ()
if len(selectedEnv) > 0 { if len(selectedEnv) > 0 {
@@ -192,3 +209,21 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env []
return nil 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)
}