| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package statusconversion // import "go.opentelemetry.io/collector/consumer/consumererror/internal/statusconversion" |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"google.golang.org/grpc/codes" |
| 12 |
"google.golang.org/grpc/status" |
| 13 |
) |
| 14 |
|
| 15 |
func Test_GetHTTPStatusCodeFromStatus(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
input *status.Status |
| 19 |
expected int |
| 20 |
}{ |
| 21 |
{ |
| 22 |
name: "Retryable Status", |
| 23 |
input: status.New(codes.Unavailable, "test"), |
| 24 |
expected: http.StatusServiceUnavailable, |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: "Non-retryable Status", |
| 28 |
input: status.New(codes.InvalidArgument, "test"), |
| 29 |
expected: http.StatusBadRequest, |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: "Specifically 429", |
| 33 |
input: status.New(codes.ResourceExhausted, "test"), |
| 34 |
expected: http.StatusTooManyRequests, |
| 35 |
}, |
| 36 |
} |
| 37 |
for _, tt := range tests { |
| 38 |
t.Run(tt.name, func(t *testing.T) { |
| 39 |
result := GetHTTPStatusCodeFromStatus(tt.input) |
| 40 |
assert.Equal(t, tt.expected, result) |
| 41 |
}) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func Test_ErrorMsgAndHTTPCodeToStatus(t *testing.T) { |
| 46 |
tests := []struct { |
| 47 |
name string |
| 48 |
errMsg string |
| 49 |
statusCode int |
| 50 |
expected *status.Status |
| 51 |
}{ |
| 52 |
{ |
| 53 |
name: "Bad Request", |
| 54 |
errMsg: "test", |
| 55 |
statusCode: http.StatusBadRequest, |
| 56 |
expected: status.New(codes.InvalidArgument, "test"), |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "Unauthorized", |
| 60 |
errMsg: "test", |
| 61 |
statusCode: http.StatusUnauthorized, |
| 62 |
expected: status.New(codes.Unauthenticated, "test"), |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "Forbidden", |
| 66 |
errMsg: "test", |
| 67 |
statusCode: http.StatusForbidden, |
| 68 |
expected: status.New(codes.PermissionDenied, "test"), |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "Not Found", |
| 72 |
errMsg: "test", |
| 73 |
statusCode: http.StatusNotFound, |
| 74 |
expected: status.New(codes.Unimplemented, "test"), |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "Too Many Requests", |
| 78 |
errMsg: "test", |
| 79 |
statusCode: http.StatusTooManyRequests, |
| 80 |
expected: status.New(codes.ResourceExhausted, "test"), |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "Bad Gateway", |
| 84 |
errMsg: "test", |
| 85 |
statusCode: http.StatusBadGateway, |
| 86 |
expected: status.New(codes.Unavailable, "test"), |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "Service Unavailable", |
| 90 |
errMsg: "test", |
| 91 |
statusCode: http.StatusServiceUnavailable, |
| 92 |
expected: status.New(codes.Unavailable, "test"), |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "Gateway Timeout", |
| 96 |
errMsg: "test", |
| 97 |
statusCode: http.StatusGatewayTimeout, |
| 98 |
expected: status.New(codes.Unavailable, "test"), |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "Unsupported Media Type", |
| 102 |
errMsg: "test", |
| 103 |
statusCode: http.StatusUnsupportedMediaType, |
| 104 |
expected: status.New(codes.Unknown, "test"), |
| 105 |
}, |
| 106 |
} |
| 107 |
for _, tt := range tests { |
| 108 |
t.Run(tt.name, func(t *testing.T) { |
| 109 |
result := NewStatusFromMsgAndHTTPCode(tt.errMsg, tt.statusCode) |
| 110 |
assert.Equal(t, tt.expected, result) |
| 111 |
}) |
| 112 |
} |
| 113 |
} |