|
| 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 | + "fmt" |
| 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(fmt.Errorf("test")), |
| 32 | + expected: status.New(codes.InvalidArgument, "Permanent error: test"), |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "Non-Permanent Error", |
| 36 | + input: fmt.Errorf("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, result) |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func Test_GetHTTPStatusCodeFromStatus(t *testing.T) { |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + input error |
| 52 | + expected int |
| 53 | + }{ |
| 54 | + { |
| 55 | + name: "Not a Status", |
| 56 | + input: fmt.Errorf("not a status error"), |
| 57 | + expected: http.StatusInternalServerError, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Retryable Status", |
| 61 | + input: status.New(codes.Unavailable, "test").Err(), |
| 62 | + expected: http.StatusServiceUnavailable, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Non-retryable Status", |
| 66 | + input: status.New(codes.InvalidArgument, "test").Err(), |
| 67 | + expected: http.StatusInternalServerError, |
| 68 | + }, |
| 69 | + } |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + result := GetHTTPStatusCodeFromStatus(tt.input) |
| 73 | + assert.Equal(t, tt.expected, result) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments