Skip to content

Commit daa30ff

Browse files
authored
Merge pull request #2900 from rumstead/feat/update-envtest-docs
📖 feat(docs): update envtest docs
2 parents 520df43 + 5cbcb8c commit daa30ff

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

docs/book/src/reference/envtest.md

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
# Configuring envtest for integration tests
22

3-
The [`controller-runtime/pkg/envtest`][envtest] Go library 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+
The [`controller-runtime/pkg/envtest`][envtest] Go library helps write integration tests for your controllers by setting up and starting an instance of etcd and the
4+
Kubernetes API server, without kubelet, controller-manager or other components.
45

56
## Installation
67

7-
The `test` make target, also called by the `docker-build` target,
8-
[downloads][setup-envtest] a set of envtest binaries (described above) to run tests with.
9-
Typically nothing needs to be done on your part,
10-
as the download and install script is fully automated,
11-
although it does require `bash` to run.
8+
Installing the binaries is as a simple as running `make envtest`. `envtest` will download the Kubernetes API server binaries to the `bin/` folder in your project
9+
by default. `make test` is the one-stop shop for downloading the binaries, setting up the test environment, and running the tests.
1210

13-
If you would like to download the tarball containing these binaries,
14-
to use in a disconnected environment for example,
15-
run the following (Kubernetes version 1.21.2 is an example version):
11+
The make targets require `bash` to run.
1612

17-
```sh
18-
export K8S_VERSION=1.21.2
19-
curl -sSLo envtest-bins.tar.gz "https://go.kubebuilder.io/test-tools/${K8S_VERSION}/$(go env GOOS)/$(go env GOARCH)"
20-
```
13+
## Installation in Air Gaped/disconnected environments
14+
If you would like to download the tarball containing the binaries, to use in a disconnected environment you can use
15+
`setup-envtest` to download the required binaries locally. There are a lot of ways to configure `setup-envtest` to avoid talking to
16+
the internet you can read about them [here](https://github.com/kubernetes-sigs/controller-runtime/tree/master/tools/setup-envtest#what-if-i-dont-want-to-talk-to-the-internet).
17+
The examples below will show how to install the Kubernetes API binaries using mostly defaults set by `setup-envtest`.
2118

22-
Then install them:
19+
### Download the binaries
20+
`make envtest` will download the `setup-envtest` binary to `./bin/`.
21+
```shell
22+
make envtest
23+
```
2324

25+
Installing the binaries using `setup-envtest` stores the binary in OS specific locations, you can read more about them
26+
[here](https://github.com/kubernetes-sigs/controller-runtime/tree/master/tools/setup-envtest#where-does-it-put-all-those-binaries)
2427
```sh
25-
mkdir /usr/local/kubebuilder
26-
tar -C /usr/local/kubebuilder --strip-components=1 -zvxf envtest-bins.tar.gz
28+
./bin/setup-envtest use 1.21.2
2729
```
2830

29-
Once these binaries are installed, you can either change the `test` target to:
31+
### Update the test make target
32+
Once these binaries are installed, change the `test` make target to include a `-i` like below. `-i` will only check for locally installed
33+
binaries and not reach out to remote resources. You could also set the `ENVTEST_INSTALLED_ONLY` env variable.
3034

3135
```makefile
3236
test: manifests generate fmt vet
33-
go test ./... -coverprofile cover.out
37+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -i --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
3438
```
3539

36-
Or configure the existing target to skip the download and point to a [custom location](#environment-variables):
37-
40+
NOTE: The `ENVTEST_K8S_VERSION` needs to match the `setup-envtest` you downloaded above. Otherwise, you will see an error like the below
3841
```sh
39-
make test SKIP_FETCH_TOOLS=1 KUBEBUILDER_ASSETS=/usr/local/kubebuilder
42+
no such version (1.24.5) exists on disk for this architecture (darwin/amd64) -- try running `list -i` to see what's on disk
4043
```
4144
42-
### Kubernetes 1.20 and 1.21 binary issues
45+
## Kubernetes 1.20 and 1.21 binary issues
4346
4447
There have been many reports of the `kube-apiserver` or `etcd` binary [hanging during cleanup][cr-1571]
4548
or misbehaving in other ways. We recommend using the 1.19.2 tools version to circumvent such issues,
@@ -77,7 +80,17 @@ Logs from the test runs are prefixed with `test-env`.
7780
7881
Controller-runtime’s [envtest][envtest] framework requires `kubectl`, `kube-apiserver`, and `etcd` binaries be present locally to simulate the API portions of a real cluster.
7982
80-
For projects built with plugin v3+ (see your PROJECT file's `layout` key), the `make test` command will install these binaries to the `testbin/` directory and use them when running tests that use `envtest`.
83+
The `make test` command will install these binaries to the `bin/` directory and use them when running tests that use `envtest`.
84+
Ie,
85+
```shell
86+
./bin/k8s/
87+
└── 1.24.2-darwin-amd64
88+
├── etcd
89+
├── kube-apiserver
90+
└── kubectl
91+
92+
1 directory, 3 files
93+
```
8194
8295
You can use environment variables and/or flags to specify the `kubectl`,`api-server` and `etcd` setup within your integration tests.
8396
@@ -95,9 +108,11 @@ See that the `test` makefile target will ensure that all is properly setup when
95108

96109
```go
97110
var _ = BeforeSuite(func(done Done) {
98-
Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../testbin/bin/kube-apiserver")).To(Succeed())
99-
Expect(os.Setenv("TEST_ASSET_ETCD", "../testbin/bin/etcd")).To(Succeed())
100-
Expect(os.Setenv("TEST_ASSET_KUBECTL", "../testbin/bin/kubectl")).To(Succeed())
111+
Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../bin/k8s/1.24.2-darwin-amd64/kube-apiserver")).To(Succeed())
112+
Expect(os.Setenv("TEST_ASSET_ETCD", "../bin/k8s/1.24.2-darwin-amd64/etcd")).To(Succeed())
113+
Expect(os.Setenv("TEST_ASSET_KUBECTL", "../bin/k8s/1.24.2-darwin-amd64/kubectl")).To(Succeed())
114+
// OR
115+
Expect(os.Setenv("KUBEBUILDER_ASSETS", "../bin/k8s/1.24.2-darwin-amd64")).To(Succeed())
101116
102117
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
103118
testenv = &envtest.Environment{}

0 commit comments

Comments
 (0)