github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/network/remove_test.go (about)

     1  package network
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/internal/test"
     9  	"github.com/docker/docker/errdefs"
    10  	"github.com/pkg/errors"
    11  	"gotest.tools/v3/assert"
    12  	is "gotest.tools/v3/assert/cmp"
    13  )
    14  
    15  func TestNetworkRemoveForce(t *testing.T) {
    16  	tests := []struct {
    17  		doc         string
    18  		args        []string
    19  		expectedErr string
    20  	}{
    21  		{
    22  			doc:  "existing network",
    23  			args: []string{"existing-network"},
    24  		},
    25  		{
    26  			doc:  "existing network (forced)",
    27  			args: []string{"--force", "existing-network"},
    28  		},
    29  		{
    30  			doc:         "non-existing network",
    31  			args:        []string{"no-such-network"},
    32  			expectedErr: "no such network: no-such-network",
    33  		},
    34  		{
    35  			doc:  "non-existing network (forced)",
    36  			args: []string{"--force", "no-such-network"},
    37  		},
    38  		{
    39  			doc:         "in-use network",
    40  			args:        []string{"in-use-network"},
    41  			expectedErr: "network is in use",
    42  		},
    43  		{
    44  			doc:         "in-use network (forced)",
    45  			args:        []string{"--force", "in-use-network"},
    46  			expectedErr: "network is in use",
    47  		},
    48  		{
    49  			doc:         "multiple networks",
    50  			args:        []string{"existing-network", "no-such-network"},
    51  			expectedErr: "no such network: no-such-network",
    52  		},
    53  		{
    54  			doc:  "multiple networks (forced)",
    55  			args: []string{"--force", "existing-network", "no-such-network"},
    56  		},
    57  		{
    58  			doc:         "multiple networks 2 (forced)",
    59  			args:        []string{"--force", "existing-network", "no-such-network", "in-use-network"},
    60  			expectedErr: "network is in use",
    61  		},
    62  	}
    63  
    64  	for _, tc := range tests {
    65  		tc := tc
    66  		t.Run(tc.doc, func(t *testing.T) {
    67  			fakeCli := test.NewFakeCli(&fakeClient{
    68  				networkRemoveFunc: func(ctx context.Context, networkID string) error {
    69  					switch networkID {
    70  					case "no-such-network":
    71  						return errdefs.NotFound(errors.New("no such network: no-such-network"))
    72  					case "in-use-network":
    73  						return errdefs.Forbidden(errors.New("network is in use"))
    74  					case "existing-network":
    75  						return nil
    76  					default:
    77  						return nil
    78  					}
    79  				},
    80  			})
    81  
    82  			cmd := newRemoveCommand(fakeCli)
    83  			cmd.SetOut(io.Discard)
    84  			cmd.SetErr(fakeCli.ErrBuffer())
    85  			cmd.SetArgs(tc.args)
    86  
    87  			err := cmd.Execute()
    88  			if tc.expectedErr == "" {
    89  				assert.NilError(t, err)
    90  			} else {
    91  				assert.Check(t, is.Contains(fakeCli.ErrBuffer().String(), tc.expectedErr))
    92  				assert.ErrorContains(t, err, "Code: 1")
    93  			}
    94  		})
    95  	}
    96  }