Merge pull request #3943 from vvoland/fix-test-removeforce

cli/rm_test: Fix TestRemoveForce race condition
This commit is contained in:
Sebastiaan van Stijn 2023-01-06 15:39:39 +01:00 committed by GitHub
commit e5b29b8975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 31 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"sort" "sort"
"sync"
"testing" "testing"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
@ -14,15 +15,27 @@ import (
) )
func TestRemoveForce(t *testing.T) { func TestRemoveForce(t *testing.T) {
var ( for _, tc := range []struct {
removed1 []string name string
removed2 []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{ cli := test.NewFakeCli(&fakeClient{
containerRemoveFunc: func(ctx context.Context, container string, options types.ContainerRemoveOptions) error { containerRemoveFunc: func(ctx context.Context, container string, options types.ContainerRemoveOptions) error {
removed1 = append(removed1, container) // containerRemoveFunc is called in parallel for each container
removed2 = append(removed2, container) // by the remove command so append must be synchronized.
mutex.Lock()
removed = append(removed, container)
mutex.Unlock()
if container == "nosuchcontainer" { if container == "nosuchcontainer" {
return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container)) return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container))
} }
@ -32,19 +45,16 @@ func TestRemoveForce(t *testing.T) {
}) })
cmd := NewRmCommand(cli) cmd := NewRmCommand(cli)
cmd.SetOut(io.Discard) cmd.SetOut(io.Discard)
cmd.SetArgs(tc.args)
t.Run("without force", func(t *testing.T) { err := cmd.Execute()
cmd.SetArgs([]string{"nosuchcontainer", "mycontainer"}) if tc.expectedErr != "" {
removed1 = []string{} assert.ErrorContains(t, err, tc.expectedErr)
assert.ErrorContains(t, cmd.Execute(), "no such container") } else {
sort.Strings(removed1) assert.NilError(t, err)
assert.DeepEqual(t, removed1, []string{"mycontainer", "nosuchcontainer"}) }
}) sort.Strings(removed)
t.Run("with force", func(t *testing.T) { assert.DeepEqual(t, removed, []string{"mycontainer", "nosuchcontainer"})
cmd.SetArgs([]string{"--force", "nosuchcontainer", "mycontainer"})
removed2 = []string{}
assert.NilError(t, cmd.Execute())
sort.Strings(removed2)
assert.DeepEqual(t, removed2, []string{"mycontainer", "nosuchcontainer"})
}) })
} }
}