Skip to content

Commit

Permalink
[receiver/prometheusremotewrite] flaky test (#37563)
Browse files Browse the repository at this point in the history
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Using httptest package instead of starting a real server avoiding port
conflicts.

#### Link to tracking issue
Fixes
#36654
  • Loading branch information
perebaj authored Jan 31, 2025
1 parent 803f0e1 commit 94415c9
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions receiver/prometheusremotewritereceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/gogo/protobuf/proto"
Expand All @@ -16,7 +17,6 @@ import (
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"
"github.com/prometheus/prometheus/storage/remote"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/receivertest"
Expand Down Expand Up @@ -58,22 +58,7 @@ func setupMetricsReceiver(t *testing.T) *prometheusRemoteWriteReceiver {
return prwReceiver.(*prometheusRemoteWriteReceiver)
}

func setupServer(t *testing.T) {
t.Helper()

prwReceiver := setupMetricsReceiver(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

assert.NoError(t, prwReceiver.Start(ctx, componenttest.NewNopHost()))
t.Cleanup(func() {
assert.NoError(t, prwReceiver.Shutdown(ctx), "Must not error shutting down")
})
}

func TestHandlePRWContentTypeNegotiation(t *testing.T) {
setupServer(t)

for _, tc := range []struct {
name string
contentType string
Expand Down Expand Up @@ -113,13 +98,16 @@ func TestHandlePRWContentTypeNegotiation(t *testing.T) {

var compressedBody []byte
snappy.Encode(compressedBody, pBuf.Bytes())
req, err := http.NewRequest(http.MethodPost, "http://localhost:9090/api/v1/write", bytes.NewBuffer(compressedBody))
assert.NoError(t, err)

req := httptest.NewRequest(http.MethodPost, "/api/v1/write", bytes.NewBuffer(compressedBody))

req.Header.Set("Content-Type", tc.contentType)
req.Header.Set("Content-Encoding", "snappy")
resp, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
w := httptest.NewRecorder()

prwReceiver := setupMetricsReceiver(t)
prwReceiver.handlePRW(w, req)
resp := w.Result()

assert.Equal(t, tc.expectedCode, resp.StatusCode)
if tc.expectedCode == http.StatusNoContent { // We went until the end
Expand Down

0 comments on commit 94415c9

Please sign in to comment.