Skip to content

Commit

Permalink
feat: add new target to setup envtest
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Jul 25, 2020
1 parent 21f9343 commit f331023
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 9 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ testbin/*
/testdata/project-v3/go.sum
/testdata/project-v3-multigroup/go.sum
/testdata/project-v3-addon/go.sum

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
testbin

# skip shell script to setup envtest from testdata and local
testdata/*/setup_envtest.sh
setup_envtest.sh
13 changes: 12 additions & 1 deletion docs/book/src/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ Build and push your image to the location specified by `IMG`:
make docker-build docker-push IMG=<some-registry>/<project-name>:tag
```

<aside class="note">
<h1>Test setup/h1>

The Makefile target `docker-build` will also calls the `test` one.
Note that `kubebuilder` will configure its requirements in the `testbin/` by default.
More info: [Configuring your test control plane][config-test].

</aside>


Deploy the controller to the cluster with image specified by `IMG`:

```bash
Expand Down Expand Up @@ -229,4 +239,5 @@ Now, follow up the [CronJob tutorial][cronjob-tutorial] to better understand how
[cronjob-tutorial]: https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html
[GOPATH-golang-docs]: https://golang.org/doc/code.html#GOPATH
[how-to-write-go-code-golang-docs]: https://golang.org/doc/code.html

[cronjob-tutorial]: https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html
[config-test]: https://book.kubebuilder.io/reference/testing/envtest.html?highlight=#configuring-your-test-control-plane
15 changes: 14 additions & 1 deletion docs/book/src/reference/envtest.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ 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.

You can use environment variables and/or flags to specify the `kubectl`, `api-server` and `etcd` setup within your
integration tests. The location of the binaries which will be used by the EnvTest is done due the following environment
variables. See:

```shell
export TEST_ASSET_KUBECTL=<kubectl bin path>
export TEST_ASSET_KUBE_APISERVER=<api-server bin path>
export TEST_ASSET_ETCD=/<etd bin path>
```

Note that `kubebuilder` scaffold in your Makefile the `testsetup` target which creates the `testbin/` directory in the
project where you will find its required binaries by default. Also, this target will setup the above environment
variables to looking for them in the created directory as well.

#### Environment Variables

| Variable name | Type | When to use |
Expand Down
10 changes: 10 additions & 0 deletions pkg/plugin/v3/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ const (
ControllerToolsVersion = "v0.3.0"
// KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project
KustomizeVersion = "v3.5.4"
//KubernetesVersion is the K8S version which will be used in the script
KubernetesVersion = "v1.18.2"
//ETCDVersion is the ETCD version which will be used in the script
ETCDVersion = "v3.4.3"
//todo: implement func which allow us get kb version
//KubebuilderVersion is the Kubebuilder version which will be used to download the script. E.g tag release
KubebuilderVersion = "master"

imageName = "controller:latest"
)
Expand Down Expand Up @@ -110,6 +117,9 @@ func (s *initScaffolder) scaffold() error {
BoilerplatePath: s.boilerplatePath,
ControllerToolsVersion: ControllerToolsVersion,
KustomizeVersion: KustomizeVersion,
KubernetesVersion: KubernetesVersion,
ETCDVersion: ETCDVersion,
KubebuilderVersion: KubebuilderVersion,
},
&templates.Dockerfile{},
&templates.DockerignoreFile{},
Expand Down
4 changes: 4 additions & 0 deletions pkg/plugin/v3/scaffolds/internal/templates/gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ func (f *GitIgnore) SetTemplateDefaults() error {
}

const gitignoreTemplate = `
# ignore shell script to setup env test
setup_envtest.sh
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
testbin
# Test binary, build with ` + "`go test -c`" + `
*.test
Expand Down
16 changes: 15 additions & 1 deletion pkg/plugin/v3/scaffolds/internal/templates/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type Makefile struct {
ControllerToolsVersion string
//Kustomize version to use in the project
KustomizeVersion string
//KubernetesVersion is the K8S version which will be used in the script
KubernetesVersion string
//ETCDVersion is the ETCD version which will be used in the script
ETCDVersion string
//KubebuilderVersion is the Kubebuilder version which will be used to download the script. E.g tag release
KubebuilderVersion string
}

// SetTemplateDefaults implements input.Template
Expand Down Expand Up @@ -70,7 +76,7 @@ endif
all: manager
# Run tests
test: generate fmt vet manifests
test: testsetup generate fmt vet manifests
go test -race -coverprofile cover.out ./...
# Build manager binary
Expand Down Expand Up @@ -153,4 +159,12 @@ KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif
# Setup binaries required to run the tests
# The script will create the testbin dir to store them
# See that it expects the Kubernetes and ETCD versions
testsetup:
curl -sSLo setup_envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/kubebuilder/{{ .KubebuilderVersion }}/scripts/setup_envtest_bins.sh
chmod +x setup_envtest.sh
./setup_envtest.sh {{ .KubernetesVersion }} {{ .ETCDVersion }}
`
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
10 changes: 9 additions & 1 deletion testdata/project-v3-addon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ endif
all: manager

# Run tests
test: generate fmt vet manifests
test: testsetup generate fmt vet manifests
go test -race -coverprofile cover.out ./...

# Build manager binary
Expand Down Expand Up @@ -97,3 +97,11 @@ KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif

# Setup binaries required to run the tests
# The script will create the testbin dir to store them
# See that it expects the Kubernetes and ETCD versions
testsetup:
curl -sSLo setup_envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/kubebuilder/master/scripts/setup_envtest_bins.sh
chmod +x setup_envtest.sh
./setup_envtest.sh v1.18.2 v3.4.3
10 changes: 9 additions & 1 deletion testdata/project-v3-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ endif
all: manager

# Run tests
test: generate fmt vet manifests
test: testsetup generate fmt vet manifests
go test -race -coverprofile cover.out ./...

# Build manager binary
Expand Down Expand Up @@ -97,3 +97,11 @@ KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif

# Setup binaries required to run the tests
# The script will create the testbin dir to store them
# See that it expects the Kubernetes and ETCD versions
testsetup:
curl -sSLo setup_envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/kubebuilder/master/scripts/setup_envtest_bins.sh
chmod +x setup_envtest.sh
./setup_envtest.sh v1.18.2 v3.4.3
10 changes: 9 additions & 1 deletion testdata/project-v3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ endif
all: manager

# Run tests
test: generate fmt vet manifests
test: testsetup generate fmt vet manifests
go test -race -coverprofile cover.out ./...

# Build manager binary
Expand Down Expand Up @@ -97,3 +97,11 @@ KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif

# Setup binaries required to run the tests
# The script will create the testbin dir to store them
# See that it expects the Kubernetes and ETCD versions
testsetup:
curl -sSLo setup_envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/kubebuilder/master/scripts/setup_envtest_bins.sh
chmod +x setup_envtest.sh
./setup_envtest.sh v1.18.2 v3.4.3

0 comments on commit f331023

Please sign in to comment.