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

Commit

Permalink
cli: Check sandbox state before to issue a StopSandbox
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Sebastien Boeuf committed Apr 25, 2018
1 parent 45e3f85 commit 163a081
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions cli/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}()

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

0 comments on commit 163a081

Please sign in to comment.