diff --git a/.golangci.yml b/.golangci.yml index c12839b260..967f6052fb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -45,8 +45,7 @@ linters: non-tests: files: - '!$test' - - '!**/*test/*.go' - - '!**/testutils/*.go' + - '!**/test/**/*.go' deny: - pkg: testing - pkg: github.com/stretchr/testify @@ -220,7 +219,6 @@ linters: disable: - float-compare - go-require - - require-error exclusions: generated: lax presets: diff --git a/cli/main_test.go b/cli/main_test.go index e42736fc21..736f74bf46 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const ( @@ -51,13 +52,13 @@ func TestFindPID(t *testing.T) { t.Run("PID", func(t *testing.T) { const pid = 4 got, err := findPID(ctx, discardLogger, pid, appPath) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, int(pid), got) }) t.Run("BinPath", func(t *testing.T) { got, err := findPID(ctx, discardLogger, -1, appPath) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, appPathPID, got) got, err = findPID(ctx, discardLogger, -1, missingPath) @@ -68,7 +69,7 @@ func TestFindPID(t *testing.T) { t.Run("OTEL_GO_AUTO_TARGET_PID", func(t *testing.T) { t.Setenv(envTargetPIDKey, "2000") got, err := findPID(ctx, discardLogger, -1, "") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, int(2000), got) t.Setenv(envTargetPIDKey, "invalid") @@ -79,7 +80,7 @@ func TestFindPID(t *testing.T) { t.Run("OTEL_GO_AUTO_TARGET_EXE", func(t *testing.T) { t.Setenv(envTargetExeKey, appPath) got, err := findPID(ctx, discardLogger, -1, "") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, appPathPID, got) }) @@ -89,21 +90,21 @@ func TestFindPID(t *testing.T) { const pid = 4 got, err := findPID(ctx, discardLogger, pid, appPath) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, int(pid), got) got, err = findPID(ctx, discardLogger, -1, appPath) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, appPathPID, got) got, err = findPID(ctx, discardLogger, -1, "") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, 2000, got) os.Unsetenv(envTargetPIDKey) got, err = findPID(ctx, discardLogger, -1, "") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, altPathPID, got) }) } diff --git a/cli/process_poller_test.go b/cli/process_poller_test.go index 676180da74..97f26addee 100644 --- a/cli/process_poller_test.go +++ b/cli/process_poller_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type entry struct { @@ -80,12 +81,12 @@ func TestProcessPollerPoll(t *testing.T) { pp := ProcessPoller{BinPath: appPath} pid, err := pp.Poll(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, appPathPID, pid) pp.BinPath = altPath pid, err = pp.Poll(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, altPathPID, pid) pp.Interval = time.Millisecond @@ -93,6 +94,6 @@ func TestProcessPollerPoll(t *testing.T) { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() pid, err = pp.Poll(ctx) - assert.ErrorIs(t, err, context.DeadlineExceeded) + assert.ErrorIs(t, err, context.DeadlineExceeded) //nolint:testifylint // Continue on failure. assert.Zero(t, pid) } diff --git a/instrumentation_test.go b/instrumentation_test.go index 4c68647251..d52a0a0cdc 100644 --- a/instrumentation_test.go +++ b/instrumentation_test.go @@ -90,7 +90,7 @@ func TestWithSampler(t *testing.T) { c, err := newInstConfig(context.Background(), []InstrumentationOption{}) require.NoError(t, err) sc, err := convertSamplerToConfig(c.sampler) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, sampling.DefaultConfig().Samplers, sc.Samplers) assert.Equal(t, sampling.ParentBasedID, sc.ActiveSampler) conf, ok := sc.Samplers[sampling.ParentBasedID] @@ -110,7 +110,7 @@ func TestWithSampler(t *testing.T) { c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()}) require.NoError(t, err) sc, err := convertSamplerToConfig(c.sampler) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, sampling.ParentBasedID, sc.ActiveSampler) parentBasedConfig, ok := sc.Samplers[sampling.ParentBasedID] assert.True(t, ok) @@ -146,7 +146,7 @@ func TestWithSampler(t *testing.T) { }) require.NoError(t, err) sc, err := convertSamplerToConfig(c.sampler) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, sampling.ParentBasedID, sc.ActiveSampler) parentBasedConfig, ok := sc.Samplers[sampling.ParentBasedID] assert.True(t, ok) diff --git a/internal/pkg/instrumentation/bpf/net/http/client/probe_test.go b/internal/pkg/instrumentation/bpf/net/http/client/probe_test.go index 4a05c6dd56..bda1869c1a 100644 --- a/internal/pkg/instrumentation/bpf/net/http/client/probe_test.go +++ b/internal/pkg/instrumentation/bpf/net/http/client/probe_test.go @@ -8,6 +8,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" semconv "go.opentelemetry.io/otel/semconv/v1.30.0" @@ -66,9 +67,9 @@ func TestConvertEvent(t *testing.T) { copy(rawFragment[:], []byte(rawFragmentString)) spId, err := trace.SpanIDFromHex("00f067aa0ba902b7") - assert.NoError(t, err) + require.NoError(t, err) trId, err := trace.TraceIDFromHex("00f067aa0ba902b700f067aa0ba902b7") - assert.NoError(t, err) + require.NoError(t, err) testCases := []struct { name string diff --git a/internal/pkg/instrumentation/kernel/cpu_linux_test.go b/internal/pkg/instrumentation/kernel/cpu_linux_test.go index 84c8266e2c..554934ab7d 100644 --- a/internal/pkg/instrumentation/kernel/cpu_linux_test.go +++ b/internal/pkg/instrumentation/kernel/cpu_linux_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func setContent(t *testing.T, path, text string) { @@ -23,23 +24,23 @@ func setNotReadable(t *testing.T, path string) { func TestGetCPUCountFromSysDevices(t *testing.T) { noFile, err := os.CreateTemp(t.TempDir(), "not_existent_fake_cpu_present") - assert.NoError(t, err) + require.NoError(t, err) notPath, err := filepath.Abs(noFile.Name()) - assert.NoError(t, err) - assert.NoError(t, noFile.Close()) - assert.NoError(t, os.Remove(noFile.Name())) + require.NoError(t, err) + require.NoError(t, noFile.Close()) + require.NoError(t, os.Remove(noFile.Name())) // Setup for testing file that doesn't exist cpuPresentPath = notPath ncpu, err := getCPUCountFromSysDevices() - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, uint64(0), ncpu) tempFile, err := os.CreateTemp(t.TempDir(), "fake_cpu_present") - assert.NoError(t, err) + require.NoError(t, err) path, err := filepath.Abs(tempFile.Name()) - assert.NoError(t, err) - assert.NoError(t, tempFile.Close()) + require.NoError(t, err) + require.NoError(t, tempFile.Close()) defer os.Remove(tempFile.Name()) // Setup for testing @@ -47,49 +48,49 @@ func TestGetCPUCountFromSysDevices(t *testing.T) { setContent(t, path, "0-7") ncpu, err = getCPUCountFromSysDevices() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(8), ncpu) setContent(t, path, "0-7,10-15") ncpu, err = getCPUCountFromSysDevices() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(14), ncpu) setContent(t, path, "0-7,10-15,20-23") ncpu, err = getCPUCountFromSysDevices() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(18), ncpu) setContent(t, path, "0-") ncpu, err = getCPUCountFromSysDevices() - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, uint64(0), ncpu) setNotReadable(t, path) ncpu, err = getCPUCountFromSysDevices() - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, uint64(0), ncpu) } func TestGetCPUCountFromProc(t *testing.T) { noFile, err := os.CreateTemp(t.TempDir(), "not_existent_fake_cpuinfo") - assert.NoError(t, err) + require.NoError(t, err) notPath, err := filepath.Abs(noFile.Name()) - assert.NoError(t, err) - assert.NoError(t, noFile.Close()) - assert.NoError(t, os.Remove(noFile.Name())) + require.NoError(t, err) + require.NoError(t, noFile.Close()) + require.NoError(t, os.Remove(noFile.Name())) // Setup for testing file that doesn't exist procInfoPath = notPath ncpu, err := getCPUCountFromProc() - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, uint64(0), ncpu) tempFile, err := os.CreateTemp(t.TempDir(), "fake_cpuinfo") - assert.NoError(t, err) + require.NoError(t, err) path, err := filepath.Abs(tempFile.Name()) - assert.NoError(t, err) - assert.NoError(t, tempFile.Close()) + require.NoError(t, err) + require.NoError(t, tempFile.Close()) defer os.Remove(tempFile.Name()) // Setup for testing @@ -97,31 +98,31 @@ func TestGetCPUCountFromProc(t *testing.T) { setContent(t, path, "processor : 0") ncpu, err = getCPUCountFromProc() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(1), ncpu) setContent(t, path, "processor : 0\nprocessor : 1") ncpu, err = getCPUCountFromProc() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(2), ncpu) setContent(t, path, "processor : 0\nprocessor : 1\nprocessor : 2") ncpu, err = getCPUCountFromProc() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(3), ncpu) setContent(t, path, "processor : 0\nprocessor : 1\nprocessor : 2\nprocessor : 3") ncpu, err = getCPUCountFromProc() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(4), ncpu) setContent(t, path, "processor : 0\n some text \nprocessor : 1") ncpu, err = getCPUCountFromProc() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(2), ncpu) setNotReadable(t, path) ncpu, err = getCPUCountFromProc() - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, uint64(0), ncpu) } diff --git a/internal/pkg/instrumentation/kernel/lockdown_linux_test.go b/internal/pkg/instrumentation/kernel/lockdown_linux_test.go index 44960ea3c2..9b1440b691 100644 --- a/internal/pkg/instrumentation/kernel/lockdown_linux_test.go +++ b/internal/pkg/instrumentation/kernel/lockdown_linux_test.go @@ -11,11 +11,12 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGetLockdownMode(t *testing.T) { noFile, err := os.CreateTemp(t.TempDir(), "not_existent_fake_lockdown") - assert.NoError(t, err) + require.NoError(t, err) notPath, err := filepath.Abs(noFile.Name()) assert.NoError(t, err) assert.NoError(t, noFile.Close()) @@ -26,7 +27,7 @@ func TestGetLockdownMode(t *testing.T) { assert.Equal(t, LockdownModeNone, getLockdownMode()) tempFile, err := os.CreateTemp(t.TempDir(), "fake_lockdown") - assert.NoError(t, err) + require.NoError(t, err) path, err := filepath.Abs(tempFile.Name()) assert.NoError(t, err) assert.NoError(t, tempFile.Close()) diff --git a/internal/pkg/instrumentation/manager_load_test.go b/internal/pkg/instrumentation/manager_load_test.go index 9b7c7edc86..bd19de4e14 100644 --- a/internal/pkg/instrumentation/manager_load_test.go +++ b/internal/pkg/instrumentation/manager_load_test.go @@ -43,9 +43,7 @@ func TestLoadProbes(t *testing.T) { pid := process.ID(id) info, err := process.NewInfo(pid, make(map[string]interface{})) - if info == nil { - t.Fatalf("failed to create process.Info: %v", err) - } + require.NotNil(t, info, "failed to create process.Info: %v", err) // Reset Info module information. info.Modules = make(map[string]*semver.Version) @@ -110,31 +108,27 @@ func setupTestModule(t *testing.T) int { // Initialize a Go module cmd := exec.Command("go", "mod", "init", "example.com/testmodule") cmd.Dir = tempDir - if err := cmd.Run(); err != nil { - t.Fatalf("failed to initialize Go module: %v", err) - } + require.NoError(t, cmd.Run(), "failed to initialize Go module") mainGoPath := filepath.Join(tempDir, "main.go") - if err := os.WriteFile(mainGoPath, []byte(mainGoContent), 0o600); err != nil { - t.Fatalf("failed to write main.go: %v", err) - } + require.NoError( + t, + os.WriteFile(mainGoPath, []byte(mainGoContent), 0o600), + "failed to write main.go", + ) // Compile the Go program binaryPath := filepath.Join(tempDir, "testbinary") cmd = exec.Command("go", "build", "-o", binaryPath, mainGoPath) cmd.Dir = tempDir - if err := cmd.Run(); err != nil { - t.Fatalf("failed to compile binary: %v", err) - } + require.NoError(t, cmd.Run(), "failed to compile binary") // Run the compiled binary cmd = exec.Command(binaryPath) cmd.Dir = tempDir cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - if err := cmd.Start(); err != nil { - t.Fatalf("failed to start binary: %v", err) - } + require.NoError(t, cmd.Start(), "failed to start binary") // Ensure the process is killed when the test ends t.Cleanup(func() { @@ -177,7 +171,7 @@ func ProbesLoad(t *testing.T, info *process.Info, p TestProbe) { } c, err := ebpf.NewCollectionWithOptions(spec, opts) - if !assert.NoError(t, err) { + if !assert.NoError(t, err) { //nolint:testifylint // Cannot use require here var ve *ebpf.VerifierError if errors.As(err, &ve) && v { t.Logf("Verifier log: %-100v\n", ve) diff --git a/internal/pkg/instrumentation/manager_test.go b/internal/pkg/instrumentation/manager_test.go index f97b542b81..e42c597865 100644 --- a/internal/pkg/instrumentation/manager_test.go +++ b/internal/pkg/instrumentation/manager_test.go @@ -307,7 +307,7 @@ func TestRunStoppingByStop(t *testing.T) { return false } }, time.Second, 10*time.Millisecond) - assert.NoError(t, err) + require.NoError(t, err) assert.True(t, p.closed.Load(), "Probe not closed") } diff --git a/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go b/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go index f36d72ed88..b9e0d65575 100644 --- a/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go +++ b/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 //go:build ebpf_test + package sampling import ( diff --git a/internal/pkg/process/id_test.go b/internal/pkg/process/id_test.go index c4d0ca26ea..fd61cc64a9 100644 --- a/internal/pkg/process/id_test.go +++ b/internal/pkg/process/id_test.go @@ -80,7 +80,7 @@ func TestIDValidate(t *testing.T) { } err := ID(1).Validate() - assert.ErrorIs(t, err, errNoID) + assert.ErrorIs(t, err, errNoID) //nolint:testifylint // Continue on failure. assert.ErrorIs(t, err, assert.AnError, "original error dropped") }) @@ -99,7 +99,7 @@ func TestIDValidate(t *testing.T) { }(sig)) err := ID(1).Validate() - assert.ErrorIs(t, err, errNoRunID) + assert.ErrorIs(t, err, errNoRunID) //nolint:testifylint // Continue on failure. assert.ErrorIs(t, err, assert.AnError, "original error dropped") }) diff --git a/internal/pkg/process/info_test.go b/internal/pkg/process/info_test.go index ca4e0f5ac7..035c26ec85 100644 --- a/internal/pkg/process/info_test.go +++ b/internal/pkg/process/info_test.go @@ -86,7 +86,7 @@ func TestInfoAlloc(t *testing.T) { wg.Wait() a, err := i.Alloc(logger) - assert.ErrorIs(t, err, assert.AnError) + assert.ErrorIs(t, err, assert.AnError) //nolint:testifylint // Continue on failure. assert.Equal(t, uint64(goroutines+1), a.StartAddr, "expected increment per error response") }) @@ -106,7 +106,7 @@ func TestInfoAlloc(t *testing.T) { wg.Wait() a, err := i.Alloc(logger) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, uint64(1), a.StartAddr, "allocate not called once") }) } diff --git a/internal/test/e2e/integration.go b/internal/test/e2e/integration.go index 2f2166cb11..5dadeca767 100644 --- a/internal/test/e2e/integration.go +++ b/internal/test/e2e/integration.go @@ -17,9 +17,10 @@ import ( "path/filepath" "runtime" "syscall" - "testing" // nolint:depguard // This is a testing utility package. + "testing" "github.com/cilium/ebpf/rlimit" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/ptrace" "go.opentelemetry.io/auto" @@ -68,9 +69,7 @@ func compile(t *testing.T, ctx context.Context, pkgPath string) string { cmd.Dir = pkgPath cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - t.Fatalf("Failed to compile binary: %v", err) - } + require.NoError(t, cmd.Run(), "Failed to compile binary") return binaryPath } @@ -127,9 +126,7 @@ func run(t *testing.T, ctx context.Context, binPath string, endpoint string) { cmd.Stderr = os.Stderr t.Log("Starting target") - if err := cmd.Start(); err != nil { - t.Fatalf("Failed to start target: %v", err) - } + require.NoError(t, cmd.Start(), "Failed to start target") t.Setenv("OTEL_SERVICE_NAME", "sample-app") t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint) @@ -144,14 +141,10 @@ func run(t *testing.T, ctx context.Context, binPath string, endpoint string) { auto.WithLogger(NewTestLogger(t)), auto.WithEnv(), ) - if err != nil { - t.Fatalf("Failed to create auto-instrumentation: %v", err) - } + require.NoError(t, err, "Failed to create auto-instrumentation") t.Log("Loading") - if err = inst.Load(ctx); err != nil { - t.Fatalf("Failed to load auto-instrumentation: %v", err) - } + require.NoError(t, inst.Load(ctx), "Failed to load auto-instrumentation") t.Log("Running") runCh := make(chan error, 1) @@ -162,9 +155,7 @@ func run(t *testing.T, ctx context.Context, binPath string, endpoint string) { var sig os.Signal = syscall.SIGCONT t.Log("Sending signal to target") - if err := cmd.Process.Signal(sig); err != nil { - t.Fatalf("Failed to send signal to target: %v", err) - } + require.NoError(t, cmd.Process.Signal(sig), "Failed to send signal to target") t.Log("Sent signal to target") doneCh := make(chan error, 1) @@ -176,23 +167,17 @@ func run(t *testing.T, ctx context.Context, binPath string, endpoint string) { case <-ctx.Done(): t.Fatal("Context ended") case err := <-runCh: - if err != nil { - t.Fatalf("Failed to run: %v", err) - } + require.NoError(t, err, "Failed to run") // Do not return. Wait for doneCh. case <-doneCh: - if err != nil { - t.Fatalf("Application failed: %v", err) - } + require.NoError(t, err, "Application failed") return } } }() t.Log("Closing instrumentation") - if err := inst.Close(); err != nil { - t.Fatalf("Failed to close auto-instrumentation: %v", err) - } + require.NoError(t, inst.Close(), "Failed to close auto-instrumentation") } // testLogger is an slog.Handler that logs to testing.T. diff --git a/internal/test/e2e/verify.go b/internal/test/e2e/verify.go index 4eb8847bc3..84f6515dbd 100644 --- a/internal/test/e2e/verify.go +++ b/internal/test/e2e/verify.go @@ -8,9 +8,9 @@ import ( "errors" "fmt" "regexp" - "testing" // nolint:depguard // This is a testing utility package. + "testing" - "github.com/stretchr/testify/assert" // nolint:depguard // This is a testing utility package. + "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/ptrace" ) diff --git a/pipeline/otelsdk/handler_test.go b/pipeline/otelsdk/handler_test.go index f4c3f0678d..e8e8f5a184 100644 --- a/pipeline/otelsdk/handler_test.go +++ b/pipeline/otelsdk/handler_test.go @@ -262,7 +262,7 @@ func TestTraceHandlerShutdown(t *testing.T) { func TestControllerTraceConcurrentSafe(t *testing.T) { handler, err := NewTraceHandler(context.Background()) - assert.NoError(t, err) + require.NoError(t, err) const goroutines = 10 diff --git a/version_test.go b/version_test.go index 1c4e277441..7e1ef15989 100644 --- a/version_test.go +++ b/version_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" ) @@ -24,18 +25,12 @@ func TestVersionSemver(t *testing.T) { func TestVersionMatchesYaml(t *testing.T) { versionYaml, err := os.ReadFile("versions.yaml") - if err != nil { - t.Fatalf("Couldn't read versions.yaml file: %e", err) - return - } + require.NoError(t, err, "Couldn't read versions.yaml file") var versionInfo map[string]interface{} err = yaml.Unmarshal(versionYaml, &versionInfo) - if err != nil { - t.Fatalf("Couldn't parse version.yaml: %e", err) - return - } + require.NoError(t, err, "Couldn't parse version.yaml") // incredibad, but it's where the intended version is declared at the moment expectedVersion := versionInfo["module-sets"].(map[string]interface{})["auto"].(map[string]interface{})["version"]