Skip to content

Commit 2b12bf2

Browse files
authored
Add dump cluster state (#39)
* feat: add dumping cluster-state teardown method * fixing the pod listing to use label filter * added dumping repository content
1 parent 2c61d41 commit 2b12bf2

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ issues:
9494
linters:
9595
enable-all: true
9696
disable:
97+
- depguard
9798
- exhaustivestruct
9899
- golint
99100
- interfacer
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package teardown
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"testing"
8+
9+
v1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/labels"
11+
"k8s.io/client-go/kubernetes"
12+
"sigs.k8s.io/e2e-framework/klient/k8s/resources"
13+
"sigs.k8s.io/e2e-framework/pkg/envconf"
14+
"sigs.k8s.io/e2e-framework/pkg/features"
15+
)
16+
17+
// DumpClusterState dumps the status of pods and logs of given controllers.
18+
func DumpClusterState(controllers ...string) features.Func {
19+
return func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
20+
t.Helper()
21+
// Dump controller logs
22+
for _, controller := range controllers {
23+
if err := dumpLogs(ctx, t, config, controller); err != nil {
24+
t.Fatalf("failed to dump logs for controller %s, %s", controller, err)
25+
}
26+
}
27+
28+
// Dump list of pods in namespace
29+
namespaces := &v1.NamespaceList{}
30+
31+
client, err := config.NewClient()
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
if err := client.Resources().List(ctx, namespaces); err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
for _, ns := range namespaces.Items {
41+
pods := &v1.PodList{}
42+
if err := client.Resources(ns.Name).List(ctx, pods); err != nil {
43+
t.Fatal(fmt.Errorf("failed to list pods in namespace %s: %w", ns.Name, err))
44+
}
45+
46+
for _, pod := range pods.Items {
47+
t.Logf("Name: %s | Namespace: %s | Status: %s", pod.Name, pod.Namespace, pod.Status.String())
48+
}
49+
}
50+
51+
return ctx
52+
}
53+
}
54+
55+
func dumpLogs(ctx context.Context, t *testing.T, config *envconf.Config, controller string) error {
56+
t.Helper()
57+
58+
client, err := config.NewClient()
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
63+
pods := &v1.PodList{}
64+
if err := client.Resources(config.Namespace()).List(ctx, pods, resources.WithLabelSelector(
65+
labels.FormatLabels(map[string]string{"app": controller})),
66+
); err != nil {
67+
t.Fatal(fmt.Errorf("failed to list pods: %w", err))
68+
}
69+
70+
if len(pods.Items) != 1 {
71+
t.Fatal(fmt.Errorf("invalid number of pods found for registry %d", len(pods.Items)))
72+
}
73+
74+
pod := pods.Items[0]
75+
76+
// creates the clientset
77+
clientset, err := kubernetes.NewForConfig(config.Client().RESTConfig())
78+
if err != nil {
79+
return fmt.Errorf("failed to create clientset: %w", err)
80+
}
81+
82+
podReq := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{})
83+
84+
reader, err := podReq.Stream(ctx)
85+
if err != nil {
86+
t.Fatal(fmt.Errorf("failed to fetch pod logs: %w", err))
87+
}
88+
89+
defer reader.Close()
90+
91+
content, err := io.ReadAll(reader)
92+
if err != nil {
93+
t.Fatal(fmt.Errorf("failed to read log: %w", err))
94+
}
95+
96+
t.Logf("Pod: %s | Log: %s", controller, string(content))
97+
98+
return nil
99+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package teardown
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"code.gitea.io/sdk/gitea"
9+
"github.com/open-component-model/ocm-e2e-framework/shared"
10+
"sigs.k8s.io/e2e-framework/pkg/envconf"
11+
"sigs.k8s.io/e2e-framework/pkg/features"
12+
)
13+
14+
// DumpRepositoryContent dumps all the files in a repository.
15+
func DumpRepositoryContent(owner, repo string) features.Func {
16+
return func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
17+
t.Helper()
18+
19+
gclient, err := gitea.NewClient(shared.BaseURL, gitea.SetToken(shared.TestUserToken))
20+
if err != nil {
21+
t.Fatal(fmt.Errorf("failed to create gitea client: %w", err))
22+
}
23+
24+
r, _, err := gclient.GetTrees(owner, repo, "main", true)
25+
if err != nil {
26+
t.Fatal(fmt.Errorf("failed to find repo for %s/%s: %w", owner, repo, err))
27+
}
28+
29+
for _, entry := range r.Entries {
30+
t.Logf("Type: %s | Path: %s", entry.Type, entry.Path)
31+
}
32+
33+
return ctx
34+
}
35+
}

test/DO_NOT_ADD_TESTS_HERE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Deprecation Notification
2+
3+
These tests are no longer kept up-to-date. E2E tests for specific controllers can be found in the controller's repository.

0 commit comments

Comments
 (0)