Files
deploy/util/exec/exec_test.go
T
Eng Zer JunandGitHub 9807275c7b chore: use t.Setenv to set env vars in tests (#14377)
This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv

Signed-off-by: Eng Zer Jun <[email protected]>
2023-07-06 19:50:08 -04:00

56 lines
1.5 KiB
Go

package exec
import (
"os/exec"
"regexp"
"syscall"
"testing"
"time"
argoexec "github.com/argoproj/pkg/exec"
"github.com/stretchr/testify/assert"
)
func Test_timeout(t *testing.T) {
t.Run("Default", func(t *testing.T) {
initTimeout()
assert.Equal(t, 90*time.Second, timeout)
})
t.Run("Default", func(t *testing.T) {
t.Setenv("ARGOCD_EXEC_TIMEOUT", "1s")
initTimeout()
assert.Equal(t, 1*time.Second, timeout)
})
}
func TestRun(t *testing.T) {
out, err := Run(exec.Command("ls"))
assert.NoError(t, err)
assert.NotEmpty(t, out)
}
func TestHideUsernamePassword(t *testing.T) {
_, err := RunWithRedactor(exec.Command("helm registry login https://charts.bitnami.com/bitnami", "--username", "foo", "--password", "bar"), nil)
assert.NotEmpty(t, err)
redactor := func(text string) string {
return regexp.MustCompile("(--username|--password) [^ ]*").ReplaceAllString(text, "$1 ******")
}
_, err = RunWithRedactor(exec.Command("helm registry login https://charts.bitnami.com/bitnami", "--username", "foo", "--password", "bar"), redactor)
assert.NotEmpty(t, err)
}
func TestRunWithExecRunOpts(t *testing.T) {
t.Setenv("ARGOCD_EXEC_TIMEOUT", "200ms")
initTimeout()
opts := ExecRunOpts{
TimeoutBehavior: argoexec.TimeoutBehavior{
Signal: syscall.SIGTERM,
ShouldWait: true,
},
}
_, err := RunWithExecRunOpts(exec.Command("sh", "-c", "trap 'trap - 15 && echo captured && exit' 15 && sleep 2"), opts)
assert.Contains(t, err.Error(), "failed timeout after 200ms")
}