Skip to content

Commit fa46150

Browse files
committed
Check for panics during test runs in envtest
Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent ff4645c commit fa46150

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

internal/test/envtest/environment.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"sigs.k8s.io/controller-runtime/pkg/client"
4949
"sigs.k8s.io/controller-runtime/pkg/envtest"
5050
"sigs.k8s.io/controller-runtime/pkg/manager"
51+
"sigs.k8s.io/controller-runtime/pkg/metrics"
5152
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
5253
"sigs.k8s.io/controller-runtime/pkg/webhook"
5354

@@ -176,10 +177,21 @@ func Run(ctx context.Context, input RunInput) int {
176177
return code
177178
}
178179

180+
var errs []error
181+
182+
if err := verifyPanicMetrics(); err != nil {
183+
errs = append(errs, errors.Wrapf(err, "panics occurred during tests"))
184+
}
185+
179186
// Tearing down the test environment
180187
if err := env.stop(); err != nil {
181-
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))
188+
errs = append(errs, errors.Wrapf(err, "failed to stop the test environment"))
182189
}
190+
191+
if len(errs) > 0 {
192+
panic(kerrors.NewAggregate(errs))
193+
}
194+
183195
return code
184196
}
185197

@@ -527,3 +539,41 @@ func (e *Environment) CreateNamespace(ctx context.Context, generateName string)
527539

528540
return ns, nil
529541
}
542+
543+
func verifyPanicMetrics() error {
544+
metricFamilies, err := metrics.Registry.Gather()
545+
if err != nil {
546+
return err
547+
}
548+
549+
var errs []error
550+
for _, metricFamily := range metricFamilies {
551+
if metricFamily.GetName() == "controller_runtime_reconcile_panics_total" {
552+
for _, controllerPanicMetric := range metricFamily.Metric {
553+
if controllerPanicMetric.Counter != nil && controllerPanicMetric.Counter.Value != nil && *controllerPanicMetric.Counter.Value > 0 {
554+
controllerName := "unknown"
555+
for _, label := range controllerPanicMetric.Label {
556+
if *label.Name == "controller" {
557+
controllerName = *label.Value
558+
}
559+
}
560+
errs = append(errs, fmt.Errorf("%.0f panics occurred in %q controller (check logs for more details)", *controllerPanicMetric.Counter.Value, controllerName))
561+
}
562+
}
563+
}
564+
565+
if metricFamily.GetName() == "controller_runtime_webhook_panics_total" {
566+
for _, webhookPanicMetric := range metricFamily.Metric {
567+
if webhookPanicMetric.Counter != nil && webhookPanicMetric.Counter.Value != nil && *webhookPanicMetric.Counter.Value > 0 {
568+
errs = append(errs, fmt.Errorf("%.0f panics occurred in webhooks (check logs for more details)", *webhookPanicMetric.Counter.Value))
569+
}
570+
}
571+
}
572+
}
573+
574+
if len(errs) > 0 {
575+
return kerrors.NewAggregate(errs)
576+
}
577+
578+
return nil
579+
}

test/framework/deployment_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,15 +436,15 @@ func verifyMetrics(data []byte) error {
436436
controllerName = *label.Value
437437
}
438438
}
439-
errs = append(errs, fmt.Errorf("panic occurred in %q controller", controllerName))
439+
errs = append(errs, fmt.Errorf("%.0f panics occurred in %q controller (check logs for more details)", *controllerPanicMetric.Counter.Value, controllerName))
440440
}
441441
}
442442
}
443443

444444
if metric == "controller_runtime_webhook_panics_total" {
445445
for _, webhookPanicMetric := range metricFamily.Metric {
446446
if webhookPanicMetric.Counter != nil && webhookPanicMetric.Counter.Value != nil && *webhookPanicMetric.Counter.Value > 0 {
447-
errs = append(errs, fmt.Errorf("panic occurred in webhook"))
447+
errs = append(errs, fmt.Errorf("%.0f panics occurred in webhooks (check logs for more details)", *webhookPanicMetric.Counter.Value))
448448
}
449449
}
450450
}

0 commit comments

Comments
 (0)