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 ErrorContains and EqualError #35483

Merged
merged 1 commit into from
Sep 30, 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
3 changes: 1 addition & 2 deletions connector/countconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,7 @@ func TestConfigErrors(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.input.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.expect)
assert.ErrorContains(t, err, tc.expect)
})
}
}
3 changes: 1 addition & 2 deletions connector/sumconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,7 @@ func TestConfigErrors(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.input.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.expect)
assert.ErrorContains(t, err, tc.expect)
})
}
}
3 changes: 1 addition & 2 deletions exporter/clickhouseexporter/exporter_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func TestLogsExporter_New(t *testing.T) {

failWithMsg := func(msg string) validate {
return func(t *testing.T, _ *logsExporter, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), msg)
require.ErrorContains(t, err, msg)
}
}

Expand Down
20 changes: 7 additions & 13 deletions exporter/kafkaexporter/kafka_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,15 @@ func TestNewExporter_err_auth_type(t *testing.T) {
texp := newTracesExporter(c, exportertest.NewNopSettings())
require.NotNil(t, texp)
err := texp.start(context.Background(), componenttest.NewNopHost())
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to load TLS config")
assert.ErrorContains(t, err, "failed to load TLS config")
mexp := newMetricsExporter(c, exportertest.NewNopSettings())
require.NotNil(t, mexp)
err = mexp.start(context.Background(), componenttest.NewNopHost())
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to load TLS config")
assert.ErrorContains(t, err, "failed to load TLS config")
lexp := newLogsExporter(c, exportertest.NewNopSettings())
require.NotNil(t, lexp)
err = lexp.start(context.Background(), componenttest.NewNopHost())
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to load TLS config")
assert.ErrorContains(t, err, "failed to load TLS config")

}

Expand All @@ -157,7 +154,7 @@ func TestNewExporter_err_compression(t *testing.T) {
require.NotNil(t, texp)
err := texp.start(context.Background(), componenttest.NewNopHost())
assert.Error(t, err)
assert.Contains(t, err.Error(), "producer.compression should be one of 'none', 'gzip', 'snappy', 'lz4', or 'zstd'. configured value idk")
assert.ErrorContains(t, err, "producer.compression should be one of 'none', 'gzip', 'snappy', 'lz4', or 'zstd'. configured value idk")
}

func TestTracesExporter_encoding_extension(t *testing.T) {
Expand Down Expand Up @@ -249,8 +246,7 @@ func TestTracesPusher_marshal_error(t *testing.T) {
}
td := testdata.GenerateTraces(2)
err := p.tracesPusher(context.Background(), td)
require.Error(t, err)
assert.Contains(t, err.Error(), expErr.Error())
assert.ErrorContains(t, err, expErr.Error())
}

func TestMetricsDataPusher(t *testing.T) {
Expand Down Expand Up @@ -331,8 +327,7 @@ func TestMetricsDataPusher_marshal_error(t *testing.T) {
}
md := testdata.GenerateMetrics(2)
err := p.metricsDataPusher(context.Background(), md)
require.Error(t, err)
assert.Contains(t, err.Error(), expErr.Error())
assert.ErrorContains(t, err, expErr.Error())
}

func TestLogsDataPusher(t *testing.T) {
Expand Down Expand Up @@ -413,8 +408,7 @@ func TestLogsDataPusher_marshal_error(t *testing.T) {
}
ld := testdata.GenerateLogs(1)
err := p.logsDataPusher(context.Background(), ld)
require.Error(t, err)
assert.Contains(t, err.Error(), expErr.Error())
assert.ErrorContains(t, err, expErr.Error())
}

type tracesErrorMarshaler struct {
Expand Down
3 changes: 1 addition & 2 deletions exporter/lokiexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ func TestConfigValidate(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
err := tc.cfg.Validate()
if tc.err != nil {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.err.Error())
assert.ErrorContains(t, err, tc.err.Error())
} else {
require.NoError(t, err)
}
Expand Down
9 changes: 3 additions & 6 deletions exporter/otelarrowexporter/internal/arrow/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,10 @@ func TestStreamStatusUnavailableInvalid(t *testing.T) {
}()
// sender should get "test unavailable" once, success second time.
err := tc.mustSendAndWait()
require.Error(t, err)
require.Contains(t, err.Error(), "test unavailable")
require.ErrorContains(t, err, "test unavailable")

err = tc.mustSendAndWait()
require.Error(t, err)
require.Contains(t, err.Error(), "test invalid")
require.ErrorContains(t, err, "test invalid")

err = tc.mustSendAndWait()
require.NoError(t, err)
Expand Down Expand Up @@ -282,8 +280,7 @@ func TestStreamStatusUnrecognized(t *testing.T) {
channel.recv <- statusUnrecognizedFor(batch.BatchId)
}()
err := tc.mustSendAndWait()
require.Error(t, err)
require.Contains(t, err.Error(), "test unrecognized")
require.ErrorContains(t, err, "test unrecognized")

// Note: do not cancel the context, the stream should be
// shutting down due to the error.
Expand Down
7 changes: 3 additions & 4 deletions exporter/otelarrowexporter/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ func TestDuplicateMetadataKeys(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.MetadataKeys = []string{"myTOKEN", "mytoken"}
err := cfg.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "duplicate")
require.Contains(t, err.Error(), "mytoken")
require.ErrorContains(t, err, "duplicate")
require.ErrorContains(t, err, "mytoken")
}

func TestMetadataExporterCardinalityLimit(t *testing.T) {
Expand Down Expand Up @@ -196,7 +195,7 @@ func TestMetadataExporterCardinalityLimit(t *testing.T) {
err = exp.ConsumeTraces(ctx, td)
require.Error(t, err)
assert.True(t, consumererror.IsPermanent(err))
assert.Contains(t, err.Error(), "too many")
assert.ErrorContains(t, err, "too many")

assert.Eventually(t, func() bool {
return rcv.requestCount.Load() == int32(cardLimit)
Expand Down
3 changes: 1 addition & 2 deletions exporter/otelarrowexporter/otelarrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,7 @@ func TestSendArrowFailedTraces(t *testing.T) {
// Send two trace items.
td := testdata.GenerateTraces(2)
err = exp.ConsumeTraces(context.Background(), td)
assert.Error(t, err)
assert.Contains(t, err.Error(), "test failed")
assert.ErrorContains(t, err, "test failed")

// Wait until it is received.
assert.Eventually(t, func() bool {
Expand Down
4 changes: 2 additions & 2 deletions exporter/signalfxexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestConsumeMetrics(t *testing.T) {
assert.Error(t, err)
assert.True(t, consumererror.IsPermanent(err))
assert.True(t, strings.HasPrefix(err.Error(), tt.expectedErrorMsg))
assert.Contains(t, err.Error(), "response content")
assert.ErrorContains(t, err, "response content")
return
}

Expand Down Expand Up @@ -1843,7 +1843,7 @@ func TestConsumeMixedMetrics(t *testing.T) {
assert.Error(t, err)
assert.True(t, consumererror.IsPermanent(err))
assert.True(t, strings.HasPrefix(err.Error(), tt.expectedErrorMsg))
assert.Contains(t, err.Error(), "response content")
assert.ErrorContains(t, err, "response content")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestTrackerStart(t *testing.T) {
if tt.wantErr {
require.Error(t, err)
if tt.errMsg != "" {
require.Contains(t, err.Error(), tt.errMsg)
require.ErrorContains(t, err, tt.errMsg)
}
} else {
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions exporter/splunkhecexporter/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,17 +1608,17 @@ func Test_pushLogData_ShouldAddResponseTo400Error(t *testing.T) {
// Sending logs using the client.
err := splunkClient.pushLogData(context.Background(), logs)
require.True(t, consumererror.IsPermanent(err), "Expecting permanent error")
require.Contains(t, err.Error(), "HTTP/0.0 400")
require.ErrorContains(t, err, "HTTP/0.0 400")
// The returned error should contain the response body responseBody.
assert.Contains(t, err.Error(), responseBody)
assert.ErrorContains(t, err, responseBody)

// An HTTP client that returns some other status code other than 400 and response body responseBody.
httpClient, _ = newTestClient(500, responseBody)
splunkClient.hecWorker = &defaultHecWorker{url, httpClient, buildHTTPHeaders(config, component.NewDefaultBuildInfo()), zap.NewNop()}
// Sending logs using the client.
err = splunkClient.pushLogData(context.Background(), logs)
require.False(t, consumererror.IsPermanent(err), "Expecting non-permanent error")
require.Contains(t, err.Error(), "HTTP 500")
require.ErrorContains(t, err, "HTTP 500")
// The returned error should not contain the response body responseBody.
assert.NotContains(t, err.Error(), responseBody)
}
Expand Down Expand Up @@ -1953,7 +1953,7 @@ func Test_pushLogData_Small_MaxContentLength(t *testing.T) {
require.Error(t, err)

assert.True(t, consumererror.IsPermanent(err))
assert.Contains(t, err.Error(), "dropped log event")
assert.ErrorContains(t, err, "dropped log event")
}
}

Expand Down
7 changes: 3 additions & 4 deletions extension/oauth2clientauthextension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func TestOAuthClientSettings(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
rc, err := newClientAuthenticator(test.settings, zap.NewNop())
if test.shouldError {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.expectedError)
assert.ErrorContains(t, err, test.expectedError)
return
}
assert.NoError(t, err)
Expand Down Expand Up @@ -296,7 +295,7 @@ func TestFailContactingOAuth(t *testing.T) {

_, err = credential.GetRequestMetadata(context.Background())
assert.ErrorIs(t, err, errFailedToGetSecurityToken)
assert.Contains(t, err.Error(), serverURL.String())
assert.ErrorContains(t, err, serverURL.String())

transport := http.DefaultTransport.(*http.Transport).Clone()
baseRoundTripper := (http.RoundTripper)(transport)
Expand All @@ -311,5 +310,5 @@ func TestFailContactingOAuth(t *testing.T) {
require.NoError(t, err)
_, err = client.Do(req)
assert.ErrorIs(t, err, errFailedToGetSecurityToken)
assert.Contains(t, err.Error(), serverURL.String())
assert.ErrorContains(t, err, serverURL.String())
}
3 changes: 1 addition & 2 deletions extension/observer/dockerobserver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ func TestApiVersionCustomError(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
err := sub.Unmarshal(cfg)
require.Error(t, err)
assert.Contains(t, err.Error(),
assert.ErrorContains(t, err,
`Hint: You may want to wrap the 'api_version' value in quotes (api_version: "1.40")`,
)

Expand Down
4 changes: 2 additions & 2 deletions extension/observer/ecsobserver/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestTask_PrivateIP(t *testing.T) {
assert.Equal(t, mode, errPINF.NetworkMode)
// doing contains on error message is not good, but this line increase test coverage from 93% to 98%
// not sure how the average coverage is calculated ...
assert.Contains(t, err.Error(), mode)
assert.ErrorContains(t, err, mode)
}
})
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestTask_MappedPort(t *testing.T) {
errMPNF := &errMappedPortNotFound{}
require.ErrorAs(t, err, &errMPNF)
assert.Equal(t, mode, errMPNF.NetworkMode)
assert.Contains(t, err.Error(), mode) // for coverage
assert.ErrorContains(t, err, mode) // for coverage
}
})
}
Expand Down
6 changes: 2 additions & 4 deletions internal/aws/proxy/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ func TestLoadEnvConfigCreds(t *testing.T) {
assert.Equal(t, cases.Val, value, "Expect the credentials value to match")

_, err = newAWSSession("ROLEARN", "TEST", zap.NewNop())
assert.Error(t, err, "expected error")
assert.Contains(t, err.Error(), "unable to handle AWS error", "expected error message")
assert.ErrorContains(t, err, "unable to handle AWS error", "expected error message")
}

func TestGetProxyUrlProxyAddressNotValid(t *testing.T) {
Expand Down Expand Up @@ -339,8 +338,7 @@ func TestProxyServerTransportInvalidProxyAddr(t *testing.T) {
_, err := proxyServerTransport(&Config{
ProxyAddress: "invalid\n",
})
assert.Error(t, err, "expected error")
assert.Contains(t, err.Error(), "invalid control character in URL")
assert.ErrorContains(t, err, "invalid control character in URL")
}

func TestProxyServerTransportHappyCase(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions internal/aws/proxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestCantGetServiceEndpoint(t *testing.T) {

_, err := NewServer(cfg, logger)
assert.Error(t, err, "NewServer should fail")
assert.Contains(t, err.Error(), "invalid region")
assert.ErrorContains(t, err, "invalid region")
}

func TestAWSEndpointInvalid(t *testing.T) {
Expand All @@ -222,7 +222,7 @@ func TestAWSEndpointInvalid(t *testing.T) {

_, err := NewServer(cfg, logger)
assert.Error(t, err, "NewServer should fail")
assert.Contains(t, err.Error(), "unable to parse AWS service endpoint")
assert.ErrorContains(t, err, "unable to parse AWS service endpoint")
}

func TestCanCreateTransport(t *testing.T) {
Expand All @@ -237,7 +237,7 @@ func TestCanCreateTransport(t *testing.T) {

_, err := NewServer(cfg, logger)
assert.Error(t, err, "NewServer should fail")
assert.Contains(t, err.Error(), "failed to parse proxy URL")
assert.ErrorContains(t, err, "failed to parse proxy URL")
}

func TestGetServiceEndpointInvalidAWSConfig(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/coreinternal/consumerretry/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ func TestConsumeLogs_ContextDeadline(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()
err := consumer.ConsumeLogs(ctx, testdata.GenerateLogsTwoLogRecordsSameResource())
assert.Error(t, err)
assert.Contains(t, err.Error(), "context is cancelled or timed out retry later")
assert.ErrorContains(t, err, "context is cancelled or timed out retry later")
}

func TestConsumeLogs_PartialRetry(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/coreinternal/timeutils/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (

func TestParseGoTimeBadLocation(t *testing.T) {
_, err := ParseGotime(time.RFC822, "02 Jan 06 15:04 BST", time.UTC)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to load location BST")
require.ErrorContains(t, err, "failed to load location BST")
}

func Test_setTimestampYear(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions internal/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func TestWatchingTimeouts(t *testing.T) {
shouldHaveTaken := time.Now().Add(100 * time.Millisecond).UnixNano()

err = cli.LoadContainerList(context.Background())
require.Error(t, err)
assert.Contains(t, err.Error(), expectedError)
assert.ErrorContains(t, err, expectedError)
observed, logs := observer.New(zapcore.WarnLevel)
cli, err = NewDockerClient(config, zap.New(observed))
assert.NotNil(t, cli)
Expand Down Expand Up @@ -120,9 +119,8 @@ func TestFetchingTimeouts(t *testing.T) {
)

assert.Nil(t, statsJSON)
require.Error(t, err)

assert.Contains(t, err.Error(), expectedError)
assert.ErrorContains(t, err, expectedError)

assert.Len(t, logs.All(), 1)
for _, l := range logs.All() {
Expand Down
3 changes: 1 addition & 2 deletions internal/kafka/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ func TestAuthentication(t *testing.T) {
config := &sarama.Config{}
err := ConfigureAuthentication(test.auth, config)
if test.err != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.err)
assert.ErrorContains(t, err, test.err)
} else {
// equalizes SCRAMClientGeneratorFunc to do assertion with the same reference.
config.Net.SASL.SCRAMClientGeneratorFunc = test.saramaConfig.Net.SASL.SCRAMClientGeneratorFunc
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/ottlfuncs/func_replace_all_patterns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,5 +627,5 @@ func Test_replaceAllPatterns_invalid_model(t *testing.T) {
invalidMode := "invalid"
exprFunc, err := replaceAllPatterns[any](target, invalidMode, "regex", replacement, function, replacementFormat)
assert.Nil(t, exprFunc)
assert.Contains(t, err.Error(), "invalid mode")
assert.ErrorContains(t, err, "invalid mode")
}
2 changes: 1 addition & 1 deletion pkg/sampling/oteltracestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestOpenTelemetryTraceStateRValuePValue(t *testing.T) {
require.Equal(t, "", otts.RValue())

// The error is oblivious to the old r-value, but that's ok.
require.Contains(t, err.Error(), "14 hex digits")
require.ErrorContains(t, err, "14 hex digits")

require.Equal(t, []KV{{"p", "2"}}, otts.ExtraValues())

Expand Down
6 changes: 2 additions & 4 deletions pkg/stanza/entry/attribute_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,11 @@ func TestAttributeFieldUnmarshalFailure(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
var fy AttributeField
err := yaml.UnmarshalStrict(tc.invalid, &fy)
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErr)
require.ErrorContains(t, err, tc.expectedErr)

var fj AttributeField
err = json.Unmarshal(tc.invalid, &fj)
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErr)
require.ErrorContains(t, err, tc.expectedErr)
})
}
}
Loading