Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Check for panics during test runs in envtest #11279

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion internal/test/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

Expand Down Expand Up @@ -176,10 +177,21 @@ func Run(ctx context.Context, input RunInput) int {
return code
}

var errs []error

if err := verifyPanicMetrics(); err != nil {
errs = append(errs, errors.Wrapf(err, "panics occurred during tests"))
}

// Tearing down the test environment
if err := env.stop(); err != nil {
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))
errs = append(errs, errors.Wrapf(err, "failed to stop the test environment"))
}

if len(errs) > 0 {
panic(kerrors.NewAggregate(errs))
}

return code
}

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

return ns, nil
}

func verifyPanicMetrics() error {
metricFamilies, err := metrics.Registry.Gather()
if err != nil {
return err
}

var errs []error
for _, metricFamily := range metricFamilies {
if metricFamily.GetName() == "controller_runtime_reconcile_panics_total" {
for _, controllerPanicMetric := range metricFamily.Metric {
if controllerPanicMetric.Counter != nil && controllerPanicMetric.Counter.Value != nil && *controllerPanicMetric.Counter.Value > 0 {
controllerName := "unknown"
for _, label := range controllerPanicMetric.Label {
if *label.Name == "controller" {
controllerName = *label.Value
}
}
errs = append(errs, fmt.Errorf("%.0f panics occurred in %q controller (check logs for more details)", *controllerPanicMetric.Counter.Value, controllerName))
}
}
}

if metricFamily.GetName() == "controller_runtime_webhook_panics_total" {
for _, webhookPanicMetric := range metricFamily.Metric {
if webhookPanicMetric.Counter != nil && webhookPanicMetric.Counter.Value != nil && *webhookPanicMetric.Counter.Value > 0 {
errs = append(errs, fmt.Errorf("%.0f panics occurred in webhooks (check logs for more details)", *webhookPanicMetric.Counter.Value))
}
}
}
}

if len(errs) > 0 {
return kerrors.NewAggregate(errs)
}

return nil
}
4 changes: 2 additions & 2 deletions test/framework/deployment_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,15 @@ func verifyMetrics(data []byte) error {
controllerName = *label.Value
}
}
errs = append(errs, fmt.Errorf("panic occurred in %q controller", controllerName))
errs = append(errs, fmt.Errorf("%.0f panics occurred in %q controller (check logs for more details)", *controllerPanicMetric.Counter.Value, controllerName))
}
}
}

if metric == "controller_runtime_webhook_panics_total" {
for _, webhookPanicMetric := range metricFamily.Metric {
if webhookPanicMetric.Counter != nil && webhookPanicMetric.Counter.Value != nil && *webhookPanicMetric.Counter.Value > 0 {
errs = append(errs, fmt.Errorf("panic occurred in webhook"))
errs = append(errs, fmt.Errorf("%.0f panics occurred in webhooks (check logs for more details)", *webhookPanicMetric.Counter.Value))
}
}
}
Expand Down
Loading