github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/scripts/install/install_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 install
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	vzconst "github.com/verrazzano/verrazzano/platform-operator/constants"
    10  	"os"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/verrazzano/verrazzano/pkg/constants"
    15  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1"
    16  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    17  
    18  	. "github.com/onsi/ginkgo/v2"
    19  	. "github.com/onsi/gomega"
    20  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  const (
    25  	waitTimeout     = 5 * time.Minute
    26  	pollingInterval = 5 * time.Second
    27  )
    28  
    29  var kubeConfigFromEnv = os.Getenv("KUBECONFIG")
    30  
    31  var t = framework.NewTestFramework("install")
    32  
    33  var beforeSuite = t.BeforeSuiteFunc(func() {})
    34  var _ = BeforeSuite(beforeSuite)
    35  var afterSuite = t.AfterSuiteFunc(func() {})
    36  var _ = AfterSuite(afterSuite)
    37  var _ = t.AfterEach(func() {})
    38  
    39  // This test checks that the Verrazzano install resource has the expected console URLs.
    40  var _ = t.Describe("Verify Verrazzano install scripts.", Label("f:platform-lcm.install"), func() {
    41  
    42  	t.Context("Check", Label("f:ui.console"), func() {
    43  		t.It("the expected Console URLs are there in the installed Verrazzano resource", func() {
    44  			// Validation for passed in cluster
    45  			Eventually(func() bool {
    46  				return validateConsoleUrlsCluster(kubeConfigFromEnv)
    47  			}, waitTimeout, pollingInterval).Should(BeTrue())
    48  		})
    49  	})
    50  })
    51  
    52  // Validate the console URLs for the admin cluster and single cluster installation
    53  func validateConsoleUrlsCluster(kubeconfig string) bool {
    54  	consoleUrls, err := getConsoleURLsFromResource(kubeconfig)
    55  	if err != nil {
    56  		t.Logs.Errorf("There is an error getting console URLs from the installed Verrazzano resource - %v", err)
    57  		return false
    58  	}
    59  	expectedConsoleUrls, err := getExpectedConsoleURLs(kubeconfig)
    60  	if err != nil {
    61  		t.Logs.Errorf("There is an error getting console URLs from ingress resources - %v", err)
    62  		return false
    63  	}
    64  	t.Logs.Infof("Expected URLs based on ingresses: %v", expectedConsoleUrls)
    65  	t.Logs.Infof("Actual URLs in Verrazzano resource: %v", consoleUrls)
    66  
    67  	return pkg.SlicesContainSameStrings(consoleUrls, expectedConsoleUrls)
    68  }
    69  
    70  // Get the list of console URLs from the status block of the installed Verrazzano resource
    71  func getConsoleURLsFromResource(kubeconfig string) ([]string, error) {
    72  	var consoleUrls []string
    73  	vz, err := pkg.GetVerrazzanoInstallResourceInClusterV1beta1(kubeconfig)
    74  	if err != nil {
    75  		return consoleUrls, err
    76  	}
    77  
    78  	if vz.Status.VerrazzanoInstance.ConsoleURL != nil {
    79  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.ConsoleURL)
    80  	}
    81  	if vz.Status.VerrazzanoInstance.GrafanaURL != nil {
    82  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.GrafanaURL)
    83  	}
    84  	if vz.Status.VerrazzanoInstance.OpenSearchURL != nil {
    85  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.OpenSearchURL)
    86  	}
    87  	if vz.Status.VerrazzanoInstance.KeyCloakURL != nil {
    88  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.KeyCloakURL)
    89  	}
    90  	if vz.Status.VerrazzanoInstance.OpenSearchDashboardsURL != nil {
    91  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.OpenSearchDashboardsURL)
    92  	}
    93  	if vz.Status.VerrazzanoInstance.KialiURL != nil {
    94  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.KialiURL)
    95  	}
    96  	if vz.Status.VerrazzanoInstance.PrometheusURL != nil {
    97  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.PrometheusURL)
    98  	}
    99  	if vz.Status.VerrazzanoInstance.RancherURL != nil {
   100  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.RancherURL)
   101  	}
   102  	if vz.Status.VerrazzanoInstance.JaegerURL != nil {
   103  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.JaegerURL)
   104  	}
   105  	if vz.Status.VerrazzanoInstance.ArgoCDURL != nil {
   106  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.ArgoCDURL)
   107  	}
   108  	if vz.Status.VerrazzanoInstance.ThanosQueryURL != nil {
   109  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.ThanosQueryURL)
   110  	}
   111  	if vz.Status.VerrazzanoInstance.ThanosRulerURL != nil {
   112  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.ThanosRulerURL)
   113  	}
   114  	if vz.Status.VerrazzanoInstance.AlertmanagerURL != nil {
   115  		consoleUrls = append(consoleUrls, *vz.Status.VerrazzanoInstance.AlertmanagerURL)
   116  	}
   117  
   118  	return consoleUrls, nil
   119  }
   120  
   121  // Get the expected console URLs for the given cluster from the ingress resources
   122  func getExpectedConsoleURLs(kubeConfig string) ([]string, error) {
   123  	var expectedUrls []string
   124  	clientset, err := pkg.GetKubernetesClientsetForCluster(kubeConfig)
   125  	if err != nil {
   126  		return expectedUrls, err
   127  	}
   128  	ingresses, err := clientset.NetworkingV1().Ingresses("").List(context.TODO(), metav1.ListOptions{})
   129  	if err != nil {
   130  		return expectedUrls, err
   131  	}
   132  
   133  	consoleURLExpected, err := isConsoleURLExpected(kubeConfig)
   134  	if err != nil {
   135  		return expectedUrls, err
   136  	}
   137  
   138  	for _, ingress := range ingresses.Items {
   139  		ingressHost := ingress.Spec.Rules[0].Host
   140  		// elasticsearch and kibana ingresses are created for permanent redirection when upgraded from older VZ releases to 1.5.0 or later.
   141  		// The Thanos Store API endpoints are not stored in the Verrazzano instance and can be ignored
   142  		// The Dex endpoint doesn't need to be exposed as a console URL
   143  		if strings.HasPrefix(ingressHost, "elasticsearch") ||
   144  			strings.HasPrefix(ingressHost, "kibana") ||
   145  			strings.HasPrefix(ingressHost, "thanos-query-store") ||
   146  			strings.HasPrefix(ingressHost, vzconst.DexHostPrefix) {
   147  			continue
   148  		}
   149  		// Any verrazzano-managed ingresses in the Rancher namespace are created for managed clusters to be
   150  		// able to use an older DNS name to connect to Rancher proxy until the managed clusters are updated.
   151  		// Those will not exist in the VZ resource.
   152  		if ingress.Namespace == constants.RancherSystemNamespace && pkg.IsVerrazzanoManaged(ingress.Labels) {
   153  			continue
   154  		}
   155  		// If it's not the console ingress, or it is and the console is enabled, add it to the expected set of URLs
   156  		if !isConsoleIngressHost(ingressHost) || consoleURLExpected {
   157  			expectedUrls = append(expectedUrls, fmt.Sprintf("https://%s", ingressHost))
   158  		}
   159  	}
   160  
   161  	return expectedUrls, nil
   162  }
   163  
   164  // isConsoleIngressHost - Returns true if the given ingress host is the one for the VZ UI console
   165  func isConsoleIngressHost(ingressHost string) bool {
   166  	return strings.HasPrefix(ingressHost, "verrazzano.")
   167  }
   168  
   169  // isConsoleURLExpected - Returns true in VZ < 1.1.1. For VZ >= 1.1.1, returns false only if explicitly disabled
   170  // in the CR or when managed cluster profile is used
   171  func isConsoleURLExpected(kubeconfigPath string) (bool, error) {
   172  	isAtleastVz111, err := pkg.IsVerrazzanoMinVersion("1.1.1", kubeconfigPath)
   173  	if err != nil {
   174  		return false, err
   175  	}
   176  	// Pre 1.1.1, the console URL was always present irrespective of whether console is enabled
   177  	// This behavior changed in VZ 1.1.1
   178  	if !isAtleastVz111 {
   179  		return true, nil
   180  	}
   181  
   182  	// In 1.1.1 and later, the console URL will only be present in the VZ status instance info if the console is enabled
   183  	vz, err := pkg.GetVerrazzanoInstallResourceInClusterV1beta1(kubeconfigPath)
   184  	if err != nil {
   185  		return false, err
   186  	}
   187  	// Return the value of the Console enabled flag if present
   188  	if vz != nil && vz.Spec.Components.Console != nil && vz.Spec.Components.Console.Enabled != nil {
   189  		return *vz.Spec.Components.Console.Enabled, nil
   190  	}
   191  	// otherwise, expect console to be enabled for all profiles but managed-cluster
   192  	return vz.Spec.Profile != vzapi.ManagedCluster, nil
   193  }