mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Improving API tests
This commit is contained in:
committed by
James Jeffrey
parent
e0569192ee
commit
5b41fe2dc7
@@ -27,12 +27,12 @@ func CallAsync(t *testing.T, u url.URL, content io.Reader) string {
|
||||
output := &bytes.Buffer{}
|
||||
err := CallFN(u.String(), content, output, "POST", []string{})
|
||||
if err != nil {
|
||||
t.Fatalf("Got unexpected error: %v", err)
|
||||
t.Errorf("Got unexpected error: %v", err)
|
||||
}
|
||||
|
||||
expectedOutput := "call_id"
|
||||
if !strings.Contains(output.String(), expectedOutput) {
|
||||
t.Fatalf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
|
||||
t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
|
||||
}
|
||||
|
||||
type CallID struct {
|
||||
@@ -43,42 +43,55 @@ func CallAsync(t *testing.T, u url.URL, content io.Reader) string {
|
||||
json.NewDecoder(output).Decode(callID)
|
||||
|
||||
if callID.CallID == "" {
|
||||
t.Fatalf("`call_id` not suppose to be empty string")
|
||||
t.Errorf("`call_id` not suppose to be empty string")
|
||||
}
|
||||
t.Logf("Async execution call ID: %v", callID.CallID)
|
||||
return callID.CallID
|
||||
}
|
||||
|
||||
func TestRouteExecutions(t *testing.T) {
|
||||
s := SetupDefaultSuite()
|
||||
|
||||
newRouteType := "async"
|
||||
|
||||
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)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
}
|
||||
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)
|
||||
|
||||
t.Run("run-sync-funcy/hello-no-input", 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, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
}
|
||||
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)
|
||||
|
||||
content := &bytes.Buffer{}
|
||||
output := &bytes.Buffer{}
|
||||
err := CallFN(u.String(), content, output, "POST", []string{})
|
||||
if err != nil {
|
||||
t.Fatalf("Got unexpected error: %v", err)
|
||||
t.Errorf("Got unexpected error: %v", err)
|
||||
}
|
||||
expectedOutput := "Hello World!\n"
|
||||
if !strings.Contains(expectedOutput, output.String()) {
|
||||
t.Fatalf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
|
||||
t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
|
||||
}
|
||||
t.Logf("Test `%v` passed.", t.Name())
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
t.Run("run-sync-funcy/hello-with-input", 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, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
}
|
||||
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)
|
||||
|
||||
content := &bytes.Buffer{}
|
||||
json.NewEncoder(content).Encode(struct {
|
||||
Name string
|
||||
@@ -86,29 +99,64 @@ func TestRouteExecutions(t *testing.T) {
|
||||
output := &bytes.Buffer{}
|
||||
err := CallFN(u.String(), content, output, "POST", []string{})
|
||||
if err != nil {
|
||||
t.Fatalf("Got unexpected error: %v", err)
|
||||
t.Errorf("Got unexpected error: %v", err)
|
||||
}
|
||||
expectedOutput := "Hello John!\n"
|
||||
if !strings.Contains(expectedOutput, output.String()) {
|
||||
t.Fatalf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
|
||||
t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
|
||||
}
|
||||
t.Logf("Test `%v` passed.", t.Name())
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
|
||||
})
|
||||
|
||||
_, err := UpdateRoute(
|
||||
t, s.Context, s.Client,
|
||||
s.AppName, s.RoutePath,
|
||||
s.Image, newRouteType, s.Format,
|
||||
s.Memory, s.RouteConfig, s.RouteHeaders, "")
|
||||
|
||||
CheckRouteResponseError(t, err)
|
||||
|
||||
t.Run("run-async-funcy/hello", 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, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
}
|
||||
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)
|
||||
|
||||
_, err := UpdateRoute(
|
||||
t, s.Context, s.Client,
|
||||
s.AppName, s.RoutePath,
|
||||
s.Image, newRouteType, s.Format,
|
||||
s.Memory, s.RouteConfig, s.RouteHeaders, "")
|
||||
|
||||
CheckRouteResponseError(t, err)
|
||||
|
||||
CallAsync(t, u, &bytes.Buffer{})
|
||||
t.Logf("Test `%v` passed.", t.Name())
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
t.Run("run-async-funcy/hello-with-status-check", 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, "sync",
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
}
|
||||
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)
|
||||
|
||||
_, err := UpdateRoute(
|
||||
t, s.Context, s.Client,
|
||||
s.AppName, s.RoutePath,
|
||||
s.Image, newRouteType, s.Format,
|
||||
s.Memory, s.RouteConfig, s.RouteHeaders, "")
|
||||
|
||||
CheckRouteResponseError(t, err)
|
||||
|
||||
callID := CallAsync(t, u, &bytes.Buffer{})
|
||||
time.Sleep(time.Second * 10)
|
||||
cfg := &call.GetCallsCallParams{
|
||||
@@ -121,35 +169,39 @@ func TestRouteExecutions(t *testing.T) {
|
||||
switch err.(type) {
|
||||
case *call.GetCallsCallNotFound:
|
||||
msg := err.(*call.GetCallsCallNotFound).Payload.Error.Message
|
||||
t.Fatalf("Unexpected error occurred: %v.", msg)
|
||||
t.Errorf("Unexpected error occurred: %v.", msg)
|
||||
}
|
||||
}
|
||||
callObject := callResponse.Payload.Call
|
||||
|
||||
if callObject.AppName != s.AppName {
|
||||
t.Fatalf("Call object app name mismatch.\n\tExpected: %v\n\tActual:%v", s.AppName, callObject.AppName)
|
||||
t.Errorf("Call object app name mismatch.\n\tExpected: %v\n\tActual:%v", s.AppName, callObject.AppName)
|
||||
}
|
||||
if callObject.ID != callID {
|
||||
t.Fatalf("Call object ID mismatch.\n\tExpected: %v\n\tActual:%v", callID, callObject.ID)
|
||||
t.Errorf("Call object ID mismatch.\n\tExpected: %v\n\tActual:%v", callID, callObject.ID)
|
||||
}
|
||||
if callObject.Path != s.RoutePath {
|
||||
t.Fatalf("Call object route path mismatch.\n\tExpected: %v\n\tActual:%v", s.RoutePath, callObject.Path)
|
||||
t.Errorf("Call object route path mismatch.\n\tExpected: %v\n\tActual:%v", s.RoutePath, callObject.Path)
|
||||
}
|
||||
if callObject.Status != "success" {
|
||||
t.Fatalf("Call object status mismatch.\n\tExpected: %v\n\tActual:%v", "success", callObject.Status)
|
||||
t.Errorf("Call object status mismatch.\n\tExpected: %v\n\tActual:%v", "success", callObject.Status)
|
||||
}
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
|
||||
})
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)
|
||||
|
||||
routePath := "/timeout"
|
||||
image := "funcy/timeout:0.0.1"
|
||||
routeType := "sync"
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
t.Run("exec-timeout-test", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
routePath := "/" + RandStringBytes(10)
|
||||
image := "funcy/timeout:0.0.1"
|
||||
routeType := "sync"
|
||||
|
||||
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)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -166,7 +218,7 @@ func TestRouteExecutions(t *testing.T) {
|
||||
CallFN(u.String(), content, output, "POST", []string{})
|
||||
|
||||
if !strings.Contains(output.String(), "Timed out") {
|
||||
t.Fatalf("Must fail because of timeout, but got error message: %v", output.String())
|
||||
t.Errorf("Must fail because of timeout, but got error message: %v", output.String())
|
||||
}
|
||||
tB := &TimeoutBody{}
|
||||
|
||||
@@ -179,24 +231,28 @@ func TestRouteExecutions(t *testing.T) {
|
||||
cfg.WithTimeout(time.Second * 60)
|
||||
callObj, err := s.Client.Call.GetCallsCall(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %s", err)
|
||||
t.Errorf("Unexpected error: %s", err)
|
||||
}
|
||||
if !strings.Contains("timeout", callObj.Payload.Call.Status) {
|
||||
t.Fatalf("Call status mismatch.\n\tExpected: %v\n\tActual: %v",
|
||||
t.Errorf("Call status mismatch.\n\tExpected: %v\n\tActual: %v",
|
||||
"output", "callObj.Payload.Call.Status")
|
||||
}
|
||||
|
||||
t.Logf("Test `%v` passed.", t.Name())
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
|
||||
routePath = "/multi-log"
|
||||
image = "funcy/multi-log:0.0.1"
|
||||
routeType = "async"
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
t.Run("exec-multi-log-test", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
routePath := "/multi-log"
|
||||
image := "funcy/multi-log:0.0.1"
|
||||
routeType := "async"
|
||||
|
||||
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)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
@@ -213,30 +269,34 @@ func TestRouteExecutions(t *testing.T) {
|
||||
|
||||
logObj, err := s.Client.Operations.GetCallsCallLog(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %s", err)
|
||||
t.Errorf("Unexpected error: %s", err)
|
||||
}
|
||||
if logObj.Payload.Log.Log == "" {
|
||||
t.Fatalf("Log entry must not be empty!")
|
||||
t.Errorf("Log entry must not be empty!")
|
||||
}
|
||||
if !strings.Contains(logObj.Payload.Log.Log, "First line") {
|
||||
t.Fatalf("Log entry must contain `First line` "+
|
||||
t.Errorf("Log entry must contain `First line` "+
|
||||
"string, but got: %v", logObj.Payload.Log.Log)
|
||||
}
|
||||
if !strings.Contains(logObj.Payload.Log.Log, "Second line") {
|
||||
t.Fatalf("Log entry must contain `Second line` "+
|
||||
t.Errorf("Log entry must contain `Second line` "+
|
||||
"string, but got: %v", logObj.Payload.Log.Log)
|
||||
}
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
|
||||
routePath = "/os.environ"
|
||||
image = "denismakogon/os.environ"
|
||||
routeType = "sync"
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
t.Run("verify-headers-separator", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
||||
routePath := "/os.environ"
|
||||
image := "denismakogon/os.environ"
|
||||
routeType := "sync"
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
@@ -254,17 +314,21 @@ func TestRouteExecutions(t *testing.T) {
|
||||
t.Errorf("HEADER_ACCEPT='application/xml, application/json; q=0.2' "+
|
||||
"should be in output, have:\n%", res)
|
||||
}
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
|
||||
routePath = "/log"
|
||||
image = "funcy/log:0.0.1"
|
||||
routeType = "async"
|
||||
CreateRoute(t, s.Context, s.Client, s.AppName, routePath, image, routeType,
|
||||
s.RouteConfig, s.RouteHeaders)
|
||||
|
||||
t.Run("exec-log-test", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := SetupDefaultSuite()
|
||||
routePath := "/log"
|
||||
image := "funcy/log:0.0.1"
|
||||
routeType := "async"
|
||||
|
||||
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)
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: Host(),
|
||||
@@ -286,13 +350,26 @@ func TestRouteExecutions(t *testing.T) {
|
||||
_, err := s.Client.Operations.GetCallsCallLog(cfg)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %s", err)
|
||||
t.Errorf("Unexpected error: %s", err)
|
||||
}
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
t.Run("exec-oversized-log-test", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Skip("Skipped until fix for https://gitlab-odx.oracle.com/odx/functions/issues/86.")
|
||||
|
||||
s := SetupDefaultSuite()
|
||||
routePath := "/log"
|
||||
image := "funcy/log:0.0.1"
|
||||
routeType := "async"
|
||||
|
||||
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)
|
||||
|
||||
size := 1 * 1024 * 1024 * 1024
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
@@ -314,15 +391,14 @@ func TestRouteExecutions(t *testing.T) {
|
||||
|
||||
logObj, err := s.Client.Operations.GetCallsCallLog(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %s", err)
|
||||
t.Errorf("Unexpected error: %s", err)
|
||||
}
|
||||
if len(logObj.Payload.Log.Log) >= size {
|
||||
t.Fatalf("Log entry suppose to be truncated up to expected size %v, got %v",
|
||||
t.Errorf("Log entry suppose to be truncated up to expected size %v, got %v",
|
||||
size/1024, len(logObj.Payload.Log.Log))
|
||||
}
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
})
|
||||
|
||||
DeleteRoute(t, s.Context, s.Client, s.AppName, routePath)
|
||||
|
||||
DeleteApp(t, s.Context, s.Client, s.AppName)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user