Skip to content

Commit ef73f4e

Browse files
doc: add envtest setup info (#3517)
1 parent ba4a446 commit ef73f4e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

website/content/en/docs/golang/project_migration_guide.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ Refer to [`memcached_controller.go`][kb_memcached_controller] for the example im
122122

123123
Run [`make manifests`][generate_crd] to generate CRD manifests. They would be generated inside the `config/crd/bases` folder.
124124

125+
## Configuring your test environment
126+
127+
Projects are scaffolded with unit tests that utilize the [envtest](https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest)
128+
library, which requires certain Kubernetes server binaries be present locally.
129+
Installation instructions can be found [here][env-test-setup].
130+
125131
## Operator Manifests
126132

127133
### Operator deployment manifests
@@ -173,3 +179,4 @@ The project can now be built, and the operator can be deployed on-cluster. For f
173179
[kb_memcached_controller]: https://github.com/operator-framework/operator-sdk/blob/v0.19.x/example/kb-memcached-operator/memcached_controller.go.tmpl
174180
[kb_quickstart]: /docs/golang/quickstart/
175181
[install_guide]: /docs/install-operator-sdk/
182+
[env-test-setup]: /docs/golang/references/env-test-setup

website/content/en/docs/golang/quickstart.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ Once this is done, there are two ways to run the operator:
288288
- As Go program outside a cluster
289289
- As a Deployment inside a Kubernetes cluster
290290
291+
## Configuring your test environment
292+
293+
Projects are scaffolded with unit tests that utilize the [envtest](https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest)
294+
library, which requires certain Kubernetes server binaries be present locally.
295+
Installation instructions can be found [here][env-test-setup].
296+
291297
### 1. Run locally outside the cluster
292298
293299
To run the operator locally execute the following command:
@@ -494,3 +500,4 @@ Also see the [advanced topics][advanced_topics] doc for more use cases and under
494500
[status_subresource]: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#status-subresource
495501
[API-groups]:https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups
496502
[legacy_CLI]:/docs/cli
503+
[env-test-setup]: /docs/golang/references/env-test-setup
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: EnvTest Setup
3+
linkTitle: EnvTest Setup
4+
weight: 50
5+
---
6+
7+
## Overview
8+
9+
This document describes how to configure the environment for the [controller tests][controller-test] which uses [envtest][envtest] and is supported by the SDK.
10+
11+
## Installing prerequisites
12+
13+
[Envtest][envtest] requires that `kubectl`, `api-server` and `etcd` be present locally. You can use this [script][script] to download these binaries into the `testbin/` directory. It may be convenient to add this script your Makefile as follows:
14+
15+
```sh
16+
# Setup binaries required to run the tests
17+
# See that it expects the Kubernetes and ETCD version
18+
K8S_VERSION = v1.18.2
19+
ETCD_VERSION = v3.4.3
20+
testbin:
21+
curl -sSLo setup_envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/kubebuilder/master/scripts/setup_envtest_bins.sh
22+
chmod +x setup_envtest.sh
23+
./setup_envtest.sh $(K8S_VERSION) $(ETCD_VERSION)
24+
```
25+
26+
27+
The above script sets these environment variables to specify where test binaries can be found. In case you would like to not use the script then, is possible to do the same configuration to inform the path of your binaries:
28+
29+
```shell
30+
$ export TEST_ASSET_KUBECTL=<kubectl-bin-path>
31+
$ export TEST_ASSET_KUBE_APISERVER=<api-server-bin-path>
32+
$ export TEST_ASSET_ETCD=<etcd-bin-path>
33+
```
34+
35+
See that the environment variables also can be specified via your `controllers/suite_test.go` such as the following example.
36+
37+
```go
38+
var _ = BeforeSuite(func(done Done) {
39+
Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../../testbin/kube-apiserver")).To(Succeed())
40+
Expect(os.Setenv("TEST_ASSET_ETCD", "../../testbin/etcd")).To(Succeed())
41+
Expect(os.Setenv("TEST_ASSET_KUBECTL", "../../testbin/kubectl")).To(Succeed())
42+
43+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
44+
testenv = &envtest.Environment{}
45+
46+
var err error
47+
cfg, err = testenv.Start()
48+
Expect(err).NotTo(HaveOccurred())
49+
50+
close(done)
51+
}, 60)
52+
53+
var _ = AfterSuite(func() {
54+
Expect(testenv.Stop()).To(Succeed())
55+
56+
Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed())
57+
Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed())
58+
Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed())
59+
60+
})
61+
```
62+
[envtest]: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest
63+
[controller-test]: https://book.kubebuilder.io/reference/writing-tests.html
64+
[script]: https://raw.githubusercontent.com/kubernetes-sigs/kubebuilder/master/scripts/setup_envtest_bins.sh

0 commit comments

Comments
 (0)