github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/verify-infra/restapi/vmi_urls_test.go (about)

     1  // Copyright (c) 2021, 2023, 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 restapi_test
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  	"time"
    10  
    11  	"github.com/hashicorp/go-retryablehttp"
    12  	. "github.com/onsi/ginkgo/v2"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1"
    15  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    16  )
    17  
    18  var (
    19  	vz             *v1beta1.Verrazzano
    20  	httpClient     *retryablehttp.Client
    21  	vmiCredentials *pkg.UsernamePassword
    22  )
    23  
    24  var beforeSuite = t.BeforeSuiteFunc(func() {
    25  	var err error
    26  	vz, err = pkg.GetVerrazzanoV1beta1()
    27  	Expect(err).To(Not(HaveOccurred()))
    28  
    29  	httpClient = pkg.EventuallyVerrazzanoRetryableHTTPClient()
    30  	vmiCredentials = pkg.EventuallyGetSystemVMICredentials()
    31  })
    32  
    33  var _ = BeforeSuite(beforeSuite)
    34  
    35  var _ = t.AfterEach(func() {})
    36  
    37  var _ = t.Describe("VMI", Label("f:infra-lcm", "f:ui.console"), func() {
    38  	const (
    39  		waitTimeout     = 10 * time.Minute
    40  		pollingInterval = 10 * time.Second
    41  	)
    42  
    43  	t.BeforeEach(func() {
    44  		// if Keycloak is disabled, we cannot get the credentials needed for basic auth, so skip the test
    45  		keycloak := vz.Status.Components["keycloak"]
    46  		if keycloak == nil || keycloak.State == v1beta1.CompStateDisabled {
    47  			Skip("Keycloak disabled, skipping test")
    48  		}
    49  	})
    50  
    51  	// GIVEN a Verrazzano custom resource
    52  	// WHEN  we attempt to access VMI endpoints present in the CR status
    53  	// THEN  we expect an HTTP OK response status code
    54  	t.DescribeTable("Access VMI endpoints",
    55  		func(getURLFromVZStatus func() *string) {
    56  			url := getURLFromVZStatus()
    57  			if url != nil {
    58  				Eventually(func() (int, error) {
    59  					return httpGet(*url)
    60  				}).WithPolling(pollingInterval).WithTimeout(waitTimeout).Should(Equal(http.StatusOK))
    61  			}
    62  		},
    63  		Entry("Grafana web UI", func() *string { return vz.Status.VerrazzanoInstance.GrafanaURL }),
    64  		Entry("Prometheus web UI", func() *string { return vz.Status.VerrazzanoInstance.PrometheusURL }),
    65  		Entry("OpenSearch", func() *string { return vz.Status.VerrazzanoInstance.OpenSearchURL }),
    66  		Entry("OpenSearch Dashboards web UI", func() *string { return vz.Status.VerrazzanoInstance.OpenSearchDashboardsURL }),
    67  	)
    68  })
    69  
    70  // httpGet issues an HTTP GET request with basic auth to the specified URL. httpGet returns the HTTP status code
    71  // and an error.
    72  func httpGet(url string) (int, error) {
    73  	req, err := retryablehttp.NewRequest("GET", url, nil)
    74  	if err != nil {
    75  		return 0, err
    76  	}
    77  	req.SetBasicAuth(vmiCredentials.Username, vmiCredentials.Password)
    78  	resp, err := httpClient.Do(req)
    79  	if err != nil {
    80  		return 0, err
    81  	}
    82  	io.ReadAll(resp.Body)
    83  	resp.Body.Close()
    84  
    85  	return resp.StatusCode, nil
    86  }