sigs.k8s.io/kubebuilder/v3@v3.14.0/docs/testing/e2e.md (about)

     1  **Running End-to-end Tests on Remote Clusters**
     2  
     3  **This document is for kubebuilder v1 only**
     4  
     5  This article outlines steps to run e2e tests on remote clusters for controllers created using `kubebuilder`. For example, after developing a database controller, the developer may want to run some e2e tests on a GKE cluster to verify the controller is working as expected. Currently, `kubebuilder` does not provide a template for running the e2e tests. This article serves to address this deficit.
     6  
     7  The steps are as follow:
     8  1.  Create a test file named `<some-file-name>_test.go` populated with template below (referring [this](https://github.com/foxish/application/blob/master/e2e/main_test.go)):
     9  ```
    10  import (
    11      "k8s.io/client-go/tools/clientcmd"
    12      clientset "k8s.io/redis-operator/pkg/client/clientset/versioned/typed/<some-group>/<some-version>"
    13      ......
    14  )
    15  
    16  // Specify kubeconfig file 
    17  func getClientConfig() (*rest.Config, error) {
    18      return clientcmd.BuildConfigFromFlags("", path.Join(os.Getenv("HOME"), "<file-path>"))
    19  }
    20  
    21  // Set up test environment
    22  var _ = Describe("<some-controller-name> should work", func() {
    23      config, err := getClientConfig()
    24      if err != nil {
    25          ......
    26      }
    27  
    28      // Construct kubernetes client
    29      k8sClient, err := kubernetes.NewForConfig(config)
    30      if err != nil {
    31          ......
    32      }
    33  
    34      // Construct controller client
    35      client, err := clientset.NewForConfig(config)
    36      if err != nil {
    37          ......
    38      }
    39  
    40      BeforeEach(func() {
    41          // Create environment-specific resources such as controller image StatefulSet,
    42          // CRDs etc. Note: refer "install.yaml" created via "kubebuilder create config"
    43          // command to have an idea of what resources to be created.
    44          ......
    45      })
    46  
    47      AfterEach(func() {
    48          // Delete all test-specific resources
    49          ......		
    50  
    51          // Delete all environment-specific resources
    52          ......
    53      })
    54  
    55      // Declare a list of testing specifications with corresponding test functions
    56      // Note: test-specific resources are normally created within the test functions
    57      It("should do something", func() {
    58          testDoSomething(k8sClient, roClient)
    59      })
    60      
    61      ......
    62  ```
    63  2.  Write some controller-specific e2e tests
    64  3.  Build controller image and upload it to an image storage website such as [gcr.io](https://cloud.google.com/container-registry/)
    65  4.  `go test <path-to-test-file>`