Skip to content

Commit 9fe7ff5

Browse files
authored
Merge pull request #8088 from dlipovetsky/dlipovetsky/vscode-envtest-integration
✨ Make it possible to run envtest-based integration tests from vscode
2 parents 6fede8c + 4888af5 commit 9fe7ff5

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,10 @@ ARTIFACTS ?= ${ROOT_DIR}/_artifacts
731731

732732
KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION))
733733

734+
.PHONY: setup-envtest
735+
setup-envtest: $(SETUP_ENVTEST) ## Set up envtest (download kubebuilder assets)
736+
@echo KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS)
737+
734738
.PHONY: test
735739
test: $(SETUP_ENVTEST) ## Run unit and integration tests
736740
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"go.testEnvFile": "${workspaceFolder}/.vscode/test.env"
3+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"type": "shell",
8+
"label": "sigs.k8s.io/cluster-api: Prepare vscode to run envtest-based tests",
9+
"detail": "Install envtest and configure the vscode-go test environment.",
10+
"group": {
11+
"kind": "test",
12+
"isDefault": true
13+
},
14+
"command": [
15+
"echo $(make setup-envtest) > ${workspaceFolder}/.vscode/test.env",
16+
],
17+
"presentation": {
18+
"echo": true,
19+
"reveal": "silent",
20+
"focus": true,
21+
"panel": "shared",
22+
"showReuseMessage": true,
23+
"clear": false
24+
},
25+
"runOptions": {
26+
"runOn": "folderOpen",
27+
"instanceLimit": 1,
28+
},
29+
"promptOnClose": true,
30+
}
31+
]
32+
}

dev/vscode-example-configuration/test.env

Whitespace-only changes.

docs/book/src/developer/repository-layout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cluster-api
1212
└───config
1313
└───controllers
1414
└───controlplane
15+
└───dev
1516
└───docs
1617
└───errors
1718
└───exp
@@ -115,6 +116,10 @@ This folder has scripts used for building, testing and developer workflow.
115116

116117
This folder consists of CI scripts related to setup, build and e2e tests. These are mostly called by CI jobs.
117118

119+
[~/dev](https://github.com/kubernetes-sigs/cluster-api/tree/main/dev)
120+
121+
This folder has example configuration for integrating Cluster API development with tools like IDEs.
122+
118123
### Util, Feature and Errors
119124

120125
[~/util](https://github.com/kubernetes-sigs/cluster-api/tree/main/util)

docs/book/src/developer/testing.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ but in this case the distinctive value of the two layers of testing is determine
9595

9696
Run `make test` to execute all unit and integration tests.
9797

98+
Integration tests use the [envtest](https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/envtest/doc.go) test framework. The tests need to know the location of the executables called by the framework. The `make test` target installs these executables, and passes this location to the tests as an environment variable.
99+
98100
<aside class="note">
99101

100102
<h1>Tips</h1>
@@ -113,6 +115,30 @@ To debug testenv unit tests it is possible to use:
113115

114116
</aside>
115117

118+
### Test execution via IDE
119+
120+
Your IDE needs to know the location of the executables called by the framework, so that it can pass the location to the tests as an environment variable.
121+
122+
<aside class="note warning">
123+
124+
<h1>Warning</h1>
125+
126+
If you see this error when running a test in your IDE, the test uses the envtest framework, and probably does not know the location of the envtest executables.
127+
128+
```console
129+
E0210 16:11:04.222471 132945 server.go:329] controller-runtime/test-env "msg"="unable to start the controlplane" "error"="fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory" "tries"=0
130+
```
131+
132+
</aside>
133+
134+
#### VSCode
135+
136+
The `dev/vscode-example-configuration` directory in the repository contains an example configuration that integrates VSCode with the envtest framework.
137+
138+
To use the example configuration, copy the files to the `.vscode` directory in the repository, and restart VSCode.
139+
140+
The configuration works as follows: Whenever the project is opened in VSCode, a VSCode task runs that installs the executables, and writes the location to a file. A setting tells [vscode-go] to initialize the environment from this file.
141+
116142
## End-to-end tests
117143

118144
The end-to-end tests are meant to verify the proper functioning of a Cluster API management cluster
@@ -536,3 +562,5 @@ In Cluster API Unit and integration test MUST use [go test].
536562
[envtest]: https://github.com/kubernetes-sigs/controller-runtime/tree/master/pkg/envtest
537563
[fakeclient]: https://github.com/kubernetes-sigs/controller-runtime/tree/master/pkg/client/fake
538564
[test/helpers]: https://github.com/kubernetes-sigs/cluster-api/tree/main/test/helpers
565+
566+
[vscode-go]: https://marketplace.visualstudio.com/items?itemName=golang.Go

0 commit comments

Comments
 (0)