From 7586b21b1f578bb171c0134affa9b90227d341b6 Mon Sep 17 00:00:00 2001 From: Julio Lopez <1953782+julio-lopez@users.noreply.github.com> Date: Tue, 18 Nov 2025 17:47:23 -0800 Subject: [PATCH] chore(general): use contexts in tests (#5009) Ref: - Subset of the changes proposed by @NathanBaulch in #4972 --- internal/editor/editor.go | 2 +- repo/blob/rclone/rclone_storage_test.go | 8 +++++--- repo/blob/sftp/sftp_storage_test.go | 7 +++++-- tests/recovery/recovery_test/recovery_test.go | 12 ++++++++---- tests/socketactivation_test/socketactivation_test.go | 9 +++++++-- tests/testenv/cli_exe_runner.go | 2 +- tests/tools/fio/fio.go | 3 ++- 7 files changed, 29 insertions(+), 14 deletions(-) diff --git a/internal/editor/editor.go b/internal/editor/editor.go index 2870ce72a..74ec75685 100644 --- a/internal/editor/editor.go +++ b/internal/editor/editor.go @@ -105,7 +105,7 @@ func readAndStripComments(fname string, withComments bool) (string, error) { args = append(args, editorArgs...) args = append(args, file) - cmd := exec.Command(editor, args...) //nolint:gosec + cmd := exec.CommandContext(ctx, editor, args...) //nolint:gosec cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout diff --git a/repo/blob/rclone/rclone_storage_test.go b/repo/blob/rclone/rclone_storage_test.go index d9e063694..7bb6ae79c 100644 --- a/repo/blob/rclone/rclone_storage_test.go +++ b/repo/blob/rclone/rclone_storage_test.go @@ -47,7 +47,8 @@ func mustGetRcloneExeOrSkip(t *testing.T) string { rcloneExe = "rclone" } - if err := exec.Command(rcloneExe, "version").Run(); err != nil { + ctx := testlogging.Context(t) + if err := exec.CommandContext(ctx, rcloneExe, "version").Run(); err != nil { if os.Getenv("CI") == "" { t.Skipf("rclone not installed: %v", err) } else { @@ -326,7 +327,8 @@ func cleanupOldData(t *testing.T, rcloneExe, remotePath string) { } } - c := exec.Command(rcloneExe, "--config", configFile, "lsjson", remotePath) + ctx := testlogging.Context(t) + c := exec.CommandContext(ctx, rcloneExe, "--config", configFile, "lsjson", remotePath) b, err := c.Output() require.NoError(t, err) @@ -347,7 +349,7 @@ func cleanupOldData(t *testing.T, rcloneExe, remotePath string) { if age > cleanupAge { t.Logf("purging: %v %v", e.Name, age) - if err := exec.Command(rcloneExe, "--config", configFile, "purge", remotePath+"/"+e.Name).Run(); err != nil { + if err := exec.CommandContext(ctx, rcloneExe, "--config", configFile, "purge", remotePath+"/"+e.Name).Run(); err != nil { t.Logf("error purging %v: %v", e.Name, err) } } diff --git a/repo/blob/sftp/sftp_storage_test.go b/repo/blob/sftp/sftp_storage_test.go index df924c385..002a13aaa 100644 --- a/repo/blob/sftp/sftp_storage_test.go +++ b/repo/blob/sftp/sftp_storage_test.go @@ -58,7 +58,8 @@ func runAndGetOutput(t *testing.T, cmd string, args ...string) ([]byte, error) { var stderr bytes.Buffer - c := exec.Command(cmd, args...) + ctx := testlogging.Context(t) + c := exec.CommandContext(ctx, cmd, args...) c.Stderr = &stderr o, err := c.Output() @@ -102,12 +103,14 @@ func startDockerSFTPServerOrSkip(t *testing.T, idRSA string) (host string, port sftpUsernameWithPasswordAuth+":"+sftpUserPassword+":::upload2") sftpEndpoint := testutil.GetContainerMappedPortAddress(t, shortContainerID, "22") + ctx := testlogging.Context(t) + // wait for SFTP server to come up. deadline := clock.Now().Add(dialTimeout) for clock.Now().Before(deadline) { t.Logf("waiting for SFTP server to come up on '%v'...", sftpEndpoint) - conn, err := net.DialTimeout("tcp", sftpEndpoint, time.Second) + conn, err := (&net.Dialer{Timeout: time.Second}).DialContext(ctx, "tcp", sftpEndpoint) if err != nil { t.Logf("err: %v", err) time.Sleep(time.Second) diff --git a/tests/recovery/recovery_test/recovery_test.go b/tests/recovery/recovery_test/recovery_test.go index 8897aeee8..3820e2992 100644 --- a/tests/recovery/recovery_test/recovery_test.go +++ b/tests/recovery/recovery_test/recovery_test.go @@ -20,6 +20,7 @@ "github.com/kopia/kopia/fs/localfs" "github.com/kopia/kopia/internal/diff" + "github.com/kopia/kopia/internal/testlogging" "github.com/kopia/kopia/tests/recovery/blobmanipulator" "github.com/kopia/kopia/tests/testenv" "github.com/kopia/kopia/tests/tools/kopiarunner" @@ -53,8 +54,9 @@ func TestSnapshotFix(t *testing.T) { t.FailNow() } + ctx := testlogging.Context(t) kopiaExe := os.Getenv("KOPIA_EXE") - cmd := exec.Command(kopiaExe, "maintenance", "run", "--full", "--force", "--safety", "none") + cmd := exec.CommandContext(ctx, kopiaExe, "maintenance", "run", "--full", "--force", "--safety", "none") err = cmd.Start() if err != nil { @@ -131,8 +133,9 @@ func TestSnapshotFixInvalidFiles(t *testing.T) { t.FailNow() } + ctx := testlogging.Context(t) kopiaExe := os.Getenv("KOPIA_EXE") - cmd := exec.Command(kopiaExe, "maintenance", "run", "--full", "--force", "--safety", "none") + cmd := exec.CommandContext(ctx, kopiaExe, "maintenance", "run", "--full", "--force", "--safety", "none") err = cmd.Start() if err != nil { @@ -207,11 +210,12 @@ func TestConsistencyWhenKill9AfterModify(t *testing.T) { require.NoError(t, err) newDir := bm.PathToTakeSnapshot + ctx := testlogging.Context(t) // connect with repository with the environment configuration, otherwise it will display "ERROR open repository: repository is not connected.kopia connect repo". kopiaExe := os.Getenv("KOPIA_EXE") - cmd := exec.Command(kopiaExe, "repo", "connect", "filesystem", "--path="+dataRepoPath, "--content-cache-size-mb", "500", "--metadata-cache-size-mb", "500", "--no-check-for-updates") + cmd := exec.CommandContext(ctx, kopiaExe, "repo", "connect", "filesystem", "--path="+dataRepoPath, "--content-cache-size-mb", "500", "--metadata-cache-size-mb", "500", "--no-check-for-updates") env := []string{"KOPIA_PASSWORD=" + testenv.TestRepoPassword} cmd.Env = append(os.Environ(), env...) @@ -220,7 +224,7 @@ func TestConsistencyWhenKill9AfterModify(t *testing.T) { t.Log(string(o)) // create snapshot with StderrPipe - cmd = exec.Command(kopiaExe, "snap", "create", newDir, "--json", "--parallel=1") + cmd = exec.CommandContext(ctx, kopiaExe, "snap", "create", newDir, "--json", "--parallel=1") // kill the kopia command before it exits t.Logf("Kill the kopia command before it exits:") diff --git a/tests/socketactivation_test/socketactivation_test.go b/tests/socketactivation_test/socketactivation_test.go index 3032da5f0..b662453f5 100644 --- a/tests/socketactivation_test/socketactivation_test.go +++ b/tests/socketactivation_test/socketactivation_test.go @@ -14,6 +14,7 @@ "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/kopia/kopia/internal/testlogging" "github.com/kopia/kopia/internal/testutil" "github.com/kopia/kopia/tests/testenv" ) @@ -37,7 +38,9 @@ func TestServerControlSocketActivated(t *testing.T) { // The KOPIA_EXE wrapper will set the LISTEN_PID variable for us env.Environment["LISTEN_FDS"] = "1" - l1, err := net.Listen("tcp", ":0") + ctx := testlogging.Context(t) + l1, err := (&net.ListenConfig{}).Listen(ctx, "tcp", ":0") + require.NoError(t, err, "Failed to open Listener") t.Cleanup(func() { l1.Close() }) @@ -109,7 +112,9 @@ func TestServerControlSocketActivatedTooManyFDs(t *testing.T) { env.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", env.RepoDir, "--override-username=another-user", "--override-hostname=another-host") // create 2 file descriptor for a single socket and pass the descriptors to the server - l1, err := net.Listen("tcp", ":0") + ctx := testlogging.Context(t) + l1, err := (&net.ListenConfig{}).Listen(ctx, "tcp", ":0") + require.NoError(t, err, "Failed to open Listener") t.Cleanup(func() { l1.Close() }) diff --git a/tests/testenv/cli_exe_runner.go b/tests/testenv/cli_exe_runner.go index 9c078d759..b1cc73ea8 100644 --- a/tests/testenv/cli_exe_runner.go +++ b/tests/testenv/cli_exe_runner.go @@ -24,7 +24,7 @@ type CLIExeRunner struct { func (e *CLIExeRunner) Start(tb testing.TB, ctx context.Context, args []string, env map[string]string) (stdout, stderr io.Reader, wait func() error, interrupt func(os.Signal)) { tb.Helper() - c := exec.Command(e.Exe, append([]string{ + c := exec.CommandContext(ctx, e.Exe, append([]string{ "--log-dir", e.LogsDir, }, args...)...) diff --git a/tests/tools/fio/fio.go b/tests/tools/fio/fio.go index 4e8cdc12c..7df348a7d 100644 --- a/tests/tools/fio/fio.go +++ b/tests/tools/fio/fio.go @@ -6,6 +6,7 @@ import ( "bytes" + "context" "fmt" "log" "math/rand" @@ -260,7 +261,7 @@ func (fr *Runner) Run(args ...string) (stdout, stderr string, err error) { log.Printf("running '%s %v'", fr.Exe, argsStr) } - c := exec.Command(fr.Exe, args...) + c := exec.CommandContext(context.Background(), fr.Exe, args...) errOut := &bytes.Buffer{} c.Stderr = errOut