github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/ha/helidon/helidon_example_test.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package helidon
     5  
     6  import (
     7  	"fmt"
     8  	dump "github.com/verrazzano/verrazzano/tests/e2e/pkg/test/clusterdump"
     9  	"io"
    10  	"net/http"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/hashicorp/go-retryablehttp"
    15  	. "github.com/onsi/ginkgo/v2"
    16  	. "github.com/onsi/gomega"
    17  
    18  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    19  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    20  	hacommon "github.com/verrazzano/verrazzano/tests/e2e/pkg/ha"
    21  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    22  )
    23  
    24  const (
    25  	shortPollingInterval = 10 * time.Second
    26  	shortWaitTimeout     = 5 * time.Minute
    27  )
    28  
    29  var (
    30  	t           = framework.NewTestFramework("ha-helidon")
    31  	clusterDump = dump.NewClusterDumpWrapper(t, namespace)
    32  	clientset   = k8sutil.GetKubernetesClientsetOrDie()
    33  
    34  	httpClient *retryablehttp.Client
    35  )
    36  
    37  var beforeSuite = clusterDump.BeforeSuiteFunc(func() {
    38  	httpClient = pkg.EventuallyVerrazzanoRetryableHTTPClient()
    39  })
    40  var _ = BeforeSuite(beforeSuite)
    41  
    42  var _ = clusterDump.AfterEach(func() {}) // Dump cluster if spec fails
    43  
    44  var _ = t.Describe("HA Hello Helidon app endpoint test", Label("f:app-lcm.helidon-workload"), func() {
    45  
    46  	// GIVEN the hello-helidon app is deployed
    47  	// WHEN we access the app endpoint repeatedly during a Kubernetes cluster upgrade
    48  	// THEN the application endpoint must be accessible during the upgrade
    49  	t.Context("accesses the endpoint", Label("f:mesh.ingress"), func() {
    50  		var host string
    51  		var url string
    52  
    53  		t.It("fetches the ingress", func() {
    54  			Eventually(func() (string, error) {
    55  				var err error
    56  				host, err = k8sutil.GetHostnameFromGateway(namespace, "")
    57  				return host, err
    58  			}, shortWaitTimeout, shortPollingInterval).Should(Not(BeEmpty()))
    59  
    60  			url = fmt.Sprintf("https://%s/greet", host)
    61  		})
    62  
    63  		hacommon.RunningUntilShutdownIt(t, "accesses /greet app URL", clientset, true, func() {
    64  			Expect(appEndpointAccessible(url, host)).Should(BeTrue())
    65  			time.Sleep(time.Second)
    66  		})
    67  	})
    68  })
    69  
    70  // appEndpointAccessible hits the hello-helidon app endpoint and validates that the
    71  // response text matches the expected text
    72  func appEndpointAccessible(url string, hostname string) bool {
    73  	req, err := retryablehttp.NewRequest("GET", url, nil)
    74  	if err != nil {
    75  		t.Logs.Errorf("Unexpected error while creating new request=%v", err)
    76  		return false
    77  	}
    78  
    79  	req.Host = hostname
    80  	req.Close = true
    81  	resp, err := httpClient.Do(req)
    82  	if resp != nil && resp.Body != nil {
    83  		defer resp.Body.Close()
    84  	}
    85  	if err != nil {
    86  		t.Logs.Errorf("Unexpected error while making http request=%v", err)
    87  		bodyStr, err := readResponseBody(resp)
    88  		if err != nil {
    89  			t.Logs.Errorf("Unexpected error while marshallling error response=%v", err)
    90  			return false
    91  		}
    92  
    93  		t.Logs.Errorf("Error Response=%v", bodyStr)
    94  		return false
    95  	}
    96  
    97  	if resp.StatusCode != http.StatusOK {
    98  		t.Logs.Errorf("Unexpected status code=%v", resp.StatusCode)
    99  		return false
   100  	}
   101  	bodyStr, err := readResponseBody(resp)
   102  	if err != nil {
   103  		t.Logs.Errorf("Unexpected error marshallling response=%v", err)
   104  		return false
   105  	}
   106  	if !strings.Contains(bodyStr, "Hello World") {
   107  		t.Logs.Errorf("Unexpected response body=%v", bodyStr)
   108  		return false
   109  	}
   110  	return true
   111  }
   112  
   113  // readResponseBody reads the response body bytes and returns it as a string
   114  func readResponseBody(resp *http.Response) (string, error) {
   115  	var body string
   116  	if resp != nil && resp.Body != nil {
   117  		bodyRaw, err := io.ReadAll(resp.Body)
   118  		if err != nil {
   119  			return "", err
   120  		}
   121  		body = string(bodyRaw)
   122  	}
   123  	return body, nil
   124  }