Merge pull request #19410 from mesosphere/jdef_run_job_with_timeout

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot 2016-01-16 05:53:40 -08:00
commit c678ef3592
3 changed files with 27 additions and 4 deletions

View File

@ -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" "$@"

View File

@ -70,6 +70,7 @@ const (
simplePodName = "nginx"
nginxDefaultOutput = "Welcome to nginx!"
simplePodPort = 80
runJobTimeout = 5 * time.Minute
)
var proxyRegexp = regexp.MustCompile("Starting to serve on 127.0.0.1:([0-9]+)")
@ -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"))

View File

@ -1193,7 +1193,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 {
@ -1202,6 +1203,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
@ -1224,8 +1230,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())