Skip to content

critest: Add test for exec #269

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

Merged
merged 3 commits into from
Mar 27, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions pkg/validate/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var _ = framework.KubeDescribe("Streaming", func() {
rc.RemovePodSandbox(podID)
})

It("runtime should support exec [Conformance]", func() {
It("runtime should support exec with tty=false and stdin=false [Conformance]", func() {
podID, podConfig = framework.CreatePodSandboxForContainer(rc)

By("create a default container")
Expand All @@ -74,10 +74,38 @@ var _ = framework.KubeDescribe("Streaming", func() {
By("start container")
startContainer(rc, containerID)

req := createDefaultExec(rc, containerID)
execReq := &runtimeapi.ExecRequest{
ContainerId: containerID,
Cmd: []string{"echo", "hello"},
Stdout: true,
Stderr: true,
}
req := createExec(rc, execReq)

By("check the output of exec")
checkExec(rc, req, "hello\n", false)
})

It("runtime should support exec with tty=true and stdin=true [Conformance]", func() {
podID, podConfig = framework.CreatePodSandboxForContainer(rc)

By("create a default container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-exec-test")

By("start container")
startContainer(rc, containerID)

execReq := &runtimeapi.ExecRequest{
ContainerId: containerID,
Cmd: []string{"echo", "hello"},
Stdout: true,
Tty: true,
Stdin: true,
}
req := createExec(rc, execReq)

By("check the output of exec")
checkExec(rc, req)
checkExec(rc, req, "hello\r\n", true)
})

It("runtime should support attach [Conformance]", func() {
Expand Down Expand Up @@ -119,39 +147,52 @@ var _ = framework.KubeDescribe("Streaming", func() {
})
})

func createDefaultExec(c internalapi.RuntimeService, containerID string) string {
By("exec default command in container: " + containerID)
req := &runtimeapi.ExecRequest{
ContainerId: containerID,
Cmd: []string{"echo", "hello"},
Stdout: true,
Stderr: true,
}

resp, err := c.Exec(req)
framework.ExpectNoError(err, "failed to exec in container %q", containerID)
func createExec(c internalapi.RuntimeService, execReq *runtimeapi.ExecRequest) string {
By("exec given command in container: " + execReq.ContainerId)
resp, err := c.Exec(execReq)
framework.ExpectNoError(err, "failed to exec in container %q", execReq.ContainerId)
framework.Logf("Get exec url: " + resp.Url)
return resp.Url
}

func checkExec(c internalapi.RuntimeService, execServerURL string) {
func checkExec(c internalapi.RuntimeService, execServerURL, stdout string, isTty bool) {
localOut := &bytes.Buffer{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Good catch 👍

localErr := &bytes.Buffer{}
localInRead, localInWrite := io.Pipe()

// Wait until output read and then shutdown localIn pipe.
go func() {
ticker := time.NewTicker(5 * time.Second)
for {
switch {
case len(localOut.String()) >= len(stdout):
fallthrough
case <-ticker.C != time.Time{}:
localInWrite.Close()
break
}
}
}()

// Only http is supported now.
// TODO: support streaming APIs via tls.
url := parseURL(c, execServerURL)
e, err := remoteclient.NewSPDYExecutor(&rest.Config{}, "POST", url)
framework.ExpectNoError(err, "failed to create executor for %q", execServerURL)

err = e.Stream(remoteclient.StreamOptions{
streamOptions := remoteclient.StreamOptions{
Stdout: localOut,
Stderr: localErr,
Tty: false,
})
}
if isTty {
streamOptions.Stdin = localInRead
streamOptions.Tty = true
}
err = e.Stream(streamOptions)
framework.ExpectNoError(err, "failed to open streamer for %q", execServerURL)

Expect(localOut.String()).To(Equal("hello\n"), "The stdout of exec should be hello")
Expect(localOut.String()).To(Equal(stdout), "The stdout of exec should be "+stdout)
Expect(localErr.String()).To(BeEmpty(), "The stderr of exec should be empty")
framework.Logf("Check exec url %q succeed", execServerURL)
}
Expand Down