github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/logging/panic_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestPanicRecorder(t *testing.T) {
    10  	rec := panics.registerPlugin("test")
    11  
    12  	output := []string{
    13  		"panic: test",
    14  		"  stack info",
    15  	}
    16  
    17  	for _, line := range output {
    18  		rec(line)
    19  	}
    20  
    21  	expected := fmt.Sprintf(pluginPanicOutput, "test", strings.Join(output, "\n"))
    22  
    23  	res := PluginPanics()
    24  	if len(res) == 0 {
    25  		t.Fatal("no output")
    26  	}
    27  
    28  	if res[0] != expected {
    29  		t.Fatalf("expected: %q\ngot: %q", expected, res[0])
    30  	}
    31  }
    32  
    33  func TestPanicLimit(t *testing.T) {
    34  	rec := panics.registerPlugin("test")
    35  
    36  	rec("panic: test")
    37  
    38  	for i := 0; i < 200; i++ {
    39  		rec(fmt.Sprintf("LINE: %d", i))
    40  	}
    41  
    42  	res := PluginPanics()
    43  	// take the extra content into account
    44  	max := strings.Count(pluginPanicOutput, "\n") + panics.maxLines
    45  	for _, out := range res {
    46  		found := strings.Count(out, "\n")
    47  		if found > max {
    48  			t.Fatalf("expected no more than %d lines, got: %d", max, found)
    49  		}
    50  	}
    51  }