You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ts...@apache.org on 2021/10/27 11:19:33 UTC

[camel-k] 01/02: chore(e2e): make route tests more resilient #2473

This is an automated email from the ASF dual-hosted git repository.

tsato pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit e77e10617c67afe5f4b478ce8ae7c969d31a0827
Author: Tadayoshi Sato <sa...@gmail.com>
AuthorDate: Wed Oct 27 15:08:49 2021 +0900

    chore(e2e): make route tests more resilient #2473
---
 e2e/common/rest_test.go         | 37 ++++++++++++------------
 e2e/common/traits/route_test.go | 64 +++++++++++++++++++----------------------
 e2e/support/test_support.go     | 10 +++++++
 3 files changed, 58 insertions(+), 53 deletions(-)

diff --git a/e2e/common/rest_test.go b/e2e/common/rest_test.go
index 3888933..ffca048 100644
--- a/e2e/common/rest_test.go
+++ b/e2e/common/rest_test.go
@@ -23,10 +23,11 @@ limitations under the License.
 package common
 
 import (
-	"bytes"
 	"fmt"
+	"io"
 	"net/http"
 	"testing"
+	"time"
 
 	. "github.com/onsi/gomega"
 	"github.com/stretchr/testify/assert"
@@ -57,7 +58,7 @@ func TestRunRest(t *testing.T) {
 			service := Service(ns, "rest-consumer")
 			Eventually(service, TestTimeoutShort).ShouldNot(BeNil())
 			Expect(Kamel("run", "-n", ns, "files/rest-producer.yaml", "-p", "serviceName=rest-consumer", "-p", "name="+name).Execute()).To(Succeed())
-			Eventually(IntegrationPodPhase(ns, "rest-producer"), TestTimeoutMedium).Should(Equal(corev1.PodRunning))
+			Eventually(IntegrationPodPhase(ns, "rest-producer"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
 			Eventually(IntegrationLogs(ns, "rest-consumer"), TestTimeoutLong).Should(ContainSubstring(fmt.Sprintf("get %s", name)))
 			Eventually(IntegrationLogs(ns, "rest-producer"), TestTimeoutLong).Should(ContainSubstring(fmt.Sprintf("%s Doe", name)))
 		})
@@ -67,10 +68,10 @@ func TestRunRest(t *testing.T) {
 				name := "Peter"
 				route := Route(ns, "rest-consumer")
 				Eventually(route, TestTimeoutShort).ShouldNot(BeNil())
-				response := httpRequest(t, fmt.Sprintf("http://%s/customers/%s", route().Spec.Host, name))
-				assert.Equal(t, fmt.Sprintf("%s Doe", name), response)
+				Eventually(RouteStatus(ns, "rest-consumer"), TestTimeoutMedium).Should(Equal("True"))
+				url := fmt.Sprintf("http://%s/customers/%s", route().Spec.Host, name)
+				Eventually(httpRequest(url), TestTimeoutMedium).Should(Equal(fmt.Sprintf("%s Doe", name)))
 				Eventually(IntegrationLogs(ns, "rest-consumer"), TestTimeoutShort).Should(ContainSubstring(fmt.Sprintf("get %s", name)))
-
 			})
 		}
 
@@ -79,20 +80,20 @@ func TestRunRest(t *testing.T) {
 	})
 }
 
-func httpRequest(t *testing.T, url string) string {
-	response, err := http.Get(url)
-	defer func() {
-		if response != nil {
-			_ = response.Body.Close()
+func httpRequest(url string) func() (string, error) {
+	return func() (string, error) {
+		client := &http.Client{Timeout: 3 * time.Second}
+		response, err := client.Get(url)
+		if err != nil {
+			return "", err
 		}
-	}()
-
-	assert.Nil(t, err)
+		defer response.Body.Close()
 
-	buf := new(bytes.Buffer)
-
-	_, err = buf.ReadFrom(response.Body)
-	assert.Nil(t, err)
+		body, err := io.ReadAll(response.Body)
+		if err != nil {
+			return "", err
+		}
 
-	return buf.String()
+		return string(body), nil
+	}
 }
diff --git a/e2e/common/traits/route_test.go b/e2e/common/traits/route_test.go
index 63eb4c5..9132e49 100644
--- a/e2e/common/traits/route_test.go
+++ b/e2e/common/traits/route_test.go
@@ -101,9 +101,7 @@ func TestRunRoutes(t *testing.T) {
 			// otherwise the route is unavailable and the http request will fail
 			time.Sleep(waitBeforeHttpRequest)
 			url := fmt.Sprintf("http://%s/hello?name=Simple", route().Spec.Host)
-			response, err := httpRequest(t, url, false)
-			assert.Nil(t, err)
-			assert.Equal(t, "Hello Simple", response)
+			Eventually(httpRequest(url, false), TestTimeoutShort).Should(Equal("Hello Simple"))
 			Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
 		})
 
@@ -119,9 +117,7 @@ func TestRunRoutes(t *testing.T) {
 			// otherwise the route is unavailable and the http request will fail
 			time.Sleep(waitBeforeHttpRequest)
 			url := fmt.Sprintf("https://%s/hello?name=TLS_Edge", route().Spec.Host)
-			response, err := httpRequest(t, url, true)
-			assert.Nil(t, err)
-			assert.Equal(t, "Hello TLS_Edge", response)
+			Eventually(httpRequest(url, true), TestTimeoutShort).Should(Equal("Hello TLS_Edge"))
 			Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
 		})
 
@@ -142,9 +138,7 @@ func TestRunRoutes(t *testing.T) {
 			time.Sleep(waitBeforeHttpRequest)
 			code := "TLS_EdgeCustomCertificate"
 			url := fmt.Sprintf("https://%s/hello?name=%s", route().Spec.Host, code)
-			response, err := httpRequest(t, url, true)
-			assert.Nil(t, err)
-			assert.Equal(t, "Hello "+code, response)
+			Eventually(httpRequest(url, true), TestTimeoutShort).Should(Equal("Hello " + code))
 			Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
 		})
 
@@ -169,9 +163,7 @@ func TestRunRoutes(t *testing.T) {
 			time.Sleep(waitBeforeHttpRequest)
 			code := "TLS_Passthrough"
 			url := fmt.Sprintf("https://%s/hello?name=%s", route().Spec.Host, code)
-			response, err := httpRequest(t, url, true)
-			assert.Nil(t, err)
-			assert.Equal(t, "Hello "+code, response)
+			Eventually(httpRequest(url, true), TestTimeoutShort).Should(Equal("Hello " + code))
 			Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
 		})
 
@@ -201,16 +193,35 @@ func TestRunRoutes(t *testing.T) {
 			time.Sleep(waitBeforeHttpRequest)
 			code := "TLS_Reencrypt"
 			url := fmt.Sprintf("https://%s/hello?name=%s", route().Spec.Host, code)
-			response, err := httpRequest(t, url, true)
-			assert.Nil(t, err)
-			assert.Equal(t, "Hello "+code, response)
+			Eventually(httpRequest(url, true), TestTimeoutShort).Should(Equal("Hello " + code))
 			Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
 		})
 		Expect(TestClient().Delete(TestContext, &secret)).To(Succeed())
 	})
 }
 
-func httpRequest(t *testing.T, url string, tlsEnabled bool) (string, error) {
+func httpRequest(url string, tlsEnabled bool) func() (string, error) {
+	return func() (string, error) {
+		client, err := httpClient(tlsEnabled, 3*time.Second)
+		if err != nil {
+			return "", err
+		}
+		response, err := client.Get(url)
+		if err != nil {
+			return "", err
+		}
+		defer response.Body.Close()
+
+		buf := new(bytes.Buffer)
+		_, err = buf.ReadFrom(response.Body)
+		if err != nil {
+			return "", err
+		}
+		return buf.String(), nil
+	}
+}
+
+func httpClient(tlsEnabled bool, timeout time.Duration) (*http.Client, error) {
 	var client http.Client
 	if tlsEnabled {
 		var transCfg http.Transport
@@ -233,25 +244,8 @@ func httpRequest(t *testing.T, url string, tlsEnabled bool) (string, error) {
 	} else {
 		client = http.Client{}
 	}
-	response, err := client.Get(url)
-	defer func() {
-		if response != nil {
-			_ = response.Body.Close()
-		}
-	}()
-	if err != nil {
-		fmt.Printf("Error making HTTP request. %s\n", err)
-		return "", err
-	}
-	assert.Nil(t, err)
-	buf := new(bytes.Buffer)
-	_, err = buf.ReadFrom(response.Body)
-	if err != nil {
-		fmt.Printf("Error reading the HTTP response. %s\n", err)
-		return "", err
-	}
-	assert.Nil(t, err)
-	return buf.String(), nil
+	client.Timeout = timeout
+	return &client, nil
 }
 
 func createSecret(ns string) (corev1.Secret, error) {
diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go
index 13fbd02..7ef3c64 100644
--- a/e2e/support/test_support.go
+++ b/e2e/support/test_support.go
@@ -532,6 +532,16 @@ func Route(ns string, name string) func() *routev1.Route {
 	}
 }
 
+func RouteStatus(ns string, name string) func() string {
+	return func() string {
+		route := Route(ns, name)()
+		if route == nil || len(route.Status.Ingress) == 0 {
+			return ""
+		}
+		return string(route.Status.Ingress[0].Conditions[0].Status)
+	}
+}
+
 func IntegrationCronJob(ns string, name string) func() *v1beta1.CronJob {
 	return func() *v1beta1.CronJob {
 		lst := v1beta1.CronJobList{