Skip to content

Commit

Permalink
chore (events): more unit tests for handling multierror
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambrt committed Oct 6, 2023
1 parent 602bbcc commit 72301c7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
11 changes: 10 additions & 1 deletion internal/event/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/eventlogger/filters/encrypt"
"github.com/hashicorp/eventlogger/formatter_filters/cloudevents"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/copystructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -1093,6 +1094,14 @@ func Test_WriteError(t *testing.T) {
info: info,
errSinkFileName: c.ErrorEvents.Name(),
},
{
name: "multierror",
ctx: testCtx,
e: multierror.Append(stderrors.New("multierror")),
opt: []event.Option{event.WithInfo("test", "info")},
info: info,
errSinkFileName: c.ErrorEvents.Name(),
},
}

for _, tt := range tests {
Expand All @@ -1111,7 +1120,7 @@ func Test_WriteError(t *testing.T) {
event.WriteError(tt.ctx, event.Op(op), tt.e, tt.opt...)
if tt.errSinkFileName != "" {
defer func() { _ = os.WriteFile(tt.errSinkFileName, nil, 0o666) }()
b, err := ioutil.ReadFile(tt.errSinkFileName)
b, err := os.ReadFile(tt.errSinkFileName)
require.NoError(err)

if tt.noOutput {
Expand Down
87 changes: 75 additions & 12 deletions internal/event/event_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
package event

import (
"bytes"
"encoding/json"
"errors"
stderrors "errors"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -41,10 +47,9 @@ func Test_newError(t *testing.T) {
fromOp: Op("valid-no-opts"),
e: fmt.Errorf("%s: valid no opts: %w", "valid-no-opts", ErrInvalidParameter),
want: &err{
ErrorFields: fmt.Errorf("%s: valid no opts: %w", "valid-no-opts", ErrInvalidParameter),
Error: "valid-no-opts: valid no opts: invalid parameter",
Version: errorVersion,
Op: Op("valid-no-opts"),
Error: "valid-no-opts: valid no opts: invalid parameter",
Version: errorVersion,
Op: Op("valid-no-opts"),
},
},
{
Expand All @@ -57,7 +62,6 @@ func Test_newError(t *testing.T) {
WithInfo("msg", "hello"),
},
want: &err{
ErrorFields: fmt.Errorf("%s: valid all opts: %w", "valid-all-opts", ErrInvalidParameter),
Error: "valid-all-opts: valid all opts: invalid parameter",
Version: errorVersion,
Op: Op("valid-all-opts"),
Expand All @@ -78,7 +82,6 @@ func Test_newError(t *testing.T) {
WithInfo("msg", "hello"),
},
want: &err{
ErrorFields: fmt.Errorf("1 error occurred:\n\t* multierror: multierror all opts: invalid parameter\n\n"),
Error: "1 error occurred:\n\t* multierror: multierror all opts: invalid parameter\n\n",
Version: errorVersion,
Op: Op("multierror"),
Expand All @@ -87,21 +90,67 @@ func Test_newError(t *testing.T) {
Info: map[string]any{"msg": "hello"},
},
},
{
name: "multierror-conversion-from-errors.Join",
fromOp: Op("test"),
e: func() error {
return errors.Join(multierror.Append(fmt.Errorf("%s: multierror all opts: %w", "multierror", ErrInvalidParameter)))
}(),
opts: []Option{
WithId("1"),
WithRequestInfo(TestRequestInfo(t)),
WithInfo("msg", "hello"),
},
want: &err{
Error: "1 error occurred:\n\t* multierror: multierror all opts: invalid parameter\n\n",
Version: errorVersion,
Op: Op("test"),
Id: "1",
RequestInfo: TestRequestInfo(t),
Info: map[string]any{"msg": "hello"},
},
},
{
name: "multierror.Group",
fromOp: Op("test"),
e: func() error {
var mg multierror.Group
mg.Go(func() error {
return multierror.Append(errors.New("error"))
})

stopErrors := mg.Wait()
return stderrors.Join(stopErrors)
}(),
opts: []Option{
WithId("1"),
WithRequestInfo(TestRequestInfo(t)),
WithInfo("msg", "hello"),
},
want: &err{
Error: "1 error occurred:\n\t* error\n\n",
Version: errorVersion,
Op: Op("test"),
Id: "1",
RequestInfo: TestRequestInfo(t),
Info: map[string]any{"msg": "hello"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := newError(tt.fromOp, tt.e, tt.opts...)
got, gotErr := newError(tt.fromOp, tt.e, tt.opts...)
if tt.wantErrIs != nil {
require.Error(err)
require.Error(gotErr)
assert.Nil(got)
assert.ErrorIs(err, tt.wantErrIs)
assert.ErrorIs(gotErr, tt.wantErrIs)
if tt.wantErrContains != "" {
assert.Contains(err.Error(), tt.wantErrContains)
assert.Contains(gotErr.Error(), tt.wantErrContains)
}
return
}
require.NoError(err)
require.NoError(gotErr)
require.NotNil(got)
opts := getOpts(tt.opts...)
if opts.withId == "" {
Expand All @@ -110,7 +159,21 @@ func Test_newError(t *testing.T) {
if opts.withRequestInfo != nil {
tt.want.RequestInfo = got.RequestInfo
}
assert.Equal(tt.want, got)
require.NotEmpty(got.ErrorFields)
// make sure it can be encoded
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
gotErr = enc.Encode(got)
assert.NoError(gotErr)
t.Log(buf.String())

buf.Reset()
enc.SetIndent("", " ")
gotErr = enc.Encode(got)
assert.NoError(gotErr)
t.Log(buf.String())

assert.Empty(cmp.Diff(tt.want, got, cmpopts.IgnoreFields(err{}, "ErrorFields")))
})
}
}
Expand Down

0 comments on commit 72301c7

Please sign in to comment.