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:
Matej Vasek 2022-10-22 06:34:15 +02:00 committed by GitHub
parent a1b9bcff08
commit 3194367e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 7 deletions

View File

@ -5,6 +5,8 @@ package knative_test
import (
"context"
"io"
"net/http"
"strings"
"testing"
"time"
@ -119,11 +121,14 @@ func TestIntegration(t *testing.T) {
Name: functionName,
Runtime: "blub",
Template: "cloudevents",
// Basic "echo" Go Function that in addition:
// * prints environment variables starting which name starts with FUNC_TEST to stderr,
// * lists files under /etc/cm and /etc/sc stderr.
// Basic HTTP service:
// * POST / will do echo -- return body back
// * 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",
ImageDigest: "sha256:69251ac335693c4d5a503e9f8a829a25b93bff6e6bddbff5b78971fe668dc71a",
ImageDigest: "sha256:2eca4de00d7569c8791634bdbb0c4d5ec8fb061b001549314591e839dabd5269",
Created: now,
Deploy: fn.DeploySpec{
Namespace: namespace,
@ -195,18 +200,30 @@ func TestIntegration(t *testing.T) {
}
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
if len(instance.Subscriptions) != 1 {
t.Error("exactly one subscription is expected")
} else {
if instance.Subscriptions[0].Broker != "testing-broker" {
t.Fatal("bad broker")
t.Error("bad broker")
}
if instance.Subscriptions[0].Source != "test-event-source" {
t.Fatal("bad source")
t.Error("bad source")
}
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 {
return &s
}