This is a bit of an RFC commit, I noticed that when we discussed a crash from the worker we looked at individual message from syslog/journald for the stacktrace deatils. I was wondering if having a more direct crash report would be more useful? We can of course also add more logrus features to flag those with tags like "crash" or something (I did not do that in this PR, I don't know much about the operational side, sorry).
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package main_test
|
|
|
|
import (
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
logrusTest "github.com/sirupsen/logrus/hooks/test"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
main "github.com/osbuild/osbuild-composer/cmd/osbuild-worker"
|
|
)
|
|
|
|
func TestCatchesPanic(t *testing.T) {
|
|
restore := main.MockRun(func() {
|
|
// simulate a crash in the main code
|
|
var foo *int
|
|
println(*foo)
|
|
})
|
|
defer restore()
|
|
|
|
// logrus setup is a bit cumbersome as we need to modify both
|
|
// the standard logger and add a mock logger.
|
|
var exitCalls []int
|
|
logrus.StandardLogger().ExitFunc = func(exitCode int) {
|
|
exitCalls = append(exitCalls, exitCode)
|
|
}
|
|
logrus.SetOutput(io.Discard)
|
|
_, hook := logrusTest.NewNullLogger()
|
|
logrus.AddHook(hook)
|
|
|
|
main.Main()
|
|
// ensure both message and stracktrace are reported in full
|
|
assert.Equal(t, logrus.FatalLevel, hook.LastEntry().Level)
|
|
msg := hook.LastEntry().Message
|
|
assert.Contains(t, msg, "worker crashed: runtime error: invalid memory address or nil pointer dereference")
|
|
assert.Contains(t, msg, "runtime/debug.Stack()")
|
|
assert.Contains(t, msg, "osbuild-worker_test.TestCatchesPanic.func1()")
|
|
|
|
assert.Equal(t, []int{1}, exitCalls)
|
|
}
|