Add exec support to client

This also fixes a deadlock in the shim's reaper where execs would lockup
and/or miss a quick exiting exec process's exit status.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2017-05-31 10:40:45 -07:00
parent f55f40eeec
commit ebf935d990
5 changed files with 234 additions and 14 deletions

View File

@@ -3,9 +3,14 @@ package containerd
import (
"bytes"
"context"
"syscall"
"testing"
)
func empty() IOCreation {
return BufferedIO(bytes.NewBuffer(nil), bytes.NewBuffer(nil), bytes.NewBuffer(nil))
}
func TestContainerList(t *testing.T) {
if testing.Short() {
t.Skip()
@@ -64,7 +69,6 @@ func TestNewContainer(t *testing.T) {
func TestContainerStart(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
client, err := New(address)
if err != nil {
@@ -133,7 +137,6 @@ func TestContainerStart(t *testing.T) {
func TestContainerOutput(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
client, err := New(address)
if err != nil {
@@ -201,3 +204,88 @@ func TestContainerOutput(t *testing.T) {
t.Errorf("expected output %q but received %q", expected, actual)
}
}
func TestContainerExec(t *testing.T) {
if testing.Short() {
t.Skip()
}
client, err := New(address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
var (
ctx = context.Background()
id = "ContainerExec"
)
image, err := client.GetImage(ctx, testImage)
if err != nil {
t.Error(err)
return
}
spec, err := GenerateSpec(WithImageConfig(ctx, image), WithProcessArgs("sleep", "100"))
if err != nil {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, spec, WithImage(image), WithNewRootFS(id, image))
if err != nil {
t.Error(err)
return
}
defer container.Delete(ctx)
task, err := container.NewTask(ctx, empty())
if err != nil {
t.Error(err)
return
}
defer task.Delete(ctx)
finished := make(chan struct{}, 1)
go func() {
if _, err := task.Wait(ctx); err != nil {
t.Error(err)
}
close(finished)
}()
// start an exec process without running the original container process info
processSpec := spec.Process
processSpec.Args = []string{
"sh", "-c",
"exit 6",
}
process, err := task.Exec(ctx, &processSpec, empty())
if err != nil {
t.Error(err)
return
}
processStatusC := make(chan uint32, 1)
go func() {
status, err := process.Wait(ctx)
if err != nil {
t.Error(err)
return
}
processStatusC <- status
}()
if err := process.Start(ctx); err != nil {
t.Error(err)
return
}
// wait for the exec to return
status := <-processStatusC
if status != 6 {
t.Errorf("expected exec exit code 6 but received %d", status)
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
t.Error(err)
}
<-finished
}