From 30b6f37c80dd7341df2a3949f18436b07a1d8bbc Mon Sep 17 00:00:00 2001 From: Jefferson Ramos Date: Tue, 17 Oct 2023 22:48:00 -0300 Subject: [PATCH] test: improvements on e2e extended test: remove deprecates, avoid nils and improve match conditions --- hack/allocate.sh | 1 + test/common/readycheck.go | 5 +++-- test/e2e/scenario_extended_flow_test.go | 18 +++++++++++++----- test/e2e/scenario_no_container_test.go | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hack/allocate.sh b/hack/allocate.sh index 930ecdde2f..024d8611ba 100755 --- a/hack/allocate.sh +++ b/hack/allocate.sh @@ -131,6 +131,7 @@ networking() { echo "Install load balancer." kubectl apply -f "https://raw.githubusercontent.com/metallb/metallb/v0.13.7/config/manifests/metallb-native.yaml" + sleep 5 kubectl wait --namespace metallb-system \ --for=condition=ready pod \ --selector=app=metallb \ diff --git a/test/common/readycheck.go b/test/common/readycheck.go index 4b4c4ab691..5f26a91081 100644 --- a/test/common/readycheck.go +++ b/test/common/readycheck.go @@ -1,6 +1,7 @@ package common import ( + "context" "testing" "time" @@ -8,7 +9,7 @@ import ( ) func WaitForFunctionReady(t *testing.T, functionName string) (revisionName string, functionUrl string) { - err := wait.PollImmediate(5*time.Second, 1*time.Minute, func() (done bool, err error) { + err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 1*time.Minute, true, func(ctx context.Context) (done bool, err error) { revisionName, functionUrl = GetKnativeServiceRevisionAndUrl(t, functionName) t.Logf("Waiting function to get ready (revision [%v])", revisionName) return revisionName != "", nil @@ -21,7 +22,7 @@ func WaitForFunctionReady(t *testing.T, functionName string) (revisionName strin // NewRevisionCheck waits for a new revision to report as ready func WaitForNewRevisionReady(t *testing.T, previousRevision string, functionName string) (newRevision string) { - err := wait.PollImmediate(5*time.Second, 1*time.Minute, func() (done bool, err error) { + err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 1*time.Minute, true, func(ctx context.Context) (done bool, err error) { newRevision = GetCurrentServiceRevision(t, functionName) t.Logf("Waiting for new revision deployment (previous revision [%v], current revision [%v])", previousRevision, newRevision) return newRevision != "" && newRevision != previousRevision, nil diff --git a/test/e2e/scenario_extended_flow_test.go b/test/e2e/scenario_extended_flow_test.go index 7cf22dbb51..1ffaa05300 100644 --- a/test/e2e/scenario_extended_flow_test.go +++ b/test/e2e/scenario_extended_flow_test.go @@ -60,11 +60,19 @@ func TestFunctionExtendedFlow(t *testing.T) { funcPort, attempts := "", 0 for funcPort == "" && attempts < 10 { t.Logf("----Function Output:\n%v\n", stdout.String()) - matches := regexp.MustCompile("Running on host port (.*)").FindStringSubmatch(stdout.String()) + findPort := func(exp string, msg string) (port string) { + matches := regexp.MustCompile(exp).FindStringSubmatch(msg) + if len(matches) > 1 { + port = matches[1] + } + return + } + funcPort = findPort("Running on host port (.*)", stdout.String()) + if funcPort == "" { + funcPort = findPort("Function started on port (.*)", stdout.String()) + } attempts++ - if len(matches) > 1 { - funcPort = matches[1] - } else { + if funcPort == "" { time.Sleep(200 * time.Millisecond) } } @@ -92,7 +100,7 @@ func TestFunctionExtendedFlow(t *testing.T) { time.Sleep(time.Second) // GET Function HTTP Endpoint - _, bodyResp := testhttp.TestGet(t, "http://:"+funcPort+"?message=local") + _, bodyResp := testhttp.TestGet(t, "http://localhost:"+funcPort+"?message=local") assert.Assert(t, strings.Contains(bodyResp, `{"message":"local"}`), "function response does not contain expected body.") // --------------------------- diff --git a/test/e2e/scenario_no_container_test.go b/test/e2e/scenario_no_container_test.go index f00e52e304..e69145bf2e 100644 --- a/test/e2e/scenario_no_container_test.go +++ b/test/e2e/scenario_no_container_test.go @@ -77,7 +77,7 @@ func TestFunctionRunWithoutContainer(t *testing.T) { time.Sleep(time.Second) // Assert Function endpoint responds - _, bodyResp := testhttp.TestGet(t, "http://:"+funcPort+"?message=run-on-host") + _, bodyResp := testhttp.TestGet(t, "http://localhost:"+funcPort+"?message=run-on-host") assert.Assert(t, strings.Contains(bodyResp, `GET /?message=run-on-host`), "function response does not contain expected body.") // Assert Func were not built