github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/test/integ/framework/framework.go (about)

     1  // Copyright (C) 2020, 2021, 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 framework
     5  
     6  import (
     7  	"crypto/rand"
     8  	"flag"
     9  	"math/big"
    10  
    11  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/constants"
    12  	"github.com/verrazzano/verrazzano-monitoring-operator/test/integ/client"
    13  	"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    14  	"k8s.io/apimachinery/pkg/runtime"
    15  	"k8s.io/apimachinery/pkg/runtime/schema"
    16  	"k8s.io/apimachinery/pkg/runtime/serializer"
    17  	"k8s.io/client-go/kubernetes"
    18  	"k8s.io/client-go/rest"
    19  	"k8s.io/client-go/tools/clientcmd"
    20  )
    21  
    22  const (
    23  	// Before represents constant for before
    24  	Before = "before"
    25  	// After represents constant for after
    26  	After = "after"
    27  )
    28  
    29  // BackupStorageType type of storage
    30  type BackupStorageType string
    31  
    32  // Global framework.
    33  var Global *Framework
    34  
    35  // Framework handles communication with the kube cluster in e2e tests.
    36  type Framework struct {
    37  	KubeClient               kubernetes.Interface
    38  	KubeClient2              kubernetes.Clientset
    39  	KubeClientExt            clientset.Clientset
    40  	RestClient               rest.RESTClient
    41  	ExternalIP               string
    42  	CRClient                 client.VMOCR
    43  	Namespace                string
    44  	OperatorNamespace        string
    45  	SkipTeardown             bool
    46  	RunID                    string
    47  	Phase                    string
    48  	IngressControllerSvcName string
    49  	Ingress                  bool
    50  	ElasticsearchVersion     string
    51  }
    52  
    53  // Setup sets up a test framework and initialises framework.Global.
    54  func Setup() error {
    55  	kubeconfig := flag.String("kubeconfig", "", "Path to kubeconfig file with authorization and master location information.")
    56  	externalIP := flag.String("externalip", "localhost", "External IP over which to access deployments.")
    57  	namespace := flag.String("namespace", "default", "Integration test namespace")
    58  	operatorNamespace := flag.String("operatorNamespace", "", "Local test run mimicks prod environments")
    59  	skipTeardown := flag.Bool("skipteardown", false, "Skips tearing down VMO instances created by the tests")
    60  	runid := flag.String("runid", "test-"+generateRandomID(3), "Optional string that will be used to uniquely identify this test run.")
    61  	phase := flag.String("phase", "", "Optional 'phase' to test ("+Before+", "+After+")")
    62  	ingressControllerSvcName := flag.String("ingressControllerSvcName", "vmi-ingress-controller", "Ingress controller service name")
    63  	ingress := flag.Bool("ingress", false, "Use ingress port for testing")
    64  	flag.Parse()
    65  
    66  	if *operatorNamespace == "" {
    67  		operatorNamespace = namespace
    68  	}
    69  
    70  	cfg, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	kubeClient, err := kubernetes.NewForConfig(cfg)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	crClient, err := client.NewCRClient(cfg)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	kubeClientExt, err := clientset.NewForConfig(cfg)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	s := schema.GroupVersion{Group: constants.VMOGroup, Version: constants.VMOVersion}
    90  	cfg.GroupVersion = &s
    91  	cfg.APIPath = "/apis"
    92  	cfg.ContentType = runtime.ContentTypeJSON
    93  	cfg.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: serializer.NewCodecFactory(&runtime.Scheme{})}
    94  	restClient, err := rest.RESTClientFor(cfg)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	Global = &Framework{
   100  		KubeClient:               kubeClient,
   101  		KubeClient2:              *kubeClient,
   102  		KubeClientExt:            *kubeClientExt,
   103  		RestClient:               *restClient,
   104  		ExternalIP:               *externalIP,
   105  		CRClient:                 crClient,
   106  		Namespace:                *namespace,
   107  		OperatorNamespace:        *operatorNamespace,
   108  		SkipTeardown:             *skipTeardown,
   109  		RunID:                    *runid,
   110  		Phase:                    *phase,
   111  		IngressControllerSvcName: *ingressControllerSvcName,
   112  		Ingress:                  *ingress,
   113  		ElasticsearchVersion:     "7.5.2",
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  // Teardown shuts down the test framework and cleans up.
   120  func Teardown() error {
   121  	Global = nil
   122  	return nil
   123  }
   124  
   125  func generateRandomID(n int) string {
   126  	var letter = []rune("abcdefghijklmnopqrstuvwxyz")
   127  	nBig, err := rand.Int(rand.Reader, big.NewInt(int64(len(letter))))
   128  	if err != nil {
   129  		panic("failed to generate random numbers")
   130  	}
   131  	id := make([]rune, n)
   132  	for i := range id {
   133  		id[i] = letter[nBig.Int64()]
   134  	}
   135  	return string(id)
   136  }