|
| 1 | +**Running End-to-end Tests on Remote Clusters** |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +The steps are as follow: |
| 6 | +1. Create a test file named `<some-file-name>_test.go` populated with template below (refer [this](https://github.com/foxish/application/blob/master/e2e/main_test.go)): |
| 7 | +``` |
| 8 | +import ( |
| 9 | + "k8s.io/client-go/tools/clientcmd" |
| 10 | + clientset "k8s.io/redis-operator/pkg/client/clientset/versioned/typed/<some-group>/<some-version>" |
| 11 | + ...... |
| 12 | +) |
| 13 | +
|
| 14 | +// Specify kubeconfig file |
| 15 | +func getClientConfig() (*rest.Config, error) { |
| 16 | + return clientcmd.BuildConfigFromFlags("", path.Join(os.Getenv("HOME"), "<file-path>")) |
| 17 | +} |
| 18 | +
|
| 19 | +// Set up test environment |
| 20 | +var _ = Describe("<some-controller-name> should work", func() { |
| 21 | + config, err := getClientConfig() |
| 22 | + if err != nil { |
| 23 | + ...... |
| 24 | + } |
| 25 | +
|
| 26 | + // Construct kubernetes client |
| 27 | + k8sClient, err := kubernetes.NewForConfig(config) |
| 28 | + if err != nil { |
| 29 | + ...... |
| 30 | + } |
| 31 | +
|
| 32 | + // Construct controller client |
| 33 | + client, err := clientset.NewForConfig(config) |
| 34 | + if err != nil { |
| 35 | + ...... |
| 36 | + } |
| 37 | +
|
| 38 | + BeforeEach(func() { |
| 39 | + // Create controller image StatefulSet using kubernetes client |
| 40 | + // Note: Refer `install.yaml` created via `kubebuilder create config` |
| 41 | + // to have an idea on what the StatefulSet's fields should look like |
| 42 | + ...... |
| 43 | +
|
| 44 | + // Create other necessary resources before starting each test |
| 45 | + ...... |
| 46 | + }) |
| 47 | +
|
| 48 | + AfterEach(func() { |
| 49 | + // Delete all created resources for testing purpose |
| 50 | + ...... |
| 51 | +
|
| 52 | + // Delete controller image StatefulSet |
| 53 | + ...... |
| 54 | + }) |
| 55 | +
|
| 56 | + // Declare a list of testing specifications and corresponding 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 .` to run the e2e tests |
0 commit comments