Skip to content

Commit 72ad2dc

Browse files
authored
Merge pull request #8042 from sbueringer/pr-envtest-envvar-kubeconfig
🌱 envtest: add env var to allow writing envtest kubeconfig
2 parents 7b79aa2 + d41a4e6 commit 72ad2dc

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

docs/book/src/developer/testing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ When running individual tests, it could happen that a testenv is started if this
107107
However, if the tests you are running don't require testenv (i.e. they are only using fake client), you can skip the testenv
108108
creation by setting the environment variable `CAPI_DISABLE_TEST_ENV` (to any non-empty value).
109109

110+
To debug testenv unit tests it is possible to use:
111+
* `CAPI_TEST_ENV_KUBECONFIG` to write out a kubeconfig for the testenv to a file location.
112+
* `CAPI_TEST_ENV_SKIP_STOP` to skip stopping the testenv after test execution.
113+
110114
</aside>
111115

112116
## End-to-end tests

internal/test/envtest/environment.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,15 @@ type RunInput struct {
103103
// because our tests require access to the *Environment. We use this field to make the created Environment available
104104
// to the consumer.
105105
//
106-
// Note: Test environment creation can be skipped by setting the environment variable `CAPI_DISABLE_TEST_ENV`. This only
107-
//
108-
// makes sense when executing tests which don't require the test environment, e.g. tests using only the fake client.
106+
// Note: Test environment creation can be skipped by setting the environment variable `CAPI_DISABLE_TEST_ENV`
107+
// to a non-empty value. This only makes sense when executing tests which don't require the test environment,
108+
// e.g. tests using only the fake client.
109+
// Note: It's possible to write a kubeconfig for the test environment to a file by setting `CAPI_TEST_ENV_KUBECONFIG`.
110+
// Note: It's possible to skip stopping the test env after the tests have been run by setting `CAPI_TEST_ENV_SKIP_STOP`
111+
// to a non-empty value.
109112
func Run(ctx context.Context, input RunInput) int {
110113
if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" {
114+
klog.Info("Skipping test env start as CAPI_DISABLE_TEST_ENV is set")
111115
return input.M.Run()
112116
}
113117

@@ -124,6 +128,16 @@ func Run(ctx context.Context, input RunInput) int {
124128
// Start the environment.
125129
env.start(ctx)
126130

131+
if kubeconfigPath := os.Getenv("CAPI_TEST_ENV_KUBECONFIG"); kubeconfigPath != "" {
132+
klog.Infof("Writing test env kubeconfig to %q", kubeconfigPath)
133+
config := kubeconfig.FromEnvTestConfig(env.Config, &clusterv1.Cluster{
134+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
135+
})
136+
if err := os.WriteFile(kubeconfigPath, config, 0600); err != nil {
137+
panic(errors.Wrapf(err, "failed to write the test env kubeconfig"))
138+
}
139+
}
140+
127141
if input.MinK8sVersion != "" {
128142
if err := version.CheckKubernetesVersion(env.Config, input.MinK8sVersion); err != nil {
129143
fmt.Printf("[IMPORTANT] skipping tests after failing version check: %v\n", err)
@@ -140,6 +154,11 @@ func Run(ctx context.Context, input RunInput) int {
140154
// Run tests
141155
code := input.M.Run()
142156

157+
if skipStop := os.Getenv("CAPI_TEST_ENV_SKIP_STOP"); skipStop != "" {
158+
klog.Info("Skipping test env stop as CAPI_TEST_ENV_SKIP_STOP is set")
159+
return code
160+
}
161+
143162
// Tearing down the test environment
144163
if err := env.stop(); err != nil {
145164
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))

0 commit comments

Comments
 (0)