From 2da280f2bbbea2478312187cd97b72c328628134 Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Mon, 3 Mar 2025 19:57:19 +0100 Subject: [PATCH] Fail tests if unhandled panics happened. We normally recover from those situations, printing stack traces instead of crashing the program. But during tests, we're not looking at the prometheus metrics or all the output. Without these checks, such panics could go unnoticed. Seems like a reasonable thing to add, unhandled panics haven't been encountered in tests. --- dmarcdb/main_test.go | 17 +++++++++++++++++ http/main_test.go | 17 +++++++++++++++++ imapserver/main_test.go | 17 +++++++++++++++++ main_test.go | 17 +++++++++++++++++ metrics/panic.go | 5 +++++ mtasts/main_test.go | 17 +++++++++++++++++ mtastsdb/main_test.go | 17 +++++++++++++++++ queue/main_test.go | 17 +++++++++++++++++ smtpserver/main_test.go | 17 +++++++++++++++++ store/main_test.go | 17 +++++++++++++++++ tlsrptsend/main_test.go | 17 +++++++++++++++++ webaccount/main_test.go | 17 +++++++++++++++++ webadmin/main_test.go | 17 +++++++++++++++++ webapisrv/main_test.go | 17 +++++++++++++++++ webmail/main_test.go | 17 +++++++++++++++++ 15 files changed, 243 insertions(+) create mode 100644 dmarcdb/main_test.go create mode 100644 http/main_test.go create mode 100644 imapserver/main_test.go create mode 100644 main_test.go create mode 100644 mtasts/main_test.go create mode 100644 mtastsdb/main_test.go create mode 100644 queue/main_test.go create mode 100644 smtpserver/main_test.go create mode 100644 store/main_test.go create mode 100644 tlsrptsend/main_test.go create mode 100644 webaccount/main_test.go create mode 100644 webadmin/main_test.go create mode 100644 webapisrv/main_test.go create mode 100644 webmail/main_test.go diff --git a/dmarcdb/main_test.go b/dmarcdb/main_test.go new file mode 100644 index 0000000..88bd2ec --- /dev/null +++ b/dmarcdb/main_test.go @@ -0,0 +1,17 @@ +package dmarcdb + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/http/main_test.go b/http/main_test.go new file mode 100644 index 0000000..3dedd91 --- /dev/null +++ b/http/main_test.go @@ -0,0 +1,17 @@ +package http + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/imapserver/main_test.go b/imapserver/main_test.go new file mode 100644 index 0000000..06caee4 --- /dev/null +++ b/imapserver/main_test.go @@ -0,0 +1,17 @@ +package imapserver + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..88ea9c4 --- /dev/null +++ b/main_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/metrics/panic.go b/metrics/panic.go index aaf0fe1..dea4553 100644 --- a/metrics/panic.go +++ b/metrics/panic.go @@ -1,6 +1,8 @@ package metrics import ( + "sync/atomic" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ) @@ -72,6 +74,9 @@ func init() { } } +var Panics atomic.Int64 + func PanicInc(name Panic) { + Panics.Add(1) metricPanic.WithLabelValues(string(name)).Inc() } diff --git a/mtasts/main_test.go b/mtasts/main_test.go new file mode 100644 index 0000000..1346a4f --- /dev/null +++ b/mtasts/main_test.go @@ -0,0 +1,17 @@ +package mtasts + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/mtastsdb/main_test.go b/mtastsdb/main_test.go new file mode 100644 index 0000000..c9a2f2c --- /dev/null +++ b/mtastsdb/main_test.go @@ -0,0 +1,17 @@ +package mtastsdb + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/queue/main_test.go b/queue/main_test.go new file mode 100644 index 0000000..c1ae6b3 --- /dev/null +++ b/queue/main_test.go @@ -0,0 +1,17 @@ +package queue + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/smtpserver/main_test.go b/smtpserver/main_test.go new file mode 100644 index 0000000..f265f2e --- /dev/null +++ b/smtpserver/main_test.go @@ -0,0 +1,17 @@ +package smtpserver + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/store/main_test.go b/store/main_test.go new file mode 100644 index 0000000..8754e10 --- /dev/null +++ b/store/main_test.go @@ -0,0 +1,17 @@ +package store + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/tlsrptsend/main_test.go b/tlsrptsend/main_test.go new file mode 100644 index 0000000..30eb08d --- /dev/null +++ b/tlsrptsend/main_test.go @@ -0,0 +1,17 @@ +package tlsrptsend + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/webaccount/main_test.go b/webaccount/main_test.go new file mode 100644 index 0000000..ec4ee9c --- /dev/null +++ b/webaccount/main_test.go @@ -0,0 +1,17 @@ +package webaccount + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/webadmin/main_test.go b/webadmin/main_test.go new file mode 100644 index 0000000..f77cd80 --- /dev/null +++ b/webadmin/main_test.go @@ -0,0 +1,17 @@ +package webadmin + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/webapisrv/main_test.go b/webapisrv/main_test.go new file mode 100644 index 0000000..9512682 --- /dev/null +++ b/webapisrv/main_test.go @@ -0,0 +1,17 @@ +package webapisrv + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +} diff --git a/webmail/main_test.go b/webmail/main_test.go new file mode 100644 index 0000000..bec3cb9 --- /dev/null +++ b/webmail/main_test.go @@ -0,0 +1,17 @@ +package webmail + +import ( + "fmt" + "os" + "testing" + + "github.com/mjl-/mox/metrics" +) + +func TestMain(m *testing.M) { + m.Run() + if metrics.Panics.Load() > 0 { + fmt.Println("unhandled panics encountered") + os.Exit(2) + } +}