|
| 1 | +## Using envtest in integration tests |
| 2 | +[`controller-runtime`](http://sigs.k8s.io/controller-runtime) offers `envtest` ([godoc](https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/envtest)), a package that helps write integration tests for your controllers by setting up and starting an instance of etcd and the Kubernetes API server, without kubelet, controller-manager or other components. |
| 3 | + |
| 4 | +Using `envtest` in integration tests follows the general flow of: |
| 5 | + |
| 6 | +``` |
| 7 | +import sigs.k8s.io/controller-runtime/pkg/envtest |
| 8 | +
|
| 9 | +//specify testEnv configuration |
| 10 | +testEnv = &envtest.Environment{ |
| 11 | + CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, |
| 12 | +} |
| 13 | +
|
| 14 | +//start testEnv |
| 15 | +cfg, err = testEnv.Start() |
| 16 | +
|
| 17 | +//write test logic |
| 18 | +
|
| 19 | +//stop testEnv |
| 20 | +err = testEnv.Stop() |
| 21 | +``` |
| 22 | + |
| 23 | +`kubebuilder` does the boilerplate setup and teardown of testEnv for you, in the ginkgo test suite that it generates under the `/controllers` directory. |
| 24 | + |
| 25 | +Logs from the test runs are prefixed with `test-env`. |
| 26 | + |
| 27 | +### Configuring your test control plane |
| 28 | +You can use environment variables and/or flags to specify the `api-server` and `etcd` setup within your integration tests. |
| 29 | + |
| 30 | +#### Environment Variables |
| 31 | + |
| 32 | +| Variable name | Type | When to use | |
| 33 | +| --- | :--- | :--- | |
| 34 | +| `USE_EXISTING_CLUSTER` | boolean | Instead of setting up a local control plane, point to the control plane of an existing cluster. | |
| 35 | +| `KUBEBUILDER_ASSETS` | path to directory | Point integration tests to a directory containing all binaries (api-server, etcd and kubectl). | |
| 36 | +| `TEST_ASSET_KUBE_APISERVER`, `TEST_ASSET_ETCD`, `TEST_ASSET_KUBECTL` | paths to, respectively, api-server, etcd and kubectl binaries | Similar to `KUBEBUILDER_ASSETS`, but more granular. Point integration tests to use binaries other than the default ones. These environment variables can also be used to ensure specific tests run with expected versions of these binaries. | |
| 37 | +| `KUBEBUILDER_CONTROLPLANE_START_TIMEOUT` and `KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT` | durations in format supported by [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration) | Specify timeouts different from the default for the test control plane to (respectively) start and stop; any test run that exceeds them will fail. | |
| 38 | +| `KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT` | boolean | Set to `true` to attach the control plane's stdout and stderr to os.Stdout and os.Stderr. This can be useful when debugging test failures, as output will include output from the control plane. | |
| 39 | + |
| 40 | + |
| 41 | +#### Flags |
| 42 | +Here's an example of modifying the flags with which to start the API server in your integration tests, compared to the default values in `envtest.DefaultKubeAPIServerFlags`: |
| 43 | + |
| 44 | +``` |
| 45 | +customApiServerFlags := []string{ |
| 46 | + "--secure-port=6884", |
| 47 | + "--admission-control=MutatingAdmissionWebhook", |
| 48 | +} |
| 49 | +
|
| 50 | +apiServerFlags := append(envtest.DefaultKubeAPIServerFlags, customApiServerFlags) |
| 51 | +
|
| 52 | +testEnv = &envtest.Environment{ |
| 53 | + CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, |
| 54 | + KubeAPIServerFlags: apiServerFlags, |
| 55 | +} |
| 56 | +``` |
0 commit comments