Merge pull request #1377 from crosbymichael/exec-delete

Allow exec process to be deleted in created state
This commit is contained in:
Kenfe-Mickaël Laventure 2017-08-21 15:27:17 -07:00 committed by GitHub
commit 89da512692
3 changed files with 86 additions and 3 deletions

View File

@ -1451,3 +1451,82 @@ func TestContainerExitedAtSet(t *testing.T) {
return
}
}
func TestDeleteContainerExecCreated(t *testing.T) {
t.Parallel()
client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
var (
image Image
ctx, cancel = testContext()
id = t.Name()
)
defer cancel()
if runtime.GOOS != "windows" {
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, WithSpec(spec), withNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
}
defer container.Delete(ctx, WithSnapshotCleanup)
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)
}()
if err := task.Start(ctx); err != nil {
t.Error(err)
return
}
// start an exec process without running the original container process info
processSpec := spec.Process
withExecExitStatus(processSpec, 6)
execID := t.Name() + "_exec"
process, err := task.Exec(ctx, execID, processSpec, empty())
if err != nil {
t.Error(err)
return
}
deleteStatus, err := process.Delete(ctx)
if err != nil {
t.Error(err)
return
}
if deleteStatus != 0 {
t.Errorf("expected delete exit code 0 but received %d", deleteStatus)
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
t.Error(err)
}
<-finished
}

View File

@ -152,7 +152,8 @@ func (p *process) Delete(ctx context.Context, opts ...ProcessDeleteOpts) (uint32
if err != nil {
return UnknownExitStatus, err
}
if status.Status != Stopped {
switch status.Status {
case Running, Paused, Pausing:
return UnknownExitStatus, errors.Wrapf(errdefs.ErrFailedPrecondition, "process must be stopped before deletion")
}
if p.io != nil {

View File

@ -59,6 +59,9 @@ func (p *process) Status() runtime.Status {
case <-p.exitCh:
status = runtime.StoppedStatus
default:
if p.hcs == nil {
return runtime.CreatedStatus
}
status = runtime.RunningStatus
}
return status
@ -83,8 +86,8 @@ func (p *process) Pid() uint32 {
}
func (p *process) ExitCode() (uint32, time.Time, error) {
if p.Status() != runtime.StoppedStatus {
return 255, time.Time{}, errors.Wrap(errdefs.ErrFailedPrecondition, "process is not stopped")
if s := p.Status(); s != runtime.StoppedStatus && s != runtime.CreatedStatus {
return 255, time.Time{}, errors.Wrapf(errdefs.ErrFailedPrecondition, "process is not stopped: %s", s)
}
return p.exitCode, p.exitTime, nil
}