|
| 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