forked from fnproject/fn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apps_api.go
127 lines (115 loc) · 3.84 KB
/
apps_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package tests
import (
"context"
"strings"
"testing"
"time"
"github.com/fnproject/fn_go/client"
"github.com/fnproject/fn_go/client/apps"
"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) {
cfg := &apps.PostAppsParams{
Body: &models.AppWrapper{
App: &models.App{
Config: config,
Name: appName,
},
},
Context: ctx,
}
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.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)
}
}
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,
}
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) {
cfg := &apps.DeleteAppsAppParams{
App: appName,
Context: ctx,
}
cfg.WithTimeout(time.Second * 60)
fnclient.Apps.DeleteAppsApp(cfg)
}