github.1git.de/docker/cli@v26.1.3+incompatible/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/api/types"
    10  	"github.com/docker/docker/errdefs"
    11  	"github.com/pkg/errors"
    12  	"gotest.tools/v3/assert"
    13  	is "gotest.tools/v3/assert/cmp"
    14  )
    15  
    16  func TestNetworkRemoveForce(t *testing.T) {
    17  	tests := []struct {
    18  		doc         string
    19  		args        []string
    20  		expectedErr string
    21  	}{
    22  		{
    23  			doc:  "existing network",
    24  			args: []string{"existing-network"},
    25  		},
    26  		{
    27  			doc:  "existing network (forced)",
    28  			args: []string{"--force", "existing-network"},
    29  		},
    30  		{
    31  			doc:         "non-existing network",
    32  			args:        []string{"no-such-network"},
    33  			expectedErr: "no such network: no-such-network",
    34  		},
    35  		{
    36  			doc:  "non-existing network (forced)",
    37  			args: []string{"--force", "no-such-network"},
    38  		},
    39  		{
    40  			doc:         "in-use network",
    41  			args:        []string{"in-use-network"},
    42  			expectedErr: "network is in use",
    43  		},
    44  		{
    45  			doc:         "in-use network (forced)",
    46  			args:        []string{"--force", "in-use-network"},
    47  			expectedErr: "network is in use",
    48  		},
    49  		{
    50  			doc:         "multiple networks",
    51  			args:        []string{"existing-network", "no-such-network"},
    52  			expectedErr: "no such network: no-such-network",
    53  		},
    54  		{
    55  			doc:  "multiple networks (forced)",
    56  			args: []string{"--force", "existing-network", "no-such-network"},
    57  		},
    58  		{
    59  			doc:         "multiple networks 2 (forced)",
    60  			args:        []string{"--force", "existing-network", "no-such-network", "in-use-network"},
    61  			expectedErr: "network is in use",
    62  		},
    63  	}
    64  
    65  	for _, tc := range tests {
    66  		tc := tc
    67  		t.Run(tc.doc, func(t *testing.T) {
    68  			fakeCli := test.NewFakeCli(&fakeClient{
    69  				networkRemoveFunc: func(ctx context.Context, networkID string) error {
    70  					switch networkID {
    71  					case "no-such-network":
    72  						return errdefs.NotFound(errors.New("no such network: no-such-network"))
    73  					case "in-use-network":
    74  						return errdefs.Forbidden(errors.New("network is in use"))
    75  					case "existing-network":
    76  						return nil
    77  					default:
    78  						return nil
    79  					}
    80  				},
    81  			})
    82  
    83  			cmd := newRemoveCommand(fakeCli)
    84  			cmd.SetOut(io.Discard)
    85  			cmd.SetErr(fakeCli.ErrBuffer())
    86  			cmd.SetArgs(tc.args)
    87  
    88  			err := cmd.Execute()
    89  			if tc.expectedErr == "" {
    90  				assert.NilError(t, err)
    91  			} else {
    92  				assert.Check(t, is.Contains(fakeCli.ErrBuffer().String(), tc.expectedErr))
    93  				assert.ErrorContains(t, err, "Code: 1")
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestNetworkRemovePromptTermination(t *testing.T) {
   100  	ctx, cancel := context.WithCancel(context.Background())
   101  	t.Cleanup(cancel)
   102  
   103  	cli := test.NewFakeCli(&fakeClient{
   104  		networkRemoveFunc: func(ctx context.Context, networkID string) error {
   105  			return errors.New("fakeClient networkRemoveFunc should not be called")
   106  		},
   107  		networkInspectFunc: func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
   108  			return types.NetworkResource{
   109  				ID:      "existing-network",
   110  				Name:    "existing-network",
   111  				Ingress: true,
   112  			}, nil, nil
   113  		},
   114  	})
   115  	cmd := newRemoveCommand(cli)
   116  	cmd.SetArgs([]string{"existing-network"})
   117  	test.TerminatePrompt(ctx, t, cmd, cli)
   118  }