Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/prometheusremotewrite] flaky test #37563

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 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 Down Expand Up @@ -131,6 +132,85 @@ func TestHandlePRWContentTypeNegotiation(t *testing.T) {
}
}

func TestHandlerPRWContentTypeNegotiation_httptest(t *testing.T) {
body := writev2.Request{}
pBuf := proto.NewBuffer(nil)
err := pBuf.Marshal(&body)
assert.NoError(t, err)

var compressedBody []byte
snappy.Encode(compressedBody, pBuf.Bytes())

// no content type
expectedCode := http.StatusUnsupportedMediaType

req := httptest.NewRequest(http.MethodPost, "/api/v1/write", bytes.NewBuffer(compressedBody))
req.Header.Set("Content-Encoding", "snappy")
req.Header.Set("Content-Type", "")
w := httptest.NewRecorder()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArthurSens It's not done yet. It's just an idea. Did you see any problem to use httptest instead of call setupServer? Like you did here ⬇️

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

httptest take care of a bunch of things. Including the creation of a valid base url and ports. For that specific case where we are just validating the expected statusCode given a header, I think that would be a good approach to avoid the flaky test thing. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pjanotti some constraint about this approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I like your approach! Let's do it!


prwReceiver := setupMetricsReceiver(t)

prwReceiver.handlePRW(w, req)

resp := w.Result()
assert.Equal(t, expectedCode, resp.StatusCode)

// unsupported content type

req = httptest.NewRequest(http.MethodPost, "/api/v1/write", bytes.NewBuffer(compressedBody))
req.Header.Set("Content-Encoding", "snappy")
req.Header.Set("Content-Type", "application/json")
w = httptest.NewRecorder()

prwReceiver.handlePRW(w, req)

resp = w.Result()
assert.Equal(t, expectedCode, resp.StatusCode)

// x-protobuf/no proto parameter

req = httptest.NewRequest(http.MethodPost, "/api/v1/write", bytes.NewBuffer(compressedBody))
req.Header.Set("Content-Encoding", "snappy")
req.Header.Set("Content-Type", "application/x-protobuf")

w = httptest.NewRecorder()

prwReceiver.handlePRW(w, req)

resp = w.Result()
assert.Equal(t, expectedCode, resp.StatusCode)

// x-protobuf/v1 proto parameter
req = httptest.NewRequest(http.MethodPost, "/api/v1/write", bytes.NewBuffer(compressedBody))
req.Header.Set("Content-Encoding", "snappy")
req.Header.Set("Content-Type", fmt.Sprintf("application/x-protobuf;proto=%s", promconfig.RemoteWriteProtoMsgV1))
w = httptest.NewRecorder()

prwReceiver.handlePRW(w, req)

resp = w.Result()
assert.Equal(t, expectedCode, resp.StatusCode)

// x-protobuf/v2 proto parameter
expectedCode = http.StatusNoContent
req = httptest.NewRequest(http.MethodPost, "/api/v1/write", nil)
req.Header.Set("Content-Encoding", "snappy")
req.Header.Set("Content-Type", fmt.Sprintf("application/x-protobuf;proto=%s", promconfig.RemoteWriteProtoMsgV2))
w = httptest.NewRecorder()

prwReceiver.handlePRW(w, req)

resp = w.Result()
assert.Equal(t, expectedCode, resp.StatusCode)

if expectedCode == http.StatusNoContent { // We went until the end
assert.NotEmpty(t, resp.Header.Get("X-Prometheus-Remote-Write-Samples-Written"))
assert.NotEmpty(t, resp.Header.Get("X-Prometheus-Remote-Write-Histograms-Written"))
assert.NotEmpty(t, resp.Header.Get("X-Prometheus-Remote-Write-Exemplars-Written"))
}
}

func TestTranslateV2(t *testing.T) {
prwReceiver := setupMetricsReceiver(t)
ctx, cancel := context.WithCancel(context.Background())
Expand Down
Loading