Skip to content

Commit

Permalink
stacktrace and errors test
Browse files Browse the repository at this point in the history
Signed-off-by: Bisakh Mondal <bisakhmondal00@gmail.com>
  • Loading branch information
bisakhmondal committed Mar 30, 2022
1 parent 093fe35 commit 6fae087
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
139 changes: 139 additions & 0 deletions pkg/errors/errors_test.go
@@ -0,0 +1,139 @@
package errors

import (
stderrors "errors"
"fmt"
"regexp"
"strconv"
"testing"

"github.com/thanos-io/thanos/pkg/testutil"
)

const msg = "test_error_message"
const wrapper = "test_wrapper"

func TestNew(t *testing.T) {
err := New(msg)
testutil.Equals(t, err.Error(), msg, "the root error message must match")

reg := regexp.MustCompile(msg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNew .*\/thanos\/pkg\/errors\/errors_test\.go:17`)
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match")
}

func TestNewFormatted(t *testing.T) {
fmtMsg := msg + " key=%v"
expectedMsg := msg + " key=value"

err := New(fmtMsg, "value")
testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match")
reg := regexp.MustCompile(expectedMsg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNewFormatted .*\/thanos\/pkg\/errors\/errors_test\.go:28`)
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match")
}

func TestWrap(t *testing.T) {
err := New(msg)
err = Wrap(err, wrapper)

expectedMsg := wrapper + ": " + msg
testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match")

reg := regexp.MustCompile(`test_wrapper[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/thanos\/pkg\/errors\/errors_test\.go:36
[[:ascii:]]+test_error_message[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/thanos\/pkg\/errors\/errors_test\.go:35`)

testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match")
}

func TestUnwrap(t *testing.T) {
// test with base error
err := New(msg)

for i, tc := range []struct {
err error
expected string
isNil bool
}{
{
// no wrapping
err: err,
isNil: true,
},
{
err: Wrap(err, wrapper),
expected: "test_error_message",
},
{
err: Wrap(Wrap(err, wrapper), wrapper),
expected: "test_wrapper: test_error_message",
},
// check primitives errors
{
err: stderrors.New("std-error"),
isNil: true,
},
{
err: Wrap(stderrors.New("std-error"), wrapper),
expected: "std-error",
},
{
err: nil,
isNil: true,
},
} {
t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) {
unwrapped := Unwrap(tc.err)
if tc.isNil {
testutil.Equals(t, unwrapped, nil)
return
}
testutil.Equals(t, unwrapped.Error(), tc.expected, "Unwrap must match expected output")
})
}
}

func TestCause(t *testing.T) {
// test with base error that implements interface containing Unwrap method
err := New(msg)

for i, tc := range []struct {
err error
expected string
isNil bool
}{
{
// no wrapping
err: err,
isNil: true,
},
{
err: Wrap(err, wrapper),
isNil: true,
},
{
err: Wrap(Wrap(err, wrapper), wrapper),
isNil: true,
},
// check primitives errors
{
err: stderrors.New("std-error"),
expected: "std-error",
},
{
err: Wrap(stderrors.New("std-error"), wrapper),
expected: "std-error",
},
{
err: nil,
isNil: true,
},
} {
t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) {
cause := Cause(tc.err)
if tc.isNil {
testutil.Equals(t, cause, nil)
return
}
testutil.Equals(t, cause.Error(), tc.expected, "Unwrap must match expected output")
})
}
}
27 changes: 27 additions & 0 deletions pkg/errors/stacktrace_test.go
@@ -0,0 +1,27 @@
package errors

import (
"strings"
"testing"
)

func caller() stacktrace {
return newStackTrace()
}

func TestStacktraceOutput(t *testing.T) {
st := caller()
expectedPhrase := "thanos/pkg/errors/stacktrace_test.go:13"
if !strings.Contains(st.String(), expectedPhrase) {
t.Fatalf("expected %v phrase into the stacktrace, received stacktrace: \n%v", expectedPhrase, st.String())
}
}

func TestStacktraceProgramCounterLen(t *testing.T) {
st := caller()
output := st.String()
lines := len(strings.Split(strings.TrimSuffix(output, "\n"), "\n"))
if len(st) != lines {
t.Fatalf("output lines vs program counter size mismatch: program counter size %v, output lines %v", len(st), lines)
}
}

0 comments on commit 6fae087

Please sign in to comment.