| •1 |
//go:build ignore |
| 2 |
// Copyright The OpenTelemetry Authors |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
package errors // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/util" |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"net/http" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"google.golang.org/grpc/codes" |
| 14 |
"google.golang.org/grpc/status" |
| 15 |
|
| 16 |
"go.opentelemetry.io/collector/consumer/consumererror" |
| 17 |
) |
| 18 |
|
| 19 |
func Test_GetStatusFromError(t *testing.T) { |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
input error |
| 23 |
expected *status.Status |
| 24 |
}{ |
| 25 |
{ |
| 26 |
name: "Status", |
| 27 |
input: status.Error(codes.Aborted, "test"), |
| 28 |
expected: status.New(codes.Aborted, "test"), |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "Permanent Error", |
| 32 |
input: consumererror.NewPermanent(errors.New("test")), |
| 33 |
expected: status.New(codes.Internal, "Permanent error: test"), |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "Non-Permanent Error", |
| 37 |
input: errors.New("test"), |
| 38 |
expected: status.New(codes.Unavailable, "test"), |
| 39 |
}, |
| 40 |
} |
| 41 |
for _, tt := range tests { |
| 42 |
t.Run(tt.name, func(t *testing.T) { |
| 43 |
result := GetStatusFromError(tt.input) |
| 44 |
assert.Equal(t, tt.expected.Err(), result) |
| 45 |
}) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func Test_GetHTTPStatusCodeFromStatus(t *testing.T) { |
| 50 |
tests := []struct { |
| 51 |
name string |
| 52 |
input *status.Status |
| 53 |
expected int |
| 54 |
}{ |
| 55 |
{ |
| 56 |
name: "Retryable Status", |
| 57 |
input: status.New(codes.Unavailable, "test"), |
| 58 |
expected: http.StatusServiceUnavailable, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "Non-retryable Status", |
| 62 |
input: status.New(codes.Internal, "test"), |
| 63 |
expected: http.StatusInternalServerError, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "Specifically 429", |
| 67 |
input: status.New(codes.ResourceExhausted, "test"), |
| 68 |
expected: http.StatusTooManyRequests, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "Specifically 400", |
| 72 |
input: status.New(codes.InvalidArgument, "test"), |
| 73 |
expected: http.StatusBadRequest, |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "Specifically 401", |
| 77 |
input: status.New(codes.Unauthenticated, "test"), |
| 78 |
expected: http.StatusUnauthorized, |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "Specifically 403", |
| 82 |
input: status.New(codes.PermissionDenied, "test"), |
| 83 |
expected: http.StatusForbidden, |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "Specifically 404", |
| 87 |
input: status.New(codes.Unimplemented, "test"), |
| 88 |
expected: http.StatusNotFound, |
| 89 |
}, |
| 90 |
} |
| 91 |
for _, tt := range tests { |
| 92 |
t.Run(tt.name, func(t *testing.T) { |
| 93 |
result := GetHTTPStatusCodeFromStatus(tt.input) |
| 94 |
assert.Equal(t, tt.expected, result) |
| 95 |
}) |
| 96 |
} |
| 97 |
} |