Skip to content

Commit 9a23f8c

Browse files
committed
chore: refactor e2e test diagnostic
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
1 parent 78d39b1 commit 9a23f8c

10 files changed

Lines changed: 86 additions & 74 deletions

test/e2e/framework/framework.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -280,20 +280,20 @@ func (f *Framework) DumpOnFailure(t *testing.T, fns ...DebugFunc) {
280280
if !t.Failed() {
281281
return
282282
}
283+
t.Logf("# Diagnostics for %s (FAILED)", t.Name())
283284
for _, fn := range fns {
284285
fn(t)
285286
}
286287
})
287288
}
288289

289-
// DebugNamespace returns a DebugFunc that dumps deployments, pods, and events
290+
// DebugNamespaces returns a DebugFunc that dumps deployments, pods, and events
290291
// for the given namespaces.
291-
func (f *Framework) DebugNamespace(namespaces ...string) DebugFunc {
292+
func (f *Framework) DebugNamespaces(namespaces ...string) DebugFunc {
292293
return func(t *testing.T) {
293294
t.Helper()
294295
for _, ns := range namespaces {
295-
t.Logf("--- Dumping debug info for namespace %s ---", ns)
296-
f.DumpNamespaceDebug(t, ns)
296+
f.dumpNamespace(t, ns)
297297
}
298298
}
299299
}
@@ -335,60 +335,58 @@ func (f *Framework) SkipIfClusterVersionBelow(t *testing.T, minVersion string) {
335335
}
336336
}
337337

338-
// DumpNamespaceDebug logs deployments (with conditions), pods (with container
339-
// statuses), and events for the given namespace. Useful as a t.Cleanup or
340-
// on-failure diagnostic helper.
341-
func (f *Framework) DumpNamespaceDebug(t *testing.T, namespace string) {
338+
func (f *Framework) dumpNamespace(t *testing.T, namespace string) {
342339
t.Helper()
343340
ctx := context.WithoutCancel(t.Context())
344341

345-
t.Log("=== BEGIN DEBUG DUMP ===")
346-
defer t.Log("=== END DEBUG DUMP ===")
342+
t.Logf("=== BEGIN NAMESPACE DEBUG DUMP ===")
343+
t.Logf("* Namespace: %s", namespace)
344+
defer t.Log("=== END NAMESPACE DEBUG DUMP ===")
347345

346+
t.Log("\n## Deployments\n")
348347
var deployments appsv1.DeploymentList
349348
if err := f.K8sClient.List(ctx, &deployments, client.InNamespace(namespace)); err != nil {
350-
t.Logf("Failed to list deployments in %s: %v", namespace, err)
349+
t.Logf("Failed to list deployments: %v", err)
351350
} else {
352-
t.Logf("Deployments in namespace %s: %d", namespace, len(deployments.Items))
353351
for _, d := range deployments.Items {
354-
t.Logf(" Deployment: name=%s replicas=%d readyReplicas=%d availableReplicas=%d",
352+
t.Logf("* Deployment: name=%s replicas=%d readyReplicas=%d availableReplicas=%d",
355353
d.Name, ptr.Deref(d.Spec.Replicas, 0), d.Status.ReadyReplicas, d.Status.AvailableReplicas)
356354
for _, c := range d.Status.Conditions {
357-
t.Logf(" condition: type=%s status=%s reason=%s message=%s",
355+
t.Logf(" * condition: type=%s status=%s reason=%s message=%s",
358356
c.Type, c.Status, c.Reason, c.Message)
359357
}
360358
}
361359
}
362360

361+
t.Log("\n## Pods\n")
363362
var pods corev1.PodList
364363
if err := f.K8sClient.List(ctx, &pods, client.InNamespace(namespace)); err != nil {
365-
t.Logf("Failed to list pods in %s: %v", namespace, err)
364+
t.Logf("Failed to list pods: %v", err)
366365
} else {
367-
t.Logf("Pods in namespace %s: %d", namespace, len(pods.Items))
368366
for _, p := range pods.Items {
369-
t.Logf(" Pod: name=%s phase=%s", p.Name, p.Status.Phase)
367+
t.Logf("* Pod: name=%s phase=%s", p.Name, p.Status.Phase)
370368
for _, cs := range p.Status.ContainerStatuses {
371369
switch {
372370
case cs.State.Running != nil:
373-
t.Logf(" container=%s ready=%v restarts=%d state=Running", cs.Name, cs.Ready, cs.RestartCount)
371+
t.Logf(" * container=%s ready=%v restarts=%d state=Running", cs.Name, cs.Ready, cs.RestartCount)
374372
case cs.State.Waiting != nil:
375-
t.Logf(" container=%s ready=%v restarts=%d state=Waiting reason=%s message=%s",
373+
t.Logf(" * container=%s ready=%v restarts=%d state=Waiting reason=%s message=%s",
376374
cs.Name, cs.Ready, cs.RestartCount, cs.State.Waiting.Reason, cs.State.Waiting.Message)
377375
case cs.State.Terminated != nil:
378-
t.Logf(" container=%s ready=%v restarts=%d state=Terminated reason=%s exitCode=%d",
376+
t.Logf(" * container=%s ready=%v restarts=%d state=Terminated reason=%s exitCode=%d",
379377
cs.Name, cs.Ready, cs.RestartCount, cs.State.Terminated.Reason, cs.State.Terminated.ExitCode)
380378
}
381379
}
382380
}
383381
}
384382

383+
t.Log("\n## Events\n")
385384
var events corev1.EventList
386385
if err := f.K8sClient.List(ctx, &events, client.InNamespace(namespace)); err != nil {
387-
t.Logf("Failed to list events in %s: %v", namespace, err)
386+
t.Logf("Failed to list events: %v", err)
388387
} else {
389-
t.Logf("Events in namespace %s: %d", namespace, len(events.Items))
390388
for _, e := range events.Items {
391-
t.Logf(" Event: involvedObject=%s/%s reason=%s message=%s type=%s count=%d",
389+
t.Logf("* Event: involvedObject=%s/%s reason=%s message=%s type=%s count=%d",
392390
e.InvolvedObject.Kind, e.InvolvedObject.Name, e.Reason, e.Message, e.Type, e.Count)
393391
}
394392
}

test/e2e/framework/uiplugin.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package framework
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
9+
uiv1 "github.com/rhobs/observability-operator/pkg/apis/uiplugin/v1alpha1"
10+
)
11+
12+
func (f *Framework) DebugUIPlugin(pluginName string) DebugFunc {
13+
return func(t *testing.T) {
14+
t.Helper()
15+
ctx := context.WithoutCancel(t.Context())
16+
17+
t.Logf("## UIPlugin %s", pluginName)
18+
var plugin uiv1.UIPlugin
19+
if err := f.K8sClient.Get(ctx, client.ObjectKey{Name: pluginName}, &plugin); err != nil {
20+
t.Logf("Failed to get UIPlugin %q: %v", pluginName, err)
21+
return
22+
}
23+
t.Logf("* UIPlugin %q generation=%d, resourceVersion=%s", pluginName, plugin.Generation, plugin.ResourceVersion)
24+
t.Logf(" * UIPlugin spec.type=%s", plugin.Spec.Type)
25+
if plugin.Spec.Monitoring != nil {
26+
if plugin.Spec.Monitoring.ClusterHealthAnalyzer != nil {
27+
t.Logf(" * UIPlugin spec.monitoring.clusterHealthAnalyzer.enabled=%v", plugin.Spec.Monitoring.ClusterHealthAnalyzer.Enabled)
28+
}
29+
if plugin.Spec.Monitoring.Incidents != nil {
30+
t.Logf(" * UIPlugin spec.monitoring.incidents.enabled=%v", plugin.Spec.Monitoring.Incidents.Enabled)
31+
}
32+
}
33+
t.Log("* UIPlugin conditions")
34+
if len(plugin.Status.Conditions) == 0 {
35+
t.Log(" * No status conditions")
36+
}
37+
for _, c := range plugin.Status.Conditions {
38+
t.Logf(" * Condition: type=%s status=%s reason=%s message=%s", c.Type, c.Status, c.Reason, c.Message)
39+
}
40+
41+
t.Logf("## UIPlugins summary")
42+
43+
var plugins uiv1.UIPluginList
44+
if err := f.K8sClient.List(ctx, &plugins); err != nil {
45+
t.Logf("Failed to list UIPlugins: %v", err)
46+
} else {
47+
t.Logf("* Total number of UIPlugins: %d", len(plugins.Items))
48+
for _, p := range plugins.Items {
49+
t.Logf(" * UIPlugin: name=%s type=%s conditions=%d", p.Name, p.Spec.Type, len(p.Status.Conditions))
50+
}
51+
}
52+
}
53+
}

test/e2e/monitoring_stack_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func assertCRDExists(t *testing.T, crds ...string) {
4848
}
4949

5050
func TestMonitoringStackController(t *testing.T) {
51-
f.DumpOnFailure(t, f.DebugNamespace(e2eTestNamespace))
51+
f.DumpOnFailure(t, f.DebugNamespaces(e2eTestNamespace))
5252
err := stack.AddToScheme(scheme.Scheme)
5353
assert.NilError(t, err, "adding stack to scheme failed")
5454
assertCRDExists(t,

test/e2e/observability_installer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestObservabilityInstallerController(t *testing.T) {
4646
}
4747

4848
func testObservabilityInstallerTracing(t *testing.T) {
49-
f.DumpOnFailure(t, f.DebugNamespace(f.OperatorNamespace, "tracing-observability"))
49+
f.DumpOnFailure(t, f.DebugNamespaces(f.OperatorNamespace, "tracing-observability"))
5050
ctx := context.Background()
5151

5252
// The ObservabilityInstaller installs operators via subscriptions,

test/e2e/operator_metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestOperatorMetrics(t *testing.T) {
14-
f.DumpOnFailure(t, f.DebugNamespace(f.OperatorNamespace))
14+
f.DumpOnFailure(t, f.DebugNamespaces(f.OperatorNamespace))
1515
t.Run("operator exposes metrics", func(t *testing.T) {
1616
pod := f.GetOperatorPod(t)
1717

test/e2e/po_admission_webhook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestPrometheusRuleWebhook(t *testing.T) {
15-
f.DumpOnFailure(t, f.DebugNamespace(e2eTestNamespace))
15+
f.DumpOnFailure(t, f.DebugNamespaces(e2eTestNamespace))
1616
assertCRDExists(t,
1717
"prometheusrules.monitoring.rhobs",
1818
)

test/e2e/prometheus_operator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type testCase struct {
2929
}
3030

3131
func TestPrometheusOperatorForNonOwnedResources(t *testing.T) {
32-
f.DumpOnFailure(t, f.DebugNamespace(e2eTestNamespace))
32+
f.DumpOnFailure(t, f.DebugNamespaces(e2eTestNamespace))
3333
resources := []client.Object{
3434
newPrometheus(nil),
3535
newAlertmanager(nil),
@@ -75,7 +75,7 @@ func TestPrometheusOperatorForNonOwnedResources(t *testing.T) {
7575
}
7676

7777
func TestPrometheusOperatorForOwnedResources(t *testing.T) {
78-
f.DumpOnFailure(t, f.DebugNamespace(e2eTestNamespace))
78+
f.DumpOnFailure(t, f.DebugNamespaces(e2eTestNamespace))
7979
resources := []client.Object{
8080
newPrometheus(ownedResourceLabels),
8181
newAlertmanager(ownedResourceLabels),

test/e2e/thanos_querier_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
func TestThanosQuerierController(t *testing.T) {
27-
f.DumpOnFailure(t, f.DebugNamespace(e2eTestNamespace))
27+
f.DumpOnFailure(t, f.DebugNamespaces(e2eTestNamespace))
2828
assertCRDExists(t, "thanosqueriers.monitoring.rhobs")
2929

3030
ts := []testCase{

test/e2e/uiplugin_cluster_health_analyzer_test.go

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"k8s.io/apimachinery/pkg/api/errors"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1717
"k8s.io/apimachinery/pkg/util/intstr"
18-
"sigs.k8s.io/controller-runtime/pkg/client"
1918

2019
uiv1 "github.com/rhobs/observability-operator/pkg/apis/uiplugin/v1alpha1"
2120
"github.com/rhobs/observability-operator/test/e2e/framework"
@@ -32,13 +31,13 @@ func clusterHealthAnalyzer(t *testing.T) {
3231
err := monv1.AddToScheme(f.K8sClient.Scheme())
3332
assert.NilError(t, err, "failed to add monv1 to scheme")
3433

35-
f.DumpOnFailure(t, f.DebugNamespace(uiPluginInstallNS))
34+
f.DumpOnFailure(t, f.DebugNamespaces(uiPluginInstallNS))
3635

3736
plugin := resetMonitoringUIPlugin(t)
3837
err = f.K8sClient.Create(t.Context(), plugin)
3938
assert.NilError(t, err, "failed to create monitoring UIPlugin")
4039

41-
f.DumpOnFailure(t, dumpUIPluginDebug(plugin.Name))
40+
f.DumpOnFailure(t, f.DebugUIPlugin(plugin.Name))
4241
t.Log("Waiting for health-analyzer deployment to become ready...")
4342
haDeployment := appsv1.Deployment{}
4443
f.GetResourceWithRetry(t, healthAnalyzerDeploymentName, uiPluginInstallNS, &haDeployment)
@@ -89,7 +88,7 @@ func clusterHealthAnalyzer(t *testing.T) {
8988
func resetMonitoringUIPlugin(t *testing.T) *uiv1.UIPlugin {
9089
plugin := &uiv1.UIPlugin{
9190
ObjectMeta: metav1.ObjectMeta{
92-
Name: "monitoring",
91+
Name: uiv1.MonitoringName,
9392
},
9493
Spec: uiv1.UIPluginSpec{
9594
Type: uiv1.TypeMonitoring,
@@ -142,6 +141,7 @@ func createRuleNamespace(t *testing.T, name string) {
142141
if err := f.K8sClient.Create(t.Context(), ns); err != nil && !errors.IsAlreadyExists(err) {
143142
t.Fatalf("failed to create rule namespace %s: %v", name, err)
144143
}
144+
f.DumpOnFailure(t, f.DebugNamespaces(name))
145145
f.CleanUp(t, func() {
146146
ctx := context.WithoutCancel(t.Context())
147147
f.K8sClient.Delete(ctx, ns)
@@ -176,42 +176,3 @@ func newAlwaysFiringRule(t *testing.T, ruleName, alertName string) *monv1.Promet
176176
})
177177
return rule
178178
}
179-
180-
func dumpUIPluginDebug(pluginName string) framework.DebugFunc {
181-
return func(t *testing.T) {
182-
t.Helper()
183-
ctx := context.WithoutCancel(t.Context())
184-
185-
var plugin uiv1.UIPlugin
186-
if err := f.K8sClient.Get(ctx, client.ObjectKey{Name: pluginName}, &plugin); err != nil {
187-
t.Logf("Failed to get UIPlugin %q: %v", pluginName, err)
188-
return
189-
}
190-
t.Logf("UIPlugin %q generation=%d, resourceVersion=%s", pluginName, plugin.Generation, plugin.ResourceVersion)
191-
t.Logf("UIPlugin spec.type=%s", plugin.Spec.Type)
192-
if plugin.Spec.Monitoring != nil {
193-
if plugin.Spec.Monitoring.ClusterHealthAnalyzer != nil {
194-
t.Logf("UIPlugin spec.monitoring.clusterHealthAnalyzer.enabled=%v", plugin.Spec.Monitoring.ClusterHealthAnalyzer.Enabled)
195-
}
196-
if plugin.Spec.Monitoring.Incidents != nil {
197-
t.Logf("UIPlugin spec.monitoring.incidents.enabled=%v", plugin.Spec.Monitoring.Incidents.Enabled)
198-
}
199-
}
200-
if len(plugin.Status.Conditions) == 0 {
201-
t.Log("UIPlugin has no status conditions")
202-
}
203-
for _, c := range plugin.Status.Conditions {
204-
t.Logf("UIPlugin condition: type=%s status=%s reason=%s message=%s", c.Type, c.Status, c.Reason, c.Message)
205-
}
206-
207-
var plugins uiv1.UIPluginList
208-
if err := f.K8sClient.List(ctx, &plugins); err != nil {
209-
t.Logf("Failed to list UIPlugins: %v", err)
210-
} else {
211-
t.Logf("Total UIPlugins in cluster: %d", len(plugins.Items))
212-
for _, p := range plugins.Items {
213-
t.Logf(" UIPlugin: name=%s type=%s conditions=%d", p.Name, p.Spec.Type, len(p.Status.Conditions))
214-
}
215-
}
216-
}
217-
}

test/e2e/uiplugin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestUIPlugin(t *testing.T) {
4646
}
4747

4848
func dashboardsUIPlugin(t *testing.T) {
49-
f.DumpOnFailure(t, f.DebugNamespace(uiPluginInstallNS))
49+
f.DumpOnFailure(t, f.DebugNamespaces(uiPluginInstallNS))
5050
db := newDashboardsUIPlugin(t)
5151
err := f.K8sClient.Create(context.Background(), db)
5252
assert.NilError(t, err, "failed to create a dashboards UIPlugin")

0 commit comments

Comments
 (0)