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 Contains or ErrorContains with testify #6161

Merged
merged 1 commit into from
Nov 4, 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
5 changes: 2 additions & 3 deletions cmd/agent/app/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"expvar"
"flag"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -150,9 +149,9 @@ func TestBuilderWithProcessorErrors(t *testing.T) {
_, err := cfg.CreateAgent(&fakeCollectorProxy{}, zap.NewNop(), metrics.NullFactory)
require.Error(t, err)
if testCase.err != "" {
assert.Contains(t, err.Error(), testCase.err)
assert.ErrorContains(t, err, testCase.err)
} else if testCase.errContains != "" {
assert.True(t, strings.Contains(err.Error(), testCase.errContains), "error must contain %s", testCase.errContains)
assert.ErrorContains(t, err, testCase.errContains, "error must contain %s", testCase.errContains)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/agent/app/configmanager/grpc/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func TestSamplingManager_GetSamplingStrategy_error(t *testing.T) {
manager := NewConfigManager(conn)
resp, err := manager.GetSamplingStrategy(context.Background(), "any")
require.Nil(t, resp)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to get sampling strategy")
assert.ErrorContains(t, err, "failed to get sampling strategy")
}

func TestSamplingManager_GetBaggageRestrictions(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/agent/app/reporter/grpc/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ func TestBuilderWithCollectors(t *testing.T) {
assert.Equal(t, conn.Target(), test.target)
}
} else {
require.Error(t, err)
assert.Contains(t, err.Error(), test.expectedError)
assert.ErrorContains(t, err, test.expectedError)
}
})
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/agent/app/reporter/grpc/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ func TestBindTLSFlagFailure(t *testing.T) {
})
require.NoError(t, err)
_, err = new(ConnBuilder).InitFromViper(v)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to process TLS options")
assert.ErrorContains(t, err, "failed to process TLS options")
}
5 changes: 2 additions & 3 deletions cmd/agent/app/reporter/grpc/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ func TestReporter_SendFailure(t *testing.T) {
defer conn.Close()
rep := NewReporter(conn, nil, zap.NewNop())
err = rep.send(context.Background(), nil, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to export spans:")
assert.ErrorContains(t, err, "failed to export spans:")
}

func TestReporter_AddProcessTags_EmptyTags(t *testing.T) {
Expand Down Expand Up @@ -211,6 +210,6 @@ func TestReporter_MultitenantEmitBatch(t *testing.T) {
}
for _, test := range tests {
err = rep.EmitBatch(context.Background(), test.in)
assert.Contains(t, err.Error(), test.err)
assert.ErrorContains(t, err, test.err)
}
}
4 changes: 2 additions & 2 deletions cmd/anonymizer/app/uiconv/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestExtractorTraceOutputFileError(t *testing.T) {
reader,
zap.NewNop(),
)
require.Contains(t, err.Error(), "cannot create output file")
require.ErrorContains(t, err, "cannot create output file")
}

func TestExtractorTraceScanError(t *testing.T) {
Expand All @@ -86,7 +86,7 @@ func TestExtractorTraceScanError(t *testing.T) {
require.NoError(t, err)

err = extractor.Run()
require.Contains(t, err.Error(), "failed when scanning the file")
require.ErrorContains(t, err, "failed when scanning the file")
}

func loadJSON(t *testing.T, fileName string, i any) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/anonymizer/app/uiconv/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestModule_TraceNonExistent(t *testing.T) {
TraceID: "2be38093ead7a083",
}
err := Extract(config, zap.NewNop())
require.Contains(t, err.Error(), "cannot open captured file")
require.ErrorContains(t, err, "cannot open captured file")
}

func TestModule_TraceOutputFileError(t *testing.T) {
Expand All @@ -65,5 +65,5 @@ func TestModule_TraceOutputFileError(t *testing.T) {
defer os.Chmod("fixtures", 0o755)

err = Extract(config, zap.NewNop())
require.Contains(t, err.Error(), "cannot create output file")
require.ErrorContains(t, err, "cannot create output file")
}
6 changes: 3 additions & 3 deletions cmd/anonymizer/app/uiconv/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestReaderTraceSuccess(t *testing.T) {
func TestReaderTraceNonExistent(t *testing.T) {
inputFile := "fixtures/trace_non_existent.json"
_, err := newSpanReader(inputFile, zap.NewNop())
require.Contains(t, err.Error(), "cannot open captured file")
require.ErrorContains(t, err, "cannot open captured file")
}

func TestReaderTraceEmpty(t *testing.T) {
Expand All @@ -48,7 +48,7 @@ func TestReaderTraceEmpty(t *testing.T) {
require.NoError(t, err)

_, err = r.NextSpan()
require.Contains(t, err.Error(), "cannot read file")
require.ErrorContains(t, err, "cannot read file")
assert.Equal(t, 0, r.spansRead)
assert.True(t, r.eofReached)
}
Expand All @@ -70,7 +70,7 @@ func TestReaderTraceInvalidJson(t *testing.T) {
require.NoError(t, err)

_, err = r.NextSpan()
require.Contains(t, err.Error(), "cannot unmarshal span")
require.ErrorContains(t, err, "cannot unmarshal span")
assert.Equal(t, 0, r.spansRead)
assert.True(t, r.eofReached)
}
3 changes: 1 addition & 2 deletions cmd/collector/app/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ func TestCollector_StartErrors(t *testing.T) {
TenancyMgr: tm,
})
err := c.Start(options)
require.Error(t, err)
assert.Contains(t, err.Error(), expErr)
require.ErrorContains(t, err, expErr)
require.NoError(t, c.Close())
})
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/collector/app/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func TestCollectorOptionsWithFailedTLSFlags(t *testing.T) {
})
require.NoError(t, err)
_, err = c.InitFromViper(v, zap.NewNop())
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to parse")
assert.ErrorContains(t, err, "failed to parse")
})
}
}
Expand All @@ -91,8 +90,7 @@ func TestCollectorOptionsWithFlags_CheckTLSReloadInterval(t *testing.T) {
prefix + ".tls.reload-interval=24h",
})
if _, ok := otlpPrefixes[prefix]; !ok {
require.Error(t, err)
assert.Contains(t, err.Error(), "unknown flag")
assert.ErrorContains(t, err, "unknown flag")
} else {
require.NoError(t, err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/collector/app/handler/grpc_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ func TestPostSpansWithError(t *testing.T) {
},
},
})
require.Error(t, err)
require.ErrorContains(t, err, test.expectedError)
require.Nil(t, r)
assert.Contains(t, err.Error(), test.expectedError)
assert.Contains(t, logBuf.String(), test.expectedLog)
assert.Len(t, processor.getSpans(), 1)
})
Expand Down
9 changes: 3 additions & 6 deletions cmd/collector/app/handler/otlp_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,22 @@ func TestStartOtlpReceiver_Error(t *testing.T) {
opts := optionsWithPorts(":-1")
tm := &tenancy.Manager{}
_, err := StartOTLPReceiver(opts, logger, spanProcessor, tm)
require.Error(t, err)
assert.Contains(t, err.Error(), "could not start the OTLP receiver")
require.ErrorContains(t, err, "could not start the OTLP receiver")

newTraces := func(consumer.ConsumeTracesFunc, ...consumer.Option) (consumer.Traces, error) {
return nil, errors.New("mock error")
}
f := otlpreceiver.NewFactory()
_, err = startOTLPReceiver(opts, logger, spanProcessor, &tenancy.Manager{}, f, newTraces, f.CreateTraces)
require.Error(t, err)
assert.Contains(t, err.Error(), "could not create the OTLP consumer")
require.ErrorContains(t, err, "could not create the OTLP consumer")

createTracesReceiver := func(
context.Context, receiver.Settings, component.Config, consumer.Traces,
) (receiver.Traces, error) {
return nil, errors.New("mock error")
}
_, err = startOTLPReceiver(opts, logger, spanProcessor, &tenancy.Manager{}, f, consumer.NewTraces, createTracesReceiver)
require.Error(t, err)
assert.Contains(t, err.Error(), "could not create the OTLP receiver")
assert.ErrorContains(t, err, "could not create the OTLP receiver")
}

func TestOtelHost_ReportFatalError(t *testing.T) {
Expand Down
9 changes: 3 additions & 6 deletions cmd/collector/app/handler/zipkin_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,20 @@ func TestStartZipkinReceiver_Error(t *testing.T) {
opts.Zipkin.HTTPHostPort = ":-1"

_, err := StartZipkinReceiver(opts, logger, spanProcessor, tm)
require.Error(t, err)
assert.Contains(t, err.Error(), "could not start Zipkin receiver")
require.ErrorContains(t, err, "could not start Zipkin receiver")

newTraces := func(consumer.ConsumeTracesFunc, ...consumer.Option) (consumer.Traces, error) {
return nil, errors.New("mock error")
}
f := zipkinreceiver.NewFactory()
_, err = startZipkinReceiver(opts, logger, spanProcessor, tm, f, newTraces, f.CreateTraces)
require.Error(t, err)
assert.Contains(t, err.Error(), "could not create Zipkin consumer")
require.ErrorContains(t, err, "could not create Zipkin consumer")

createTracesReceiver := func(
context.Context, receiver.Settings, component.Config, consumer.Traces,
) (receiver.Traces, error) {
return nil, errors.New("mock error")
}
_, err = startZipkinReceiver(opts, logger, spanProcessor, tm, f, consumer.NewTraces, createTracesReceiver)
require.Error(t, err)
assert.Contains(t, err.Error(), "could not create Zipkin receiver")
assert.ErrorContains(t, err, "could not create Zipkin receiver")
}
3 changes: 1 addition & 2 deletions cmd/es-rollover/app/init/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package init
import (
"errors"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -69,7 +68,7 @@ func TestIndexCreateIfNotExist(t *testing.T) {
indexClient.On("CreateIndex", "jaeger-span").Return(test.returnErr)
err := createIndexIfNotExist(indexClient, "jaeger-span")
if test.containsError != "" {
assert.True(t, strings.Contains(err.Error(), test.containsError))
assert.ErrorContains(t, err, test.containsError)
} else {
assert.Equal(t, test.expectedErr, err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/internal/docs/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package docs

import (
"os"
"strings"
"testing"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,7 +35,7 @@ func TestOutputFormats(t *testing.T) {
if err == nil {
f, err := os.ReadFile(test.file)
require.NoError(t, err)
assert.True(t, strings.Contains(string(f), "documentation"))
assert.Contains(t, string(f), "documentation")
} else {
assert.Equal(t, test.err, err.Error())
}
Expand All @@ -55,7 +54,7 @@ func TestDocsForParent(t *testing.T) {
require.NoError(t, err)
f, err := os.ReadFile("root_command.md")
require.NoError(t, err)
assert.True(t, strings.Contains(string(f), "some description"))
assert.Contains(t, string(f), "some description")
}

func TestMain(m *testing.M) {
Expand Down
5 changes: 2 additions & 3 deletions cmd/internal/env/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package env

import (
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,8 +17,8 @@ func TestCommand(t *testing.T) {
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.Run(cmd, nil)
assert.True(t, strings.Contains(buf.String(), "METRICS_BACKEND"))
assert.True(t, strings.Contains(buf.String(), "SPAN_STORAGE"))
assert.Contains(t, buf.String(), "METRICS_BACKEND")
assert.Contains(t, buf.String(), "SPAN_STORAGE")
}

func TestMain(m *testing.M) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/internal/flags/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ func TestAdminWithFailedFlags(t *testing.T) {
})
require.NoError(t, err)
err = adminServer.initFromViper(v, logger)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to parse admin server TLS options")
assert.ErrorContains(t, err, "failed to parse admin server TLS options")
}

func TestAdminServerTLS(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/internal/flags/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func TestStartErrors(t *testing.T) {
require.NoError(t, err)
err = s.Start(v)
if test.expErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.expErr)
require.ErrorContains(t, err, test.expErr)
return
}
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func TestExporterStartBadNameError(t *testing.T) {
},
}
err := exp.start(context.Background(), host)
require.Error(t, err)
require.ErrorContains(t, err, "cannot find storage factory")
}

Expand All @@ -95,7 +94,6 @@ func TestExporterStartBadSpanstoreError(t *testing.T) {
},
}
err := exp.start(context.Background(), host)
require.Error(t, err)
require.ErrorContains(t, err, "mocked error")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ func TestGetStorageFactoryError(t *testing.T) {
factory: nil,
})
err := s.Start(context.Background(), host)
require.Error(t, err)
require.Contains(t, err.Error(), "cannot find storage factory")
require.ErrorContains(t, err, "cannot find storage factory")
}

func TestStorageExtensionStartError(t *testing.T) {
Expand Down Expand Up @@ -160,7 +159,7 @@ func TestStorageExtensionStartError(t *testing.T) {
assert.Eventually(t, func() bool {
return startStatus.Load() != nil
}, 5*time.Second, 100*time.Millisecond)
require.Contains(t, startStatus.Load().Err().Error(), "error starting cleaner server")
require.ErrorContains(t, startStatus.Load().Err(), "error starting cleaner server")
}

type testHost struct {
Expand Down
Loading
Loading