Skip to content

Commit f136350

Browse files
authored
Merge pull request #1055 from pivotal-k8s/document-envtest-options
Document envtest options
2 parents 3f9e124 + 729e307 commit f136350

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

docs/book/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
- [Writing controller tests](./reference/writing-tests.md)
7272
- [Metrics](./reference/metrics.md)
7373

74+
- [Using envtest in integration tests](./reference/testing/envtest.md)
75+
7476
---
7577

7678
[Appendix: The TODO Landing Page](./TODO.md)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
```

docs/book/src/reference/writing-tests.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Writing controller tests
22

33
Testing Kubernetes controller is a big subject, and the boilerplate testing
4-
files generated for you by kubebuilder are fairly minimal. Until more
5-
documentation has been written, your best bet to get started is to look at some
4+
files generated for you by kubebuilder are fairly minimal.
5+
6+
[Writing and Running Integration Tests](/reference/testing/integration.md) documents steps to consider when writing integration steps for your controllers, and available options for configuring your test control plane using [`envtest`](https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/envtest).
7+
8+
Until more documentation has been written, your best bet to get started is to look at some
69
existing examples, such as:
710

811
* Azure Databricks Operator: see their fully fleshed-out

docs/testing/integration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
**Writing and Running Integration Tests**
22

3-
This article explores steps to write and run integration tests for Kubebuilder. Kubebuilder provides a template for writing integration tests. You can simply run all integration (and unit) tests within the project by running: `make test`
3+
This article explores steps to write and run integration tests for controllers created using Kubebuilder. Kubebuilder provides a template for writing integration tests. You can simply run all integration (and unit) tests within the project by running: `make test`
44

55
For example, there is a controller watches *Parent* objects. The *Parent* objects create *Child* objects. Note that the *Child* objects must have their `.ownerReferences` field setting to the `Parent` objects. You can find the template under `pkg/controller/parent/parent_controller_test.go`:
66
```
@@ -12,7 +12,7 @@ import (
1212
childv1alpha1 "k8s.io/childrepo/pkg/apis/child/v1alpha1"
1313
parentapis "k8s.io/parent/pkg/apis"
1414
parentv1alpha1 "k8s.io/parentrepo/pkg/apis/parent/v1alpha1"
15-
15+
1616
...<other import items>...
1717
)
1818
@@ -77,4 +77,4 @@ func TestReconcile(t *testing.T) {
7777

7878
The manager is started as part of the test itself (`StartTestManager` function).
7979

80-
Both functions are located in `pkg/controller/parent/parent_controller_suite_test.go` file. The file also contains a `TestMain` function that allows you to specify CRD directory paths for the testing environment.
80+
Both functions are located in `pkg/controller/parent/parent_controller_suite_test.go` file. The file also contains a `TestMain` function that allows you to specify CRD directory paths for the testing environment.

0 commit comments

Comments
 (0)