Skip to content

Commit 539b440

Browse files
Merge pull request #1717 from alexander-demicev/e2ecleanup
Print error in artifacts collection instead of failing the suite
2 parents b3abf23 + 1a2bd25 commit 539b440

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

test/e2e/specs/import_gitops.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,21 +375,15 @@ func CreateUsingGitOpsSpec(ctx context.Context, inputGetter func() CreateUsingGi
375375
AfterEach(func() {
376376
By(fmt.Sprintf("Collecting artifacts for %s/%s", input.ClusterName, specName))
377377

378-
err := testenv.CollectArtifacts(ctx, testenv.CollectArtifactsInput{
378+
testenv.TryCollectArtifacts(ctx, testenv.CollectArtifactsInput{
379379
Path: input.ClusterName + "-bootstrap-" + specName,
380380
BootstrapKubeconfigPath: input.BootstrapClusterProxy.GetKubeconfigPath(),
381381
})
382-
if err != nil {
383-
log.FromContext(ctx).Error(err, "failed to collect artifacts for the bootstrap cluster")
384-
}
385382

386-
err = testenv.CollectArtifacts(ctx, testenv.CollectArtifactsInput{
383+
testenv.TryCollectArtifacts(ctx, testenv.CollectArtifactsInput{
387384
KubeconfigPath: originalKubeconfig.TempFilePath,
388385
Path: input.ClusterName + "-workload-" + specName,
389386
})
390-
if err != nil {
391-
log.FromContext(ctx).Error(err, "failed to collect artifacts for the child cluster")
392-
}
393387

394388
// If SKIP_RESOURCE_CLEANUP=true & if the SkipDeletionTest is true, all the resources should stay as they are,
395389
// nothing should be deleted. If SkipDeletionTest is true, deleting the git repo will delete the clusters too.

test/e2e/suites/v2prov/v2prov_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
corev1 "k8s.io/api/core/v1"
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232
"sigs.k8s.io/controller-runtime/pkg/envtest/komega"
33-
"sigs.k8s.io/controller-runtime/pkg/log"
3433

3534
provisioningv1 "github.com/rancher/turtles/api/rancher/provisioning/v1"
3635
"github.com/rancher/turtles/test/e2e"
@@ -171,24 +170,18 @@ var _ = Describe("[v2prov] [Azure] Creating a cluster with v2prov should still w
171170
AfterEach(func() {
172171
By(fmt.Sprintf("Collecting artifacts for %s/%s", clusterName, specName))
173172

174-
err := testenv.CollectArtifacts(ctx, testenv.CollectArtifactsInput{
173+
testenv.TryCollectArtifacts(ctx, testenv.CollectArtifactsInput{
175174
Path: clusterName + "-bootstrap-" + specName,
176175
BootstrapKubeconfigPath: bootstrapClusterProxy.GetKubeconfigPath(),
177176
})
178-
if err != nil {
179-
log.FromContext(ctx).Error(err, "failed to collect artifacts for the bootstrap cluster")
180-
}
181177

182-
err = testenv.CollectArtifacts(ctx, testenv.CollectArtifactsInput{
178+
testenv.TryCollectArtifacts(ctx, testenv.CollectArtifactsInput{
183179
KubeconfigPath: rancherKubeconfig.TempFilePath,
184180
Path: clusterName + "-workload-" + specName,
185181
})
186-
if err != nil {
187-
log.FromContext(ctx).Error(err, "failed to collect artifacts for the child cluster")
188-
}
189182

190183
By("Deleting cluster from Rancher")
191-
err = bootstrapClusterProxy.GetClient().Delete(ctx, rancherCluster)
184+
err := bootstrapClusterProxy.GetClient().Delete(ctx, rancherCluster)
192185
Expect(err).NotTo(HaveOccurred(), "Failed to delete rancher cluster")
193186

194187
By("Waiting for the rancher cluster record to be removed")

test/testenv/cleanup.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,23 @@ func CollectArtifacts(ctx context.Context, input CollectArtifactsInput) error {
122122

123123
log.Info("Running kubectl:", "command", strings.Join(aargs, " "))
124124
err := cmd.Run()
125-
log.Info("stderr:", "stderr", stderr.Bytes())
126-
log.Info("stdout:", "stdout", stdout.Bytes())
127-
return err
125+
126+
stderrStr := stderr.String()
127+
stdoutStr := stdout.String()
128+
if strings.TrimSpace(stderrStr) != "" {
129+
log.Info("stderr:", "stderr", stderrStr)
130+
}
131+
if strings.TrimSpace(stdoutStr) != "" {
132+
log.Info("stdout:", "stdout", stdoutStr)
133+
}
134+
135+
if err != nil {
136+
if exitErr, ok := err.(*exec.ExitError); ok {
137+
return fmt.Errorf("artifact collection failed, exit code: %d, error %w", exitErr.ExitCode(), err)
138+
}
139+
return fmt.Errorf("artifact collection failed, error: %w", err)
140+
}
141+
return nil
128142
}
129143

130144
func DumpBootstrapCluster(ctx context.Context) {
@@ -134,3 +148,11 @@ func DumpBootstrapCluster(ctx context.Context) {
134148
return
135149
}
136150
}
151+
152+
// TryCollectArtifacts calls CollectArtifacts and logs any error instead of returning it.
153+
// This is useful in AfterEach/Defer flows where artifact collection failures should not fail the suite.
154+
func TryCollectArtifacts(ctx context.Context, input CollectArtifactsInput) {
155+
if err := CollectArtifacts(ctx, input); err != nil {
156+
log.FromContext(ctx).Error(err, "artifact collection failed", "path", input.Path)
157+
}
158+
}

0 commit comments

Comments
 (0)