mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-25 01:00:16 -05:00
record all plugin panics, and print on main exit
Create a logger that will record any apparent crash output for later processing. If the cli command returns with a non-zero exit status, check for any recorded crashes and add those to the output.
This commit is contained in:
51
internal/logging/panic_test.go
Normal file
51
internal/logging/panic_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPanicRecorder(t *testing.T) {
|
||||
rec := panics.registerPlugin("test")
|
||||
|
||||
output := []string{
|
||||
"panic: test",
|
||||
" stack info",
|
||||
}
|
||||
|
||||
for _, line := range output {
|
||||
rec(line)
|
||||
}
|
||||
|
||||
expected := fmt.Sprintf(pluginPanicOutput, "test", strings.Join(output, "\n"))
|
||||
|
||||
res := PluginPanics()
|
||||
if len(res) == 0 {
|
||||
t.Fatal("no output")
|
||||
}
|
||||
|
||||
if res[0] != expected {
|
||||
t.Fatalf("expected: %q\ngot: %q", expected, res[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPanicLimit(t *testing.T) {
|
||||
rec := panics.registerPlugin("test")
|
||||
|
||||
rec("panic: test")
|
||||
|
||||
for i := 0; i < 200; i++ {
|
||||
rec(fmt.Sprintf("LINE: %d", i))
|
||||
}
|
||||
|
||||
res := PluginPanics()
|
||||
// take the extra content into account
|
||||
max := strings.Count(pluginPanicOutput, "\n") + panics.maxLines
|
||||
for _, out := range res {
|
||||
found := strings.Count(out, "\n")
|
||||
if found > max {
|
||||
t.Fatalf("expected no more than %d lines, got: %d", max, found)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user