| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package statusutil |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"google.golang.org/genproto/googleapis/rpc/errdetails" |
| 14 |
"google.golang.org/grpc/codes" |
| 15 |
"google.golang.org/grpc/status" |
| 16 |
"google.golang.org/protobuf/proto" |
| 17 |
"google.golang.org/protobuf/types/known/durationpb" |
| 18 |
) |
| 19 |
|
| 20 |
func Test_ErrorMsgAndHTTPCodeToStatus(t *testing.T) { |
| 21 |
tests := []struct { |
| 22 |
name string |
| 23 |
errMsg string |
| 24 |
statusCode int |
| 25 |
expected *status.Status |
| 26 |
}{ |
| 27 |
{ |
| 28 |
name: "Bad Request", |
| 29 |
errMsg: "test", |
| 30 |
statusCode: http.StatusBadRequest, |
| 31 |
expected: status.New(codes.InvalidArgument, "test"), |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "Unauthorized", |
| 35 |
errMsg: "test", |
| 36 |
statusCode: http.StatusUnauthorized, |
| 37 |
expected: status.New(codes.Unauthenticated, "test"), |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "Forbidden", |
| 41 |
errMsg: "test", |
| 42 |
statusCode: http.StatusForbidden, |
| 43 |
expected: status.New(codes.PermissionDenied, "test"), |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "Not Found", |
| 47 |
errMsg: "test", |
| 48 |
statusCode: http.StatusNotFound, |
| 49 |
expected: status.New(codes.Unimplemented, "test"), |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "Too Many Requests", |
| 53 |
errMsg: "test", |
| 54 |
statusCode: http.StatusTooManyRequests, |
| 55 |
expected: status.New(codes.ResourceExhausted, "test"), |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "Bad Gateway", |
| 59 |
errMsg: "test", |
| 60 |
statusCode: http.StatusBadGateway, |
| 61 |
expected: status.New(codes.Unavailable, "test"), |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "Service Unavailable", |
| 65 |
errMsg: "test", |
| 66 |
statusCode: http.StatusServiceUnavailable, |
| 67 |
expected: status.New(codes.Unavailable, "test"), |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "Gateway Timeout", |
| 71 |
errMsg: "test", |
| 72 |
statusCode: http.StatusGatewayTimeout, |
| 73 |
expected: status.New(codes.Unavailable, "test"), |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "Unsupported Media Type", |
| 77 |
errMsg: "test", |
| 78 |
statusCode: http.StatusUnsupportedMediaType, |
| 79 |
expected: status.New(codes.Unknown, "test"), |
| 80 |
}, |
| 81 |
} |
| 82 |
for _, tt := range tests { |
| 83 |
t.Run(tt.name, func(t *testing.T) { |
| 84 |
result := NewStatusFromMsgAndHTTPCode(tt.errMsg, tt.statusCode) |
| 85 |
assert.Equal(t, tt.expected, result) |
| 86 |
}) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestGetRetryInfo(t *testing.T) { |
| 91 |
tests := []struct { |
| 92 |
name string |
| 93 |
input *status.Status |
| 94 |
expected *errdetails.RetryInfo |
| 95 |
}{ |
| 96 |
{ |
| 97 |
name: "NoDetails", |
| 98 |
input: status.New(codes.InvalidArgument, "test"), |
| 99 |
expected: nil, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "WithRetryInfoDetails", |
| 103 |
input: func() *status.Status { |
| 104 |
st := status.New(codes.ResourceExhausted, "test") |
| 105 |
dt, err := st.WithDetails(&errdetails.RetryInfo{RetryDelay: durationpb.New(1 * time.Second)}) |
| 106 |
require.NoError(t, err) |
| 107 |
return dt |
| 108 |
}(), |
| 109 |
expected: &errdetails.RetryInfo{RetryDelay: durationpb.New(1 * time.Second)}, |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "WithOtherDetails", |
| 113 |
input: func() *status.Status { |
| 114 |
st := status.New(codes.ResourceExhausted, "test") |
| 115 |
dt, err := st.WithDetails(&errdetails.ErrorInfo{Reason: "my reason"}) |
| 116 |
require.NoError(t, err) |
| 117 |
return dt |
| 118 |
}(), |
| 119 |
expected: nil, |
| 120 |
}, |
| 121 |
{ |
| 122 |
name: "WithMultipleDetails", |
| 123 |
input: func() *status.Status { |
| 124 |
st := status.New(codes.ResourceExhausted, "test") |
| 125 |
dt, err := st.WithDetails( |
| 126 |
&errdetails.ErrorInfo{Reason: "my reason"}, |
| 127 |
&errdetails.RetryInfo{RetryDelay: durationpb.New(1 * time.Second)}) |
| 128 |
require.NoError(t, err) |
| 129 |
return dt |
| 130 |
}(), |
| 131 |
expected: &errdetails.RetryInfo{RetryDelay: durationpb.New(1 * time.Second)}, |
| 132 |
}, |
| 133 |
} |
| 134 |
for _, tt := range tests { |
| 135 |
t.Run(tt.name, func(t *testing.T) { |
| 136 |
result := GetRetryInfo(tt.input) |
| 137 |
assert.True(t, proto.Equal(tt.expected, result)) |
| 138 |
}) |
| 139 |
} |
| 140 |
} |