Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a timeout for job runs in case something gets stuck #19410

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cluster/test-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ TEST_ARGS="$@"

echo "Running e2e tests:" 1>&2
echo "./hack/ginkgo-e2e.sh ${TEST_ARGS}" 1>&2
exec "${KUBE_ROOT}/hack/ginkgo-e2e.sh" ${TEST_ARGS}
exec "${KUBE_ROOT}/hack/ginkgo-e2e.sh" "$@"
4 changes: 4 additions & 0 deletions test/e2e/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
simplePodName = "nginx"
nginxDefaultOutput = "Welcome to nginx!"
simplePodPort = 80
runJobTimeout = 5 * time.Minute
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about util.ForeverTestTimeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was concerned that foreverTestTimeout might be too short. and there are other timeouts in this file as well, so this seemed consistent

)

var proxyRegexp = regexp.MustCompile("Starting to serve on 127.0.0.1:([0-9]+)")
Expand Down Expand Up @@ -911,8 +912,11 @@ var _ = Describe("Kubectl client", func() {

It("should create a job from an image, then delete the job [Conformance]", func() {
By("executing a command with run --rm and attach with stdin")
t := time.NewTimer(runJobTimeout)
defer t.Stop()
runOutput := newKubectlCommand(nsFlag, "run", jobName, "--image=busybox", "--rm=true", "--restart=Never", "--attach=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
withStdinData("abcd1234").
withTimeout(t.C).
execOrDie()
Expect(runOutput).To(ContainSubstring("abcd1234"))
Expect(runOutput).To(ContainSubstring("stdin closed"))
Expand Down
25 changes: 22 additions & 3 deletions test/e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,8 @@ func kubectlCmd(args ...string) *exec.Cmd {
// kubectlBuilder is used to build, custimize and execute a kubectl Command.
// Add more functions to customize the builder as needed.
type kubectlBuilder struct {
cmd *exec.Cmd
cmd *exec.Cmd
timeout <-chan time.Time
}

func newKubectlCommand(args ...string) *kubectlBuilder {
Expand All @@ -1198,6 +1199,11 @@ func newKubectlCommand(args ...string) *kubectlBuilder {
return b
}

func (b *kubectlBuilder) withTimeout(t <-chan time.Time) *kubectlBuilder {
b.timeout = t
return b
}

func (b kubectlBuilder) withStdinData(data string) *kubectlBuilder {
b.cmd.Stdin = strings.NewReader(data)
return &b
Expand All @@ -1220,8 +1226,21 @@ func (b kubectlBuilder) exec() (string, error) {
cmd.Stdout, cmd.Stderr = &stdout, &stderr

Logf("Running '%s %s'", cmd.Path, strings.Join(cmd.Args[1:], " ")) // skip arg[0] as it is printed separately
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("Error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v\n", cmd, cmd.Stdout, cmd.Stderr, err)
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("Error starting %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v\n", cmd, cmd.Stdout, cmd.Stderr, err)
}
errCh := make(chan error, 1)
go func() {
errCh <- cmd.Wait()
}()
select {
case err := <-errCh:
if err != nil {
return "", fmt.Errorf("Error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v\n", cmd, cmd.Stdout, cmd.Stderr, err)
}
case <-b.timeout:
b.cmd.Process.Kill()
return "", fmt.Errorf("Timed out waiting for command %v:\nCommand stdout:\n%v\nstderr:\n%v\n", cmd, cmd.Stdout, cmd.Stderr)
}
Logf("stdout: %q", stdout.String())
Logf("stderr: %q", stderr.String())
Expand Down