github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/verify-install/clusterapi/clusterapi_test.go (about)

     1  // Copyright (c) 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 clusterapi
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"time"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/verrazzano/verrazzano/pkg/constants"
    14  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    15  	"github.com/verrazzano/verrazzano/pkg/vzcr"
    16  	"github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/rancher"
    17  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    18  	capipkg "github.com/verrazzano/verrazzano/tests/e2e/pkg/clusterapi"
    19  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    20  	"k8s.io/apimachinery/pkg/api/errors"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  	"k8s.io/client-go/dynamic"
    25  )
    26  
    27  const (
    28  	waitTimeout     = 5 * time.Minute
    29  	pollingInterval = 10 * time.Second
    30  )
    31  
    32  var t = framework.NewTestFramework("clusterapi")
    33  
    34  var _ = t.Describe("Cluster API", Label("f:platform-lcm.install"), func() {
    35  	t.Context("after successful installation", func() {
    36  		// GIVEN the Cluster API is installed
    37  		// WHEN we check to make sure the pods exist
    38  		// THEN we successfully find the pods in the cluster
    39  		capipkg.WhenClusterAPIInstalledIt(t, "expected pods are running", func() {
    40  			pods := []string{"capi-controller-manager", "capi-ocne-bootstrap-controller-manager", "capi-ocne-control-plane-controller-manager", "capoci-controller-manager"}
    41  			Eventually(func() (bool, error) {
    42  				result, err := pkg.PodsRunning(constants.VerrazzanoCAPINamespace, pods)
    43  				if err != nil {
    44  					t.Logs.Errorf("Pods %v are not running in the namespace: %v, error: %v", pods, constants.VerrazzanoCAPINamespace, err)
    45  				}
    46  				return result, err
    47  			}, waitTimeout, pollingInterval).Should(BeTrue(), "Expected ClusterAPI Pods should be running")
    48  		})
    49  		capipkg.WhenClusterAPIInstalledIt(t, "namespace has the expected label", func() {
    50  			Eventually(func() (bool, error) {
    51  				return pkg.DoesNamespaceHasVerrazzanoLabel(constants.VerrazzanoCAPINamespace)
    52  			}, waitTimeout, pollingInterval).Should(BeTrue(), "ClusterAPI namespace should have expected label")
    53  		})
    54  	})
    55  })
    56  
    57  var _ = t.Describe("KontainerDriver status", Label("f:platform-lcm.install"), func() {
    58  
    59  	t.Context("after successful installation", func() {
    60  		kubeconfig := getKubeConfigOrAbort()
    61  		inClusterVZ, err := pkg.GetVerrazzanoInstallResourceInClusterV1beta1(kubeconfig)
    62  		if err != nil {
    63  			AbortSuite(fmt.Sprintf("Failed to get Verrazzano from the cluster: %v", err))
    64  		}
    65  		rancherConfigured := vzcr.IsComponentStatusEnabled(inClusterVZ, rancher.ComponentName)
    66  
    67  		var clientset dynamic.Interface
    68  
    69  		// Get dynamic client
    70  		Eventually(func() (dynamic.Interface, error) {
    71  			kubePath, err := k8sutil.GetKubeConfigLocation()
    72  			if err != nil {
    73  				return nil, err
    74  			}
    75  			clientset, err = pkg.GetDynamicClientInCluster(kubePath)
    76  			return clientset, err
    77  		}, waitTimeout, pollingInterval).ShouldNot(BeNil())
    78  
    79  		capipkg.WhenClusterAPIInstalledIt(t, "kontainerdrivers must be ready", func() {
    80  			if !rancherConfigured {
    81  				Skip("Skipping test because Rancher is not configured")
    82  			}
    83  			driversActive := func() bool {
    84  				cattleDrivers, err := listKontainerDrivers(clientset)
    85  				if err != nil {
    86  					return false
    87  				}
    88  
    89  				allActive := true
    90  				// The condition of each driver must be active
    91  				for _, driver := range cattleDrivers.Items {
    92  					status := driver.UnstructuredContent()["status"].(map[string]interface{})
    93  					conditions := status["conditions"].([]interface{})
    94  					driverActive := false
    95  					for _, condition := range conditions {
    96  						conditionData := condition.(map[string]interface{})
    97  						if conditionData["type"].(string) == "Active" && conditionData["status"].(string) == "True" {
    98  							driverActive = true
    99  							break
   100  						}
   101  					}
   102  					if !driverActive {
   103  						t.Logs.Infof("Driver %s not Active", driver.GetName())
   104  						allActive = false
   105  					}
   106  				}
   107  				return allActive
   108  			}
   109  			Eventually(driversActive, waitTimeout, pollingInterval).Should(BeTrue())
   110  		})
   111  
   112  		capipkg.WhenClusterAPIInstalledIt(t, "expected kontainerdrivers must exist", func() {
   113  			if !rancherConfigured {
   114  				Skip("Skipping test because Rancher is not configured")
   115  			}
   116  			expectedDriversFound := func() bool {
   117  				cattleDrivers, err := listKontainerDrivers(clientset)
   118  				if err != nil {
   119  					t.Logs.Info(err.Error())
   120  					return false
   121  				}
   122  
   123  				foundCount := 0
   124  				// Each driver is expected to exist
   125  				for _, driver := range cattleDrivers.Items {
   126  					switch driver.GetName() {
   127  					case "amazonelasticcontainerservice":
   128  						foundCount++
   129  					case "azurekubernetesservice":
   130  						foundCount++
   131  					case "googlekubernetesengine":
   132  						foundCount++
   133  					case "ociocneengine":
   134  						foundCount++
   135  					case "oraclecontainerengine":
   136  						foundCount++
   137  					}
   138  				}
   139  				return foundCount == 5
   140  			}
   141  			Eventually(expectedDriversFound, waitTimeout, pollingInterval).Should(BeTrue())
   142  		})
   143  	})
   144  })
   145  
   146  func getKubeConfigOrAbort() string {
   147  	kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
   148  	if err != nil {
   149  		AbortSuite(fmt.Sprintf("Failed to get default kubeconfig path: %s", err.Error()))
   150  	}
   151  	return kubeconfigPath
   152  }
   153  
   154  func listKontainerDrivers(clientset dynamic.Interface) (*unstructured.UnstructuredList, error) {
   155  	cattleDrivers, err := clientset.Resource(schema.GroupVersionResource{
   156  		Group:    "management.cattle.io",
   157  		Version:  "v3",
   158  		Resource: "kontainerdrivers",
   159  	}).List(context.TODO(), metav1.ListOptions{})
   160  
   161  	if err != nil {
   162  		if errors.IsNotFound(err) {
   163  			t.Logs.Info("No kontainerdrivers found")
   164  		} else {
   165  			t.Logs.Errorf("Failed to list kontainerdrivers: %v", err)
   166  		}
   167  	}
   168  	return cattleDrivers, err
   169  }