-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle wrapped errors, add unit tests
- Loading branch information
1 parent
012ce50
commit ea02fff
Showing
2 changed files
with
99 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/elastic/fleet-server/v7/internal/pkg/es" | ||
testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log" | ||
"github.com/stretchr/testify/require" | ||
"go.elastic.co/apm/v2" | ||
"go.elastic.co/apm/v2/apmtest" | ||
) | ||
|
||
func Test_ErrorResp(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err error | ||
expectedTags map[string]interface{} | ||
}{{ | ||
name: "generic error", | ||
err: fmt.Errorf("generic error"), | ||
}, { | ||
name: "elastic error", | ||
err: &es.ErrElastic{}, | ||
expectedTags: map[string]interface{}{ | ||
"error_type": "ErrElastic", | ||
}, | ||
}, { | ||
name: "wrapped elastic error", | ||
err: fmt.Errorf("wrapped error: %w", &es.ErrElastic{}), | ||
expectedTags: map[string]interface{}{ | ||
"error_type": "ErrElastic", | ||
}, | ||
}} | ||
|
||
tracer := apmtest.NewRecordingTracer() | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
logger := testlog.SetLogger(t) | ||
tracer.ResetPayloads() | ||
|
||
tx := tracer.StartTransaction("test", "test") | ||
ctx := apm.ContextWithTransaction(context.Background(), tx) | ||
ctx = logger.WithContext(ctx) | ||
|
||
wr := httptest.NewRecorder() | ||
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost", nil) | ||
require.NoError(t, err) | ||
|
||
ErrorResp(wr, req, tc.err) | ||
tx.End() | ||
ch := make(chan struct{}, 1) | ||
tracer.Flush(ch) | ||
|
||
payloads := tracer.Payloads() | ||
require.Len(t, payloads.Transactions, 1) | ||
require.Len(t, payloads.Errors, 1) | ||
|
||
tags := make(map[string]interface{}) | ||
for _, tag := range payloads.Transactions[0].Context.Tags { | ||
tags[tag.Key] = tag.Value | ||
} | ||
for k, v := range tc.expectedTags { | ||
require.Contains(t, tags, k, "expected tag is missing") | ||
require.Equal(t, v, tags[k], "expected tag value does not match") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_ErrorResp_NoTransaction(t *testing.T) { | ||
tracer := apmtest.NewRecordingTracer() | ||
ctx := testlog.SetLogger(t).WithContext(context.Background()) | ||
|
||
wr := httptest.NewRecorder() | ||
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost", nil) | ||
require.NoError(t, err) | ||
|
||
ErrorResp(wr, req, fmt.Errorf("some error")) | ||
ch := make(chan struct{}, 1) | ||
tracer.Flush(ch) | ||
|
||
payloads := tracer.Payloads() | ||
require.Len(t, payloads.Transactions, 0) | ||
require.Len(t, payloads.Errors, 0) | ||
} |