Skip to content

Commit

Permalink
feat: envtest setup with docs per project
Browse files Browse the repository at this point in the history
  • Loading branch information
Camila Macedo committed Aug 5, 2020
1 parent 21f9343 commit de17c49
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 96 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ docs/book/book/
# skip bin
bin/*
testbin/*
testdata/project-v3-addon/testbin/*
testdata/project-v3-multigroup/testbin/*
testdata/project-v3/testbin/*

# skip .out files (coverage tests)
*.out
Expand Down
11 changes: 9 additions & 2 deletions docs/book/src/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,26 @@ arch=$(go env GOARCH)

# download kubebuilder and extract it to tmp
curl -L https://go.kubebuilder.io/dl/2.3.1/${os}/${arch} | tar -xz -C /tmp/
```

<aside class="note">
<h1>(No longer required for V3+) Envtest setup</h1>

If you are using v2 run:

```bash
# move to a long-term location and put it on your path
# (you'll need to set the KUBEBUILDER_ASSETS env var if you put it somewhere else)
sudo mv /tmp/kubebuilder_2.3.1_${os}_${arch} /usr/local/kubebuilder
export PATH=$PATH:/usr/local/kubebuilder/bin
```
</aside>

<aside class="note">
<h1>Using master branch</h1>

Also, you can install a master snapshot from `https://go.kubebuilder.io/dl/latest/${os}/${arch}`.


s
</aside>

<aside class="note">
Expand Down
37 changes: 36 additions & 1 deletion docs/book/src/reference/envtest.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ err = testEnv.Stop()
Logs from the test runs are prefixed with `test-env`.

### Configuring your test control plane
You can use environment variables and/or flags to specify the `api-server` and `etcd` setup within your integration tests.

The controller-runtime’s [envtest](https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest) framework requires `kubectl`, `kube-apiserver`, and `etcd` binaries to simulate the API portions of a real cluster.

For projects built with `v3+`, these binaries and setup will be made via the `test` makefile target by default where a directory called `testbin` will be added to project with them.

You can use environment variables and/or flags to specify the `kubectl`,`api-server` and `etcd` setup within your integration tests.

#### Environment Variables

Expand All @@ -37,6 +42,35 @@ You can use environment variables and/or flags to specify the `api-server` and `
| `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. |
| `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. |

See that the `test` makefile target will ensure that all is properly setup when you are use them. However, if you would like to run the tests without use the targets then, it is possible to set the environment variables directly in the code of your `suite_test.go` which would like:

```go
var _ = BeforeSuite(func(done Done) {
Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../testbin/bin/kube-apiserver")).To(Succeed())
Expect(os.Setenv("TEST_ASSET_ETCD", "../testbin/bin/etcd")).To(Succeed())
Expect(os.Setenv("TEST_ASSET_KUBECTL", "../testbin/bin/kubectl")).To(Succeed())

logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
testenv = &envtest.Environment{}

var err error
cfg, err = testenv.Start()
Expect(err).NotTo(HaveOccurred())

close(done)
}, 60)

var _ = AfterSuite(func() {
Expect(testenv.Stop()).To(Succeed())

Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed())
Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed())
Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed())

})
```

**NOTE:** The above setup is useful if you would like to run the tests via an IDE for example.

#### Flags
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`:
Expand All @@ -56,6 +90,7 @@ testEnv = &envtest.Environment{
}
```


### Testing considerations

Unless you're using an existing cluster, keep in mind that no built-in controllers are running in the test context. In some ways, the test control plane will behave differently from "real" clusters, and that might have an impact on how you write tests. One common example is garbage collection; because there are no controllers monitoring built-in resources, objects do not get deleted, even if an `OwnerReference` is set up.
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/v3/scaffolds/internal/templates/gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const gitignoreTemplate = `
*.so
*.dylib
bin
testbin/*
# Test binary, build with ` + "`go test -c`" + `
*.test
Expand Down
5 changes: 4 additions & 1 deletion pkg/plugin/v3/scaffolds/internal/templates/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ endif
all: manager
# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate fmt vet manifests
go test -race -coverprofile cover.out ./...
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
# Build manager binary
manager: generate fmt vet
Expand Down
86 changes: 0 additions & 86 deletions scripts/setup_envtest_bins.sh

This file was deleted.

3 changes: 0 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,4 @@ GO111MODULE=on test_project project-v3 3-alpha
GO111MODULE=on test_project project-v3-multigroup 3-alpha
GO111MODULE=on test_project project-v3-addon 3-alpha

# test script that setup envtest
./scripts/setup_envtest_bins.sh v1.18.2 v3.4.3

exit $rc
5 changes: 4 additions & 1 deletion testdata/project-v3-addon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ endif
all: manager

# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate fmt vet manifests
go test -race -coverprofile cover.out ./...
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
Expand Down
5 changes: 4 additions & 1 deletion testdata/project-v3-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ endif
all: manager

# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate fmt vet manifests
go test -race -coverprofile cover.out ./...
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
Expand Down
5 changes: 4 additions & 1 deletion testdata/project-v3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ endif
all: manager

# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate fmt vet manifests
go test -race -coverprofile cover.out ./...
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
Expand Down

0 comments on commit de17c49

Please sign in to comment.