Skip to content

Commit

Permalink
[chore]: enable go-require rule from testifylint (#35190)
Browse files Browse the repository at this point in the history
#### Description

Testifylint is a linter that provides best practices with the use of
testify.

This PR enables
[go-require](https://github.com/Antonboom/testifylint?tab=readme-ov-file#go-require)
rule from [testifylint](https://github.com/Antonboom/testifylint)

---------

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored and jriguera committed Oct 4, 2024
1 parent c92d627 commit 3c2a004
Show file tree
Hide file tree
Showing 58 changed files with 329 additions and 301 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ linters-settings:
disable:
- float-compare
- formatter
- go-require
- require-error
- suite-subtest-run
- useless-assert
Expand Down
2 changes: 1 addition & 1 deletion Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ GOTESTSUM := $(TOOLS_BIN_DIR)/gotestsum
TESTIFYLINT := $(TOOLS_BIN_DIR)/testifylint

GOTESTSUM_OPT?= --rerun-fails=1
TESTIFYLINT_OPT?= --enable-all --disable=float-compare,formatter,go-require,require-error,suite-subtest-run,useless-assert
TESTIFYLINT_OPT?= --enable-all --disable=float-compare,formatter,require-error,suite-subtest-run,useless-assert

# BUILD_TYPE should be one of (dev, release).
BUILD_TYPE?=release
Expand Down
3 changes: 2 additions & 1 deletion cmd/telemetrygen/internal/e2etest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestGenerateTraces(t *testing.T) {
}
go func() {
err = traces.Start(cfg)
require.NoError(t, err)
assert.NoError(t, err)
}()
require.Eventually(t, func() bool {
return len(sink.AllTraces()) > 0
Expand Down
2 changes: 1 addition & 1 deletion connector/spanmetricsconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func TestConcurrentShutdown(t *testing.T) {
for i := 0; i < concurrency; i++ {
go func() {
err := p.Shutdown(ctx)
require.NoError(t, err)
assert.NoError(t, err)
wg.Done()
}()
}
Expand Down
8 changes: 4 additions & 4 deletions exporter/carbonexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestConsumeMetrics(t *testing.T) {
defer writersWG.Done()
<-startCh
for j := 0; j < tt.writesPerProducer; j++ {
require.NoError(t, exp.ConsumeMetrics(context.Background(), tt.md))
assert.NoError(t, exp.ConsumeMetrics(context.Background(), tt.md))
}
}()
}
Expand Down Expand Up @@ -332,10 +332,10 @@ func (cs *carbonServer) start(t *testing.T, numExpectedReq int) {
// Close is expected to cause error.
return
}
require.NoError(t, err)
assert.NoError(t, err)
go func(conn net.Conn) {
defer func() {
require.NoError(t, conn.Close())
assert.NoError(t, conn.Close())
}()

reader := bufio.NewReader(conn)
Expand All @@ -344,7 +344,7 @@ func (cs *carbonServer) start(t *testing.T, numExpectedReq int) {
if errors.Is(err, io.EOF) {
return
}
require.NoError(t, err)
assert.NoError(t, err)

if cs.expectedContainsValue != "" {
assert.Contains(t, string(buf), cs.expectedContainsValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ func TestPushMetadata(t *testing.T) {
assert.Equal(t, "apikey", r.Header.Get("DD-Api-Key"))
assert.Equal(t, "otelcontribcol/1.0", r.Header.Get("User-Agent"))
reader, err := gzip.NewReader(r.Body)
require.NoError(t, err)
assert.NoError(t, err)
body, err := io.ReadAll(reader)
require.NoError(t, err)
assert.NoError(t, err)

var recvMetadata payload.HostMetadata
err = json.Unmarshal(body, &recvMetadata)
require.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, mockMetadata, recvMetadata)
})

Expand Down
2 changes: 1 addition & 1 deletion exporter/honeycombmarkerexporter/logs_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestExportMarkers(t *testing.T) {
decodedBody := map[string]any{}
err := json.NewDecoder(req.Body).Decode(&decodedBody)

require.NoError(t, err)
assert.NoError(t, err)

assert.Equal(t, len(tt.attributeMap), len(decodedBody))

Expand Down
2 changes: 1 addition & 1 deletion exporter/loadbalancingexporter/log_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func TestRollingUpdatesWhenConsumeLogs(t *testing.T) {
return
case <-ticker.C:
go func() {
require.NoError(t, p.ConsumeLogs(ctx, randomLogs()))
assert.NoError(t, p.ConsumeLogs(ctx, randomLogs()))
}()
}
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/loadbalancingexporter/metrics_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ func TestRollingUpdatesWhenConsumeMetrics(t *testing.T) {
return
case <-ticker.C:
go func() {
require.NoError(t, p.ConsumeMetrics(ctx, randomMetrics(t, 1, 1, 1, 1)))
assert.NoError(t, p.ConsumeMetrics(ctx, randomMetrics(t, 1, 1, 1, 1)))
}()
}
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/loadbalancingexporter/trace_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func TestRollingUpdatesWhenConsumeTraces(t *testing.T) {
return
case <-ticker.C:
go func() {
require.NoError(t, p.ConsumeTraces(ctx, randomTraces()))
assert.NoError(t, p.ConsumeTraces(ctx, randomTraces()))
}()
}
}
Expand Down
12 changes: 6 additions & 6 deletions exporter/lokiexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func TestPushLogData(t *testing.T) {
// prepare
ts := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
encPayload, err := io.ReadAll(r.Body)
require.NoError(t, err)
assert.NoError(t, err)

decPayload, err := snappy.Decode(nil, encPayload)
require.NoError(t, err)
assert.NoError(t, err)

err = proto.Unmarshal(decPayload, actualPushRequest)
require.NoError(t, err)
assert.NoError(t, err)
}))
defer ts.Close()

Expand Down Expand Up @@ -241,14 +241,14 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
// prepare
ts := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
encPayload, err := io.ReadAll(r.Body)
require.NoError(t, err)
assert.NoError(t, err)

decPayload, err := snappy.Decode(nil, encPayload)
require.NoError(t, err)
assert.NoError(t, err)

pr := &push.PushRequest{}
err = proto.Unmarshal(decPayload, pr)
require.NoError(t, err)
assert.NoError(t, err)

actualPushRequestPerTenant[r.Header.Get("X-Scope-OrgID")] = pr
}))
Expand Down
9 changes: 5 additions & 4 deletions exporter/opensearchexporter/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumererror"
Expand Down Expand Up @@ -112,13 +113,13 @@ func TestOpenSearchTraceExporter(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
docs := getReceivedDocuments(r.Body)
require.LessOrEqualf(t, requestCount, len(tc.RequestHandlers), "Test case generated more requests than it has response for.")
assert.LessOrEqualf(t, requestCount, len(tc.RequestHandlers), "Test case generated more requests than it has response for.")
tc.RequestHandlers[requestCount].ValidateReceivedDocuments(t, requestCount, docs)

w.WriteHeader(200)
response, _ := os.ReadFile(tc.RequestHandlers[requestCount].ResponseJSONPath)
_, err = w.Write(response)
require.NoError(t, err)
assert.NoError(t, err)

requestCount++
}))
Expand Down Expand Up @@ -242,13 +243,13 @@ func TestOpenSearchLogExporter(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
docs := getReceivedDocuments(r.Body)
require.LessOrEqualf(t, requestCount, len(tc.RequestHandlers), "Test case generated more requests than it has response for.")
assert.LessOrEqualf(t, requestCount, len(tc.RequestHandlers), "Test case generated more requests than it has response for.")
tc.RequestHandlers[requestCount].ValidateReceivedDocuments(t, requestCount, docs)

w.WriteHeader(200)
response, _ := os.ReadFile(tc.RequestHandlers[requestCount].ResponseJSONPath)
_, err = w.Write(response)
require.NoError(t, err)
assert.NoError(t, err)

requestCount++
}))
Expand Down
18 changes: 9 additions & 9 deletions exporter/otelarrowexporter/internal/arrow/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ func TestArrowExporterStreamRace(t *testing.T) {
defer wg.Done()
// This blocks until the cancelation.
_, err := tc.exporter.SendAndWait(callctx, twoTraces)
require.Error(t, err)
assert.Error(t, err)

stat, is := status.FromError(err)
require.True(t, is, "is a gRPC status error: %v", err)
require.Equal(t, codes.Canceled, stat.Code())
assert.True(t, is, "is a gRPC status error: %v", err)
assert.Equal(t, codes.Canceled, stat.Code())
}()
}

Expand Down Expand Up @@ -547,8 +547,8 @@ func TestArrowExporterStreaming(t *testing.T) {
defer wg.Done()
for data := range channel.sendChannel() {
traces, err := testCon.TracesFrom(data)
require.NoError(t, err)
require.Len(t, traces, 1)
assert.NoError(t, err)
assert.Len(t, traces, 1)
actualOutput = append(actualOutput, traces[0])
channel.recv <- statusOKFor(data.BatchId)
}
Expand Down Expand Up @@ -606,7 +606,7 @@ func TestArrowExporterHeaders(t *testing.T) {
actualOutput = append(actualOutput, nil)
} else {
_, err := hpd.Write(data.Headers)
require.NoError(t, err)
assert.NoError(t, err)
actualOutput = append(actualOutput, md)
md = metadata.MD{}
}
Expand Down Expand Up @@ -698,7 +698,7 @@ func TestArrowExporterIsTraced(t *testing.T) {
actualOutput = append(actualOutput, nil)
} else {
_, err := hpd.Write(data.Headers)
require.NoError(t, err)
assert.NoError(t, err)
actualOutput = append(actualOutput, md)
md = metadata.MD{}
}
Expand Down Expand Up @@ -786,8 +786,8 @@ func TestArrowExporterStreamLifetimeAndShutdown(t *testing.T) {

for data := range channel.sendChannel() {
traces, err := testCon.TracesFrom(data)
require.NoError(t, err)
require.Len(t, traces, 1)
assert.NoError(t, err)
assert.Len(t, traces, 1)
atomic.AddUint64(&actualCount, 1)
channel.recv <- statusOKFor(data.BatchId)
}
Expand Down
3 changes: 2 additions & 1 deletion exporter/prometheusexporter/end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/exporter/exportertest"
Expand All @@ -38,7 +39,7 @@ func TestEndToEndSummarySupport(t *testing.T) {
dropWizardServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
// Serve back the metrics as if they were from DropWizard.
_, err := rw.Write([]byte(dropWizardResponse))
require.NoError(t, err)
assert.NoError(t, err)
currentScrapeIndex++
if currentScrapeIndex == 8 { // We shall let the Prometheus receiver scrape the DropWizard mock server, at least 8 times.
wg.Done() // done scraping dropWizardResponse 8 times
Expand Down
4 changes: 2 additions & 2 deletions exporter/sapmexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ func TestCompression(t *testing.T) {
assert.EqualValues(t, compression, tt.receivedCompression)

payload, err := decompress(r.Body, compression)
require.NoError(t, err)
assert.NoError(t, err)

var sapm splunksapm.PostSpansRequest
err = sapm.Unmarshal(payload)
require.NoError(t, err)
assert.NoError(t, err)

w.WriteHeader(200)
tracesReceived = true
Expand Down
14 changes: 7 additions & 7 deletions exporter/splunkhecexporter/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func runMetricsExport(cfg *Config, metrics pmetric.Metrics, expectedBatchesNum i
defer s.Close()
go func() {
if e := s.Serve(listener); e != http.ErrServerClosed {
require.NoError(t, e)
assert.NoError(t, e)
}
}()

Expand Down Expand Up @@ -282,7 +282,7 @@ func runTraceExport(testConfig *Config, traces ptrace.Traces, expectedBatchesNum
defer s.Close()
go func() {
if e := s.Serve(listener); e != http.ErrServerClosed {
require.NoError(t, e)
assert.NoError(t, e)
}
}()

Expand Down Expand Up @@ -342,7 +342,7 @@ func runLogExport(cfg *Config, ld plog.Logs, expectedBatchesNum int, t *testing.
defer s.Close()
go func() {
if e := s.Serve(listener); e != http.ErrServerClosed {
require.NoError(t, e)
assert.NoError(t, e)
}
}()

Expand Down Expand Up @@ -1287,7 +1287,7 @@ func TestErrorReceived(t *testing.T) {
defer s.Close()
go func() {
if e := s.Serve(listener); e != http.ErrServerClosed {
require.NoError(t, e)
assert.NoError(t, e)
}
}()

Expand Down Expand Up @@ -1376,7 +1376,7 @@ func TestHeartbeatStartupFailed(t *testing.T) {
defer s.Close()
go func() {
if e := s.Serve(listener); e != http.ErrServerClosed {
require.NoError(t, e)
assert.NoError(t, e)
}
}()
factory := NewFactory()
Expand Down Expand Up @@ -1415,7 +1415,7 @@ func TestHeartbeatStartupPass_Disabled(t *testing.T) {
defer s.Close()
go func() {
if e := s.Serve(listener); e != http.ErrServerClosed {
require.NoError(t, e)
assert.NoError(t, e)
}
}()
factory := NewFactory()
Expand Down Expand Up @@ -1450,7 +1450,7 @@ func TestHeartbeatStartupPass(t *testing.T) {
defer s.Close()
go func() {
if e := s.Serve(listener); e != http.ErrServerClosed {
require.NoError(t, e)
assert.NoError(t, e)
}
}()
factory := NewFactory()
Expand Down
Loading

0 comments on commit 3c2a004

Please sign in to comment.