mirror of
https://github.com/kopia/kopia.git
synced 2025-12-23 22:57:50 -05:00
chore(general): use contexts in tests (#5009)
Ref: - Subset of the changes proposed by @NathanBaulch in #4972
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:")
|
||||
|
||||
@@ -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() })
|
||||
|
||||
@@ -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...)...)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user