mirror of https://github.com/knative/func.git
test: improve knative tests (#1378)
* test: knative integration test invokes function Signed-off-by: Matej Vasek <mvasek@redhat.com> * test: change Fatal->Error Signed-off-by: Matej Vasek <mvasek@redhat.com> Signed-off-by: Matej Vasek <mvasek@redhat.com>
This commit is contained in:
parent
a1b9bcff08
commit
3194367e88
|
@ -5,6 +5,8 @@ package knative_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -119,11 +121,14 @@ func TestIntegration(t *testing.T) {
|
||||||
Name: functionName,
|
Name: functionName,
|
||||||
Runtime: "blub",
|
Runtime: "blub",
|
||||||
Template: "cloudevents",
|
Template: "cloudevents",
|
||||||
// Basic "echo" Go Function that in addition:
|
// Basic HTTP service:
|
||||||
// * prints environment variables starting which name starts with FUNC_TEST to stderr,
|
// * POST / will do echo -- return body back
|
||||||
// * lists files under /etc/cm and /etc/sc stderr.
|
// * GET /info will get info about environment:
|
||||||
|
// * environment variables starting which name starts with FUNC_TEST,
|
||||||
|
// * files under /etc/cm and /etc/sc.
|
||||||
|
// * application also prints the same info to stderr on startup
|
||||||
Image: "quay.io/mvasek/func-test-service",
|
Image: "quay.io/mvasek/func-test-service",
|
||||||
ImageDigest: "sha256:69251ac335693c4d5a503e9f8a829a25b93bff6e6bddbff5b78971fe668dc71a",
|
ImageDigest: "sha256:2eca4de00d7569c8791634bdbb0c4d5ec8fb061b001549314591e839dabd5269",
|
||||||
Created: now,
|
Created: now,
|
||||||
Deploy: fn.DeploySpec{
|
Deploy: fn.DeploySpec{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
|
@ -195,18 +200,30 @@ func TestIntegration(t *testing.T) {
|
||||||
}
|
}
|
||||||
t.Logf("instance: %+v", instance)
|
t.Logf("instance: %+v", instance)
|
||||||
|
|
||||||
|
// try to invoke the function
|
||||||
|
reqBody := "Hello World!"
|
||||||
|
respBody, err := postText(ctx, instance.Route, reqBody)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
t.Log("resp body:\n" + respBody)
|
||||||
|
if !strings.Contains(respBody, reqBody) {
|
||||||
|
t.Error("response body doesn't contain request body")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// verify that trigger info is included in describe output
|
// verify that trigger info is included in describe output
|
||||||
if len(instance.Subscriptions) != 1 {
|
if len(instance.Subscriptions) != 1 {
|
||||||
t.Error("exactly one subscription is expected")
|
t.Error("exactly one subscription is expected")
|
||||||
} else {
|
} else {
|
||||||
if instance.Subscriptions[0].Broker != "testing-broker" {
|
if instance.Subscriptions[0].Broker != "testing-broker" {
|
||||||
t.Fatal("bad broker")
|
t.Error("bad broker")
|
||||||
}
|
}
|
||||||
if instance.Subscriptions[0].Source != "test-event-source" {
|
if instance.Subscriptions[0].Source != "test-event-source" {
|
||||||
t.Fatal("bad source")
|
t.Error("bad source")
|
||||||
}
|
}
|
||||||
if instance.Subscriptions[0].Type != "test-event-type" {
|
if instance.Subscriptions[0].Type != "test-event-type" {
|
||||||
t.Fatal("bad type")
|
t.Error("bad type")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,6 +286,26 @@ func TestIntegration(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func postText(ctx context.Context, url, reqBody string) (respBody string, err error) {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(reqBody))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req.Header.Add("Content-Type", "text/plain")
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
bs, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(bs), nil
|
||||||
|
}
|
||||||
|
|
||||||
func ptr[T interface{}](s T) *T {
|
func ptr[T interface{}](s T) *T {
|
||||||
return &s
|
return &s
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue