Skip to content

Commit

Permalink
Extending test coverage for compression types
Browse files Browse the repository at this point in the history
  • Loading branch information
rnishtala-sumo committed Sep 10, 2024
1 parent f4b7497 commit f478e7d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
5 changes: 5 additions & 0 deletions config/configcompression/compressiontype.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (ct *Type) UnmarshalText(in []byte) error {
}

// IsZstd returns true if the compression type is zstd.
// The specified compression level is not validated.
// Because zstd supports returning an encoder level that closest matches the compression ratio of a specific zstd compression level.
// Many input values will provide the same compression level.
func (ct *Type) IsZstd() bool {
parts := strings.Split(string(*ct), "/")
return parts[0] == string(TypeZstd)
Expand All @@ -68,6 +71,7 @@ func (ct *Type) IsGzip() bool {
levelStr == zlib.NoCompression {
return true
}
return false
}
return true
}
Expand All @@ -90,6 +94,7 @@ func (ct *Type) IsZlib() bool {
levelStr == zlib.NoCompression {
return true
}
return false
}
return true
}
Expand Down
113 changes: 113 additions & 0 deletions config/configcompression/compressiontype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,116 @@ func TestUnmarshalText(t *testing.T) {
})
}
}

func TestIsZstd(t *testing.T) {
tests := []struct {
name string
input Type
expected bool
}{
{
name: "ValidZstd",
input: TypeZstd,
expected: true,
},
{
name: "InvalidZstd",
input: TypeGzip,
expected: false,
},
{
name: "ValidZstdLevel",
input: "zstd/11",
expected: true,
},
{
name: "ValidZstdLevel",
input: "zstd/One",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.input.IsZstd())
})
}
}

func TestIsGzip(t *testing.T) {
tests := []struct {
name string
input Type
expected bool
}{
{
name: "ValidGzip",
input: TypeGzip,
expected: true,
},
{
name: "InvalidGzip",
input: TypeZlib,
expected: false,
},
{
name: "ValidZlibCompressionLevel",
input: "gzip/1",
expected: true,
},
{
name: "InvalidZlibCompressionLevel",
input: "gzip/10",
expected: false,
},
{
name: "InvalidZlibCompressionLevel",
input: "gzip/one",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.input.IsGzip())
})
}
}

func TestIsZlib(t *testing.T) {
tests := []struct {
name string
input Type
expected bool
err bool
}{
{
name: "ValidZlib",
input: TypeZlib,
expected: true,
},
{
name: "InvalidZlib",
input: TypeGzip,
expected: false,
},
{
name: "ValidZlibCompressionLevel",
input: "zlib/1",
expected: true,
},
{
name: "InvalidZlibCompressionLevel",
input: "zlib/10",
expected: false,
},
{
name: "InvalidZlibCompressionLevel",
input: "zlib/one",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.input.IsZlib())
})
}
}
16 changes: 14 additions & 2 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ func TestHTTPClientCompression(t *testing.T) {
reqBody: compressedZstdBody.Bytes(),
shouldError: false,
},
{
name: "InvalidZlib",
encoding: "zlib/one",
reqBody: compressedZstdBody.Bytes(),
shouldError: true,
},
{
name: "InvalidSnappy",
encoding: "snappy/1",
reqBody: compressedZstdBody.Bytes(),
shouldError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -108,13 +120,13 @@ func TestHTTPClientCompression(t *testing.T) {
Compression: tt.encoding,
}
client, err := clientSettings.ToClient(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
require.NoError(t, err)
res, err := client.Do(req)
if tt.shouldError {
assert.Error(t, err)
return
}
require.NoError(t, err)
res, err := client.Do(req)
require.NoError(t, err)

_, err = io.ReadAll(res.Body)
require.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"time"

"github.com/klauspost/compress/zlib"
"github.com/rs/cors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -134,11 +135,11 @@ func NewDefaultClientConfig() ClientConfig {
}
}

func setCompression(compressionField configcompression.Type) (compressionType configcompression.Type, compressionLevel int) {
// Gets the compression type and level from the configuration.
func getCompression(compressionField configcompression.Type) (compressionType configcompression.Type, compressionLevel int) {
parts := strings.Split(string(compressionField), "/")

// Set compression type
compressionLevel = 1
compressionLevel = zlib.DefaultCompression
compressionType = configcompression.Type(parts[0])
if len(parts) > 1 {
levelStr := parts[1]
Expand Down Expand Up @@ -235,7 +236,7 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
// Supporting gzip, zlib, deflate, snappy, and zstd; none is treated as uncompressed.
if hcs.Compression.IsCompressed() {
if hcs.Compression.IsZstd() || hcs.Compression.IsGzip() || hcs.Compression.IsZlib() {
compressionType, compressionLevel := setCompression(hcs.Compression)
compressionType, compressionLevel := getCompression(hcs.Compression)
compression := CompressionOptions{compressionType, compressionLevel}
clientTransport, err = newCompressRoundTripper(clientTransport, compression)
if err != nil {
Expand Down

0 comments on commit f478e7d

Please sign in to comment.