Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20.10 backport] cli/rm_test: Fix TestRemoveForce race condition #3944

Merged
merged 1 commit into from Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 41 additions & 31 deletions cli/command/container/rm_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"sort"
"sync"
"testing"

"github.com/docker/cli/internal/test"
Expand All @@ -14,37 +15,46 @@ import (
)

func TestRemoveForce(t *testing.T) {
var (
removed1 []string
removed2 []string
)
for _, tc := range []struct {
name string
args []string
expectedErr string
}{
{name: "without force", args: []string{"nosuchcontainer", "mycontainer"}, expectedErr: "no such container"},
{name: "with force", args: []string{"--force", "nosuchcontainer", "mycontainer"}, expectedErr: ""},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
var removed []string
mutex := new(sync.Mutex)

cli := test.NewFakeCli(&fakeClient{
containerRemoveFunc: func(ctx context.Context, container string, options types.ContainerRemoveOptions) error {
removed1 = append(removed1, container)
removed2 = append(removed2, container)
if container == "nosuchcontainer" {
return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container))
}
return nil
},
Version: "1.36",
})
cmd := NewRmCommand(cli)
cmd.SetOut(ioutil.Discard)
cli := test.NewFakeCli(&fakeClient{
containerRemoveFunc: func(ctx context.Context, container string, options types.ContainerRemoveOptions) error {
// containerRemoveFunc is called in parallel for each container
// by the remove command so append must be synchronized.
mutex.Lock()
removed = append(removed, container)
mutex.Unlock()

if container == "nosuchcontainer" {
return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container))
}
return nil
},
Version: "1.36",
})
cmd := NewRmCommand(cli)
cmd.SetOut(ioutil.Discard)
cmd.SetArgs(tc.args)

t.Run("without force", func(t *testing.T) {
cmd.SetArgs([]string{"nosuchcontainer", "mycontainer"})
removed1 = []string{}
assert.ErrorContains(t, cmd.Execute(), "no such container")
sort.Strings(removed1)
assert.DeepEqual(t, removed1, []string{"mycontainer", "nosuchcontainer"})
})
t.Run("with force", func(t *testing.T) {
cmd.SetArgs([]string{"--force", "nosuchcontainer", "mycontainer"})
removed2 = []string{}
assert.NilError(t, cmd.Execute())
sort.Strings(removed2)
assert.DeepEqual(t, removed2, []string{"mycontainer", "nosuchcontainer"})
})
err := cmd.Execute()
if tc.expectedErr != "" {
assert.ErrorContains(t, err, tc.expectedErr)
} else {
assert.NilError(t, err)
}
sort.Strings(removed)
assert.DeepEqual(t, removed, []string{"mycontainer", "nosuchcontainer"})
})
}
}