Debian Codegraph @

receiver/otlpreceiver/internal/errors/errors_test.go

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