Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
container: allow to stop a paused container
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bergwolf committed Jul 23, 2019
1 parent f886c0b commit c472a01
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 15 deletions.
6 changes: 3 additions & 3 deletions cli/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/kill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
Expand Down
4 changes: 0 additions & 4 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/pkg/vcmock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions virtcontainers/pkg/vcmock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/pkg/vcmock/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c472a01

Please sign in to comment.