Skip to content

Commit ee2c4e5

Browse files
committed
Refactor kubectl without stdin test
1 parent fcba5fe commit ee2c4e5

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

test/e2e/kubectl/kubectl.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,24 @@ var _ = SIGDescribe("Kubectl client", func() {
558558
})
559559

560560
ginkgo.It("should support inline execution and attach", func() {
561+
waitForStdinContent := func(pod, content string) string {
562+
var logOutput string
563+
err := wait.Poll(10*time.Second, 5*time.Minute, func() (bool, error) {
564+
logOutput = framework.RunKubectlOrDie(ns, "logs", pod)
565+
return strings.Contains(logOutput, content), nil
566+
})
567+
568+
gomega.Expect(err).To(gomega.BeNil(), fmt.Sprintf("unexpected error waiting for '%v' output", content))
569+
return logOutput
570+
}
571+
561572
ginkgo.By("executing a command with run and attach with stdin")
562573
// We wait for a non-empty line so we know kubectl has attached
563-
runOutput := framework.NewKubectlCommand(ns, "run", "run-test", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--stdin", "--", "sh", "-c", "while [ -z \"$s\" ]; do read s; sleep 1; done; echo read:$s && cat && echo 'stdin closed'").
574+
framework.NewKubectlCommand(ns, "run", "run-test", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--stdin", "--", "sh", "-c", "echo -n read: && cat && echo 'stdin closed'").
564575
WithStdinData("value\nabcd1234").
565576
ExecOrDie(ns)
577+
578+
runOutput := waitForStdinContent("run-test", "stdin closed")
566579
gomega.Expect(runOutput).To(gomega.ContainSubstring("read:value"))
567580
gomega.Expect(runOutput).To(gomega.ContainSubstring("abcd1234"))
568581
gomega.Expect(runOutput).To(gomega.ContainSubstring("stdin closed"))
@@ -575,38 +588,32 @@ var _ = SIGDescribe("Kubectl client", func() {
575588
// "stdin closed", but hasn't exited yet.
576589
// We wait 10 seconds before printing to give time to kubectl to attach
577590
// to the container, this does not solve the race though.
578-
runOutput = framework.NewKubectlCommand(ns, "run", "run-test-2", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--leave-stdin-open=true", "--", "sh", "-c", "sleep 10; cat && echo 'stdin closed'").
591+
framework.NewKubectlCommand(ns, "run", "run-test-2", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--leave-stdin-open=true", "--", "sh", "-c", "cat && echo 'stdin closed'").
579592
WithStdinData("abcd1234").
580593
ExecOrDie(ns)
594+
595+
runOutput = waitForStdinContent("run-test-2", "stdin closed")
581596
gomega.Expect(runOutput).ToNot(gomega.ContainSubstring("abcd1234"))
582597
gomega.Expect(runOutput).To(gomega.ContainSubstring("stdin closed"))
583598

584599
gomega.Expect(c.CoreV1().Pods(ns).Delete(context.TODO(), "run-test-2", metav1.DeleteOptions{})).To(gomega.BeNil())
585600

586601
ginkgo.By("executing a command with run and attach with stdin with open stdin should remain running")
587-
runOutput = framework.NewKubectlCommand(ns, "run", "run-test-3", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--leave-stdin-open=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
602+
framework.NewKubectlCommand(ns, "run", "run-test-3", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--leave-stdin-open=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
588603
WithStdinData("abcd1234\n").
589604
ExecOrDie(ns)
605+
606+
runOutput = waitForStdinContent("run-test-3", "abcd1234")
607+
gomega.Expect(runOutput).To(gomega.ContainSubstring("abcd1234"))
590608
gomega.Expect(runOutput).ToNot(gomega.ContainSubstring("stdin closed"))
609+
591610
g := func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) }
592611
runTestPod, _, err := polymorphichelpers.GetFirstPod(f.ClientSet.CoreV1(), ns, "run=run-test-3", 1*time.Minute, g)
593612
framework.ExpectNoError(err)
594613
if !e2epod.CheckPodsRunningReady(c, ns, []string{runTestPod.Name}, time.Minute) {
595614
framework.Failf("Pod %q of Job %q should still be running", runTestPod.Name, "run-test-3")
596615
}
597616

598-
// NOTE: we cannot guarantee our output showed up in the container logs before stdin was closed, so we have
599-
// to loop test.
600-
err = wait.PollImmediate(time.Second, time.Minute, func() (bool, error) {
601-
if !e2epod.CheckPodsRunningReady(c, ns, []string{runTestPod.Name}, 1*time.Second) {
602-
framework.Failf("Pod %q of Job %q should still be running", runTestPod.Name, "run-test-3")
603-
}
604-
logOutput := framework.RunKubectlOrDie(ns, "logs", runTestPod.Name)
605-
gomega.Expect(logOutput).ToNot(gomega.ContainSubstring("stdin closed"))
606-
return strings.Contains(logOutput, "abcd1234"), nil
607-
})
608-
framework.ExpectNoError(err)
609-
610617
gomega.Expect(c.CoreV1().Pods(ns).Delete(context.TODO(), "run-test-3", metav1.DeleteOptions{})).To(gomega.BeNil())
611618
})
612619

0 commit comments

Comments
 (0)