container: allow to stop a paused container

When a container is paused and something goes terribly
wrong, we still need to be able to clean thing up. A paused
container should be able to transit to stopped state as well
so that we can delete it properly.

Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
Peng Tao 2019-07-18 21:05:21 -07:00
parent f886c0bf35
commit c472a01006
6 changed files with 11 additions and 15 deletions

View File

@ -200,7 +200,7 @@ func TestDeleteSandbox(t *testing.T) {
assert.Error(err)
assert.True(vcmock.IsMockError(err))
testingImpl.StopSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
testingImpl.StopSandboxFunc = func(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
return sandbox, nil
}
@ -310,7 +310,7 @@ func TestDeleteSandboxRunning(t *testing.T) {
}, nil
}
testingImpl.StopSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
testingImpl.StopSandboxFunc = func(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
return sandbox, nil
}
@ -564,7 +564,7 @@ func TestDeleteCLIFunctionSuccess(t *testing.T) {
}, nil
}
testingImpl.StopSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
testingImpl.StopSandboxFunc = func(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
return sandbox, nil
}

View File

@ -29,7 +29,7 @@ var (
return &vcmock.Container{}, nil
}
testStopSandboxFuncReturnNil = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
testStopSandboxFuncReturnNil = func(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
return &vcmock.Sandbox{}, nil
}
)

View File

@ -971,10 +971,6 @@ func (c *Container) stop() error {
return nil
}
if c.sandbox.state.State != types.StateReady && c.sandbox.state.State != types.StateRunning {
return fmt.Errorf("Sandbox not ready or running, impossible to stop the container")
}
if err := c.state.ValidTransition(c.state.State, types.StateStopped); err != nil {
return err
}

View File

@ -84,9 +84,9 @@ func (m *VCMock) StartSandbox(ctx context.Context, sandboxID string) (vc.VCSandb
}
// StopSandbox implements the VC function of the same name.
func (m *VCMock) StopSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
func (m *VCMock) StopSandbox(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
if m.StopSandboxFunc != nil {
return m.StopSandboxFunc(ctx, sandboxID)
return m.StopSandboxFunc(ctx, sandboxID, force)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)

View File

@ -339,22 +339,22 @@ func TestVCMockStopSandbox(t *testing.T) {
assert.Nil(m.StopSandboxFunc)
ctx := context.Background()
_, err := m.StopSandbox(ctx, testSandboxID)
_, err := m.StopSandbox(ctx, testSandboxID, false)
assert.Error(err)
assert.True(IsMockError(err))
m.StopSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
m.StopSandboxFunc = func(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
return &Sandbox{}, nil
}
sandbox, err := m.StopSandbox(ctx, testSandboxID)
sandbox, err := m.StopSandbox(ctx, testSandboxID, false)
assert.NoError(err)
assert.Equal(sandbox, &Sandbox{})
// reset
m.StopSandboxFunc = nil
_, err = m.StopSandbox(ctx, testSandboxID)
_, err = m.StopSandbox(ctx, testSandboxID, false)
assert.Error(err)
assert.True(IsMockError(err))
}

View File

@ -54,7 +54,7 @@ type VCMock struct {
StartSandboxFunc func(ctx context.Context, sandboxID string) (vc.VCSandbox, error)
StatusSandboxFunc func(ctx context.Context, sandboxID string) (vc.SandboxStatus, error)
StatsContainerFunc func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStats, error)
StopSandboxFunc func(ctx context.Context, sandboxID string) (vc.VCSandbox, error)
StopSandboxFunc func(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error)
CreateContainerFunc func(ctx context.Context, sandboxID string, containerConfig vc.ContainerConfig) (vc.VCSandbox, vc.VCContainer, error)
DeleteContainerFunc func(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error)