From 2ff904d5cc548d023c6610933e3ca420f068560e Mon Sep 17 00:00:00 2001 From: Raj Nishtala Date: Mon, 9 Sep 2024 15:49:34 -0400 Subject: [PATCH] Extending test coverage for compression types --- config/configcompression/compressiontype.go | 2 + .../configcompression/compressiontype_test.go | 113 ++++++++++++++++++ config/confighttp/compression_test.go | 16 ++- 3 files changed, 129 insertions(+), 2 deletions(-) diff --git a/config/configcompression/compressiontype.go b/config/configcompression/compressiontype.go index 820c85be9831..5204774af137 100644 --- a/config/configcompression/compressiontype.go +++ b/config/configcompression/compressiontype.go @@ -68,6 +68,7 @@ func (ct *Type) IsGzip() bool { levelStr == zlib.NoCompression { return true } + return false } return true } @@ -90,6 +91,7 @@ func (ct *Type) IsZlib() bool { levelStr == zlib.NoCompression { return true } + return false } return true } diff --git a/config/configcompression/compressiontype_test.go b/config/configcompression/compressiontype_test.go index cf8166d5a245..0fc7c3e8a40c 100644 --- a/config/configcompression/compressiontype_test.go +++ b/config/configcompression/compressiontype_test.go @@ -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()) + }) + } +} diff --git a/config/confighttp/compression_test.go b/config/confighttp/compression_test.go index 89fd471c04ce..680156f03736 100644 --- a/config/confighttp/compression_test.go +++ b/config/confighttp/compression_test.go @@ -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) { @@ -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)