|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package elasticsearchexporter |
| 5 | + |
| 6 | +import ( |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "go.opentelemetry.io/collector/component" |
| 16 | + "go.opentelemetry.io/collector/component/componentstatus" |
| 17 | + "go.uber.org/zap" |
| 18 | +) |
| 19 | + |
| 20 | +func TestComponentStatus(t *testing.T) { |
| 21 | + statusChan := make(chan *componentstatus.Event, 1) |
| 22 | + reporter := &testStatusReporter{statusChan} |
| 23 | + esLogger := clientLogger{ |
| 24 | + Logger: zap.New(nil), |
| 25 | + logRequestBody: false, |
| 26 | + logResponseBody: false, |
| 27 | + componentHost: reporter, |
| 28 | + } |
| 29 | + |
| 30 | + // Pass in an error and make sure it's sent to the component status reporter |
| 31 | + _ = esLogger.LogRoundTrip(nil, nil, io.EOF, time.Now(), 0) |
| 32 | + select { |
| 33 | + case event := <-statusChan: |
| 34 | + assert.ErrorIs(t, event.Err(), io.EOF, "LogRoundTrip should report a component status error wrapping its error parameter") |
| 35 | + assert.Equal(t, componentstatus.StatusRecoverableError, event.Status(), "LogRoundTrip on an error parameter should report a recoverable error") |
| 36 | + default: |
| 37 | + require.Fail(t, "LogRoundTrip with an error should report a recoverable error status") |
| 38 | + } |
| 39 | + |
| 40 | + // Pass in an http error status and make sure it's sent to the component status reporter |
| 41 | + _ = esLogger.LogRoundTrip( |
| 42 | + &http.Request{URL: &url.URL{}}, |
| 43 | + &http.Response{StatusCode: http.StatusUnauthorized, Status: "401 Unauthorized"}, |
| 44 | + nil, time.Now(), 0) |
| 45 | + select { |
| 46 | + case event := <-statusChan: |
| 47 | + err := event.Err() |
| 48 | + require.Error(t, err, "LogRoundTrip with an http error status should report a component status error") |
| 49 | + assert.Contains(t, err.Error(), "401 Unauthorized", "LogRoundTrip with an http error status should include the status in its error state") |
| 50 | + assert.Equal(t, componentstatus.StatusRecoverableError, event.Status(), "LogRoundTrip with an http error status should report a recoverable error") |
| 51 | + default: |
| 52 | + require.Fail(t, "LogRoundTrip with an http error code should report a recoverable error status") |
| 53 | + } |
| 54 | + |
| 55 | + // Pass in a 409 (duplicate document) and make sure it doesn't report a new status |
| 56 | + _ = esLogger.LogRoundTrip( |
| 57 | + &http.Request{URL: &url.URL{}}, |
| 58 | + &http.Response{StatusCode: http.StatusConflict, Status: "409 duplicate"}, |
| 59 | + nil, time.Now(), 0) |
| 60 | + select { |
| 61 | + case <-statusChan: |
| 62 | + assert.Fail(t, "LogRoundTrip with a 409 should not change the component status") |
| 63 | + default: |
| 64 | + } |
| 65 | + |
| 66 | + // Pass in an http success status and make sure the component status returns to OK |
| 67 | + _ = esLogger.LogRoundTrip( |
| 68 | + &http.Request{URL: &url.URL{}}, |
| 69 | + &http.Response{StatusCode: http.StatusOK}, nil, time.Now(), 0) |
| 70 | + select { |
| 71 | + case event := <-statusChan: |
| 72 | + assert.NoError(t, event.Err(), "LogRoundTrip with a success status shouldn't report a component status error") |
| 73 | + assert.Equal(t, componentstatus.StatusOK, event.Status(), "LogRoundTrip with a success status should report component status OK") |
| 74 | + default: |
| 75 | + require.Fail(t, "LogRoundTrip with an http success should report component status OK") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +type testStatusReporter struct { |
| 80 | + statusChan chan *componentstatus.Event |
| 81 | +} |
| 82 | + |
| 83 | +func (tsr *testStatusReporter) Report(event *componentstatus.Event) { |
| 84 | + tsr.statusChan <- event |
| 85 | +} |
| 86 | + |
| 87 | +func (tsr *testStatusReporter) GetExtensions() map[component.ID]component.Component { |
| 88 | + return make(map[component.ID]component.Component) |
| 89 | +} |
0 commit comments