Skip to content

Commit

Permalink
chore: use Contains or ErrorContains with testify
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Nov 4, 2024
1 parent 1378e91 commit b51639c
Show file tree
Hide file tree
Showing 60 changed files with 136 additions and 221 deletions.
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)
}
}
6 changes: 3 additions & 3 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")
assert.ErrorContains(t, err, "cannot create output file")
}

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

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

func loadJSON(t *testing.T, fileName string, i any) {
b, err := os.ReadFile(fileName)
require.NoError(t, err)
err = json.Unmarshal(b, i)
require.NoError(t, err, "Failed to parse json fixture file %s", fileName)
assert.NoError(t, err, "Failed to parse json fixture file %s", fileName)
}
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")
assert.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")
assert.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")
assert.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)
}
5 changes: 3 additions & 2 deletions cmd/anonymizer/app/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

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

Expand Down Expand Up @@ -65,7 +66,7 @@ func TestNew(t *testing.T) {
MappingFile: tempDir + "/mapping.json",
}
_, err := New(config, nopLogger)
require.ErrorContains(t, err, "cannot create output file")
assert.ErrorContains(t, err, "cannot create output file")
})

t.Run("AnonymizedFile does not exist", func(t *testing.T) {
Expand All @@ -75,7 +76,7 @@ func TestNew(t *testing.T) {
MappingFile: tempDir + "/mapping.json",
}
_, err := New(config, nopLogger)
require.ErrorContains(t, err, "cannot create output file")
assert.ErrorContains(t, err, "cannot create output file")
})
}

Expand Down
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
8 changes: 3 additions & 5 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,10 +90,9 @@ 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)
assert.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
2 changes: 1 addition & 1 deletion cmd/jaeger/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCommand(t *testing.T) {

cmd.ParseFlags([]string{"--config", "bad-file-name"})
err := cmd.Execute()
require.ErrorContains(t, err, "bad-file-name")
assert.ErrorContains(t, err, "bad-file-name")
}

func TestCheckConfigAndRun_DefaultConfig(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ func TestExporterStartBadNameError(t *testing.T) {
},
}
err := exp.start(context.Background(), host)
require.Error(t, err)
require.ErrorContains(t, err, "cannot find storage factory")
assert.ErrorContains(t, err, "cannot find storage factory")
}

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

func TestExporter(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/jaeger/internal/extension/jaegerquery/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestServerStart(t *testing.T) {
},
}.Execute(t)
} else {
require.ErrorContains(t, err, tt.expectedErr)
assert.ErrorContains(t, err, tt.expectedErr)
}
})
}
Expand Down
Loading

0 comments on commit b51639c

Please sign in to comment.