Skip to content

Commit

Permalink
Dump namespace contents of failed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timebertt committed Jun 12, 2024
1 parent dd9f433 commit 71a9f77
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
e2e-kind:
runs-on: ubuntu-latest
env:
ARTIFACTS: artifacts
ARTIFACTS: ${{github.workspace}}/artifacts

steps:
- uses: actions/checkout@v4
Expand All @@ -26,5 +26,5 @@ jobs:
if: always()
with:
name: e2e-artifacts
path: artifacts
path: ${{env.ARTIFACTS}}
if-no-files-found: error
56 changes: 56 additions & 0 deletions test/e2e/workload/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package workload

import (
"context"
"os"
"os/exec"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -27,10 +31,62 @@ func PrepareTestNamespace() string {
Expect(testClient.Create(context.Background(), namespace)).To(Succeed())
logf.Log.Info("Created test namespace", "namespace", namespace.Name)

DeferCleanup(func() {
DumpNamespaceContents(namespace.Name)
})

DeferCleanup(func() {
logf.Log.Info("Deleting test namespace", "namespace", namespace.Name)
Expect(testClient.Delete(context.Background(), namespace)).To(Or(Succeed(), BeNotFoundError()))
})

return namespace.Name
}

// DumpNamespaceContents dumps all relevant objects in the test namespace to a dedicated ARTIFACTS directory if the
// spec failed to help deflaking/debugging tests.
func DumpNamespaceContents(namespace string) {
dir := os.Getenv("ARTIFACTS")
if !CurrentSpecReport().Failed() || dir == "" {
return
}

dir = filepath.Join(dir, namespace)
logf.Log.Info("Dumping contents of test namespace", "namespace", namespace, "dir", dir)

// nolint:gosec // this is test code
Expect(os.MkdirAll(dir, 0755)).To(Succeed())

DumpEventsInNamespace(namespace, dir)

for _, kind := range []string{
"pods",
"deployments",
"replicasets",
"statefulsets",
"daemonsets",
"controllerrevisions",
} {
DumpObjectsInNamespace(namespace, kind, dir)
}
}

func DumpEventsInNamespace(namespace, dir string) {
// nolint:gosec // this is test code
file, err := os.OpenFile(filepath.Join(dir, "events.log"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
Expect(err).NotTo(HaveOccurred())

session, err := gexec.Start(exec.Command("kubectl", "-n", namespace, "get", "events"), file, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
}

func DumpObjectsInNamespace(namespace, kind, dir string) {
// nolint:gosec // this is test code
file, err := os.OpenFile(filepath.Join(dir, kind+".yaml"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
Expect(err).NotTo(HaveOccurred())

session, err := gexec.Start(exec.Command("kubectl", "-n", namespace, "get", kind, "-oyaml"), file, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
}

0 comments on commit 71a9f77

Please sign in to comment.