github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/container/rm_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"sort"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/docker/cli/internal/test"
    12  	"github.com/docker/docker/api/types/container"
    13  	"github.com/docker/docker/errdefs"
    14  	"gotest.tools/v3/assert"
    15  )
    16  
    17  func TestRemoveForce(t *testing.T) {
    18  	for _, tc := range []struct {
    19  		name        string
    20  		args        []string
    21  		expectedErr string
    22  	}{
    23  		{name: "without force", args: []string{"nosuchcontainer", "mycontainer"}, expectedErr: "no such container"},
    24  		{name: "with force", args: []string{"--force", "nosuchcontainer", "mycontainer"}, expectedErr: ""},
    25  	} {
    26  		tc := tc
    27  		t.Run(tc.name, func(t *testing.T) {
    28  			var removed []string
    29  			mutex := new(sync.Mutex)
    30  
    31  			cli := test.NewFakeCli(&fakeClient{
    32  				containerRemoveFunc: func(ctx context.Context, container string, options container.RemoveOptions) error {
    33  					// containerRemoveFunc is called in parallel for each container
    34  					// by the remove command so append must be synchronized.
    35  					mutex.Lock()
    36  					removed = append(removed, container)
    37  					mutex.Unlock()
    38  
    39  					if container == "nosuchcontainer" {
    40  						return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container))
    41  					}
    42  					return nil
    43  				},
    44  				Version: "1.36",
    45  			})
    46  			cmd := NewRmCommand(cli)
    47  			cmd.SetOut(io.Discard)
    48  			cmd.SetArgs(tc.args)
    49  
    50  			err := cmd.Execute()
    51  			if tc.expectedErr != "" {
    52  				assert.ErrorContains(t, err, tc.expectedErr)
    53  			} else {
    54  				assert.NilError(t, err)
    55  			}
    56  			sort.Strings(removed)
    57  			assert.DeepEqual(t, removed, []string{"mycontainer", "nosuchcontainer"})
    58  		})
    59  	}
    60  }