diff --git a/pkg/cmd/cli/backup/delete_test.go b/pkg/cmd/cli/backup/delete_test.go index 85718541c..3278e8153 100644 --- a/pkg/cmd/cli/backup/delete_test.go +++ b/pkg/cmd/cli/backup/delete_test.go @@ -75,7 +75,7 @@ func TestDeleteCommand(t *testing.T) { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestDeleteCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestDeleteCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err := veleroexec.RunCommand(cmd) if err != nil { diff --git a/pkg/cmd/cli/backup/describe_test.go b/pkg/cmd/cli/backup/describe_test.go index 7ca12ae3e..5d5486473 100644 --- a/pkg/cmd/cli/backup/describe_test.go +++ b/pkg/cmd/cli/backup/describe_test.go @@ -63,7 +63,7 @@ func TestNewDescribeCommand(t *testing.T) { if os.Getenv(cmdtest.CaptureFlag) == "1" { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewDescribeCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewDescribeCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err := veleroexec.RunCommand(cmd) diff --git a/pkg/cmd/cli/backup/download_test.go b/pkg/cmd/cli/backup/download_test.go index aaada0db3..999799ff3 100644 --- a/pkg/cmd/cli/backup/download_test.go +++ b/pkg/cmd/cli/backup/download_test.go @@ -91,7 +91,7 @@ func TestNewDownloadCommand(t *testing.T) { assert.NoError(t, e) return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewDownloadCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewDownloadCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) _, stderr, err := veleroexec.RunCommand(cmd) diff --git a/pkg/cmd/cli/backup/get_test.go b/pkg/cmd/cli/backup/get_test.go index 511c33f51..28c41a6de 100644 --- a/pkg/cmd/cli/backup/get_test.go +++ b/pkg/cmd/cli/backup/get_test.go @@ -63,7 +63,7 @@ func TestNewGetCommand(t *testing.T) { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewGetCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err := veleroexec.RunCommand(cmd) require.NoError(t, err) @@ -84,7 +84,7 @@ func TestNewGetCommand(t *testing.T) { e = d.Execute() require.NoError(t, e) - cmd = exec.Command(os.Args[0], []string{"-test.run=TestNewGetCommand"}...) + cmd = exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err = veleroexec.RunCommand(cmd) require.NoError(t, err) diff --git a/pkg/cmd/cli/backuplocation/delete_test.go b/pkg/cmd/cli/backuplocation/delete_test.go index cbc09c514..70ff7568b 100644 --- a/pkg/cmd/cli/backuplocation/delete_test.go +++ b/pkg/cmd/cli/backuplocation/delete_test.go @@ -66,7 +66,7 @@ func TestNewDeleteCommand(t *testing.T) { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewDeleteCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewDeleteCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err := veleroexec.RunCommand(cmd) diff --git a/pkg/cmd/cli/backuplocation/get_test.go b/pkg/cmd/cli/backuplocation/get_test.go index 2e4c5510c..d45231fce 100644 --- a/pkg/cmd/cli/backuplocation/get_test.go +++ b/pkg/cmd/cli/backuplocation/get_test.go @@ -50,7 +50,7 @@ func TestNewGetCommand(t *testing.T) { c.Execute() return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewGetCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) _, stderr, err := veleroexec.RunCommand(cmd) diff --git a/pkg/cmd/cli/backuplocation/set_test.go b/pkg/cmd/cli/backuplocation/set_test.go index 4b35693af..0ebf8a8d3 100644 --- a/pkg/cmd/cli/backuplocation/set_test.go +++ b/pkg/cmd/cli/backuplocation/set_test.go @@ -99,7 +99,7 @@ func TestSetCommand_Execute(t *testing.T) { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestSetCommand_Execute"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestSetCommand_Execute"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) _, stderr, err := veleroexec.RunCommand(cmd) diff --git a/pkg/cmd/cli/bug/bug.go b/pkg/cmd/cli/bug/bug.go index 9f87e3f5a..33fbb38b7 100644 --- a/pkg/cmd/cli/bug/bug.go +++ b/pkg/cmd/cli/bug/bug.go @@ -18,6 +18,7 @@ package bug import ( "bytes" + "context" "errors" "fmt" "net/url" @@ -147,7 +148,7 @@ func getKubectlVersion() (string, error) { return "", errors.New("kubectl not found on PATH") } - kubectlCmd := exec.Command("kubectl", "version") + kubectlCmd := exec.CommandContext(context.Background(), "kubectl", "version") var outbuf bytes.Buffer kubectlCmd.Stdout = &outbuf if err := kubectlCmd.Start(); err != nil { @@ -207,16 +208,17 @@ func renderToString(bugInfo *VeleroBugInfo) (string, error) { // a platform specific binary. func showIssueInBrowser(body string) error { url := issueURL + "?body=" + url.QueryEscape(body) + ctx := context.Background() switch runtime.GOOS { case "darwin": - return exec.Command("open", url).Start() + return exec.CommandContext(ctx, "open", url).Start() case "linux": if cmdExistsOnPath("xdg-open") { - return exec.Command("xdg-open", url).Start() + return exec.CommandContext(ctx, "xdg-open", url).Start() } return fmt.Errorf("velero can't open a browser window using the command '%s'", "xdg-open") case "windows": - return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + return exec.CommandContext(ctx, "rundll32", "url.dll,FileProtocolHandler", url).Start() default: return fmt.Errorf("velero can't open a browser window on platform %s", runtime.GOOS) } diff --git a/pkg/cmd/cli/restore/delete_test.go b/pkg/cmd/cli/restore/delete_test.go index 9085e4cd9..e98f9b804 100644 --- a/pkg/cmd/cli/restore/delete_test.go +++ b/pkg/cmd/cli/restore/delete_test.go @@ -75,7 +75,7 @@ func TestDeleteCommand(t *testing.T) { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestDeleteCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestDeleteCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err := veleroexec.RunCommand(cmd) if err != nil { diff --git a/pkg/cmd/cli/restore/describe_test.go b/pkg/cmd/cli/restore/describe_test.go index 717fe2b7c..7c2ffe5f8 100644 --- a/pkg/cmd/cli/restore/describe_test.go +++ b/pkg/cmd/cli/restore/describe_test.go @@ -63,7 +63,7 @@ func TestNewDescribeCommand(t *testing.T) { if os.Getenv(cmdtest.CaptureFlag) == "1" { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewDescribeCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewDescribeCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err := veleroexec.RunCommand(cmd) diff --git a/pkg/cmd/cli/restore/get_test.go b/pkg/cmd/cli/restore/get_test.go index df2c25488..22b10e152 100644 --- a/pkg/cmd/cli/restore/get_test.go +++ b/pkg/cmd/cli/restore/get_test.go @@ -62,7 +62,7 @@ func TestNewGetCommand(t *testing.T) { return } - cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewGetCommand"}...) + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand"}...) cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) stdout, _, err := veleroexec.RunCommand(cmd) require.NoError(t, err) diff --git a/pkg/install/import_test.go b/pkg/install/import_test.go index ae5e8b41d..e77befe46 100644 --- a/pkg/install/import_test.go +++ b/pkg/install/import_test.go @@ -22,7 +22,8 @@ func TestPkgImportNoCloudProvider(t *testing.T) { t.Logf("Current test file path: %s", filename) t.Logf("Current test directory: %s", filepath.Dir(filename)) // should be this package name // go list -f {{.Deps}} ./ - cmd := exec.Command( + cmd := exec.CommandContext( + t.Context(), "go", "list", "-f", diff --git a/pkg/plugin/clientmgmt/process/client_builder.go b/pkg/plugin/clientmgmt/process/client_builder.go index 5bffa5259..ceeac8db7 100644 --- a/pkg/plugin/clientmgmt/process/client_builder.go +++ b/pkg/plugin/clientmgmt/process/client_builder.go @@ -18,6 +18,7 @@ limitations under the License. package process import ( + "context" "os" "os/exec" @@ -78,7 +79,7 @@ func (b *clientBuilder) clientConfig() *hcplugin.ClientConfig { string(common.PluginKindItemBlockAction): ibav1.NewItemBlockActionPlugin(common.ClientLogger(b.clientLogger)), }, Logger: b.pluginLogger, - Cmd: exec.Command(b.commandName, b.commandArgs...), //nolint:gosec // Internal call. No need to check the command line. + Cmd: exec.CommandContext(context.Background(), b.commandName, b.commandArgs...), //nolint:gosec // Internal call. No need to check the command line. } } diff --git a/pkg/plugin/clientmgmt/process/client_builder_test.go b/pkg/plugin/clientmgmt/process/client_builder_test.go index d92addf76..14278ae8f 100644 --- a/pkg/plugin/clientmgmt/process/client_builder_test.go +++ b/pkg/plugin/clientmgmt/process/client_builder_test.go @@ -65,9 +65,11 @@ func TestClientConfig(t *testing.T) { string(common.PluginKindItemBlockAction): ibav1.NewItemBlockActionPlugin(common.ClientLogger(logger)), }, Logger: cb.pluginLogger, - Cmd: exec.Command(cb.commandName, cb.commandArgs...), + Cmd: exec.CommandContext(t.Context(), cb.commandName, cb.commandArgs...), } cc := cb.clientConfig() - assert.Equal(t, expected, cc) + assert.Equal(t, expected.HandshakeConfig, cc.HandshakeConfig) + assert.Equal(t, expected.AllowedProtocols, cc.AllowedProtocols) + assert.Equal(t, expected.Plugins, cc.Plugins) } diff --git a/pkg/plugin/framework/import_test.go b/pkg/plugin/framework/import_test.go index 29be81122..ee1439329 100644 --- a/pkg/plugin/framework/import_test.go +++ b/pkg/plugin/framework/import_test.go @@ -22,7 +22,8 @@ func TestPkgImportNoCloudProvider(t *testing.T) { t.Logf("Current test file path: %s", filename) t.Logf("Current test directory: %s", filepath.Dir(filename)) // should be this package name // go list -f {{.Deps}} ./ - cmd := exec.Command( + cmd := exec.CommandContext( + t.Context(), "go", "list", "-f", diff --git a/pkg/restic/command.go b/pkg/restic/command.go index ef0ba5655..feb41642d 100644 --- a/pkg/restic/command.go +++ b/pkg/restic/command.go @@ -77,7 +77,7 @@ func (c *Command) String() string { // Cmd returns an exec.Cmd for the command. func (c *Command) Cmd() *exec.Cmd { parts := c.StringSlice() - cmd := exec.Command(parts[0], parts[1:]...) //nolint:gosec // Internal call. No need to check the parameter. + cmd := exec.Command(parts[0], parts[1:]...) //nolint:gosec,noctx // Internal call. No need to check the parameter. No to add context for deprecated Restic. cmd.Dir = c.Dir if len(c.Env) > 0 { diff --git a/test/e2e/migration/migration.go b/test/e2e/migration/migration.go index f91bcffff..08f1bac00 100644 --- a/test/e2e/migration/migration.go +++ b/test/e2e/migration/migration.go @@ -142,6 +142,7 @@ func (m *migrationE2E) Backup() error { "Fail to set images for the migrate-from Velero installation.") m.veleroCLI2Version.VeleroCLI, err = veleroutil.InstallVeleroCLI( + m.Ctx, m.veleroCLI2Version.VeleroVersion) Expect(err).To(Succeed()) } diff --git a/test/e2e/upgrade/upgrade.go b/test/e2e/upgrade/upgrade.go index 6832c160c..6d081f1c1 100644 --- a/test/e2e/upgrade/upgrade.go +++ b/test/e2e/upgrade/upgrade.go @@ -115,7 +115,10 @@ func BackupUpgradeRestoreTest(useVolumeSnapshots bool, veleroCLI2Version VeleroC //Download velero CLI if it's empty according to velero CLI version By(fmt.Sprintf("Install the expected old version Velero CLI (%s) for installing Velero", veleroCLI2Version.VeleroVersion), func() { - veleroCLI2Version.VeleroCLI, err = InstallVeleroCLI(veleroCLI2Version.VeleroVersion) + veleroCLI2Version.VeleroCLI, err = InstallVeleroCLI( + oneHourTimeout, + veleroCLI2Version.VeleroVersion, + ) Expect(err).To(Succeed()) }) } diff --git a/test/util/common/common.go b/test/util/common/common.go index b49c61e50..561f75040 100644 --- a/test/util/common/common.go +++ b/test/util/common/common.go @@ -33,7 +33,7 @@ func GetListByCmdPipes(ctx context.Context, cmdLines []*OsCommandLine) ([]string var cmds []*exec.Cmd for _, cmdline := range cmdLines { - cmd := exec.Command(cmdline.Cmd, cmdline.Args...) + cmd := exec.CommandContext(ctx, cmdline.Cmd, cmdline.Args...) cmds = append(cmds, cmd) } fmt.Println(cmds) diff --git a/test/util/velero/velero_utils.go b/test/util/velero/velero_utils.go index 65f56bf03..8dbcbb2bd 100644 --- a/test/util/velero/velero_utils.go +++ b/test/util/velero/velero_utils.go @@ -915,12 +915,12 @@ func CheckVeleroVersion(ctx context.Context, veleroCLI string, expectedVer strin return nil } -func InstallVeleroCLI(version string) (string, error) { +func InstallVeleroCLI(ctx context.Context, version string) (string, error) { var tempVeleroCliDir string name := "velero-" + version + "-" + runtime.GOOS + "-" + runtime.GOARCH postfix := ".tar.gz" tarball := name + postfix - err := wait.PollImmediate(time.Second*5, time.Minute*5, func() (bool, error) { + err := wait.PollUntilContextTimeout(ctx, time.Second*5, time.Minute*5, true, func(ctx context.Context) (bool, error) { tempFile, err := getVeleroCliTarball("https://github.com/vmware-tanzu/velero/releases/download/" + version + "/" + tarball) if err != nil { return false, errors.WithMessagef(err, "failed to get Velero CLI tarball") @@ -930,7 +930,7 @@ func InstallVeleroCLI(version string) (string, error) { return false, errors.WithMessagef(err, "failed to create temp dir for tarball extraction") } - cmd := exec.Command("tar", "-xvf", tempFile.Name(), "-C", tempVeleroCliDir) + cmd := exec.CommandContext(ctx, "tar", "-xvf", tempFile.Name(), "-C", tempVeleroCliDir) defer os.Remove(tempFile.Name()) if _, err := cmd.Output(); err != nil {