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

[chore]: use testify instead of testing.Fatal or testing.Error in extension and internal #36837

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 3 additions & 6 deletions extension/encoding/avrologencodingextension/avro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewAvroLogsUnmarshaler(t *testing.T) {
schema, data := createAVROTestData(t)

deserializer, err := newAVROStaticSchemaDeserializer(schema)
if err != nil {
t.Errorf("Did not expect an error, got %q", err.Error())
}
require.NoError(t, err, "Did not expect an error")

logMap, err := deserializer.Deserialize(data)
if err != nil {
t.Fatalf("Did not expect an error, got %q", err.Error())
}
require.NoError(t, err, "Did not expect an error")

assert.Equal(t, int64(1697187201488000000), logMap["timestamp"].(time.Time).UnixNano())
assert.Equal(t, "host1", logMap["hostname"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ func TestInvalidUnmarshal(t *testing.T) {
t.Parallel()

schema, err := loadAVROSchemaFromFile("testdata/schema1.avro")
if err != nil {
t.Fatalf("Failed to read avro schema file: %q", err.Error())
}

require.NoError(t, err, "Failed to read avro schema file")

e, err := newExtension(&Config{Schema: string(schema)})
assert.NoError(t, err)
Expand Down
9 changes: 3 additions & 6 deletions extension/encoding/avrologencodingextension/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/linkedin/goavro/v2"
"github.com/stretchr/testify/require"
)

func encodeAVROLogTestData(codec *goavro.Codec, data string) []byte {
Expand Down Expand Up @@ -41,14 +42,10 @@ func createAVROTestData(t *testing.T) (string, []byte) {
t.Helper()

schema, err := loadAVROSchemaFromFile("testdata/schema1.avro")
if err != nil {
t.Fatalf("Failed to read avro schema file: %q", err.Error())
}
require.NoError(t, err, "Failed to read avro schema file")

codec, err := goavro.NewCodec(string(schema))
if err != nil {
t.Fatalf("Failed to create avro code from schema: %q", err.Error())
}
require.NoError(t, err, "Failed to create avro code from schema")

data := encodeAVROLogTestData(codec, `{
"timestamp": 1697187201488,
Expand Down
5 changes: 1 addition & 4 deletions extension/observer/dockerobserver/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ func containerJSON(t *testing.T) dtypes.ContainerJSON {
require.NoError(t, err)

var container dtypes.ContainerJSON
err = json.Unmarshal(containerRaw, &container)
if err != nil {
t.Fatal(err)
}
require.NoError(t, json.Unmarshal(containerRaw, &container))
return container
}

Expand Down
6 changes: 2 additions & 4 deletions internal/aws/cwlogs/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -185,10 +186,7 @@ func TestAddLogEventWithValidation(t *testing.T) {
logEvent := NewEvent(timestampMs, largeEventContent)
expectedTruncatedContent := (*logEvent.InputLogEvent.Message)[0:(defaultMaxEventPayloadBytes-perEventHeaderBytes-len(truncatedSuffix))] + truncatedSuffix

err := p.AddLogEntry(logEvent)
if err != nil {
t.Errorf("Error adding log entry: %v", err)
}
require.NoError(t, p.AddLogEntry(logEvent), "Error adding log entry")
assert.Equal(t, expectedTruncatedContent, *logEvent.InputLogEvent.Message)

logEvent = NewEvent(timestampMs, "")
Expand Down
9 changes: 3 additions & 6 deletions internal/aws/k8s/k8sclient/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -49,12 +50,8 @@ users:
provideClusterInfo: true
`
tmpfile, err := os.CreateTemp("", "kubeconfig")
if err != nil {
t.Error(err)
}
if err := os.WriteFile(tmpfile.Name(), []byte(content), 0o600); err != nil {
t.Error(err)
}
require.NoError(t, err)
require.NoError(t, os.WriteFile(tmpfile.Name(), []byte(content), 0o600))
// overwrite the default kube config path
kubeConfigPath = tmpfile.Name()
return kubeConfigPath
Expand Down
4 changes: 1 addition & 3 deletions internal/aws/metrics/metric_calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,5 @@ func TestSweep(t *testing.T) {
require.NoError(t, mwe.Shutdown())
for range sweepEvent { // nolint
}
if !closed.Load() {
t.Errorf("Sweeper did not terminate.")
}
assert.True(t, closed.Load(), "Sweeper did not terminate.")
}
22 changes: 10 additions & 12 deletions internal/common/docker/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package docker

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDockerImageToElements(t *testing.T) {
Expand Down Expand Up @@ -133,18 +136,13 @@ func TestDockerImageToElements(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
image, err := ParseImageName(tt.args.image)
if (err != nil) != tt.wantErr {
t.Errorf("ParseImageName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if image.Repository != tt.wantRepository {
t.Errorf("ParseImageName() repository = %v, want %v", image.Repository, tt.wantRepository)
}
if image.Tag != tt.wantTag {
t.Errorf("ParseImageName() tag = %v, want %v", image.Tag, tt.wantTag)
}
if image.SHA256 != tt.wantSHA256 {
t.Errorf("ParseImageName() hash = %v, want %v", image.SHA256, tt.wantSHA256)
if !tt.wantErr {
assert.NoError(t, err, "ParseImageName() error = %v, wantErr %v", err, tt.wantErr)
assert.Equal(t, tt.wantRepository, image.Repository, "ParseImageName() repository = %v, want %v", image.Repository, tt.wantRepository)
assert.Equal(t, tt.wantTag, image.Tag, "ParseImageName() tag = %v, want %v", image.Tag, tt.wantTag)
assert.Equal(t, tt.wantSHA256, image.SHA256, "ParseImageName() hash = %v, want %v", image.SHA256, tt.wantSHA256)
} else {
require.Error(t, err)
}
})
}
Expand Down
8 changes: 2 additions & 6 deletions internal/coreinternal/parseutils/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,7 @@ func BenchmarkURLToMap(b *testing.B) {
m := make(map[string]any)
v := "https://dev:[email protected]:8443/v1/app/stage?token=d9e28b1d-2c7b-4853-be6a-d94f34a5d4ab&env=prod&env=stage&token=c6fa29f9-a31b-4584-b98d-aa8473b0e18d&region=us-east1b&mode=fast"
u, err := url.ParseRequestURI(v)
if err != nil {
b.Fatal(err)
}
require.NoError(b, err)
for n := 0; n < b.N; n++ {
_, _ = urlToMap(u, m)
}
Expand All @@ -467,9 +465,7 @@ func BenchmarkQueryToMap(b *testing.B) {
m := make(map[string]any)
v := "?token=d9e28b1d-2c7b-4853-be6a-d94f34a5d4ab&env=prod&env=stage&token=c6fa29f9-a31b-4584-b98d-aa8473b0e18d&region=us-east1b&mode=fast"
u, err := url.ParseQuery(v)
if err != nil {
b.Fatal(err)
}
require.NoError(b, err)
for n := 0; n < b.N; n++ {
queryToMap(u, m)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -27,36 +28,22 @@ var (

func TestFormat(t *testing.T) {
s, err := Format(format1, dt1)
if err != nil {
t.Fatal(err)
}
if s != value1 {
t.Errorf("Given: %v, expected: %v", s, value1)
}
require.NoError(t, err)
assert.Equal(t, s, value1, "Given: %v, expected: %v", s, value1)

s, err = Format(format2, dt1)
if err != nil {
t.Fatal(err)
}
if s != value2 {
t.Errorf("Given: %v, expected: %v", s, value2)
}
require.NoError(t, err)
assert.Equal(t, s, value2, "Given: %v, expected: %v", s, value2)
}

func TestParse(t *testing.T) {
dt, err := Parse(format1, value1)
if err != nil {
t.Error(err)
} else if dt != dt1 {
t.Errorf("Given: %v, expected: %v", dt, dt1)
}
require.NoError(t, err)
assert.Equal(t, dt, dt1, "Given: %v, expected: %v", dt, dt1)

dt, err = Parse(format2, value2)
if err != nil {
t.Error(err)
} else if dt != dt2 {
t.Errorf("Given: %v, expected: %v", dt, dt2)
}
require.NoError(t, err)
assert.Equal(t, dt, dt2, "Given: %v, expected: %v", dt, dt2)
}

func TestZulu(t *testing.T) {
Expand All @@ -69,14 +56,11 @@ func TestZulu(t *testing.T) {
} {
t.Run(input, func(t *testing.T) {
dt, err := Parse(format, input)
if err != nil {
t.Error(err)
} else if dt.UnixNano() != dt1.UnixNano() {
// We compare the unix nanoseconds because Go has a subtle parsing difference between "Z" and "+0000".
// The former returns a Time with the UTC timezone, the latter returns a Time with a 0000 time zone offset.
// (See Go's documentation for `time.Parse`.)
t.Errorf("Given: %v, expected: %v", dt, dt1)
}
require.NoError(t, err)
// We compare the unix nanoseconds because Go has a subtle parsing difference between "Z" and "+0000".
// The former returns a Time with the UTC timezone, the latter returns a Time with a 0000 time zone offset.
// (See Go's documentation for `time.Parse`.)
assert.Equal(t, dt.UnixNano(), dt1.UnixNano(), "Given: %v, expected: %v", dt, dt1)
})
}
}
Expand Down
8 changes: 2 additions & 6 deletions internal/docker/docker_test_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ import (

func testListener(t *testing.T) (net.Listener, string) {
f, err := os.CreateTemp(os.TempDir(), "testListener")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
addr := f.Name()
require.NoError(t, os.Remove(addr))

listener, err := net.Listen("unix", addr)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return listener, addr
}
Loading