@@ -103,11 +103,15 @@ type RunInput struct {
103
103
// because our tests require access to the *Environment. We use this field to make the created Environment available
104
104
// to the consumer.
105
105
//
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.
109
112
func Run (ctx context.Context , input RunInput ) int {
110
113
if os .Getenv ("CAPI_DISABLE_TEST_ENV" ) != "" {
114
+ klog .Info ("Skipping test env start as CAPI_DISABLE_TEST_ENV is set" )
111
115
return input .M .Run ()
112
116
}
113
117
@@ -124,6 +128,16 @@ func Run(ctx context.Context, input RunInput) int {
124
128
// Start the environment.
125
129
env .start (ctx )
126
130
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
+
127
141
if input .MinK8sVersion != "" {
128
142
if err := version .CheckKubernetesVersion (env .Config , input .MinK8sVersion ); err != nil {
129
143
fmt .Printf ("[IMPORTANT] skipping tests after failing version check: %v\n " , err )
@@ -140,6 +154,11 @@ func Run(ctx context.Context, input RunInput) int {
140
154
// Run tests
141
155
code := input .M .Run ()
142
156
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
+
143
162
// Tearing down the test environment
144
163
if err := env .stop (); err != nil {
145
164
panic (fmt .Sprintf ("Failed to stop the test environment: %v" , err ))
0 commit comments