Add Processes() to client

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-05-31 13:25:39 -07:00
parent 8ec5c30d83
commit ff54c88e99
3 changed files with 89 additions and 0 deletions

View File

@ -289,3 +289,74 @@ func TestContainerExec(t *testing.T) {
} }
<-finished <-finished
} }
func TestContainerProcesses(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 = "ContainerProcesses"
)
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)
statusC := make(chan uint32, 1)
go func() {
status, err := task.Wait(ctx)
if err != nil {
t.Error(err)
}
statusC <- status
}()
pid := task.Pid()
if pid <= 0 {
t.Errorf("invalid task pid %d", pid)
}
processes, err := task.Processes(ctx)
if err != nil {
t.Error(err)
return
}
if l := len(processes); l != 1 {
t.Errorf("expected 1 process but received %d", l)
}
if len(processes) > 0 {
actual := processes[0]
if pid != actual {
t.Errorf("expected pid %d but received %d", pid, actual)
}
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
t.Error(err)
}
<-statusC
}

View File

@ -26,10 +26,13 @@ type process struct {
spec *specs.Process spec *specs.Process
} }
// Pid returns the pid of the process
// The pid is not set until start is called and returns
func (p *process) Pid() uint32 { func (p *process) Pid() uint32 {
return p.pid return p.pid
} }
// Start starts the exec process
func (p *process) Start(ctx context.Context) error { func (p *process) Start(ctx context.Context) error {
data, err := json.Marshal(p.spec) data, err := json.Marshal(p.spec)
if err != nil { if err != nil {

15
task.go
View File

@ -31,6 +31,7 @@ type Task interface {
Status(context.Context) (TaskStatus, error) Status(context.Context) (TaskStatus, error)
Wait(context.Context) (uint32, error) Wait(context.Context) (uint32, error)
Exec(context.Context, *specs.Process, IOCreation) (Process, error) Exec(context.Context, *specs.Process, IOCreation) (Process, error)
Processes(context.Context) ([]uint32, error)
} }
type Process interface { type Process interface {
@ -143,3 +144,17 @@ func (t *task) Exec(ctx context.Context, spec *specs.Process, ioCreate IOCreatio
pidSync: make(chan struct{}), pidSync: make(chan struct{}),
}, nil }, nil
} }
func (t *task) Processes(ctx context.Context) ([]uint32, error) {
response, err := t.client.TaskService().Processes(ctx, &execution.ProcessesRequest{
ContainerID: t.containerID,
})
if err != nil {
return nil, err
}
var out []uint32
for _, p := range response.Processes {
out = append(out, p.Pid)
}
return out, nil
}