Skip to content

Commit

Permalink
Add OTLP compression check tests
Browse files Browse the repository at this point in the history
Signed-off-by: SungJin1212 <[email protected]>
  • Loading branch information
SungJin1212 committed Nov 13, 2024
1 parent df00017 commit 222c82d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/util/push/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package push

import (
"bytes"
"compress/gzip"
"context"
"io"
"net/http"
Expand Down Expand Up @@ -233,6 +234,8 @@ func TestOTLPWriteHandler(t *testing.T) {
format string
expectedStatusCode int
expectedErrMsg string
compression bool
encodingType string
}{
{
description: "Test proto format write",
Expand Down Expand Up @@ -260,6 +263,21 @@ func TestOTLPWriteHandler(t *testing.T) {
expectedStatusCode: http.StatusBadRequest,
expectedErrMsg: "received message larger than max",
},
{
description: "valid encoding type: gzip",
maxRecvMsgSize: 10000,
format: jsonContentType,
expectedStatusCode: http.StatusOK,
encodingType: "gzip",
compression: true,
},
{
description: "invalid encoding type: snappy",
maxRecvMsgSize: 10000,
format: jsonContentType,
expectedStatusCode: http.StatusBadRequest,
encodingType: "snappy",
},
}

for _, test := range tests {
Expand All @@ -268,20 +286,40 @@ func TestOTLPWriteHandler(t *testing.T) {
ctx = user.InjectOrgID(ctx, "user-1")
var req *http.Request

compressionFunc := func(t *testing.T, body []byte) []byte {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
_, err := gz.Write(body)
require.NoError(t, err)
require.NoError(t, gz.Close())

return b.Bytes()
}

if test.format == pbContentType {
buf, err := exportRequest.MarshalProto()
require.NoError(t, err)

if test.compression {
buf = compressionFunc(t, buf)
}

req, err = http.NewRequestWithContext(ctx, "", "", bytes.NewReader(buf))
require.NoError(t, err)
req.Header.Set("Content-Type", pbContentType)
req.Header.Set("Content-Encoding", test.encodingType)
} else {
buf, err := exportRequest.MarshalJSON()
require.NoError(t, err)

if test.compression {
buf = compressionFunc(t, buf)
}

req, err = http.NewRequestWithContext(ctx, "", "", bytes.NewReader(buf))
require.NoError(t, err)
req.Header.Set("Content-Type", jsonContentType)
req.Header.Set("Content-Encoding", test.encodingType)
}

push := verifyOTLPWriteRequestHandler(t, cortexpb.API)
Expand Down

0 comments on commit 222c82d

Please sign in to comment.