mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-31 23:36:12 +00:00
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:
parent
45e3f858f0
commit
163a081776
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user