Skip to content

Commit

Permalink
[chore] prefer usage of testify package vs manual checks (open-teleme…
Browse files Browse the repository at this point in the history
…try#17964)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jan 24, 2023
1 parent 8a7c98d commit 6970681
Show file tree
Hide file tree
Showing 44 changed files with 208 additions and 473 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package alibabacloudlogserviceexporter

import (
"reflect"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -113,8 +112,6 @@ func TestLogsDataToLogService(t *testing.T) {

sort.Sort(logKeyValuePairs(gotLogPairs[j]))
sort.Sort(logKeyValuePairs(wantLogs[j]))
if !reflect.DeepEqual(gotLogPairs[j], wantLogs[j]) {
t.Errorf("Unsuccessful conversion \nGot:\n\t%v\nWant:\n\t%v", gotLogPairs, wantLogs)
}
assert.Equal(t, wantLogs[j], gotLogPairs[j])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package alibabacloudlogserviceexporter

import (
"reflect"
"sort"
"testing"

Expand Down Expand Up @@ -137,9 +136,7 @@ func TestMetricDataToLogService(t *testing.T) {
for j := 0; j < len(gotLogs); j++ {
sort.Sort(logKeyValuePairs(gotLogPairs[j]))
sort.Sort(logKeyValuePairs(wantLogs[j]))
if !reflect.DeepEqual(gotLogPairs[j], wantLogs[j]) {
t.Errorf("Unsuccessful conversion \nGot:\n\t%v\nWant:\n\t%v", gotLogPairs, wantLogs)
}
assert.Equal(t, wantLogs[j], gotLogPairs[j])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -67,9 +66,7 @@ func TestTraceDataToLogService(t *testing.T) {
for j := 0; j < len(gotLogs); j++ {
sort.Sort(logKeyValuePairs(gotLogPairs[j]))
sort.Sort(logKeyValuePairs(wantLogs[j]))
if !reflect.DeepEqual(gotLogPairs[j], wantLogs[j]) {
t.Errorf("Unsuccessful conversion \nGot:\n\t%v\nWant:\n\t%v", gotLogPairs[j], wantLogs[j])
}
assert.Equal(t, wantLogs[j], gotLogPairs[j])
}
}

Expand Down
5 changes: 2 additions & 3 deletions exporter/awsemfexporter/datapoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,8 @@ func TestIntDataPointSlice_At(t *testing.T) {
deltaMetricMetadata: tt.fields.deltaMetricMetadata,
NumberDataPointSlice: tt.fields.NumberDataPointSlice,
}
if got, _ := dps.At(tt.args.i); !reflect.DeepEqual(got, tt.want) {
t.Errorf("At() = %v, want %v", got, tt.want)
}
got, _ := dps.At(tt.args.i)
assert.Equal(t, tt.want, got)
})
}
}
2 changes: 1 addition & 1 deletion exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func assertDimsEqual(t *testing.T, expected, actual [][]string) {
assert.Equal(t, len(expected), len(actual))
expectedDimensions := normalizeDimensionality(expected)
actualDimensions := normalizeDimensionality(actual)
assert.True(t, reflect.DeepEqual(expectedDimensions, actualDimensions))
assert.Equal(t, expectedDimensions, actualDimensions)
}

// cWMeasurementEqual returns true if CW Measurements are equal.
Expand Down
9 changes: 3 additions & 6 deletions exporter/awsxrayexporter/internal/translator/cause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
Expand Down Expand Up @@ -167,9 +168,7 @@ func TestCauseWithStatusMessage(t *testing.T) {
assert.NotNil(t, filtered)
assert.NotNil(t, cause)
w := testWriters.borrow()
if err := w.Encode(cause); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(cause))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, errorMsg))
Expand All @@ -194,9 +193,7 @@ func TestCauseWithHttpStatusMessage(t *testing.T) {
assert.NotNil(t, filtered)
assert.NotNil(t, cause)
w := testWriters.borrow()
if err := w.Encode(cause); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(cause))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, errorMsg))
Expand Down
37 changes: 10 additions & 27 deletions exporter/awsxrayexporter/internal/translator/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
Expand All @@ -37,9 +38,7 @@ func TestClientSpanWithURLAttribute(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "https://api.example.com/users/junit"))
Expand All @@ -60,9 +59,7 @@ func TestClientSpanWithSchemeHostTargetAttributes(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "https://api.example.com/users/junit"))
Expand All @@ -87,9 +84,7 @@ func TestClientSpanWithPeerAttributes(t *testing.T) {
assert.Equal(t, "10.8.17.36", *httpData.Request.ClientIP)

w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "http:https://kb234.example.com:8080/users/junit"))
Expand Down Expand Up @@ -122,9 +117,7 @@ func TestClientSpanWithPeerIp4Attributes(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "http:https://10.8.17.36:8080/users/junit"))
Expand All @@ -143,9 +136,7 @@ func TestClientSpanWithPeerIp6Attributes(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "https://2001:db8:85a3::8a2e:370:7334/users/junit"))
Expand All @@ -165,9 +156,7 @@ func TestServerSpanWithURLAttribute(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "https://api.example.com/users/junit"))
Expand All @@ -188,9 +177,7 @@ func TestServerSpanWithSchemeHostTargetAttributes(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "https://api.example.com/users/junit"))
Expand All @@ -212,9 +199,7 @@ func TestServerSpanWithSchemeServernamePortTargetAttributes(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "https://api.example.com/users/junit"))
Expand All @@ -238,9 +223,7 @@ func TestServerSpanWithSchemeNamePortTargetAttributes(t *testing.T) {
assert.NotNil(t, httpData)
assert.NotNil(t, filtered)
w := testWriters.borrow()
if err := w.Encode(httpData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(httpData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "http:https://kb234.example.com:8080/users/junit"))
Expand Down
5 changes: 2 additions & 3 deletions exporter/awsxrayexporter/internal/translator/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.8.0"
Expand Down Expand Up @@ -259,9 +260,7 @@ func TestClientSpanWithDbComponent(t *testing.T) {
assert.Equal(t, "remote", *segment.Namespace)

w := testWriters.borrow()
if err := w.Encode(segment); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(segment))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, spanName))
Expand Down
9 changes: 3 additions & 6 deletions exporter/awsxrayexporter/internal/translator/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)
Expand All @@ -30,9 +31,7 @@ func TestServiceFromResource(t *testing.T) {

assert.NotNil(t, service)
w := testWriters.borrow()
if err := w.Encode(service); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(service))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "semver:1.1.4"))
Expand All @@ -45,9 +44,7 @@ func TestServiceFromResourceWithNoServiceVersion(t *testing.T) {

assert.NotNil(t, service)
w := testWriters.borrow()
if err := w.Encode(service); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(service))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "v1"))
Expand Down
5 changes: 2 additions & 3 deletions exporter/awsxrayexporter/internal/translator/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
Expand All @@ -42,9 +43,7 @@ func TestClientSpanWithStatementAttribute(t *testing.T) {
assert.NotNil(t, sqlData)

w := testWriters.borrow()
if err := w.Encode(sqlData); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(sqlData))
jsonStr := w.String()
testWriters.release(w)
assert.True(t, strings.Contains(jsonStr, "mysql:https://db.example.com:3306/customers"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
Expand All @@ -38,9 +39,7 @@ func TestWriterPoolBasic(t *testing.T) {
assert.Equal(t, 0, w.buffer.Len())
resource := pcommon.NewResource()
segment, _ := MakeSegment(span, resource, nil, false, nil)
if err := w.Encode(*segment); err != nil {
assert.Fail(t, "invalid json")
}
require.NoError(t, w.Encode(*segment))
jsonStr := w.String()
assert.Equal(t, len(jsonStr), w.buffer.Len())
wp.release(w)
Expand Down
4 changes: 1 addition & 3 deletions exporter/humioexporter/traces_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ func TestPushTraceData_TransientOnPartialFailure(t *testing.T) {
assert.False(t, consumererror.IsPermanent(err))

tErr := consumererror.Traces{}
if ok := errors.As(err, &tErr); !ok {
assert.Fail(t, "PushTraceData did not return a Traces error")
}
require.ErrorAs(t, err, &tErr)
assert.Equal(t, 1, tErr.GetTraces().ResourceSpans().Len())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package dimensions

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

Expand Down Expand Up @@ -217,10 +217,7 @@ func TestGetDimensionUpdateFromMetadata(t *testing.T) {
"-_.",
)
require.NoError(t, err)
got := getDimensionUpdateFromMetadata(tt.args.metadata, *converter)
if !reflect.DeepEqual(*got, *tt.want) {
t.Errorf("getDimensionUpdateFromMetadata() = %v, want %v", *got, *tt.want)
}
assert.Equal(t, tt.want, getDimensionUpdateFromMetadata(tt.args.metadata, *converter))
})
}
}
Expand Down
27 changes: 12 additions & 15 deletions exporter/signalfxexporter/internal/hostmetadata/host_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ package hostmetadata

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -74,15 +75,13 @@ func TestFillOSSpecificData(t *testing.T) {
syscallUname = tt.args.syscallUname
t.Setenv("HOST_ETC", tt.args.etc)
in := &hostOS{}
if err := fillPlatformSpecificOSData(in); err != nil {
if !tt.wantErr {
t.Errorf("fillPlatformSpecificOSData returned an error %v", err)
}
err := fillPlatformSpecificOSData(in)
if tt.wantErr {
assert.Error(t, err)
return
}
if !reflect.DeepEqual(in, tt.want) {
t.Errorf("fillPlatformSpecificOSData() = %v, want %v", in, tt.want)
}
require.NoError(t, err)
assert.Equal(t, tt.want, in)
})
syscallUname = unix.Uname
}
Expand Down Expand Up @@ -127,15 +126,13 @@ func TestFillPlatformSpecificCPUData(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
syscallUname = tt.args.syscallUname
in := &hostCPU{}
if err := fillPlatformSpecificCPUData(in); err != nil {
if !tt.wantErr {
t.Errorf("fillPlatformSpecificCPUData returned an error %v", err)
}
err := fillPlatformSpecificCPUData(in)
if tt.wantErr {
assert.Error(t, err)
return
}
if !reflect.DeepEqual(in, tt.want) {
t.Errorf("fillPlatformSpecificCPUData() = %v, want %v", in, tt.want)
}
require.NoError(t, err)
assert.Equal(t, tt.want, in)
})
syscallUname = unix.Uname
}
Expand Down
Loading

0 comments on commit 6970681

Please sign in to comment.