Add annotations to routes and apps (#866)

Adds 'annotations' attribute to Routes and Apps
This commit is contained in:
Owen Cliffe
2018-03-20 18:02:49 +00:00
committed by GitHub
parent 845f40ee86
commit d25b5af59d
31 changed files with 1838 additions and 669 deletions

View File

@@ -2,6 +2,7 @@ package tests
import (
"context"
"log"
"strings"
"testing"
"time"
@@ -11,117 +12,57 @@ import (
"github.com/fnproject/fn_go/models"
)
func CheckAppResponseError(t *testing.T, e error) {
if e != nil {
switch err := e.(type) {
case *apps.DeleteAppsAppNotFound:
t.Errorf("Unexpected error occurred: %v Original Location: %s", err.Payload.Error.Message, MyCaller())
t.FailNow()
case *apps.DeleteAppsAppDefault:
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
t.FailNow()
case *apps.PostAppsDefault:
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
t.FailNow()
case *apps.GetAppsAppNotFound:
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:
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
t.FailNow()
case *apps.PatchAppsAppDefault:
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
t.FailNow()
case *apps.PatchAppsAppNotFound:
t.Errorf("Unexpected error occurred: %v. Original Location: %s", err.Payload.Error.Message, MyCaller())
t.FailNow()
case *apps.PatchAppsAppBadRequest:
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 Original Location: %s", err, MyCaller())
t.FailNow()
}
}
}
func CreateAppNoAssert(ctx context.Context, fnclient *client.Fn, appName string, config map[string]string) (*apps.PostAppsOK, error) {
// PostApp creates an app and esures it is deleted on teardown if it was created
func (s *TestHarness) PostApp(app *models.App) (*apps.PostAppsOK, error) {
cfg := &apps.PostAppsParams{
Body: &models.AppWrapper{
App: &models.App{
Config: config,
Name: appName,
},
App: app,
},
Context: ctx,
Context: s.Context,
}
ok, err := fnclient.Apps.PostApps(cfg)
ok, err := s.Client.Apps.PostApps(cfg)
if err == nil {
approutesLock.Lock()
_, got := appsandroutes[appName]
if !got {
appsandroutes[appName] = []string{}
}
approutesLock.Unlock()
s.createdApps[ok.Payload.App.Name] = true
}
return ok, err
}
func CreateApp(t *testing.T, ctx context.Context, fnclient *client.Fn, appName string, config map[string]string) {
appPayload, err := CreateAppNoAssert(ctx, fnclient, appName, config)
CheckAppResponseError(t, err)
if !strings.Contains(appName, appPayload.Payload.App.Name) {
t.Errorf("App name mismatch.\nExpected: %v\nActual: %v",
appName, appPayload.Payload.App.Name)
// GivenAppExists creates an app and ensures it is deleted on teardown, this fatals if the app is not created
func (s *TestHarness) GivenAppExists(t *testing.T, app *models.App) {
appPayload, err := s.PostApp(app)
if err != nil {
t.Fatalf("Failed to create app %v", app)
}
if !strings.Contains(app.Name, appPayload.Payload.App.Name) {
t.Fatalf("App name mismatch.\nExpected: %v\nActual: %v",
app.Name, appPayload.Payload.App.Name)
}
}
func CreateUpdateApp(t *testing.T, ctx context.Context, fnclient *client.Fn, appName string, config map[string]string) *apps.PatchAppsAppOK {
CreateApp(t, ctx, fnclient, appName, map[string]string{"A": "a"})
cfg := &apps.PatchAppsAppParams{
App: appName,
Body: &models.AppWrapper{
App: &models.App{
Config: config,
Name: "",
},
},
Context: ctx,
// AppMustExist fails the test if the specified app does not exist
func (s *TestHarness) AppMustExist(t *testing.T, appName string) *models.App {
app, err := s.Client.Apps.GetAppsApp(&apps.GetAppsAppParams{
App: s.AppName,
Context: s.Context,
})
if err != nil {
t.Fatalf("Expected new route to create app got %v", err)
return nil
}
appPayload, err := fnclient.Apps.PatchAppsApp(cfg)
CheckAppResponseError(t, err)
return appPayload
}
func DeleteApp(t *testing.T, ctx context.Context, fnclient *client.Fn, appName string) {
cfg := &apps.DeleteAppsAppParams{
App: appName,
Context: ctx,
}
_, err := fnclient.Apps.DeleteAppsApp(cfg)
CheckAppResponseError(t, err)
}
func GetApp(t *testing.T, ctx context.Context, fnclient *client.Fn, appName string) *models.App {
cfg := &apps.GetAppsAppParams{
App: appName,
Context: ctx,
}
app, err := fnclient.Apps.GetAppsApp(cfg)
CheckAppResponseError(t, err)
return app.Payload.App
}
func DeleteAppNoT(ctx context.Context, fnclient *client.Fn, appName string) {
func safeDeleteApp(ctx context.Context, fnclient *client.Fn, appName string) {
cfg := &apps.DeleteAppsAppParams{
App: appName,
Context: ctx,
}
cfg.WithTimeout(time.Second * 60)
fnclient.Apps.DeleteAppsApp(cfg)
_, err := fnclient.Apps.DeleteAppsApp(cfg)
if _, ok := err.(*apps.DeleteAppsAppNotFound); err != nil && !ok {
log.Printf("Error cleaning up app %s: %v", appName, err)
}
}