Test Start() function

... with some integrationy unit tests
This commit is contained in:
Hannes Hörl 2018-01-12 11:43:51 +00:00
parent 9990f29acd
commit 9ff0bc948a
2 changed files with 71 additions and 2 deletions

View File

@ -28,7 +28,7 @@ func Start(command *exec.Cmd, startMessage string, startTimeout time.Duration) (
detectedStart := stdErr.Detect(startMessage)
timedOut := time.After(startTimeout)
session, err := gexec.Start(command, gbytes.NewBuffer(), stdErr)
session, err := gexec.Start(command, nil, stdErr)
if err != nil {
return session, err
}
@ -37,7 +37,8 @@ func Start(command *exec.Cmd, startMessage string, startTimeout time.Duration) (
case <-detectedStart:
return session, nil
case <-timedOut:
return session, fmt.Errorf("timeout waiting for apiserver to start serving")
session.Terminate()
return session, fmt.Errorf("timeout waiting for process to start serving")
}
}

View File

@ -0,0 +1,68 @@
package internal_test
import (
. "k8s.io/kubectl/pkg/framework/test/internal"
"os/exec"
"time"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Start", func() {
var (
command *exec.Cmd
timeout time.Duration
)
BeforeEach(func() {
command = getSimpleCommand()
timeout = 200 * time.Millisecond
})
It("can start a process", func() {
timeout = 5 * time.Second
session, err := Start(command, "loop 3", timeout)
Expect(err).NotTo(HaveOccurred())
Consistently(session.ExitCode).Should(BeNumerically("==", -1))
})
Context("when process takes too long to start", func() {
It("returns an timeout error", func() {
session, err := Start(command, "loop 3000", timeout)
Expect(err).To(MatchError(ContainSubstring("timeout")))
Eventually(session.ExitCode, 10).Should(BeNumerically("==", 143))
})
})
Context("when command cannot be started", func() {
BeforeEach(func() {
command = exec.Command("/notexistent")
})
It("propagates the error", func() {
_, err := Start(command, "does not matter", timeout)
Expect(os.IsNotExist(err)).To(BeTrue())
})
})
})
func getSimpleCommand() *exec.Cmd {
return exec.Command(
"bash", "-c",
`
i=0
while true
do
echo "loop $i" >&2
let 'i += 1'
sleep 0.2
done
`,
)
}