cli: Check sandbox state before to issue a StopSandbox

The way a delete works, it was always trying to stop the sandbox, even
when the force flag was not enabled. Because we want to be able to stop
the sandbox from a kill command, this means a sandbox stop might be
called twice, and we don't want the second stop to fail, leading to the
failure of the delete command.

That's why this commit checks for the sandbox status before to try
stopping the sandbox.

Fixes #246

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2018-04-25 09:01:53 -07:00
parent 45e3f858f0
commit 163a081776
2 changed files with 44 additions and 1 deletions

View File

@ -105,10 +105,17 @@ func delete(containerID string, force bool) error {
}
func deleteSandbox(sandboxID string) error {
if _, err := vci.StopSandbox(sandboxID); err != nil {
status, err := vci.StatusSandbox(sandboxID)
if err != nil {
return err
}
if oci.StateToOCIState(status.State) != oci.StateStopped {
if _, err := vci.StopSandbox(sandboxID); err != nil {
return err
}
}
if _, err := vci.DeleteSandbox(sandboxID); err != nil {
return err
}

View File

@ -192,6 +192,23 @@ func TestDeleteSandbox(t *testing.T) {
assert.Error(err)
assert.True(vcmock.IsMockError(err))
testingImpl.StatusSandboxFunc = func(sandboxID string) (vc.SandboxStatus, error) {
return vc.SandboxStatus{
ID: sandbox.ID(),
State: vc.State{
State: vc.StateReady,
},
}, nil
}
defer func() {
testingImpl.StatusSandboxFunc = nil
}()
err = delete(sandbox.ID(), false)
assert.Error(err)
assert.True(vcmock.IsMockError(err))
testingImpl.StopSandboxFunc = func(sandboxID string) (vc.VCSandbox, error) {
return sandbox, nil
}
@ -297,11 +314,21 @@ func TestDeleteSandboxRunning(t *testing.T) {
assert.Error(err)
assert.False(vcmock.IsMockError(err))
testingImpl.StatusSandboxFunc = func(sandboxID string) (vc.SandboxStatus, error) {
return vc.SandboxStatus{
ID: sandbox.ID(),
State: vc.State{
State: vc.StateRunning,
},
}, nil
}
testingImpl.StopSandboxFunc = func(sandboxID string) (vc.VCSandbox, error) {
return sandbox, nil
}
defer func() {
testingImpl.StatusSandboxFunc = nil
testingImpl.StopSandboxFunc = nil
}()
@ -525,6 +552,15 @@ func TestDeleteCLIFunctionSuccess(t *testing.T) {
}, nil
}
testingImpl.StatusSandboxFunc = func(sandboxID string) (vc.SandboxStatus, error) {
return vc.SandboxStatus{
ID: sandbox.ID(),
State: vc.State{
State: vc.StateReady,
},
}, nil
}
testingImpl.StopSandboxFunc = func(sandboxID string) (vc.VCSandbox, error) {
return sandbox, nil
}